Skip to content

Commit 2bf0f7b

Browse files
committed
new attempt at batching training
1 parent cfdf083 commit 2bf0f7b

File tree

6 files changed

+252
-11
lines changed

6 files changed

+252
-11
lines changed

data/ci_2_inputs.npy

614 KB
Binary file not shown.

data/ci_2_outputs.npy

430 KB
Binary file not shown.

numpy_main_2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
import numpy_neural_network_2 as nn
3+
import z_helper as h
4+
import time
5+
np.set_printoptions(linewidth=200)
6+
7+
data_input = np.load("data/ci_2_inputs.npy")
8+
data_output = np.load("data/ci_2_outputs.npy")
9+
10+
print(data_input.shape)
11+
print(data_output.shape)
12+
13+
print("Begin compiling!")
14+
begin_time = time.time_ns()
15+
compiled_nn_values = nn.make_neural_network(layer_sizes=[data_input.shape[1], data_output.shape[1]], layer_activations=[h.sigmoid])
16+
nn.train_auto(data_input[:10], data_output[:10], data_input[10: 15], data_output[10: 15], compiled_nn_values)
17+
end_time = time.time_ns()
18+
print("Compile time:", (end_time-begin_time) / 1e9)
19+
20+
np.random.seed(420)
21+
total_accuracy = 0.0
22+
begin_total = time.time_ns()
23+
n = 10
24+
for i in range(n):
25+
26+
random_seed = np.random.randint(10, 1010)
27+
np.random.seed(random_seed)
28+
29+
train_input, validate_input, test_input = h.kfold(7, data_input, random_seed)
30+
train_output, validate_output, test_output = h.kfold(7, data_output, random_seed)
31+
nn_values = nn.make_neural_network(layer_sizes=[train_input.shape[1], 20, train_output.shape[1]], layer_activations=[h.sigmoid, h.softmax_2])
32+
33+
begin_time = time.time_ns()
34+
epochs, current_mse = nn.train_auto(train_input, train_output, validate_input, validate_output, nn_values)
35+
end_time = time.time_ns()
36+
37+
train_mse = nn.calculate_MSE(train_input, train_output, nn_values)
38+
test_mse = nn.calculate_MSE(test_input, test_output, nn_values)
39+
40+
accuracy_test = nn.evaluate(test_input, test_output, nn_values)
41+
total_accuracy += accuracy_test
42+
print("Seed:", random_seed, "Epochs:", epochs, "Time:", (end_time-begin_time)/1e9, "Accuracy:", accuracy_test, "Tr:", train_mse, "V:", current_mse, "T:", test_mse)
43+
print("Average Accuracy:", total_accuracy / n, "Average Time:", ((time.time_ns()-begin_total)/1e9) / n)

numpy_neural_network_2.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import numpy as np
2+
from numba.experimental import jitclass
3+
from numba import njit, types, typed, prange
4+
import z_helper as h
5+
import time
6+
7+
from numba.core.errors import NumbaTypeSafetyWarning
8+
import warnings
9+
10+
warnings.simplefilter('ignore', category=NumbaTypeSafetyWarning)
11+
12+
# spec = [
13+
# ("layer_sizes", types.ListType(types.int64)),
14+
# ("layer_activations", types.ListType(types.FunctionType(types.float64[:, ::1](types.float64[:, ::1], types.boolean)))),
15+
# ("weights", types.ListType(types.float64[:, ::1])),
16+
# ("biases", types.ListType(types.float64[:, ::1])),
17+
# ("layer_outputs", types.ListType(types.float64[:, ::1])),
18+
# ("learning_rate", types.float64),
19+
# ]
20+
# @jitclass(spec)
21+
22+
23+
class NeuralNetwork:
24+
def __init__(self, layer_sizes, layer_activations, weights, biases, layer_outputs, learning_rate):
25+
self.layer_sizes = layer_sizes
26+
self.layer_activations = layer_activations
27+
self.weights = weights
28+
self.biases = biases
29+
self.layer_outputs = layer_outputs
30+
self.learning_rate = learning_rate
31+
32+
33+
def make_neural_network(layer_sizes, layer_activations, learning_rate=0.05, low=-2, high=2):
34+
for size in layer_sizes:
35+
assert size > 0
36+
37+
# Initialize typed layer sizes list.
38+
# typed_layer_sizes = typed.List()
39+
# for size in layer_sizes:
40+
# typed_layer_sizes.append(size)
41+
# print(typeof(typed_layer_sizes))
42+
typed_layer_sizes = layer_sizes
43+
44+
# Initialie typed layer activation method strings list.
45+
# prototype = types.FunctionType(types.float64[:, ::1](types.float64[:, ::1], types.boolean))
46+
# typed_layer_activations = typed.List.empty_list(prototype)
47+
# for activation in layer_activations:
48+
# typed_layer_activations.append(activation)
49+
# print(typedof(typed_layer_activations))
50+
typed_layer_activations = layer_activations
51+
52+
# Initialize weights between every neuron in all adjacent layers.
53+
# typed_weights = typed.List()
54+
# for i in range(1, len(layer_sizes)):
55+
# typed_weights.append(np.random.uniform(low, high, (layer_sizes[i-1], layer_sizes[i])))
56+
# print(typeof(typed_weights))
57+
typed_weights = [np.random.uniform(low, high, (layer_sizes[i-1], layer_sizes[i])) for i in range(1, len(layer_sizes))]
58+
59+
# Initialize biases for every neuron in all layers
60+
# typed_biases = typed.List()
61+
# for i in range(1, len(layer_sizes)):
62+
# typed_biases.append(np.random.uniform(low, high, (layer_sizes[i], 1)))
63+
# print(typeof(typed_biases))
64+
typed_biases = [np.random.uniform(low, high, (layer_sizes[i],)) for i in range(1, len(layer_sizes))]
65+
66+
# Initialize empty list of output of every neuron in all layers.
67+
# typed_layer_outputs = typed.List()
68+
# for i in range(len(layer_sizes)):
69+
# typed_layer_outputs.append(np.zeros((layer_sizes[i], 1)))
70+
# print(typeof(typed_layer_outputs))
71+
typed_layer_outputs = [np.zeros((layer_sizes[i],1)) for i in range(len(layer_sizes))]
72+
73+
typed_learning_rate = learning_rate
74+
return NeuralNetwork(typed_layer_sizes, typed_layer_activations, typed_weights, typed_biases, typed_layer_outputs, typed_learning_rate)
75+
76+
77+
# @njit
78+
def calculate_output(input_data, nn):
79+
assert input_data.shape[1] == nn.layer_sizes[0]
80+
y = input_data
81+
for i in prange(len(nn.weights)):
82+
y = nn.layer_activations[i](np.dot(y, nn.weights[i]) + nn.biases[i], False)
83+
return y
84+
85+
86+
# @njit
87+
def feed_forward_layers(input_data, nn):
88+
assert input_data.shape[1] == nn.layer_sizes[0]
89+
nn.layer_outputs[0] = input_data
90+
for i in prange(len(nn.weights)):
91+
ac = np.dot(nn.layer_outputs[i], nn.weights[i]) + nn.biases[i]
92+
nn.layer_outputs[i+1] = nn.layer_activations[i](ac, False)
93+
94+
def train_batch(input_data, desired_output_data, nn):
95+
feed_forward_layers(input_data, nn)
96+
error = (desired_output_data - nn.layer_outputs[-1]) * nn.layer_activations[-1](nn.layer_outputs[-1], True)
97+
98+
temp_weights = []
99+
temp_biases = []
100+
101+
temp_weights.insert(0, nn.weights[-1] + nn.learning_rate * np.dot(nn.layer_outputs[-2].T, error) / input_data.shape[0])
102+
temp_biases.insert(0, nn.biases[-1] + nn.learning_rate * np.average(error, axis=0))
103+
104+
length_weights = len(nn.weights)
105+
for i in range(1, length_weights):
106+
i = length_weights - i - 1
107+
error = np.dot(error, nn.weights[i+1].T) * nn.layer_activations[i](nn.layer_outputs[i+1], True)
108+
temp_weights.insert(0, nn.weights[i] + nn.learning_rate * np.dot(nn.layer_outputs[i].T, error) / input_data.shape[0])
109+
temp_biases.insert(0, nn.biases[i] + nn.learning_rate * np.average(error, axis=0))
110+
111+
nn.weights = temp_weights
112+
nn.biases = temp_biases
113+
114+
115+
# @njit(parallel=True)
116+
def calculate_MSE(input_data, desired_output_data, nn):
117+
assert input_data.shape[0] == desired_output_data.shape[0]
118+
sum_error = np.sum(np.power(desired_output_data - calculate_output(input_data, nn), 2))
119+
return sum_error / len(input_data)
120+
121+
122+
# @njit
123+
def train_auto(train_input_data, train_desired_output_data, validate_input_data, validate_output_data, nn):
124+
previous_mse = 1.0
125+
current_mse = 0.0
126+
epochs = 0
127+
batch_size = 8
128+
129+
130+
while(current_mse < previous_mse):
131+
epochs += 1
132+
previous_mse = calculate_MSE(validate_input_data, validate_output_data, nn)
133+
b,e = 0, batch_size
134+
while(e < len(train_input_data) + 1):
135+
train_batch(train_input_data[b:e], train_desired_output_data[b:e], nn)
136+
b += batch_size
137+
e += batch_size
138+
current_mse = calculate_MSE(validate_input_data, validate_output_data, nn)
139+
return epochs, current_mse
140+
141+
142+
# @njit(parallel=True)
143+
def evaluate(input_data, desired_output_data, nn):
144+
output_max = calculate_output(input_data, nn).argmax(axis=1)
145+
desired_output_max = desired_output_data.argmax(axis=1)
146+
difference_output_max = output_max - desired_output_max
147+
correct = np.count_nonzero(difference_output_max == 0)
148+
return correct / input_data.shape[0]
149+
150+
151+
# @njit
152+
def print_weights_and_biases(nn):
153+
weights = np.clip(nn.weights[0], 0.001, 0.999)
154+
biases = np.clip(nn.biases[0], 0.001, 0.999)
155+
print(weights)
156+
print(biases)

prepare_data.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ def prepare_ci_data():
2929
data_output = h.import_from_csv("data/ci_targets.txt", int)
3030

3131
data_output = np.array([h.class_to_array(np.amax(data_output), x) for x in data_output])
32-
data_input = data_input.reshape((len(data_input), -1, 1))
33-
data_output = data_output.reshape((len(data_output), -1, 1))
34-
np.save("data/ci_inputs", data_input)
35-
np.save("data/ci_outputs", data_output)
3632

37-
prepare_mnist_data()
33+
# print(data_input.shape)
34+
# print(data_output.shape)
35+
36+
# data_input = data_input.reshape((len(data_input), -1, 1))
37+
# data_output = data_output.reshape((len(data_output), -1, 1))
38+
np.save("data/ci_2_inputs", data_input)
39+
np.save("data/ci_2_outputs", data_output)
40+
41+
prepare_ci_data()

z_helper.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from numba import njit
2+
from numba import njit, typeof
33

44

55
def import_from_csv(path, data_type):
@@ -19,31 +19,69 @@ def kfold(k, data, seed=99):
1919
return data[fold_size*2:], data[:fold_size], data[fold_size:fold_size*2]
2020

2121

22-
@njit('float64[:, ::1](float64[:, ::1], boolean)')
22+
@njit
23+
def np_func(npfunc, axis, arr):
24+
assert arr.ndim == 2
25+
assert axis in [0, 1]
26+
if axis == 0:
27+
result = np.empty(arr.shape[1])
28+
for i in range(len(result)):
29+
result[i] = npfunc(arr[:, i])
30+
else:
31+
result = np.empty(arr.shape[0])
32+
for i in range(len(result)):
33+
result[i] = npfunc(arr[i, :])
34+
return result
35+
36+
37+
@njit
38+
def np_argmax(axis, arr):
39+
return np_func(np.argmax, axis, arr)
40+
41+
42+
@njit
43+
def np_max(axis, arr):
44+
return np_func(np.max, axis, arr)
45+
46+
47+
@njit
48+
def np_mean(axis, arr):
49+
return np_func(np.mean, axis, arr)
50+
51+
52+
# @njit('float64[:, ::1](float64[:, ::1], boolean)')
2353
def sigmoid(x, derivative):
2454
if derivative:
2555
return x * (1.0 - x)
2656
else:
2757
return 1.0 / (1.0 + np.exp(-x))
2858

2959

30-
@njit('float64[:, ::1](float64[:, ::1], boolean)')
60+
# @njit('float64[:, ::1](float64[:, ::1], boolean)')
3161
def relu(x, derivative):
3262
if derivative:
3363
return np.where(x <= 0.0, 0.0, 1.0)
3464
else:
3565
return np.maximum(0.0, x)
3666

3767

38-
@njit('float64[:, ::1](float64[:, ::1], boolean)')
68+
# @njit('float64[:, ::1](float64[:, ::1], boolean)')
3969
def leaky_relu(x, derivative):
4070
if derivative:
4171
return np.where(x <= 0.0, -0.01*x, 1.0)
4272
else:
4373
return np.maximum(-0.01*x, x)
4474

4575

46-
@njit('float64[:, ::1](float64[:, ::1], boolean)')
76+
# @njit('float64[:, ::1](float64[:, ::1], boolean)')
4777
def softmax(x, derivative):
4878
e_x = np.exp(x - np.max(x))
49-
return e_x / e_x.sum()
79+
result = e_x / e_x.sum()
80+
return result
81+
82+
83+
# @njit('float64[:, ::1](float64[:, ::1], boolean)')
84+
def softmax_2(x, derivative):
85+
tmp = x - np_max(1, x).reshape(-1, 1)
86+
exp_tmp = np.exp(tmp)
87+
return exp_tmp / exp_tmp.sum(axis=1).reshape(-1, 1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy