def train_val(dataset, configs, indexes, error_function): TR_indexes, VL_indexes, TS_indexes = indexes deepESN = DeepESN(configs) states = deepESN.computeState((dataset.inputs), deepESN.IPconf.DeepIP) train_states = select_indexes(states, list(TR_indexes) + list(VL_indexes), configs.transient) train_targets = np.asarray( select_indexes(dataset.targets, list(TR_indexes) + list(VL_indexes), configs.transient)) test_states = select_indexes(states, TS_indexes, configs.transient) test_targets = np.asarray( select_indexes(dataset.targets, TS_indexes, configs.transient)) if configs.rebalance_states: train_states, train_targets = deepESN.rebalanceStates( train_states, train_targets) deepESN.trainReadout(train_states, train_targets, configs.reg) train_outputs = deepESN.computeOutput(train_states) train_error = error_function(train_outputs, train_targets) test_outputs = deepESN.computeOutput(test_states) test_error = error_function(test_outputs, test_targets) print('Test ACC: ', np.mean(test_error), '\n') #plot_network_outputs(select_indexes(dataset.inputs, list(TR_indexes) + list(VL_indexes), configs.transient), train_outputs) #plot_network_outputs(select_indexes(dataset.inputs, list(TS_indexes), configs.transient), test_outputs) return test_error, deepESN
def main(): # fix a seed for the reproducibility of results np.random.seed(7) # dataset path path = 'datasets' dataset, Nu, error_function, optimization_problem, TR_indexes, VL_indexes, TS_indexes = load_pianomidi(path, computeMusicAccuracy) # load configuration for pianomidi task configs = config_pianomidi(list(TR_indexes) + list(VL_indexes)) # Be careful with memory usage Nr = 100 # number of recurrent units Nl = 5 # number of recurrent layers reg = 10.0**-2; transient = 5 deepESN = DeepESN(Nu, Nr, Nl, configs) states = deepESN.computeState(dataset.inputs, deepESN.IPconf.DeepIP) train_states = select_indexes(states, list(TR_indexes) + list(VL_indexes), transient) train_targets = select_indexes(dataset.targets, list(TR_indexes) + list(VL_indexes), transient) test_states = select_indexes(states, TS_indexes) test_targets = select_indexes(dataset.targets, TS_indexes) deepESN.trainReadout(train_states, train_targets, reg) train_outputs = deepESN.computeOutput(train_states) train_error = error_function(train_outputs, train_targets) print('Training ACC: ', np.mean(train_error), '\n') test_outputs = deepESN.computeOutput(test_states) test_error = error_function(test_outputs, test_targets) print('Test ACC: ', np.mean(test_error), '\n')
def main(): # dataset path path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(path, 'datasets/pezzetto/*') print("Loading data from {}".format(path)) dataset, Nu, error_function, optimization_problem, TR_indexes, VL_indexes, TS_indexes = load_EQ(path, F1_score) X_train = np.array([dataset.inputs[i] for i in TR_indexes[-int(len(TR_indexes) * 1):]] + [dataset.inputs[i] for i in VL_indexes]) y_train = np.array([dataset.targets[i] for i in TR_indexes[-int(len(TR_indexes) * 1):]] + [dataset.targets[i] for i in VL_indexes]) X_test = np.array([dataset.inputs[i] for i in TS_indexes[:int(len(TS_indexes) * 1)]]) y_test = np.array([dataset.targets[i] for i in TS_indexes[:int(len(TS_indexes) * 1)]]) print("DATASET TR_LEN={}, TS_LEN={}".format(len(X_train), len(X_test))) # load configuration for Earthquake task configs = config_EQ(list(range(X_test.shape[0])), Nu) print(X_train.shape) # plt = plot_timeseries_clf(X_train[:3], y_train[:3], save=False) # plt.show() # params = {"design_deep": [True], "eta": [0.01], "rho": [0.9], "iss": [10], "Nr": [20, 50, 100], "min_layers": [5], "max_layers": [15]} # best_params, cv_scores, gs = model_selection(configs, X_train, y_train, params, error_function, save_dir="./log/", # k_fold=3, outer_k_fold=0, shuffle=True, n_jobs=4, verbose=10) # print("#--> DoubleCV score: {}".format(cv_scores.mean())) print("\nOn validation:\n") # deep_esn = deep_esn_skl(configs=configs.to_dict(), Nl:5, rho:0.9, design_deep=False) configs.Nl = 4 configs.Nr = 100 configs.rhos = 1.0 configs.lis = 1.0 configs.iss = 10 deep_esn = DeepESN(configs) print("BEST Nl=", deep_esn.Nl) print("BEST rhos=", deep_esn.rhos) print("BEST Nr=", deep_esn.Nr) # Training states = deep_esn.computeState(X_train, deep_esn.IPconf.DeepIP) indexes = np.arange(len(X_train)) train_states = select_indexes(states, indexes, configs.transient) train_targets = np.asarray(select_indexes(y_train, indexes, configs.transient)) if configs.rebalance_states: train_states, train_targets = deep_esn.rebalanceStates(train_states, train_targets) deep_esn.trainReadout(train_states, train_targets, configs.reg) # Evaluate Training states = deep_esn.computeState(X_train, deep_esn.IPconf.DeepIP) test_states = select_indexes(states, indexes, configs.transient) y_train_pred = deep_esn.computeOutput(test_states) y_train_true = select_indexes(y_train, indexes, configs.transient) print("BEST_ESN TRAIN SCORE: ", error_function(y_train_pred, y_train_true)) # Evaluate Test states = deep_esn.computeState(X_test, deep_esn.IPconf.DeepIP) indexes = np.arange(len(X_test)) test_states = select_indexes(states, indexes, configs.transient) y_pred = deep_esn.computeOutput(test_states) y_true = select_indexes(y_test, indexes, configs.transient) print("BEST_ESN TEST SCORE: ", error_function(y_pred, y_true)) print(np.array(y_true).shape) plt = plot_min_max_mean_acc_prediction(np.array([X_test[i] for i in range(0, 3)]), np.array([[y_pred[0][(5000 - configs.transient) * i:(5000 - configs.transient) * (i + 1)]] for i in range(0, 3)]), np.array([y_true[i] for i in range(0, 3)]), configs.transient, error_function, save=True) plt.show() y_true = np.concatenate(y_true, 1) # for classification result, threshold = 0 y_pred[y_pred > 0] = 1 y_pred[y_pred <= 0] = -1 print(classification_report(y_true.squeeze(), y_pred.squeeze())) print("-" * 50)