def _clf_mlp(trX,teX,trY,teY):
	print "MLP"
	print trX.shape,"trX shape"
	print "Enter Layer for MLP"
	layer=input()
	# print "enter delIdx"
	# delIdx=input()
	# while(delIdx):
	# 	trX=np.delete(trX,-1,axis=0)
	# 	trY=np.delete(trY,-1,axis=0)
	# 	delIdx=delIdx-1
	print "factors",factors(trX.shape[0])	
	teY=teY.astype(np.int32)
	trY=trY.astype(np.int32)
	print trX.shape,"trX shape"
	print "enter no of mini batch"
	mini_batch=int(input())
	mlp = TfMultiLayerPerceptron(eta=0.01, 
                             epochs=100, 
                             hidden_layers=layer,
                             activations=['relu' for i in range(len(layer))],
                             print_progress=3, 
                             minibatches=mini_batch, 
                             optimizer='adam',
                             random_seed=1)
	mlp.fit(trX,trY)
	pred=mlp.predict(teX)
	print _f_count(teY),"test f count"
	pred=pred.astype(np.int32)
	print _f_count(pred),"pred f count"
	conf_mat=confusion_matrix(teY, pred)
	process_cm(conf_mat, to_print=True)
	print precision_score(teY,pred),"Precision Score"
	print recall_score(teY,pred),"Recall Score"
	print roc_auc_score(teY,pred), "ROC_AUC"
Beispiel #2
0
def test_score_function_ftrl():
    mlp = MLP(
        epochs=100, eta=0.5, hidden_layers=[5], optimizer="ftrl", activations=["logistic"], minibatches=1, random_seed=1
    )
    mlp.fit(X, y)
    acc = mlp.score(X, y)
    assert acc == 1.0, acc
def test_fail_minibatches():
    mlp = MLP(epochs=100,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=13,
              random_seed=1)
    mlp.fit(X, y)
    assert (y == mlp.predict(X)).all()
def test_fail_minibatches():
    mlp = MLP(epochs=100,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=13,
              random_seed=1)
    mlp.fit(X, y)
    assert (y == mlp.predict(X)).all()
def test_valid_acc():
    mlp = MLP(epochs=3,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)

    mlp.fit(X, y, X_valid=X[:100], y_valid=y[:100])
    assert len(mlp.valid_acc_) == 3
def test_train_acc():
    mlp = MLP(epochs=3,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)

    mlp.fit(X, y)
    assert len(mlp.train_acc_) == 3
def test_score_function_adagrad():
    mlp = MLP(epochs=100,
              eta=0.5,
              hidden_layers=[5],
              optimizer='adagrad',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    acc = mlp.score(X, y)
    assert acc == 1.0, acc
def test_score_function_adagrad():
    mlp = MLP(epochs=100,
              eta=0.5,
              hidden_layers=[5],
              optimizer='adagrad',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    acc = mlp.score(X, y)
    assert acc == 1.0, acc
def test_train_acc():
    mlp = MLP(epochs=3,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)

    mlp.fit(X, y)
    assert len(mlp.train_acc_) == 3
def test_valid_acc():
    mlp = MLP(epochs=3,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)

    mlp.fit(X, y, X_valid=X[:100], y_valid=y[:100])
    assert len(mlp.valid_acc_) == 3
def test_binary_sgd():
    mlp = MLP(epochs=10,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=len(y_bin),
              random_seed=1)

    mlp.fit(X_bin, y_bin)
    assert (y_bin == mlp.predict(X_bin)).all()
def test_binary_sgd():
    mlp = MLP(epochs=10,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=len(y_bin),
              random_seed=1)

    mlp.fit(X_bin, y_bin)
    assert (y_bin == mlp.predict(X_bin)).all()
def test_multiclass_gd_dropout():
    mlp = MLP(epochs=100,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1,
              dropout=0.05)
    mlp.fit(X, y)
    acc = round(mlp.score(X, y), 2)
    assert acc == 0.67, acc
def test_multiclass_gd_dropout():
    mlp = MLP(epochs=100,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1,
              dropout=0.05)
    mlp.fit(X, y)
    acc = round(mlp.score(X, y), 2)
    assert acc == 0.67, acc
Beispiel #15
0
def test_multiclass_gd_acc():
    mlp = MLP(
        epochs=100,
        eta=0.5,
        hidden_layers=[5],
        optimizer="gradientdescent",
        activations=["logistic"],
        minibatches=1,
        random_seed=1,
    )
    mlp.fit(X, y)
    assert (y == mlp.predict(X)).all()
def test_multiclass_gd_learningdecay():
    mlp = MLP(epochs=5,
              eta=0.5,
              hidden_layers=[15],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              decay=[0.5, 1.0],
              random_seed=1)
    mlp.fit(X, y)
    expect = [3.11, 2.12, 1.79, 1.65, 1.59]
    np.testing.assert_almost_equal(expect, mlp.cost_, decimal=2)
def test_multiclass_gd_learningdecay():
    mlp = MLP(epochs=5,
              eta=0.5,
              hidden_layers=[15],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              decay=[0.5, 1.0],
              random_seed=1)
    mlp.fit(X, y)
    expect = [3.107878, 2.124671, 1.786916, 1.65095, 1.590468]
    np.testing.assert_almost_equal(expect, mlp.cost_, decimal=2)
def test_multiclass_probas():
    mlp = MLP(epochs=500,
              eta=0.5,
              hidden_layers=[10],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    idx = [0, 50, 149]  # sample labels: 0, 1, 2
    y_pred = mlp.predict_proba(X[idx])
    exp = np.array([[1.0, 0.0, 0.0], [0.0, 0.9, 0.1], [0.0, 0.1, 0.9]])
    np.testing.assert_almost_equal(y_pred, exp, 1)
Beispiel #19
0
def test_binary_gd_relu():
    mlp = MLP(
        epochs=100,
        eta=0.5,
        hidden_layers=[5],
        optimizer="gradientdescent",
        activations=["relu"],
        minibatches=1,
        random_seed=1,
    )

    mlp.fit(X_bin, y_bin)
    assert (y_bin == mlp.predict(X_bin)).all()
def test_multiclass_probas():
    mlp = MLP(epochs=500,
              eta=0.5,
              hidden_layers=[10],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    idx = [0, 50, 149]  # sample labels: 0, 1, 2
    y_pred = mlp.predict_proba(X[idx])
    exp = np.array([[1.0, 0.0, 0.0],
                    [0.0, 0.9, 0.1],
                    [0.0, 0.1, 0.9]])
    np.testing.assert_almost_equal(y_pred, exp, 1)
def test_continue_learning():
    mlp = MLP(epochs=25,
              eta=0.5,
              hidden_layers=[5],
              optimizer='gradientdescent',
              activations=['logistic'],
              minibatches=1,
              random_seed=1)
    mlp.fit(X, y)
    assert np.sum(y == mlp.predict(X)) == 144, np.sum(y == mlp.predict(X))
    mlp.fit(X, y, init_params=False)
    assert np.sum(y == mlp.predict(X)) == 150, np.sum(y == mlp.predict(X))
def test_mapping():
    mlp = MLP()
    w, b = mlp._layermapping(n_features=10,
                             n_classes=11,
                             hidden_layers=[8, 7, 6])

    expect_b = {1: [[8], 'n_hidden_1'],
                2: [[7], 'n_hidden_2'],
                3: [[6], 'n_hidden_3'],
                'out': [[11], 'n_classes']}

    expect_w = {1: [[10, 8], 'n_features, n_hidden_1'],
                2: [[8, 7], 'n_hidden_1, n_hidden_2'],
                3: [[7, 6], 'n_hidden_2, n_hidden_3'],
                'out': [[6, 11], 'n_hidden_3, n_classes']}

    assert expect_b == b, b
    assert expect_w == w, w
def test_mapping():
    mlp = MLP()
    w, b = mlp._layermapping(n_features=10,
                             n_classes=11,
                             hidden_layers=[8, 7, 6])

    expect_b = {1: [[8], 'n_hidden_1'],
                2: [[7], 'n_hidden_2'],
                3: [[6], 'n_hidden_3'],
                'out': [[11], 'n_classes']}

    expect_w = {1: [[10, 8], 'n_features, n_hidden_1'],
                2: [[8, 7], 'n_hidden_1, n_hidden_2'],
                3: [[7, 6], 'n_hidden_2, n_hidden_3'],
                'out': [[6, 11], 'n_hidden_3, n_classes']}

    assert expect_b == b, b
    assert expect_w == w, w
Beispiel #24
0
def test_mapping():
    mlp = MLP()
    w, b = mlp._layermapping(n_features=10, n_classes=11, hidden_layers=[8, 7, 6])

    expect_b = {
        "1": [[8], "n_hidden_1"],
        "2": [[7], "n_hidden_2"],
        "3": [[6], "n_hidden_3"],
        "out": [[11], "n_classes"],
    }

    expect_w = {
        "1": [[10, 8], "n_features, n_hidden_1"],
        "2": [[8, 7], "n_hidden_1, n_hidden_2"],
        "3": [[7, 6], "n_hidden_2, n_hidden_3"],
        "out": [[6, 11], "n_hidden_3, n_classes"],
    }

    assert expect_b == b, b
    assert expect_w == w, w
Beispiel #25
0
def _clf_mlp(trX, teX, trY, teY):
    print "MLP"
    print trX.shape, "trX shape"
    print "Enter Layer for MLP"
    layer = input()
    # print "enter delIdx"
    # delIdx=input()
    # while(delIdx):
    # 	trX=np.delete(trX,-1,axis=0)
    # 	trY=np.delete(trY,-1,axis=0)
    # 	delIdx=delIdx-1
    print "factors", factors(trX.shape[0])
    teY = teY.astype(np.int32)
    trY = trY.astype(np.int32)
    print trX.shape, "trX shape"
    print "enter no of mini batch"
    mini_batch = int(input())
    mlp = TfMultiLayerPerceptron(
        eta=0.01,
        epochs=100,
        hidden_layers=layer,
        activations=['relu' for i in range(len(layer))],
        print_progress=3,
        minibatches=mini_batch,
        optimizer='adam',
        random_seed=1)
    mlp.fit(trX, trY)
    pred = mlp.predict(teX)
    print _f_count(teY), "test f count"
    pred = pred.astype(np.int32)
    print _f_count(pred), "pred f count"
    conf_mat = confusion_matrix(teY, pred)
    process_cm(conf_mat, to_print=True)
    print precision_score(teY, pred), "Precision Score"
    print recall_score(teY, pred), "Recall Score"
    print roc_auc_score(teY, pred), "ROC_AUC"
Beispiel #26
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 = TfMultiLayerPerceptron(eta=0.5,
                             epochs=20,
                             hidden_layers=[10],
                             activations=['logistic'],
                             optimizer='gradientdescent',
                             print_progress=3,
                             minibatches=1,
                             random_seed=1)


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()