Exemple #1
0
def test_shuffle():

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=1,
                      shuffle_init=True,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    assert round(acc, 2) == 0.99, "Acc: %s" % acc

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=1,
                      shuffle_init=True,
                      shuffle_epoch=True,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    assert round(acc, 2) == 0.99, "Acc: %s" % acc
Exemple #2
0
def test_shuffle():

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=1,
                      shuffle_init=True,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    assert round(acc, 2) == 0.99, "Acc: %s" % acc

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=1,
                      shuffle_init=True,
                      shuffle_epoch=True,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    assert round(acc, 2) == 0.99, "Acc: %s" % acc
Exemple #3
0
def test_score_function():

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=1,
                      shuffle_init=False,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    acc = nn.score(X_std, y)
    assert round(acc, 2) == 0.99, "Acc: %s" % acc
Exemple #4
0
def test_score_function():

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=1,
                      shuffle_init=False,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    acc = nn.score(X_std, y)
    assert round(acc, 2) == 0.99, "Acc: %s" % acc
Exemple #5
0
def test_gradient_checking():
    nn3 = NeuralNetMLP(n_output=len(np.unique(y)),
                       n_features=X_std.shape[1],
                       n_hidden=25,
                       l2=0.0,
                       l1=0.0,
                       epochs=1,
                       eta=0.01,
                       alpha=0.0,
                       decrease_const=0.0,
                       minibatches=1,
                       shuffle_init=False,
                       shuffle_epoch=False,
                       random_seed=1)

    for epoch in range(10):
        eucldist = nn3._gradient_checking(X=X_std, y=y)
        assert eucldist < 1e-07, 'Gradient difference is %s' % eucldist
def test_gradient_descent():

    nn = NeuralNetMLP(n_output=3,
             n_features=X.shape[1],
             n_hidden=10,
             l2=0.0,
             l1=0.0,
             epochs=10,
             eta=0.01,
             minibatches=1,
             shuffle=True,
             random_state=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    #print(acc)
    assert(round(acc, 2) == 0.87)
Exemple #7
0
def test_gradient_checking():
    nn3 = NeuralNetMLP(n_output=len(np.unique(y)),
                       n_features=X_std.shape[1],
                       n_hidden=25,
                       l2=0.0,
                       l1=0.0,
                       epochs=1,
                       eta=0.01,
                       alpha=0.0,
                       decrease_const=0.0,
                       minibatches=1,
                       shuffle_init=False,
                       shuffle_epoch=False,
                       random_seed=1)

    for epoch in range(10):
        eucldist = nn3._gradient_checking(X=X_std, y=y)
        assert eucldist < 1e-07, 'Gradient difference is %s' % eucldist
Exemple #8
0
def test_binary():
    X0 = X_std[0:100]  # class 0 and class 1
    y0 = y[0:100]  # class 0 and class 1

    nn = NeuralNetMLP(n_output=2,
                      n_features=X0.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=10,
                      shuffle_init=True,
                      shuffle_epoch=True,
                      random_seed=1)
    nn.fit(X0, y0)
    y_pred = nn.predict(X0)
    acc = np.sum(y0 == y_pred, axis=0) / float(X0.shape[0])
    assert round(acc, 2) == 1.0, "Acc: %s" % acc
def test_gradient_descent():

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      random_weights=[-1.0, 1.0],
                      minibatches=1,
                      shuffle_init=False,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    assert(round(acc, 2) == 0.97)
Exemple #10
0
def test_minibatch():
    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=15,
                      alpha=2.0,
                      eta=0.05,
                      minibatches=10,
                      shuffle_init=True,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    print(acc)
    assert(round(acc, 2) == 0.97)
Exemple #11
0
def test_binary():
    X0 = X_std[0:100]  # class 0 and class 1
    y0 = y[0:100]  # class 0 and class 1

    nn = NeuralNetMLP(n_output=2,
                      n_features=X0.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      minibatches=10,
                      shuffle_init=True,
                      shuffle_epoch=True,
                      random_seed=1)
    nn.fit(X0, y0)
    y_pred = nn.predict(X0)
    acc = np.sum(y0 == y_pred, axis=0) / float(X0.shape[0])
    assert round(acc, 2) == 1.0, "Acc: %s" % acc
Exemple #12
0
def test_minibatch():
    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=15,
                      alpha=2.0,
                      eta=0.05,
                      minibatches=10,
                      shuffle_init=True,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    print(acc)
    assert (round(acc, 2) == 0.97)
Exemple #13
0
def test_gradient_descent():

    nn = NeuralNetMLP(n_output=3,
                      n_features=X.shape[1],
                      n_hidden=10,
                      l2=0.0,
                      l1=0.0,
                      epochs=100,
                      eta=0.1,
                      random_weights=[-1.0, 1.0],
                      minibatches=1,
                      shuffle_init=False,
                      shuffle_epoch=False,
                      random_seed=1)

    nn.fit(X_std, y)
    y_pred = nn.predict(X_std)
    acc = np.sum(y == y_pred, axis=0) / float(X_std.shape[0])
    assert (round(acc, 2) == 0.97)
Exemple #14
0
def test_binary():
    X0 = X_std[0:100] # class 0 and class 1
    y0 = y[0:100] # class 0 and class 1

    nn = NeuralNetMLP(n_output=1,
             n_features=X0.shape[1],
             n_hidden=10,
             l2=0.0,
             l1=0.0,
             epochs=10,
             eta=0.01,
             minibatches=10,
             shuffle=True,
             random_state=1)
    nn.fit(X0, y0)
    y_pred = nn.predict(X0)
    acc = np.sum(y0 == y_pred, axis=0) / float(X0.shape[0])
    #print(y_pred)
    assert(round(acc, 2) == 1.0)
Exemple #15
0
X, y = iris_data()

X = X[:, [0, 3]]
X = X[0:100]
y = y[0:100]

X[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std()
X[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std()

nn = NeuralNetMLP(n_output=len(np.unique(y)),
                  n_features=X.shape[1],
                  n_hidden=50,
                  l2=0.00,
                  l1=0.0,
                  epochs=300,
                  eta=0.01,
                  alpha=0.0,
                  decrease_const=0.0,
                  minibatches=1,
                  shuffle_init=False,
                  shuffle_epoch=False,
                  random_seed=1,
                  print_progress=3)

nn = nn.fit(X, y)

y_pred = nn.predict(X)

acc = np.sum(y == y_pred, axis=0) / X.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))

plot_decision_regions(X, y, clf=nn, legend=2)
Exemple #16
0
X, y = iris_data()

X = X[:, [0, 3]]
X = X[0:100]
y = y[0:100]

X[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
X[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()

nn = NeuralNetMLP(n_output = len(np.unique(y)),
                  n_features = X.shape[1],
                  n_hidden = 50,
                  l2 = 0.00,
                  l1 = 0.0,
                  epochs  = 300,
                  eta = 0.01,
                  alpha = 0.0,
                  decrease_const = 0.0,
                  minibatches = 1,
                  shuffle_init = False,
                  shuffle_epoch = False,
                  random_seed = 1,
                  print_progress = 3)

nn = nn.fit(X, y)

y_pred = nn.predict(X)

acc = np.sum(y == y_pred, axis=0) / X.shape[0]
print('Accuracy: %.2f%%' % (acc * 100))