Esempio n. 1
0
def loss_model(model: Model,
               dataset_id,
               dataset_prefix,
               batch_size=128,
               train_data_subset=None,
               cutoff=None,
               normalize_timeseries=False):
    X_train, y_train, _, _, is_timeseries = load_dataset_at(
        dataset_id, normalize_timeseries=normalize_timeseries)

    y_train = to_categorical(y_train, len(np.unique(y_train)))

    optm = Adam(lr=1e-3)
    model.compile(optimizer=optm,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    model.load_weights("./ets/algorithms/MLSTM/weights/%s_weights.h5" %
                       dataset_prefix)
    #print("Weights loaded from ", "./weights/%s_weights.h5" % dataset_prefix)

    if train_data_subset is not None:
        X_train = X_train[:train_data_subset]
        y_train = y_train[:train_data_subset]

    #print("\nEvaluating : ")
    loss, accuracy = model.evaluate(X_train, y_train, batch_size=batch_size)
    #print()
    #print("Final Loss : ", loss)

    return loss
Esempio n. 2
0
def run():
    X_train, y_train, X_test, y_test, is_timeseries = load_dataset_at(
        DATASET_INDEX, fold_index=None, normalize_timeseries=False)
    start = time.time()
    model = generate_model(len(X_test[0][0]), 8)
    train_model(model,
                DATASET_INDEX,
                dataset_prefix="current",
                epochs=600,
                batch_size=128)
    train = time.time() - start
    res = evaluate_model(model,
                         DATASET_INDEX,
                         dataset_prefix="current",
                         batch_size=128)
    test = time.time() - start
    return res, train, test
Esempio n. 3
0
def compute_average_gradient_norm(model: Model,
                                  dataset_id,
                                  dataset_fold_id=None,
                                  batch_size=128,
                                  cutoff=None,
                                  normalize_timeseries=False,
                                  learning_rate=1e-3):
    f = open("./ets/algorithms/MLSTM/utils/constants.txt", "r")
    lines = f.readlines()
    MAX_NB_VARIABLES = 0
    for line in lines:
        if "VARIABLES" in line:
            line = line.split("=")
            MAX_NB_VARIABLES = int(line[1])
    f.close()
    X_train, y_train, X_test, y_test, is_timeseries = load_dataset_at(
        dataset_id,
        fold_index=dataset_fold_id,
        normalize_timeseries=normalize_timeseries)
    max_timesteps, sequence_length = calculate_dataset_metrics(X_train)

    if sequence_length != MAX_NB_VARIABLES:
        if cutoff is None:
            choice = cutoff_choice(dataset_id, sequence_length)
        else:
            assert cutoff in [
                'pre', 'post'
            ], 'Cutoff parameter value must be either "pre" or "post"'
            choice = cutoff

        if choice not in ['pre', 'post']:
            return
        else:
            X_train, X_test = cutoff_sequence(X_train, X_test, choice,
                                              dataset_id, sequence_length)

    y_train = to_categorical(y_train, len(np.unique(y_train)))

    optm = Adam(lr=learning_rate)
    model.compile(optimizer=optm,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    average_gradient = _average_gradient_norm(model, X_train, y_train,
                                              batch_size)
Esempio n. 4
0
def evaluate_model(model: Model,
                   dataset_id,
                   dataset_prefix,
                   dataset_fold_id=None,
                   batch_size=128,
                   test_data_subset=None,
                   cutoff=None,
                   normalize_timeseries=False):
    f = open("./ets/algorithms/MLSTM/utils/constants.txt", "r")
    lines = f.readlines()
    MAX_NB_VARIABLES = 0
    for line in lines:
        if "VARIABLES" in line:
            line = line.split("=")
            MAX_NB_VARIABLES = int(line[1])
    f.close()
    _, _, X_test, y_test, is_timeseries = load_dataset_at(
        dataset_id,
        fold_index=dataset_fold_id,
        normalize_timeseries=normalize_timeseries)
    max_timesteps, max_nb_variables = calculate_dataset_metrics(X_test)

    if max_nb_variables != MAX_NB_VARIABLES:
        if cutoff is None:
            choice = cutoff_choice(dataset_id, max_nb_variables)
        else:
            assert cutoff in [
                'pre', 'post'
            ], 'Cutoff parameter value must be either "pre" or "post"'
            choice = cutoff

        if choice not in ['pre', 'post']:
            return
        else:
            _, X_test = cutoff_sequence(None, X_test, choice, dataset_id,
                                        max_nb_variables)

    if not is_timeseries:
        X_test = pad_sequences(X_test,
                               maxlen=MAX_NB_VARIABLES[dataset_id],
                               padding='post',
                               truncating='post')
    y_test = to_categorical(y_test, len(np.unique(y_test)))
    optm = Adam(lr=1e-3)
    model.compile(optimizer=optm,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    if dataset_fold_id is None:
        weight_fn = "./ets/algorithms/MLSTM/weights/%s_weights.h5" % dataset_prefix
    else:
        weight_fn = "./ets/algorithms/MLSTM/weights/%s_fold_%d_weights.h5" % (
            dataset_prefix, dataset_fold_id)
    model.load_weights(weight_fn)

    if test_data_subset is not None:
        X_test = X_test[:test_data_subset]
        y_test = y_test[:test_data_subset]

    #print("\nEvaluating : ")
    pre = model.predict(X_test, batch_size=batch_size)
    classes = pre.argmax(axis=-1)
    #print()
    #print("Final Accuracy : ")
    result = []
    for item in classes:
        result.append((len(X_test[0][0]), item))

    return result
Esempio n. 5
0
def train_model(model: Model,
                dataset_id,
                dataset_prefix,
                dataset_fold_id=None,
                epochs=50,
                batch_size=128,
                val_subset=None,
                cutoff=None,
                normalize_timeseries=False,
                learning_rate=1e-3,
                monitor='loss',
                optimization_mode='auto',
                compile_model=True):
    f = open("./ets/algorithms/MLSTM/utils/constants.txt", "r")
    lines = f.readlines()
    MAX_NB_VARIABLES = 0
    for line in lines:
        if "VARIABLES" in line:
            line = line.split("=")
            MAX_NB_VARIABLES = int(line[1])
    f.close()
    X_train, y_train, X_test, y_test, is_timeseries = load_dataset_at(
        dataset_id,
        fold_index=dataset_fold_id,
        normalize_timeseries=normalize_timeseries)
    max_timesteps, max_nb_variables = calculate_dataset_metrics(X_train)

    if max_nb_variables != MAX_NB_VARIABLES:
        if cutoff is None:
            choice = cutoff_choice(dataset_id, max_nb_variables)
        else:
            assert cutoff in [
                'pre', 'post'
            ], 'Cutoff parameter value must be either "pre" or "post"'
            choice = cutoff

        if choice not in ['pre', 'post']:
            return
        else:
            X_train, X_test = cutoff_sequence(X_train, X_test, choice,
                                              dataset_id, max_nb_variables)

    classes = np.unique(y_train)
    le = LabelEncoder()
    y_ind = le.fit_transform(y_train.ravel())
    recip_freq = len(y_train) / (len(le.classes_) *
                                 np.bincount(y_ind).astype(np.float64))
    class_weight = recip_freq[le.transform(classes)]

    #print("Class weights : ", class_weight)

    y_train = to_categorical(y_train, len(np.unique(y_train)))
    y_test = to_categorical(y_test, len(np.unique(y_test)))

    if is_timeseries:
        factor = 1. / np.cbrt(2)
    else:
        factor = 1. / np.sqrt(2)

    if dataset_fold_id is None:
        weight_fn = "./ets/algorithms/MLSTM/weights/%s_weights.h5" % dataset_prefix
    else:
        weight_fn = "./ets/algorithms/MLSTM/weights/%s_fold_%d_weights.h5" % (
            dataset_prefix, dataset_fold_id)

    model_checkpoint = ModelCheckpoint(weight_fn,
                                       verbose=1,
                                       mode=optimization_mode,
                                       monitor=monitor,
                                       save_best_only=True,
                                       save_weights_only=True)
    reduce_lr = ReduceLROnPlateau(monitor=monitor,
                                  patience=100,
                                  mode=optimization_mode,
                                  factor=factor,
                                  cooldown=0,
                                  min_lr=1e-4,
                                  verbose=2)
    callback_list = [model_checkpoint, reduce_lr]

    optm = Adam(lr=learning_rate)

    if compile_model:
        model.compile(optimizer=optm,
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

    if val_subset is not None:
        X_test = X_test[:val_subset]
        y_test = y_test[:val_subset]
    #print(X_test[0][0])
    model.fit(X_train,
              y_train,
              batch_size=batch_size,
              epochs=epochs,
              callbacks=callback_list,
              class_weight=class_weight,
              verbose=0,
              validation_data=(X_test, y_test))