|
| 1 | +# python-neural-network |
| 2 | +A simple fully connected feed forward neural network written in python from scratch using numpy. It is possible to have multiple hidden layers, change the amount of neurons per layer & have a different activation function per layer. |
| 3 | + |
| 4 | +Written in python 3.7.7 |
| 5 | + |
| 6 | +If you have any tips on how to imporve performace, let me know! |
| 7 | + |
| 8 | +``` |
| 9 | +import numpy as np |
| 10 | +import z_helper as h |
| 11 | +``` |
| 12 | + |
| 13 | +``` |
| 14 | + random_seed = random.randint(10, 1010) |
| 15 | + np.random.seed(random_seed) |
| 16 | +
|
| 17 | + data_input = h.import_from_csv("data/features.txt", float) |
| 18 | + data_output = h.import_from_csv("data/targets.txt", int) |
| 19 | + data_output = np.array([h.class_to_array(np.amax(data_output), x) for x in data_output]) |
| 20 | +
|
| 21 | + train_input, validate_input, test_input = h.kfold(4, data_input, random_seed) |
| 22 | + train_output, validate_output, test_output = h.kfold(4, data_output, random_seed) |
| 23 | +
|
| 24 | + nn = NeuralNetwork(layer_sizes=[10, 15, 7], layer_activations=["sigmoid", "sigmoid"]) |
| 25 | +
|
| 26 | + # print("Beginning training") |
| 27 | + previous_mse = 1 |
| 28 | + current_mse = 0 |
| 29 | + epochs = 0 |
| 30 | + while(current_mse < previous_mse): |
| 31 | + previous_mse = h.calculate_MSE(nn, validate_input, validate_output) |
| 32 | + for i in range(len(train_input)): |
| 33 | + nn.train(train_input[i], train_output[i]) |
| 34 | + current_mse = h.calculate_MSE(nn, validate_input, validate_output) |
| 35 | + |
| 36 | + epochs += 1 |
| 37 | + # if epochs % 10 == 0: print("Epoch: " + str(epochs) + " MSE: " + str(current_mse)) |
| 38 | +
|
| 39 | +
|
| 40 | + train_mse = h.calculate_MSE(nn, train_input, train_output) |
| 41 | + test_mse = h.calculate_MSE(nn, test_input, test_output) |
| 42 | + print("Random_Seed: " + str(random_seed) + " Epochs: " + str(epochs) + " Tr: " + str(train_mse) + " V: " + str(current_mse) + " T: " + str(test_mse)) |
| 43 | +``` |
0 commit comments