def test_esn_regressor_jobs() -> None: print('\ntest_esn_regressor_jobs():') X, y = mackey_glass(n_timesteps=8000) X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=False) param_grid = { "input_to_node": [ InputToNode(bias_scaling=.1, hidden_layer_size=10, input_activation='identity', random_state=42), InputToNode(bias_scaling=.1, hidden_layer_size=50, input_activation='identity', random_state=42) ], "node_to_node": [ NodeToNode(spectral_radius=0., hidden_layer_size=10, random_state=42), NodeToNode(spectral_radius=1, hidden_layer_size=50, random_state=42) ], "regressor": [IncrementalRegression(alpha=.0001), IncrementalRegression(alpha=.01)], 'random_state': [42] } esn = GridSearchCV(estimator=ESNRegressor(), param_grid=param_grid) esn.fit(X_train.reshape(-1, 1), y_train, n_jobs=2) y_esn = esn.predict(X_test.reshape(-1, 1)) print("tests - esn:\n sin | cos \n {0}".format(y_test - y_esn)) print("best_params_: {0}".format(esn.best_params_)) print("best_score: {0}".format(esn.best_score_)) np.testing.assert_allclose(1, esn.best_score_, atol=1e-1)
def test_node_to_node_invalid_hls() -> None: print('\ntest_node_to_node_invalid_hls():') X = np.zeros(shape=(10, 500)) with pytest.raises(ValueError): n2n = NodeToNode(hidden_layer_size=0) X = np.zeros(shape=(10, 3)) n2n.fit(X)
def test_node_to_node_dense() -> None: print('\ntest_node_to_node_dense():') n2n = NodeToNode(hidden_layer_size=5, sparsity=1., reservoir_activation='tanh', spectral_radius=1., random_state=42) X = np.zeros(shape=(10, 5)) n2n.fit(X) print(n2n._recurrent_weights) assert n2n._recurrent_weights.shape == (5, 5) assert n2n.__sizeof__() != 0 assert n2n.recurrent_weights is not None
def test_node_to_node_bidirectional() -> None: print('\ntest_node_to_node_bidirectional():') X = np.zeros(shape=(10, 5)) with pytest.raises(ValueError): n2n = NodeToNode(hidden_layer_size=5, sparsity=2 / 5, reservoir_activation='tanh', spectral_radius=1., bidirectional="True", random_state=42) n2n.fit(X) n2n = NodeToNode(hidden_layer_size=5, sparsity=2 / 5, reservoir_activation='tanh', spectral_radius=1., bidirectional=True, random_state=42) n2n.fit(X) n2n.transform(X) assert n2n._recurrent_weights.shape == (5, 5) assert n2n._hidden_layer_state.shape == (10, 10)
def test_node_to_node_invalid_leakage() -> None: print('\ntest_node_to_node_bidirectional():') X = np.zeros(shape=(10, 5)) with pytest.raises(ValueError): n2n = NodeToNode(hidden_layer_size=5, sparsity=2 / 5, reservoir_activation='tanh', spectral_radius=1., leakage=1.1, random_state=42) n2n.fit(X) with pytest.raises(ValueError): n2n = NodeToNode(hidden_layer_size=5, sparsity=2 / 5, reservoir_activation='tanh', spectral_radius=1., leakage=0, random_state=42) n2n.fit(X)
future_len = 1 scaler = MinMaxScaler(feature_range=(-1, 1)).fit(X=X) # Echo State Network preparation # In[7]: base_input_to_nodes = InputToNode(hidden_layer_size=100, activation='identity', k_in=1, input_scaling=0.6, bias_scaling=0.0) base_nodes_to_nodes = NodeToNode(hidden_layer_size=100, spectral_radius=0.9, leakage=1.0, bias_scaling=0.0, k_rec=10) esn = ESNRegressor(input_to_node=base_input_to_nodes, node_to_node=base_nodes_to_nodes, regressor=IncrementalRegression(alpha=1e-8), random_state=10) # Training and Prediction. # In[8]: X_train = scaler.transform(X[0:train_len]) y_train = scaler.transform(X[1:train_len + 1]) X_test = scaler.transform(X[train_len + 1:-1])
bias_scaling=0.1) R_i2n = input_to_node.fit_transform(U) print(U.shape, R_i2n.shape) # Node-to-Node # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ # | | | | # ----| Input-to-Node |------| Node-to-Node |------ # u[n]|_ _ _ _ _ _ _ _|r'[n] |_ _ _ _ _ _ _ |r[n] # U R_i2n R_n2n # Initialize, fit and apply NodeToNode node_to_node = NodeToNode(hidden_layer_size=50, reservoir_activation="tanh", spectral_radius=1.0, leakage=0.9, bidirectional=False) R_n2n = node_to_node.fit_transform(R_i2n) print(U.shape, R_n2n.shape) # Node-to-Output # _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ # | | | | | | # ----|Input-to-Node |-----|Node-to-Node |-----|Node-to-Output | # u[n]| _ _ _ _ _ _ _|r'[n]|_ _ _ _ _ _ _|r[n] | _ _ _ _ _ _ _ | # U R_i2n R_n2n | # Initialize, fit and apply NodeToOutput y_pred = Ridge().fit(R_n2n, y).predict(R_n2n) print(y_pred.shape)
all_wavs_m = glob.glob(r"C:\Temp\SpLxDataLondonStudents2008\M\*.wav") print(len(all_wavs_m)) all_wavs_n = glob.glob(r"C:\Temp\SpLxDataLondonStudents2008\N\*.wav") print(len(all_wavs_n)) base_input_to_node = InputToNode(hidden_layer_size=500, input_activation='identity', k_in=5, input_scaling=14.6, bias_scaling=0.0, random_state=1) base_node_to_node = NodeToNode(hidden_layer_size=500, spectral_radius=0.8, leakage=0.5, k_rec=16, bidirectional=True, random_state=1) base_reg = IncrementalRegression(alpha=1.7e-10) base_esn = ESNRegressor(input_to_node=base_input_to_node, node_to_node=base_node_to_node, regressor=base_reg) esn = base_esn t1 = time.time() Parallel(n_jobs=1, verbose=50)(delayed(train_esn)( base_input_to_node, base_node_to_node, base_reg, frame_length, all_wavs_m) for frame_length in [7, 9, 11, 21, 31, 41, 81]) print("Finished in {0} seconds!".format(time.time() - t1))
def test_node_to_node_invalid_sparsity() -> None: print('\ntest_node_to_node_invalid_sparsity():') X = np.zeros(shape=(10, 500)) with pytest.raises(ValueError): n2n = NodeToNode(sparsity=1.1) n2n.fit(X) with pytest.raises(ValueError): n2n = NodeToNode(sparsity=0.0) n2n.fit(X) with pytest.raises(ValueError): n2n = NodeToNode(k_rec=-1) n2n.fit(X) with pytest.raises(ValueError): n2n = NodeToNode(k_rec=500) n2n.fit(X)
def test_node_to_node_invalid_activation() -> None: print('\ntest_node_to_node_invalid_activation():') X = np.zeros(shape=(10, 500)) with pytest.raises(ValueError): n2n = NodeToNode(reservoir_activation="test") n2n.fit(X)
def test_input_to_node_invalid_spectral_radius() -> None: print('\ntest_input_to_node_invalid_spectral_radius():') X = np.zeros(shape=(10, 500)) with pytest.raises(ValueError): n2n = NodeToNode(spectral_radius=-1e-5) n2n.fit(X)