Exemple #1
0
    def __init__(self,
                 eta=0.5,
                 epochs=50,
                 hidden_layers=None,
                 n_classes=None,
                 momentum=0.0,
                 l1=0.0,
                 l2=0.0,
                 dropout=1.0,
                 decrease_const=0.0,
                 minibatches=1,
                 random_seed=None,
                 print_progress=0):
        epochs = int(epochs)

        warnings.filterwarnings(module='mlxtend*',
                                action='ignore',
                                category=FutureWarning)
        warnings.filterwarnings(module='mlxtend*',
                                action='ignore',
                                category=RuntimeWarning)
        if hidden_layers is None:
            hidden_layers = [50]
        _MultiLayerPerceptron.__init__(self, eta, epochs, hidden_layers,
                                       n_classes, momentum, l1, l2, dropout,
                                       decrease_const, minibatches,
                                       random_seed, print_progress)
        BaseWrapperClf.__init__(self)
def test_progress_3():
    mlp = MLP(epochs=1,
              eta=0.05,
              hidden_layers=[10],
              minibatches=1,
              print_progress=3,
              random_seed=1)
    mlp.fit(X, y)
def test_score_function():
    mlp = MLP(epochs=20,
              eta=0.05,
              hidden_layers=[25],
              minibatches=5,
              random_seed=1)
    mlp.fit(X, y)
    acc = mlp.score(X, y)
    assert acc == 1.0, acc
def test_multiclass_minibatch_acc():
    mlp = MLP(epochs=20,
              eta=0.05,
              hidden_layers=[25],
              minibatches=5,
              random_seed=1)
    mlp.fit(X, y)
    assert round(mlp.cost_[-1], 3) == 0.024, mlp.cost_[-1]
    assert (y == mlp.predict(X)).all()
def test_binary_gd():
    mlp = MLP(epochs=20,
              eta=0.05,
              hidden_layers=[25],
              minibatches=5,
              random_seed=1)

    mlp.fit(X_bin, y_bin)
    assert (y_bin == mlp.predict(X_bin)).all()
def test_multiclass_gd_acc():
    mlp = MLP(epochs=20,
              eta=0.05,
              hidden_layers=[10],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    assert round(mlp.cost_[0], 2) == 0.55, mlp.cost_[0]
    assert round(mlp.cost_[-1], 2) == 0.01, mlp.cost_[-1]
    assert (y == mlp.predict(X)).all()
def test_predict_proba():
    mlp = MLP(epochs=20,
              eta=0.05,
              hidden_layers=[10],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)

    pred = mlp.predict_proba(X[0, np.newaxis])
    exp = np.array([[0.6, 0.2, 0.2]])
    np.testing.assert_almost_equal(pred, exp, decimal=1)
def test_momentum_1():
    mlp = MLP(epochs=20,
              eta=0.05,
              momentum=0.1,
              hidden_layers=[25],
              minibatches=len(y),
              random_seed=1)

    mlp.fit(X, y)
    assert round(mlp.cost_[-1], 4) == 0.0057, mlp.cost_[-1]
    assert (y == mlp.predict(X)).all()
def test_decay_function():
    mlp = MLP(epochs=20,
              eta=0.05,
              decrease_const=0.01,
              hidden_layers=[25],
              minibatches=5,
              random_seed=1)

    mlp.fit(X, y)
    assert mlp._decr_eta < mlp.eta
    acc = mlp.score(X, y)
    assert round(acc, 2) == 0.98, acc
Exemple #10
0
def test_multiclass_gd_acc():
    mlp = MLP(epochs=20,
              eta=0.05,
              hidden_layers=[10],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    assert round(mlp.cost_[0], 2) == 0.55, mlp.cost_[0]

    if round(mlp.cost_[-1], 2) == 0.25:
        warnings.warn('About 10% of the time, mlp.cost_[-1] is'
                      ' 0.247213137424 when tested via Travis CI.'
                      ' Likely, it is an architecture-related problem but'
                      ' should be looked into in future.')
    else:
        assert round(mlp.cost_[-1], 2) == 0.01, mlp.cost_[-1]
        assert (y == mlp.predict(X)).all()
def test_retrain():
    mlp = MLP(epochs=5,
              eta=0.05,
              hidden_layers=[10],
              minibatches=len(y),
              random_seed=1)

    mlp.fit(X, y)
    cost_1 = mlp.cost_[-1]
    mlp.fit(X, y)
    cost_2 = mlp.cost_[-1]
    mlp.fit(X, y, init_params=False)
    cost_3 = mlp.cost_[-1]

    assert cost_2 == cost_1
    assert cost_3 < (cost_2 / 2.0)
def test_retrain():
    mlp = MLP(epochs=10,
              eta=0.05,
              hidden_layers=[25],
              minibatches=len(y),
              random_seed=1)

    mlp.fit(X, y)
    cost_1 = mlp.cost_[-1]
    mlp.fit(X, y, init_params=False)

    assert round(cost_1, 3) == 0.058, cost_1
    assert round(mlp.cost_[-1], 3) == 0.023, mlp.cost_[-1]
    assert (y == mlp.predict(X)).all()
def test_retrain():
    mlp = MLP(epochs=10,
              eta=0.05,
              hidden_layers=[25],
              minibatches=len(y),
              random_seed=1)

    mlp.fit(X, y)
    cost_1 = mlp.cost_[-1]
    mlp.fit(X, y, init_params=False)

    assert round(cost_1, 3) == 0.058, cost_1
    assert round(mlp.cost_[-1], 3) == 0.023, mlp.cost_[-1]
    assert (y == mlp.predict(X)).all()
Exemple #14
0
X, y = iris_data()
X = X[:, [0, 3]]

# standardize training data
X_std = (X - X.mean(axis=0)) / X.std(axis=0)

print X_std


# Gradient Descent

nn1 = MLP(hidden_layers=[50],
          l2 = 0.00,
          l1 = 0.0,
          epochs=150,
          eta=0.05,
          momentum=0.1,
          decrease_const=0.0,
          minibatches=1,
          random_seed=1,
          print_progress=3)

nn1 = nn1.fit(X_std, y)
fig = plot_decision_regions(X=X_std, y=y, clf=nn1, legend=2)
plt.title('Multi-layer perception w. 1 hidden layer (logistic sigmod)')
plt.show()

plt.plot(range(len(nn1.cost_)), nn1.cost_)
plt.ylabel("Cost")
plt.xlabel("Epochs")
plt.show()
Exemple #15
0
from mlxtend.data import iris_data
X, y = iris_data()

# standardize training data
X_std = (X - X.mean(axis=0)) / X.std(axis=0)

from mlxtend.classifier import MultiLayerPerceptron as MLP

nn1 = MLP(hidden_layers=[10],
          l2=0.00,
          l1=0.0,
          epochs=10000,
          eta=0.001,
          momentum=0.1,
          decrease_const=0.0,
          minibatches=1,
          random_seed=1,
          print_progress=3)

nn1 = nn1.fit(X_std, y)
print('\nAccuracy: %.2f%%' % (100 * nn1.score(X_std, y)))
from mlxtend.data import iris_data
X, y = iris_data()
X = X[:, [0, 3]]

# normaliza dados de treinamento
X_std = (X - X.mean(axis=0)) / X.std(axis=0)

from mlxtend.classifier import MultiLayerPerceptron as MLP

nn1 = MLP(hidden_layers=[50],
          l2=0.00,
          l1=0.0,
          epochs=150,
          eta=0.05,
          momentum=0.1,
          decrease_const=0.0,
          minibatches=1,
          random_seed=1,
          print_progress=3)

nn1 = nn1.fit(X_std, y)

from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt

fig = plot_decision_regions(X=X_std, y=y, clf=nn1, legend=2)
plt.title('Multi-layer Perceptron w. 1 camada oculta (sigmoidal)')
plt.show()

import matplotlib.pyplot as plt
plt.plot(range(len(nn1.cost_)), nn1.cost_)
Exemple #17
0
    plt.show()


plot_digit(X, y, 3500)

from mlxtend.preprocessing import standardize

X_train_std, params = standardize(X_train,
                                  columns=range(X_train.shape[1]),
                                  return_params=True)
X_test_std = standardize(X_test, columns=range(X_test.shape[1]), params=params)
nn1 = MLP(hidden_layers=[150],
          l2=0.00,
          l1=0.0,
          epochs=100,
          eta=0.005,
          momentum=0.0,
          decrease_const=0.0,
          minibatches=100,
          random_seed=1,
          print_progress=3)
nn1.fit(X_train_std, y_train)

plt.plot(range(len(nn1.cost_)), nn1.cost_)
plt.ylabel('Cost')
plt.xlabel('Epochs')
plt.show()

print('Train Accuracy: %.2f%%' % (100 * nn1.score(X_train_std, y_train)))
print('Test Accuracy: %.2f%%' % (100 * nn1.score(X_test_std, y_test)))
X = X[:, [0, 3]]

# standardize training data
X_std = (X - X.mean(axis=0)) / X.std(axis=0)

# ### Gradient Descent

# In[41]:

from mlxtend.classifier import MultiLayerPerceptron as MLP

nn1 = MLP(hidden_layers=[50],
          l2=0.00,
          l1=0.0,
          epochs=150,
          eta=0.05,
          momentum=0.1,
          decrease_const=0.0,
          minibatches=1,
          random_seed=1,
          print_progress=3)

nn1 = nn1.fit(X_std, y)

# In[42]:

from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt

fig = plot_decision_regions(X=X_std, y=y, clf=nn1, legend=2)
plt.title('Multi-layer Perceptron w. 1 hidden layer (logistic sigmoid)')
plt.show()
from mlxtend.classifier import MultiLayerPerceptron as MLP
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt
import numpy as np 

X = np.asarray([[6.1,1.4],[7.7,2.3],[6.3,2.4],[6.4,1.8],[6.2,1.8],[6.9,2.1],
[6.7,2.4],[6.9,2.3],[5.8,1.9],[6.8,2.3],[6.7,2.5],[6.7,2.3],[6.3,1.9],[6.5,2.1 ],[6.2,2.3],[5.9,1.8]] ) 

X = (X - X.mean(axis=0)) / X.std(axis=0)

y = np.asarray([0,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2]) 

nn = MLP(hidden_layers=[50],l2=0.00,l1=0.0,epochs=150,eta=0.05,
momentum=0.1,decrease_const=0.0,minibatches=1,random_seed=1,print_progress=3)
nn = nn.fit(X, y)

fig = plot_decision_regions(X=X, y=y, clf=nn, legend=2)
plt.show()
print('Accuracy(epochs = 150): %.2f%%' % (100 * nn.score(X, y)))

nn.epochs = 250
nn = nn.fit(X, y)
fig = plot_decision_regions(X=X, y=y, clf=nn, legend=2)
plt.title('epochs = 250')
plt.show()
print('Accuracy(epochs = 250): %.2f%%' % (100 * nn.score(X, y)))

plt.plot(range(len(nn.cost_)), nn.cost_)
plt.title('Gradient Descent training (minibatches=1)')
plt.xlabel('Epochs')
plt.ylabel('Cost')
Exemple #20
0
gs = gridspec.GridSpec(2, 2)  #xw
X, y = make_moons(n_samples=100, random_state=123)
fig = plt.figure(figsize=(10, 8))

ppn = Perceptron(epochs=50, eta=0.05, random_seed=0)
ppn.fit(X, y)
ada = Adaline(epochs=50, eta=0.05, random_seed=0)
ada.fit(X, y)

mlp = MultiLayerPerceptron(n_output=len(np.unique(y)),
                           n_features=X.shape[1],
                           n_hidden=150,
                           l2=0.0,
                           l1=0.0,
                           epochs=500,
                           eta=0.01,
                           alpha=0.0,
                           decrease_const=0.0,
                           minibatches=1,
                           shuffle_init=False,
                           shuffle_epoch=False,
                           random_seed=0)

mlp = mlp.fit(X, y)

for clf, lab, grd in zip([ppn, ppn, mlp],
                         ['Perceptron', 'Adaline', 'MLP (logistic sigmoid)'],
                         itertools.product([0, 1], repeat=2)):

    clf.fit(X, y)
    ax = plt.subplot(gs[grd[0], grd[1]])