├── ArtificialNeuralNetwork.py ├── ArtificialNeuralNetworkUtility.py └── README.md /ArtificialNeuralNetwork.py: -------------------------------------------------------------------------------- 1 | from ArtificialNeuralNetworkUtility import sigmoid 2 | from ArtificialNeuralNetworkUtility import between 3 | from ArtificialNeuralNetworkUtility import make_matrix 4 | 5 | use_bias = 1 6 | squash = sigmoid 7 | 8 | class ArtificialNeuralNetwork: 9 | 10 | def __init__(self, layer_sizes): 11 | self.layers = [] 12 | self.learn_rate = 0.1 13 | 14 | for l in range(len(layer_sizes)): 15 | layer_size = layer_sizes[l] 16 | prev_layer_size = (0 if l == 0 else layer_sizes[l-1]) 17 | layer = Layer(l, layer_size, prev_layer_size) 18 | self.layers.append(layer) 19 | 20 | def train(self): 21 | pass 22 | 23 | def predict(self, input): 24 | """ 25 | Return the network prediction for the input. 26 | """ 27 | self.set_input(input) 28 | self.fwd_propagate() 29 | 30 | return self.get_output() 31 | 32 | def update_weights(self): 33 | pass 34 | 35 | def set_input(self, input_vector): 36 | input_layer = self.layers[0] 37 | 38 | for i in range(0, input_layer.n_neurons): 39 | input_layer.output[i + use_bias] = input_vector[i] 40 | 41 | def fwd_propagate(self): 42 | """ 43 | Forward propagate the input signal through the neural network. 44 | """ 45 | # Exclude the last layer 46 | for l in range(len(self.layers) - 1): 47 | src_layer = self.layers[l] 48 | dest_layer = self.layers[l + 1] 49 | for j in range(0, dest_layer.n_neurons): 50 | input_sum = 0 51 | for i in range(0, src_layer.n_neurons + use_bias): 52 | input_sum += dest_layer.weight[i][j] * src_layer.output[i] 53 | dest_layer.input[j] = input_sum 54 | dest_layer.output[j + use_bias] = squash(input_sum) 55 | 56 | def get_output(self): 57 | output_layer = self.layers[-1] 58 | res = [0] * output_layer.n_neurons 59 | for i in range(0, len(res)): 60 | res[i] = output_layer.output[i + use_bias] 61 | 62 | return res 63 | 64 | class Layer: 65 | 66 | def __init__(self, id, layer_size, prev_layer_size): 67 | self.id = id 68 | self.n_neurons = layer_size 69 | self.bias_val = 1 70 | 71 | self.input = [0] * self.n_neurons 72 | 73 | self.output = [0] * (self.n_neurons + 1) 74 | self.output[0] = self.bias_val 75 | 76 | self.error = [0] * self.n_neurons 77 | 78 | self.weight = make_matrix(prev_layer_size + use_bias, self.n_neurons) 79 | for i in range(len(self.weight)): 80 | for j in range(len(self.weight[i])): 81 | self.weight[i][j] = between(-0.2, 0.2) 82 | 83 | if __name__ == '__main__': 84 | # The AND function 85 | and_ann = ArtificialNeuralNetwork([2, 1]) 86 | inputs = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]] 87 | targets = [[0.0], [0.0], [0.0], [1.0]] 88 | 89 | # The OR function 90 | or_ann = AritifialNeuralNetwork([2, 1]) 91 | inputs = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]] 92 | targets = [[0.0], [1.0], [1.0], [1.0]] 93 | 94 | # Make predictions without training 95 | for i in range(len(targets)): 96 | print(and_ann.predict(inputs[i])) 97 | -------------------------------------------------------------------------------- /ArtificialNeuralNetworkUtility.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | 4 | def sigmoid(x): 5 | """ 6 | The sigmoid function. Returns a number between 0 and 1. 7 | """ 8 | return 1.0 / (1 + math.exp(-x)) 9 | 10 | def between(min, max): 11 | """ 12 | Get a random value from the given interval 13 | """ 14 | return random.random() * (max - min) + min 15 | 16 | def make_matrix(N, M): 17 | """ 18 | Make a matrix (N x M) 19 | """ 20 | return [[0 for i in range(M)] for i in range(N)] 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Artificial Neural Networks (ANNs) in Python 2 | 3 | Designing an artificial neural network from scratch. 4 | --------------------------------------------------------------------------------