|
5 | 5 | import time
|
6 | 6 | import random
|
7 | 7 |
|
8 |
| -np.set_printoptions(linewidth=200) |
9 |
| -random_seed = random.randint(10, 1010) |
10 |
| -np.random.seed(random_seed) |
11 |
| - |
12 |
| - |
13 | 8 | class NeuralNetwork:
|
14 | 9 |
|
15 | 10 | def __init__(self, layer_sizes, layer_activations, learning_rate=0.1, low=-2, high=2):
|
@@ -62,39 +57,41 @@ def calculate_SSE(self, input_data, desired_output_data):
|
62 | 57 | desired_output_data = np.array(desired_output_data).reshape(-1, 1)
|
63 | 58 | assert len(input_data) == self.layer_sizes[0]
|
64 | 59 | assert len(desired_output_data) == self.layer_sizes[-1]
|
65 |
| - #Error is difference between desired_output & actual output. |
66 | 60 | return np.sum(np.power(desired_output_data - self.calculate_output(input_data), 2))
|
67 | 61 |
|
68 | 62 | def print_weights_and_biases(self):
|
69 | 63 | print(self.weights)
|
70 | 64 | print(self.biases)
|
71 | 65 |
|
72 |
| -print("Random seed: " + str(random_seed)) |
73 |
| - |
74 |
| -data_input = h.import_from_csv("data/features.txt", float) |
75 |
| -data_output = h.import_from_csv("data/targets.txt", int) |
76 |
| -data_output = np.array([h.class_to_array(np.amax(data_output), x) for x in data_output]) |
77 |
| - |
78 |
| -train_input, validate_input, test_input = h.kfold(4, data_input, random_seed) |
79 |
| -train_output, validate_output, test_output = h.kfold(4, data_output, random_seed) |
80 | 66 |
|
81 |
| -nn = NeuralNetwork(layer_sizes=[10, 15, 7], layer_activations=["sigmoid", "sigmoid"]) |
82 |
| - |
83 |
| -print("Beginning training") |
84 |
| -previous_mse = 1 |
85 |
| -current_mse = 0 |
86 |
| -num_epochs = 0 |
87 |
| -while(current_mse < previous_mse): |
88 |
| - previous_mse = h.calculate_MSE(nn, validate_input, validate_output) |
89 |
| - for i in range(len(train_input)): |
90 |
| - nn.train(train_input[i], train_output[i]) |
91 |
| - current_mse = h.calculate_MSE(nn, validate_input, validate_output) |
92 |
| - |
93 |
| - num_epochs += 1 |
94 |
| - if num_epochs % 10 == 0: print("Epoch: " + str(num_epochs) + " MSE: " + str(current_mse)) |
95 |
| - |
96 |
| - |
97 |
| -train_mse = h.calculate_MSE(nn, train_input, train_output) |
98 |
| -test_mse = h.calculate_MSE(nn, test_input, test_output) |
99 |
| -print("Results:") |
100 |
| -print("Tr: " + str(train_mse) + " V: " + str(current_mse) + " T: " + str(test_mse)) |
| 67 | +np.set_printoptions(linewidth=200) |
| 68 | +for i in range(5): |
| 69 | + random_seed = random.randint(10, 1010) |
| 70 | + np.random.seed(random_seed) |
| 71 | + |
| 72 | + data_input = h.import_from_csv("data/features.txt", float) |
| 73 | + data_output = h.import_from_csv("data/targets.txt", int) |
| 74 | + data_output = np.array([h.class_to_array(np.amax(data_output), x) for x in data_output]) |
| 75 | + |
| 76 | + train_input, validate_input, test_input = h.kfold(4, data_input, random_seed) |
| 77 | + train_output, validate_output, test_output = h.kfold(4, data_output, random_seed) |
| 78 | + |
| 79 | + nn = NeuralNetwork(layer_sizes=[10, 15, 7], layer_activations=["sigmoid", "sigmoid"]) |
| 80 | + |
| 81 | + # print("Beginning training") |
| 82 | + previous_mse = 1 |
| 83 | + current_mse = 0 |
| 84 | + epochs = 0 |
| 85 | + while(current_mse < previous_mse): |
| 86 | + previous_mse = h.calculate_MSE(nn, validate_input, validate_output) |
| 87 | + for i in range(len(train_input)): |
| 88 | + nn.train(train_input[i], train_output[i]) |
| 89 | + current_mse = h.calculate_MSE(nn, validate_input, validate_output) |
| 90 | + |
| 91 | + epochs += 1 |
| 92 | + # if epochs % 10 == 0: print("Epoch: " + str(epochs) + " MSE: " + str(current_mse)) |
| 93 | + |
| 94 | + |
| 95 | + train_mse = h.calculate_MSE(nn, train_input, train_output) |
| 96 | + test_mse = h.calculate_MSE(nn, test_input, test_output) |
| 97 | + print("Random_Seed: " + str(random_seed) + " Epochs: " + str(epochs) + " Tr: " + str(train_mse) + " V: " + str(current_mse) + " T: " + str(test_mse)) |
0 commit comments