Esempio n. 1
0
def test_esn_output_unchanged() -> None:
    X, y = load_digits(return_X_y=True, as_sequence=True)
    shape1 = y[0].shape
    esn = ESNClassifier(hidden_layer_size=50).fit(X, y)
    print(esn)
    shape2 = y[0].shape
    assert (shape1 == shape2)
Esempio n. 2
0
def test_esn_classifier_instance_fit() -> None:
    X, y = load_digits(return_X_y=True, as_sequence=True)
    esn = ESNClassifier(hidden_layer_size=50).fit(X[0], np.repeat(y[0], 8))
    assert (esn.sequence_to_value is False)
    y_pred = esn.predict_proba(X[0])
    assert (y_pred.ndim == 2)
    y_pred = esn.predict_log_proba(X[0])
    assert (y_pred.ndim == 2)
Esempio n. 3
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)
Esempio n. 4
0
def test_esn_classifier_partial_fit() -> None:
    X, y = load_digits(return_X_y=True, as_sequence=True)
    esn = ESNClassifier(hidden_layer_size=50, verbose=True)
    for k in range(10):
        esn.partial_fit(X[k],
                        np.repeat(y[k], 8),
                        classes=np.arange(10),
                        postpone_inverse=True)
    print(esn.__sizeof__())
    print(esn.hidden_layer_state)
    esn = ESNClassifier(hidden_layer_size=50, regressor=Ridge())
    with pytest.raises(BaseException):
        for k in range(10):
            esn.partial_fit(X[k],
                            np.repeat(y[k], 8),
                            classes=np.arange(10),
                            postpone_inverse=True)
Esempio n. 5
0
def test_esn_classifier_sequence_to_value() -> None:
    X, y = load_digits(return_X_y=True, as_sequence=True)
    esn = ESNClassifier(hidden_layer_size=50).fit(X, y)
    y_pred = esn.predict(X)
    assert (len(y) == len(y_pred))
    assert (len(y_pred[0]) == 1)
    assert (esn.sequence_to_value is True)
    assert (esn.decision_strategy == "winner_takes_all")
    y_pred = esn.predict_proba(X)
    assert (y_pred[0].ndim == 1)
    y_pred = esn.predict_log_proba(X)
    assert (y_pred[0].ndim == 1)
    esn.sequence_to_value = False
    y_pred = esn.predict(X)
    assert (len(y_pred[0]) == 8)
    y_pred = esn.predict_proba(X)
    assert (y_pred[0].ndim == 2)
    y_pred = esn.predict_log_proba(X)
    assert (y_pred[0].ndim == 2)
Esempio n. 6
0
from sklearn.model_selection import train_test_split
from sklearn.model_selection import (ParameterGrid, RandomizedSearchCV,
                                     cross_validate)
from sklearn.utils.fixes import loguniform
from scipy.stats import uniform
from sklearn.metrics import make_scorer

from pyrcn.model_selection import SequentialSearchCV
from pyrcn.echo_state_network import ESNClassifier
from pyrcn.metrics import accuracy_score
from pyrcn.datasets import load_digits

# Load the dataset (part of scikit-learn) and consists of 1797 8x8 images.
# We are using our dataloader that is derived from scikit-learns dataloader and
# returns arrays of 8x8 sequences and corresponding labels.
X, y = load_digits(return_X_y=True, as_sequence=True)
print("Number of digits: {0}".format(len(X)))
print("Shape of digits {0}".format(X[0].shape))

# Split dataset in training and test
# Afterwards, we split the dataset into training and test sets.
# We train the ESN using 80% of the digits and test it using the remaining
# images.
stratify = np.asarray([np.unique(yt) for yt in y]).flatten()
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    stratify=stratify,
                                                    random_state=42)
X_tr = np.copy(X_train)
y_tr = np.copy(y_train)
Esempio n. 7
0
def test_esn_classifier_not_fitted() -> None:
    X, y = load_digits(return_X_y=True, as_sequence=True)
    with pytest.raises(NotFittedError):
        ESNClassifier(hidden_layer_size=50, verbose=True).predict(X)