|
| 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) |
0 commit comments