예제 #1
0
def test_serialize_deserialize():
    # Tests if the serialized neural network is the same to its deserialized version but different to other neural network
    a = neural_network(2, 4, 1)
    a.set_learning_rate(5)
    b = neural_network.deserialize(a.serialize())
    c = neural_network(2, 4, 1)
    assert type(a.serialize()) == bytes
    assert jsonpickle.encode(a) == jsonpickle.encode(b)
    assert jsonpickle.encode(a) != jsonpickle.encode(c)
    assert jsonpickle.encode(b) != jsonpickle.encode(c)
예제 #2
0
def test_train():
    # Tests the train method by using a very simple dataset
    a = neural_network(1, 8, 1)
    a.set_learning_rate(10)
    for x in range(3):
        a.train([x], [1])
    assert a.predict([1])[0] > 0.9
 def __init__(self, ind):
     # score
     # sprite info
     self.index = 0  # in sprite
     self.indexGen = cycle([0, 1, 2, 1])  # sprite cycle
     self.loopIter = 0  # to change index every 5th iteraton
     # position in screen
     self.x = -1
     self.y = -1
     self.playerShmVals = {'val': 0, 'dir': 1}
     self.GLOBAL_INDEX = ind
     self.brain = nn.neural_network(3, 1, 1)  ##input = dx, dy, y
     return
예제 #4
0
def face_recog_nn():

    # load the fe data
    fe = pd.read_csv('../../data/fwi_final.csv')

    # define what columns are predictors and what column is the response
    x_columns = [str(x) for x in range(0, 127 + 1)]
    y_column = ['id']

    # create train and test datasets
    x_train, x_test, y_train, y_test = train_test_split(fe[x_columns],
                                                        fe[y_column],
                                                        test_size=0.33,
                                                        random_state=42)
    print("x_train original shape: " + str(x_train.shape))
    print("x_test original shape: " + str(x_test.shape))

    # before transposing use stadardScaler to standardize data
    scaler = preprocessing.StandardScaler().fit(x_train)
    x_train = scaler.transform(x_train)
    x_test = scaler.transform(x_test)

    # reshape the training and test variables for use by NN
    x_train = x_train.T
    y_train = y_train.T
    x_test = x_test.T
    y_test = y_test.T

    # explore dataset
    num_pred, m_train = x_train.shape
    m_test = x_test.shape[1]
    print("Number of training examples: " + str(m_train))
    print("Number of testing examples: " + str(m_test))
    print("Number of predictors:" + str(num_pred))
    print("x_train's shape: " + str(x_train.shape))
    print("x_test's shape: " + str(x_test.shape))
    print("y_train shape: " + str(y_train.shape))
    print("y_test shape: " + str(y_test.shape))

    # initialize constants defining the model
    layer_dims = [128, 64, 32, 16, 12]  # 4-layer model
    nn = neural_network(layer_dims,
                        x_train,
                        y_train,
                        num_iterations=3000,
                        print_cost=True)
    nn.train()

    pred_train = nn.predict()
    pred_test = nn.predict(x_test, y_test)
예제 #5
0
def main():

    # Training data containing the 4 posible outputs
    trainig = [{
        'inputs': [0, 0],
        'output': [0]
    }, {
        'inputs': [0, 1],
        'output': [1]
    }, {
        'inputs': [1, 0],
        'output': [1]
    }, {
        'inputs': [1, 1],
        'output': [0]
    }]

    # Neural Network with 2 input nodes, 4 nodes in the hidden layer and 1 ouput node
    nn = neural_network(2, 4, 1)
    print('Training NN with 20000 iterations')
    for x in range(20000):
        # Use random data from the training set in every iteration
        data = random.choice(trainig)
        # Training with that data
        nn.train(data['inputs'], data['output'])
        # To print the progress every 10%
        if x % 2000 is 0:
            print((x / 2000 * 10), '%')

    # List of all the posible inputs
    tests = [[0, 0], [0, 1], [1, 0], [1, 1]]
    for test in tests:
        # Get the NN prediction of each input
        prediction = nn.predict(test)[0]
        # Print the results
        print('XOR:', test, 'PREDICTION:', round(prediction), 'PRECISION',
              round(prediction, 4))
예제 #6
0
def nnet(trainData, testData, hidden_count=3):
    print("***************In Neural Network module***********************")
    label_dict = {0: '0', 1: '90', 2: '180', 3: '270'}
    ids_train, X_train, Y_train = nn.getData(trainData)
    ids_test, X_test, Y_test = nn.getData(testData)
    neural_net = nn.neural_network(hidden_count, alpha=10e-7, iterations=1000)
    print("Fitting Training Data in neural_network of hidden_count:",
          hidden_count)
    neural_net.fit(X_train, Y_train)
    print("**************Predicting Test Data****************************")
    P_Y_given_X, Z = neural_net.predict(X_test)
    Predictions = np.argmax(P_Y_given_X, axis=1)
    print("Classification Accuracy:",
          neural_net.classification_rate(Y_test, Predictions))
    print("confusion_matrix")
    cm = neural_net.conf_matrix(label_dict, Y_test, Predictions)
    print(cm)
    print("making file nnet_output.txt")
    neural_net.make_file(label_dict, ids_test, Predictions)

    print("***********Saving Trained Neural Network Model..****************")
    f = open("model-nn.p", "w")
    pickle.dump(neural_net, f)
    f.close()
예제 #7
0
파일: main.py 프로젝트: olk/py3lnn
def create(input_nodes, hidden_nodes, output_nodes, learning_rate):
    return nn.neural_network(input_nodes, hidden_nodes, output_nodes,
                             learning_rate)
예제 #8
0
from nn import neural_network
import utils as utils

num_epochs = 1000

hidden_layers = 16
learning_rate = 0.1
mnist_1 = neural_network('MNIST', num_epochs, hidden_layers, learning_rate)
learning_rate = 0.01
mnist_2 = neural_network('MNIST', num_epochs, hidden_layers, learning_rate)

hidden_layers = 32
learning_rate = 0.1
mnist_3 = neural_network('MNIST', num_epochs, hidden_layers, learning_rate)
learning_rate = 0.01
mnist_4 = neural_network('MNIST', num_epochs, hidden_layers, learning_rate)

values = [mnist_1, mnist_2, mnist_3, mnist_4]
colors = ['cornflowerblue', 'mediumseagreen', 'indianred', 'darkgoldenrod']
labels = [
    'R = 0.1, H = 16', 'R = 0.01, H = 16', 'R = 0.1, H = 32',
    'R = 0.01, H = 32'
]

title = 'MNIST Loss During Training'
path = 'mnist/'
utils.make_dir(path)
path = path + 'loss.png'
utils.plot_together(values, colors, labels, title, path, False)
예제 #9
0
# Import necessary packages

import pandas as pd
import sys
from nn import neural_network

# Load our data as .csv file
dataset = pd.read_csv('chr22qc_example.csv')  # A" (m x n)

if (len(sys.argv) < 1):
    print("Please provide at least one hidden layer neurons number")
    exit(1)

hidden_neurons = []
for i in range(1, len(sys.argv)):
    hidden_neurons.append(int(sys.argv[i]))
print("Run neural network")
neural_network(dataset, hidden_neurons)

#print(dataset) # 2504 rows x 11159 columns
예제 #10
0
    def test_neural_net(self):
        obj = nn.neural_network([2, 3, 2, 4])
        data = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8]]
        target = [[1, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0],
                  [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1]]

        matrix = np.array([[[.1, .2, .3], [.4, .5, .6], [.7, .8, .9]],
                           [[.10, .11, .12, .13], [.14, .15, .16, .17]],
                           [[.18, .19, .20], [.21, .22, .23], [.24, .25, .26],
                            [.27, .28, .29]]])

        ans = np.array([
            0.002, 0.019, 0.025, 0.001, 0.033, 0.039, 0., 0.05, 0.057, 0.085,
            0.08, 0.089, 0.092, 0.086, 0.083, 0.092, 0.095, 0.355, 0.23, 0.243,
            0.371, 0.239, 0.253, 0.386, 0.25, 0.264, 0.402, 0.261, 0.275
        ])

        fprime = obj.back_propogation(data, target)
        vec = obj.roll_mat(matrix)
        cal = np.array(fprime(vec))
        self.assertAlmostEqual(cal.all(), ans.all(), delta=1)
        self.assertEqual(
            np.array(obj.unroll_vector(vec)).all(),
            np.array(matrix).all())
        obj.change_network_theta(matrix)
        cost_func = obj.compute_cost(data, target)
        cost = cost_func(vec)
        self.assertAlmostEqual(cost, 3.5, delta=1)

        obj = nn.neural_network([2, 3, 2, 4], activation_func='tanh')

        ans = np.array([
            0.007, 0.024, 0.03, 0.001, 0.033, 0.039, 0., 0.05, 0.056, 0.142,
            0.132, 0.147, 0.149, 0.127, 0.121, 0.135, 0.137, 0.102, 0.061,
            0.075, 0.152, 0.076, 0.095, 0.2, 0.097, 0.121, 0.246, 0.118, 0.148
        ])

        fprime = obj.back_propogation(data, target)
        vec = obj.roll_mat(matrix)
        cal = np.array(fprime(vec))
        self.assertAlmostEqual(cal.all(), ans.all(), delta=1)
        self.assertEqual(
            np.array(obj.unroll_vector(vec)).all(),
            np.array(matrix).all())
        obj.change_network_theta(matrix)
        cost_func = obj.compute_cost(data, target)
        cost = cost_func(vec)
        self.assertAlmostEqual(cost, 4.1, delta=1)

        f = open('train.csv', 'r')
        temp = csv.reader(f)
        train_data = []
        flag = 0
        for row in temp:
            if flag == 0:
                flag = 1
                continue
            train_data.append([float(row[0]), float(row[1])])

        f = open('test.csv', 'r')
        temp = csv.reader(f)
        test_data = []
        flag = 0
        for row in temp:
            if flag == 0:
                flag = 1
                continue
            test_data.append([float(row[0]), float(row[1])])

        f = open('train_target.csv', 'r')
        temp = csv.reader(f)
        target_train = []
        flag = 0
        for row in temp:
            if flag == 0:
                flag = 1
                continue
            target_train.append([float(row[0]), float(row[1])])

        f = open('test_target.csv', 'r')
        temp = csv.reader(f)
        target_test = []
        flag = 0
        for row in temp:
            if flag == 0:
                flag = 1
                continue
            target_test.append([float(row[0]), float(row[1])])

        train_data = train_data[0:100]
        target_train = target_train[0:100]

        circle = nn.neural_network([2, 4, 2], activation_func='tanh')
        circle.train(train_data, target_train)
        positive = 0
        num_examples = len(test_data)
        output = []
        for j in range(num_examples):
            output.append(circle.predict(test_data[j]))
            if output[-1] == target_test[j]:
                positive += 1
        accuracy = positive / num_examples
        self.assertAlmostEqual(accuracy, 0.95, delta=0.1)
예제 #11
0
from nn import neural_network
import utils as utils

num_epochs = 1000

hidden_layers = 32
learning_rate = 0.001
cifar100_1 = neural_network('CIFAR-100', num_epochs, hidden_layers, learning_rate)
learning_rate = 0.0001
cifar100_2 = neural_network('CIFAR-100', num_epochs, hidden_layers, learning_rate)

hidden_layers = 64
learning_rate = 0.001
cifar100_3 = neural_network('CIFAR-100', num_epochs, hidden_layers, learning_rate)
learning_rate = 0.0001
cifar100_4 = neural_network('CIFAR-100', num_epochs, hidden_layers, learning_rate)

values = [cifar100_1, cifar100_2, cifar100_3, cifar100_4]
colors = ['salmon', 'sienna', 'cadetblue', 'darkslateblue']
labels = ['R = 0.001, H = 32', 'R = 0.0001, H = 32', 'R = 0.001, H = 64', 'R = 0.0001, H = 64']

title = 'CIFAR-100 Loss During Training'
path = 'cifar-100/'
utils.make_dir(path)
path = path + 'loss.png'
utils.plot_together(values, colors, labels, title, path, False)
예제 #12
0
파일: driver.py 프로젝트: sant-sh/cosmos
import nn
import numpy as np
import os
import matplotlib.pyplot as plt

path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "mnist.npz")
with np.load(path) as data:
    training_images = data["training_images"]
    training_labels = data["training_labels"]

layer_sizes = (784, 5, 10)

net = nn.neural_network(layer_sizes)
net.accuracy(training_images, training_labels)
예제 #13
0
import nn
import numpy as np

"""
creating the neural network object with following dimensions:
2 input nodes
5 hidden layer 1 nodes
5 hidden layer 2 nodes
1 output node
"""

nn = nn.neural_network([2,5,5,1])

#Creating sample data for it to learn from, in this case I will make it learn on an AND table

x = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[0],[0],[1]])

#Training the neural network on the dummy dataset

nn.backward_pass(x,y,100000,0.1)

#Showing test results on a couple of sample data

x_test = np.array([[0,1],[1,1]])
print(nn.forward_pass(x_test))