Exemplo n.º 1
0
def test_input_to_node_invalid_hls() -> None:
    print('\ntest_input_to_node_invalid_hls():')
    X = np.zeros(shape=(10, 500))
    with pytest.raises(ValueError):
        i2n = InputToNode(hidden_layer_size=0)
        X = np.zeros(shape=(10, 3))
        i2n.fit(X)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def test_iris_ensemble_iterative_regression() -> None:
    print('\ntest_iris_ensemble_iterative_regression():')
    X_train, X_test, y_train, y_test = train_test_split(
        X_iris, y_iris, test_size=5, random_state=42)

    cls = ELMClassifier(
        input_to_node=FeatureUnion([
            ('tanh', InputToNode(hidden_layer_size=10, random_state=42,
                                 input_activation='tanh')),
            ('bounded_relu', InputToNode(hidden_layer_size=10, random_state=42,
                                         input_activation='bounded_relu'))]),
        regressor=IncrementalRegression(alpha=.01),
        random_state=42)

    for samples in np.split(np.arange(0, X_train.shape[0]), 5):
        cls.partial_fit(X_train[samples, :], y_train[samples],
                        classes=np.arange(3, dtype=int))
    y_predicted = cls.predict(X_test)

    for record in range(len(y_test)):
        print('predicted: {0} \ttrue: {1}'
              .format(y_predicted[record], y_test[record]))

    print('score: {0}'.format(cls.score(X_test, y_test)))
    print('proba: {0}'.format(cls.predict_proba(X_test)))
    print('log_proba: {0}'.format(cls.predict_log_proba(X_test)))
    assert cls.score(X_test, y_test) >= 4./5.
Exemplo n.º 4
0
def test_input_to_node_sparse() -> None:
    print('\ntest_input_to_node_sparse():')
    i2n = InputToNode(hidden_layer_size=5,
                      sparsity=2 / 5,
                      input_activation='tanh',
                      input_scaling=1.,
                      bias_scaling=1.,
                      random_state=42)
    X = np.zeros(shape=(10, 3))
    i2n.fit(X)
    assert i2n._input_weights.shape == (3, 5)
    assert safe_sparse_dot(X, i2n._input_weights).shape == (10, 5)
    i2n = InputToNode(hidden_layer_size=5,
                      k_in=2,
                      input_activation='tanh',
                      input_scaling=1.,
                      bias_scaling=1.,
                      random_state=42)
    X = np.zeros(shape=(10, 3))
    i2n.fit(X)
    assert i2n._input_weights.shape == (3, 5)
    assert safe_sparse_dot(X, i2n._input_weights).shape == (10, 5)
    assert i2n.__sizeof__() != 0
    assert i2n.input_weights is not None
    assert i2n.bias_weights is not None
Exemplo n.º 5
0
def test_node_to_node_hebbian() -> None:
    print('\ntest_node_to_node_hebbian():')
    i2n = InputToNode(hidden_layer_size=5,
                      sparsity=2 / 5,
                      input_activation='tanh',
                      input_scaling=1.,
                      bias_scaling=1.,
                      random_state=42)
    X = np.zeros(shape=(10, 3))
    i2n.fit(X)
    n2n = HebbianNodeToNode(hidden_layer_size=5,
                            sparsity=2 / 5,
                            reservoir_activation='tanh',
                            spectral_radius=1.,
                            random_state=42,
                            learning_rate=0.01)
    n2n.fit(i2n.transform(X))
    n2n = HebbianNodeToNode(hidden_layer_size=5,
                            sparsity=2 / 5,
                            reservoir_activation='tanh',
                            spectral_radius=1.,
                            random_state=42,
                            learning_rate=0.01,
                            training_method="anti_hebbian")
    n2n.fit(i2n.transform(X))
    n2n = HebbianNodeToNode(hidden_layer_size=5,
                            sparsity=2 / 5,
                            reservoir_activation='tanh',
                            spectral_radius=1.,
                            random_state=42,
                            learning_rate=0.01,
                            training_method="oja")
    n2n.fit(i2n.transform(X))
    n2n = HebbianNodeToNode(hidden_layer_size=5,
                            sparsity=2 / 5,
                            reservoir_activation='tanh',
                            spectral_radius=1.,
                            random_state=42,
                            learning_rate=0.01,
                            training_method="anti_oja")
    n2n.fit(i2n.transform(X))
    i2n_hidden = i2n.transform(X)
    print(n2n.transform(i2n_hidden))
    print(n2n._recurrent_weights)
    assert n2n._recurrent_weights.shape == (5, 5)
    assert safe_sparse_dot(i2n.transform(X),
                           n2n._recurrent_weights).shape == (10, 5)
Exemplo n.º 6
0
def test_esn_classifier_no_valid_params() -> None:
    X, y = load_digits(return_X_y=True, as_sequence=True)
    with pytest.raises(TypeError):
        ESNClassifier(input_to_node=ESNRegressor()).fit(X, y)
    with pytest.raises(TypeError):
        ESNClassifier(node_to_node=ESNRegressor()).fit(X, y)
    with pytest.raises(TypeError):
        ESNClassifier(input_to_node=ESNRegressor()).fit(X, y)
    with pytest.raises(ValueError):
        ESNClassifier(requires_sequence="True").fit(X, y)
    with pytest.raises(TypeError):
        ESNClassifier(regressor=InputToNode()).fit(X, y)
Exemplo n.º 7
0
def test_elm_regressor_jobs() -> None:
    print('\ntest_elm_regressor_jobs():')
    X = np.linspace(0, 10, 2000)
    y = np.hstack((np.sin(X).reshape(-1, 1), np.cos(X).reshape(-1, 1)))
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=10, random_state=42)
    param_grid = {
        'input_to_node': [
            InputToNode(
                bias_scaling=10., hidden_layer_size=20, random_state=42),
            InputToNode(
                bias_scaling=10., hidden_layer_size=50, random_state=42)],
        'regressor': [IncrementalRegression(alpha=.0001),
                      IncrementalRegression(alpha=.01)],
        'random_state': [42]}
    elm = GridSearchCV(ELMRegressor(), param_grid)
    elm.fit(X_train.reshape(-1, 1), y_train, n_jobs=2)
    y_elm = elm.predict(X_test.reshape(-1, 1))
    print("tests - elm:\n sin | cos \n {0}".format(y_test-y_elm))
    print("best_params_: {0}".format(elm.best_params_))
    print("best_score: {0}".format(elm.best_score_))
    np.testing.assert_allclose(y_test, y_elm, atol=1e-1)
Exemplo n.º 8
0
def input2node_distribution(directory):
    X, y = get_mnist(directory)

    X /= 255.

    pca = PCA(n_components=784).fit(X)
    X_pca = np.matmul(X, pca.components_.T)

    list_activation = ['tanh', 'relu', 'bounded_relu']
    list_train = [X, X_pca]

    fig, axs = plt.subplots(nrows=2, ncols=3)

    for idx_activation in range(len(list_activation)):
        activation = list_activation[idx_activation]

        for idx_train in range(len(list_train)):
            ax = axs[idx_train, idx_activation]
            train = list_train[idx_train]

            if activation in ['tanh', '']:
                i2n = InputToNode(hidden_layer_size=1,
                                  random_state=82,
                                  input_scaling=50 / 784,
                                  bias_scaling=0.,
                                  activation=activation)
            elif activation in ['relu', 'bounded_relu']:
                i2n = InputToNode(hidden_layer_size=1,
                                  random_state=82,
                                  input_scaling=1.,
                                  bias_scaling=0.,
                                  activation=activation)

            node_out = i2n.fit_transform(train, y)
            hist, bin_edges = np.histogram(node_out, bins=20, density=True)

            np.delete(bin_edges[:-1], hist <= 1e-3)
            np.delete(hist, hist <= 1e-3)

            if activation == 'bounded_relu':
                ax.hist(node_out,
                        label=activation,
                        density=True,
                        bins=[.0, .1, .9, 1.],
                        color=tud_colors['lightblue'])
            else:
                ax.hist(node_out,
                        label=activation,
                        density=True,
                        bins=20,
                        color=tud_colors['lightblue'])

            ax.grid(axis='y')
            ax.set_yscale('log')

            x_ticks = np.min(node_out), np.max(node_out)
            ax.set_xlim(x_ticks)

            if activation == 'tanh':
                x_ticks += (0.0, )
            ax.set_xticks(x_ticks)
            ax.set_xticklabels(
                ['{0:.1f}'.format(x_tick) for x_tick in x_ticks])

    axs[0, 0].set_title('tanh, orig.')
    axs[0, 1].set_title('relu, orig.')
    axs[0, 2].set_title('b. relu, orig.')
    axs[1, 0].set_title('tanh, pca')
    axs[1, 1].set_title('relu, pca')
    axs[1, 2].set_title('b. relu, pca')

    # plt.tight_layout()
    fig.tight_layout()
    fig.savefig(os.path.join(directory, 'node-out.pdf'), format='pdf')
    fig.savefig(os.path.join(directory, 'node-out.eps'), format='eps')
    plt.rc('pgf', texsystem='pdflatex')
Exemplo n.º 9
0
# of $[0 1]$.

# In[5]:

train_len = 3000
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.
Exemplo n.º 10
0
def test_input_to_node_invalid_sparsity() -> None:
    print('\ntest_input_to_node_invalid_sparsity():')
    X = np.zeros(shape=(10, 500))
    with pytest.raises(ValueError):
        i2n = InputToNode(sparsity=1.1)
        i2n.fit(X)
    with pytest.raises(ValueError):
        i2n = InputToNode(sparsity=0.0)
        i2n.fit(X)
    with pytest.raises(ValueError):
        i2n = InputToNode(k_in=-1)
        i2n.fit(X)
    with pytest.raises(ValueError):
        i2n = InputToNode(k_in=500)
        i2n.fit(X)
Exemplo n.º 11
0
def test_input_to_node_invalid_activation() -> None:
    print('\ntest_input_to_node_invalid_activation():')
    X = np.zeros(shape=(10, 500))
    with pytest.raises(ValueError):
        i2n = InputToNode(input_activation="test")
        i2n.fit(X)
Exemplo n.º 12
0
def test_input_to_node_invalid_input_scaling() -> None:
    print('\ntest_input_to_node_invalid_input_scaling():')
    X = np.zeros(shape=(10, 500))
    with pytest.raises(ValueError):
        i2n = InputToNode(input_scaling=0)
        i2n.fit(X)
Exemplo n.º 13
0
from pyrcn.metrics import accuracy_score
from pyrcn.datasets import load_digits

# Generate a toy dataset
U, y = make_blobs(n_samples=100, n_features=10)

# Input-to-Node
#      _ _ _ _ _ _ _ _
#     |               |
# ----| Input-to-Node |------
# u[n]|_ _ _ _ _ _ _ _|r'[n]
# U                    R_i2n

input_to_node = InputToNode(hidden_layer_size=50,
                            k_in=5,
                            input_activation="tanh",
                            input_scaling=1.0,
                            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,
Exemplo n.º 14
0
    times = np.arange(start=0, stop=intervals[-1, 1], step=0.01)
    annotations = np.zeros(shape=times.shape)
    for count, interval in enumerate(intervals):
        annotations[np.multiply(times >= interval[0], times < interval[1])] =\
            annot[count]
    return times, annotations


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
Exemplo n.º 15
0
def test_elm_classifier_no_valid_params() -> None:
    X, y = load_digits(return_X_y=True)
    with pytest.raises(TypeError):
        ELMClassifier(input_to_node=ELMRegressor()).fit(X, y)
    with pytest.raises(TypeError):
        ELMClassifier(regressor=InputToNode()).fit(X, y)