def test_nn(hid_node, learning_rate, activation_function, pruning_choice):
    acc_stat = []
    con_mat_stat = []
    for i in range(len(fold_groups)):
        print("Round: " + str(i + 1))

        # Select test set and train data
        test_group_idx = i
        test_X, test_y = fold_groups[i]

        train_group_idx = list(five_sets - {i})
        train_X = [fold_groups[idx][0] for idx in train_group_idx]
        train_y = [fold_groups[idx][1] for idx in train_group_idx]

        train_X = np.concatenate(train_X)
        train_y = np.concatenate(train_y)

        # Initialize model
        in_node = train_X.shape[1]
        out_node = 1
        NN = NeuralNetwork(in_node,
                           hid_node,
                           out_node,
                           activation=activation_function,
                           seed=None)

        # Train the model
        if pruning_choice:
            NN.fit_model(train_X,
                         train_y,
                         learning_epochs=20,
                         learning_rate=learning_rate,
                         pruning=pruning_choice,
                         pruning_threshold=0.2,
                         pruning_norm='L1',
                         pruning_epoch=10)
        else:
            NN.fit_model(train_X,
                         train_y,
                         learning_epochs=20,
                         learning_rate=learning_rate,
                         pruning=pruning_choice)
        acc = NN.accuracy(test_X, test_y)

        # Compute accuracy and confusion matrix
        acc = 0
        y_pred = NN.predict_result(test_X)
        con_mat = np.zeros((2, 2))
        for i in range(len(y_pred)):
            con_mat[int(y_pred[i]), int(test_y[i])] += 1
            if test_y[i] == y_pred[i]:
                acc += 1
        con_mat_stat.append(con_mat)

        acc = acc / len(y_pred)
        acc_stat.append(acc)

        print('\nACCURACY: ', acc)
        print('CONFUSION MATRIX: \n', con_mat)
        print("\n")

    print("\nACCURACY AVERAGE: ", np.average(acc_stat))
    print("CONFUSION MATRIX AVERAGE: \n", np.average(con_mat_stat, axis=0))
示例#2
0
print("Performing Principal Component Analysis...")
# Perform Principal Component Analysis on the train and test data
for j in range(len(train_data)):
    train_data[j] = pca.fit_transform(train_data[j]).ravel()
for j in range(len(test_data)):
    test_data[j] = pca.fit_transform(test_data[j]).ravel()

accuracy = 0
# create an instance of the network
nn = NeuralNetwork(1000, 500, 3, 0.01)

print("Training...")
# iterate N times perform training at each step
for epoch in range(100):
    for j in range(len(train_data)):
        nn.train(train_data[j], labels[j])
    accuracy = nn.accuracy(test_data, test_labels)
    print("\tepoch {i}, accuracy = {accuracy}".format(i=epoch + 1,
                                                      accuracy=accuracy))

# ask the user if they'd like to save the trained model, then the appropriate action is taken
save = input("Save current model with accuracy %.4f? [Y/N]: " % accuracy)
if save == "Y":
    model_file_path = "models/model.json"
    nn.write_to_file(model_file_path)
    print("Model saved to %s" % model_file_path)
else:
    print("Model not saved.")
print("Done")
示例#3
0
    hid_node = 7
    out_node = 3
    NN = NeuralNetwork(in_node,
                       hid_node,
                       out_node,
                       activation='sigmoid',
                       seed=None)

    # Train the model
    NN.fit_model(train_X,
                 train_y,
                 learning_epochs=500,
                 learning_rate=0.2,
                 pruning=False,
                 verbose=False)
    acc = NN.accuracy(test_X, test_y)

    # compute accuracy and confusion matrix
    acc = 0
    y_pred = NN.predict_result(test_X)
    con_mat = np.zeros((3, 3))
    for i in range(len(y_pred)):
        con_mat[int(y_pred[i]), int(test_y[i])] += 1
        if test_y[i] == y_pred[i]:
            acc += 1
    con_mat_stat.append(con_mat)

    acc = acc / len(y_pred)
    acc_stat.append(acc)

    print('\nACCURACY: ', acc)
示例#4
0
print("Performing Principal Component Analysis...")
# Perform Principal Component Analysis on the train and test data
for j in range(len(train_data)):
    train_data[j] = pca.fit_transform(train_data[j]).ravel()
for j in range(len(test_data)):
    test_data[j] = pca.fit_transform(test_data[j]).ravel()

accuracy = 0
# create an instance of the network
nn = NeuralNetwork(1000, 500, 3, 0.01)

print("Training...")
# iterate N times perform training at each step
for epoch in range(100):
    for j in range(len(train_data)):
        nn.train(train_data[j], labels[j])
    accuracy = nn.accuracy(train_data, labels)
    print("\tepoch {i}, accuracy = {accuracy}".format(i=epoch + 1,
                                                      accuracy=accuracy))

# ask the user if they'd like to save the trained model, then the appropriate action is taken
save = input("Save current model with accuracy %.4f? [Y/N]: " % accuracy)
if save == "Y":
    model_file_path = "models/model.json"
    nn.write_to_file(model_file_path)
    print("Model saved to %s" % model_file_path)
else:
    print("Model not saved.")
print("Done")