Exemplo n.º 1
0
def optim():
    """Determine best lambda parameter"""
    #list of lambda values to try
    lambdas = [
        0, 0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24
    ]
    #lambdas = [2.56, 5.12, 10, 20, 40, 80, 160, 320, 640, 1280, 2560]

    #list to record training parameters
    histories = []
    J_train = []
    J_test = []
    accuracies = []

    for l in lambdas:
        #define and train model
        model = NeuralNet(alpha=0.003,
                          n_iter=40000,
                          lamb=l,
                          hidden_size=28,
                          n_labels=2)
        Theta1, Theta2, cost_history = model.fit(X_train, Y_train)

        #append parameters to lists
        histories.append(cost_history)
        J_train.append(cost_history[-1])
        X_test_1 = model.add_intercept(X_test)
        J_test.append(model.forward_prop(X_test_1, Y_test, Theta1, Theta2)[4])
        accuracies.append(model.predict(Theta1, Theta2, X_test, Y_test)[1])

    #plot costs VS lambda
    fig, ax = plt.subplots()
    ax.plot(lambdas, J_train, color="C0", label="training")
    ax.plot(lambdas, J_test, color="C2", label="testing")
    ax.set_xlabel("Lambda")
    ax.set_ylabel("Cost")
    ax.legend()
    plt.show()

    #plot accuracies VS lambdas
    fig, ax = plt.subplots()
    ax.plot(lambdas, accuracies)
    ax.set_xlabel("Lambda")
    ax.set_ylabel("Accuracy on test sample")
    plt.show()

    return accuracies
Exemplo n.º 2
0
def train():
    """Train the neural network"""
    model = NeuralNet(alpha=0.003,
                      n_iter=300000,
                      lamb=5,
                      hidden_size=28,
                      n_labels=2)

    start = time.time()
    Theta1, Theta2, cost_history = model.fit(X, Y)
    stop = time.time()
    elapsed = stop - start

    pred, accu = model.predict(Theta1, Theta2, X_test, Y_test)

    print("Time to train: {:.1f} seconds".format(elapsed))
    print("Model accuracy: {:.3f}".format(accu))
    model.plot_history(cost_history)

    return Theta1, Theta2, pred, accu, cost_history
Exemplo n.º 3
0
from parse_json import parse_json
import sys
import json
import numpy as np
import pandas as pd

if __name__ == "__main__":
    # <learning-rate> <#hidden-units> <#epochs> <train-set-file> <test-set-file>
    if len(sys.argv) == 1:
        learning_rate = 0.05
        hidden_units = 7
        num_epochs = 20
        train = "datasets/heart_train.json"
        test = "datasets/heart_test.json"
    else:
        tmp, learning_rate, hidden_units, num_epochs, train, test = sys.argv
        learning_rate = float(learning_rate)
        hidden_units = int(hidden_units)
        num_epochs = int(num_epochs)

    # parse the json file for data
    X_train, y_train, meta_train = parse_json(train)
    X_test, y_test, meta_test = parse_json(test)

    nn = NeuralNet(learning_rate=learning_rate,
                   num_epoch=num_epochs,
                   hidden_dim=(hidden_units, ))

    nn.fit(X_train, y_train, meta_train, verbose=True)
    nn.predict(X_test, y_true=y_test, verbose=True)
    hidden_units = 7
    num_epochs = 50
    train = "datasets/heart_train.json"
    test = "datasets/heart_test.json"
    
    # parse the json file for data
    X_train, y_train, meta_train = parse_json(train)
    X_test, y_test, meta_test = parse_json(test)
    
    print("model,epoch,F1_train,F1_test")
    # Run logistic regression 
    for epoch in range(1,num_epochs+1):
        logistic = NeuralNet(learning_rate=learning_rate, num_epoch=epoch,hidden_dim=())
              
        logistic.fit(X_train, y_train, meta_train)
        F1_train = logistic.predict(X_train, y_true=y_train, F1=True)
        F1_test = logistic.predict(X_test, y_true=y_test, F1=True)
        print("logistic-0.05-heart,{},{},{}".format(epoch,F1_train,F1_test))

    # Run neural network
    for epoch in range(1,num_epochs+1):
        nnet = NeuralNet(learning_rate=learning_rate, num_epoch=epoch,hidden_dim=(hidden_units,))
              
        nnet.fit(X_train, y_train, meta_train)
        F1_train = nnet.predict(X_train, y_true=y_train, F1=True)
        F1_test = nnet.predict(X_test, y_true=y_test, F1=True)
        print("nnet-0.05-7-heart,{},{},{}".format(epoch,F1_train,F1_test))