Example #1
0
def test_reproducibility_Win():

    seed0 = default_rng(78946312)
    W0 = generate_input_weights(100,
                                50,
                                input_scaling=1.2,
                                proba=0.4,
                                seed=seed0)

    seed1 = default_rng(78946312)
    W1 = generate_input_weights(100,
                                50,
                                input_scaling=1.2,
                                proba=0.4,
                                seed=seed1)

    seed2 = default_rng(6135435)
    W2 = generate_input_weights(100,
                                50,
                                input_scaling=1.2,
                                proba=0.4,
                                seed=seed2)

    assert_array_almost_equal(W0, W1)
    assert_raises(AssertionError, assert_array_almost_equal, W0, W2)
Example #2
0
def init_esn(training_type):

    # Common parameters
    n_inputs = 1
    input_bias = True # add a constant input to 1
    n_outputs = 1
    n_reservoir = 300 # number of recurrent units
    leak_rate = 0.6 # leaking rate (=1/time_constant_of_neurons)
    spectral_radius = 0.5 # Scaling of recurrent matrix
    input_scaling = 1. # Scaling of input matrix
    regularization_coef = 0.02

    W = fast_spectral_initialization(n_reservoir, spectral_radius=spectral_radius)
    Win = generate_input_weights(n_reservoir, n_inputs, input_scaling=input_scaling, input_bias=input_bias)

    if training_type == 'online':
        Wout = np.zeros((n_outputs, n_reservoir + 1))
        esn = ESNOnline(leak_rate, W, Win, Wout,
                        alpha_coef=regularization_coef, input_bias=input_bias)
    elif training_type == 'online_feedback':
        Wout = np.zeros((n_outputs, n_reservoir + 1))
        Wfb = generate_input_weights(n_reservoir, n_outputs, input_bias=False)
        fbfunc = lambda x: x
        esn = ESNOnline(leak_rate, W, Win, Wout,
                        alpha_coef=regularization_coef, input_bias=input_bias, Wfb=Wfb, fbfunc=fbfunc)
    elif training_type == 'offline':
        esn = ESN(leak_rate, W, Win, 
                  input_bias=input_bias, ridge=regularization_coef)
    else:
        raise RuntimeError(f"training_type = [{training_type}] unknown")
    
    return esn
Example #3
0
def test_reproducibility_Win():

    seed0 = default_rng(78946312)
    W0 = generate_input_weights(100,
                                50,
                                input_scaling=1.2,
                                proba=0.4,
                                seed=seed0)

    seed1 = default_rng(78946312)
    W1 = generate_input_weights(100,
                                50,
                                input_scaling=1.2,
                                proba=0.4,
                                seed=seed1)

    seed2 = default_rng(6135435)
    W2 = generate_input_weights(100,
                                50,
                                input_scaling=1.2,
                                proba=0.4,
                                seed=seed2)

    assert_allclose(W0.toarray(), W1.toarray())

    with pytest.raises(AssertionError):
        assert_allclose(W0.toarray(), W2.toarray())
Example #4
0
def test_generate_inputs_features(proba, iss):

    Win = generate_input_weights(100, 20, input_scaling=iss,
                                 proba=proba, seed=1234)
    Win_noiss = generate_input_weights(100, 20, input_scaling=1.0,
                                       proba=proba, seed=1234)

    result_proba = np.sum(Win != 0.0) / Win.size

    assert_almost_equal(result_proba, proba, decimal=1)

    assert_array_almost_equal(Win / iss, Win_noiss, decimal=3)
Example #5
0
def test_generate_inputs_shape(N, dim_input, input_bias, expected):

    Win = generate_input_weights(N,
                                 dim_input,
                                 input_bias=input_bias)

    assert Win.shape == expected
Example #6
0
    def objective(dataset, config, *, iss, N, sr, leak, ridge):

        # unpack train and test data, with target values.
        train_data, test_data = dataset
        x_train, y_train = train_data
        x_test, y_test = test_data

        x_train, y_train = x_train.reshape(-1, 1), y_train.reshape(-1, 1)
        x_test, y_test = x_test.reshape(-1, 1), y_test.reshape(-1, 1)

        nb_features = x_train.shape[1]

        instances = config["instances_per_trial"]

        losses = []
        rmse = []
        for n in range(instances):
            # builds an ESN given the input parameters
            W = mat_gen.fast_spectral_initialization(N=N, spectral_radius=sr)

            Win = mat_gen.generate_input_weights(nbr_neuron=N,
                                                 dim_input=nb_features,
                                                 input_bias=True,
                                                 input_scaling=iss)

            reservoir = ESN(lr=leak,
                            W=W,
                            Win=Win,
                            input_bias=True,
                            ridge=ridge)

            # train and test the model
            reservoir.train(inputs=[x_train],
                            teachers=[y_train],
                            wash_nr_time_step=20,
                            verbose=False,
                            workers=1)

            outputs, _ = reservoir.run(inputs=[x_test],
                                       verbose=False,
                                       workers=1)

            losses.append(metrics.mean_squared_error(outputs[0], y_test))
            rmse.append(
                metrics.mean_squared_error(outputs[0], y_test, squared=False))

        # returns a dictionnary of metrics. The 'loss' key is mandatory when
        # using Hyperopt.
        return {'loss': np.mean(losses), 'rmse': np.mean(rmse)}
Example #7
0
def test_generate_inputs_features(proba, iss):

    Win = generate_input_weights(100,
                                 20,
                                 input_scaling=iss,
                                 proba=proba,
                                 seed=1234)
    Win_noiss = generate_input_weights(100,
                                       20,
                                       input_scaling=1.0,
                                       proba=proba,
                                       seed=1234)

    if sparse.issparse(Win):
        result_proba = np.count_nonzero(Win.toarray()) / Win.toarray().size
    else:
        result_proba = np.count_nonzero(Win) / Win.size

    assert_allclose(result_proba, proba, rtol=1e-2)

    if sparse.issparse(Win):
        assert_allclose(Win.toarray() / iss, Win_noiss.toarray(), rtol=1e-4)
    else:
        assert_allclose(Win / iss, Win_noiss, rtol=1e-4)
Example #8
0
def esn():
    W = generate_internal_weights(N,
                                  sr=SR,
                                  proba=1.0,
                                  dist="uniform",
                                  seed=rng)
    Win = generate_input_weights(N,
                                 1,
                                 input_scaling=IS,
                                 proba=1,
                                 input_bias=True,
                                 seed=rng)

    a = np.ones((1, N))
    b = np.zeros((1, N))

    return W, Win, a, b
Example #9
0
def test_generate_inputs_features_exception(proba, iss):
    with pytest.raises(Exception):
        generate_input_weights(100, 20, input_scaling=iss, proba=proba)
Example #10
0
def test_generate_inputs_shape_exception(N, dim_input, input_bias):
    with pytest.raises(ValueError):
        generate_input_weights(N, dim_input, input_bias=input_bias)
    sparsity = 0.81
    leak = 0.04
    alpha_coef = 10.**(-3.7)
else:
    sr = 1.1
    sparsity = 0.85
    leak = 0.05
    alpha_coef = 10.**(-3.5)

# build an ESN online, i.e. trained with FORCE learning after each sample

W = mat_gen.fast_spectra_initialization(N, spectral_radius=sr,
                                        proba=sparsity)  #reservoir matrix
Win = mat_gen.generate_input_weights(
    nbr_neuron=N,
    dim_input=nb_features,  #input matrix
    input_bias=True,
    input_scaling=iss)
Wout = np.zeros((output_size, N + 1))  #output matrix to be optimized

reservoir = ESNOnline(lr=leak, W=W, Win=Win, Wout=Wout, alpha_coef=alpha_coef)

if __name__ == "__main__":

    if use_save:
        Wout = np.load(pth + "Wout" + name_id + ".npy", allow_pickle=True)
        Win = np.load(pth + "Win" + name_id + ".npy", allow_pickle=True)
        W = np.load(pth + "W" + name_id + ".npy", allow_pickle=True)
        W = W.item()
        reservoir = ESNOnline(lr=leak,
                              W=W,