|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 9, |
| 6 | + "metadata": { |
| 7 | + "collapsed": false |
| 8 | + }, |
| 9 | + "outputs": [ |
| 10 | + { |
| 11 | + "name": "stdout", |
| 12 | + "output_type": "stream", |
| 13 | + "text": [ |
| 14 | + "Random starting synaptic weights: \n", |
| 15 | + "[[-0.16595599]\n", |
| 16 | + " [ 0.44064899]\n", |
| 17 | + " [-0.99977125]]\n", |
| 18 | + "error after 0 iterations: 0.578374046722\n", |
| 19 | + "error after 1000 iterations: 0.0353771814512\n", |
| 20 | + "error after 2000 iterations: 0.024323319584\n", |
| 21 | + "error after 3000 iterations: 0.0196075022358\n", |
| 22 | + "error after 4000 iterations: 0.016850233908\n", |
| 23 | + "error after 5000 iterations: 0.014991814044\n", |
| 24 | + "error after 6000 iterations: 0.0136320935305\n", |
| 25 | + "error after 7000 iterations: 0.01258242301\n", |
| 26 | + "error after 8000 iterations: 0.0117408289409\n", |
| 27 | + "error after 9000 iterations: 0.0110467781322\n", |
| 28 | + "New synaptic weights after training: \n", |
| 29 | + "[[ 12.79547496]\n", |
| 30 | + " [ -4.2162058 ]\n", |
| 31 | + " [ -4.21608782]]\n", |
| 32 | + "Considering new situation [1, 0, 0] -> ?: \n", |
| 33 | + "[ 0.99999723]\n" |
| 34 | + ] |
| 35 | + } |
| 36 | + ], |
| 37 | + "source": [ |
| 38 | + "# from https://iamtrask.github.io//2015/07/12/basic-python-network/\n", |
| 39 | + "import numpy as np\n", |
| 40 | + "\n", |
| 41 | + "class NeuralNetwork():\n", |
| 42 | + " def __init__(self):\n", |
| 43 | + " # Seed the random number generator, so it generates the same numbers\n", |
| 44 | + " # every time the program runs.\n", |
| 45 | + " np.random.seed(1)\n", |
| 46 | + "\n", |
| 47 | + " # We model a single neuron, with 3 input connections and 1 output connection.\n", |
| 48 | + " # We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1\n", |
| 49 | + " # and mean 0.\n", |
| 50 | + " self.synaptic_weights = 2 * np.random.random((3, 1)) - 1\n", |
| 51 | + "\n", |
| 52 | + " # The Sigmoid function, which describes an S shaped curve.\n", |
| 53 | + " # We pass the weighted sum of the inputs through this function to\n", |
| 54 | + " # normalise them between 0 and 1.\n", |
| 55 | + " def __sigmoid(self, x):\n", |
| 56 | + " return 1 / (1 + np.exp(-x))\n", |
| 57 | + "\n", |
| 58 | + " # The derivative of the Sigmoid function.\n", |
| 59 | + " # This is the gradient of the Sigmoid curve.\n", |
| 60 | + " # It indicates how confident we are about the existing weight.\n", |
| 61 | + " def __sigmoid_derivative(self, x):\n", |
| 62 | + " return x * (1 - x)\n", |
| 63 | + "\n", |
| 64 | + " # We train the neural network through a process of trial and error.\n", |
| 65 | + " # Adjusting the synaptic weights each time.\n", |
| 66 | + " def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):\n", |
| 67 | + " for iteration in iter(range(number_of_training_iterations)):\n", |
| 68 | + " # Pass the training set through our neural network (a single neuron).\n", |
| 69 | + " output = self.think(training_set_inputs)\n", |
| 70 | + "\n", |
| 71 | + " # Calculate the error (The difference between the desired output\n", |
| 72 | + " # and the predicted output).\n", |
| 73 | + " error = training_set_outputs - output\n", |
| 74 | + "\n", |
| 75 | + " # Multiply the error by the input and again by the gradient of the Sigmoid curve.\n", |
| 76 | + " # This means less confident weights are adjusted more.\n", |
| 77 | + " # This means inputs, which are zero, do not cause changes to the weights.\n", |
| 78 | + " adjustment = np.dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))\n", |
| 79 | + "\n", |
| 80 | + " # Adjust the weights.\n", |
| 81 | + " self.synaptic_weights += adjustment\n", |
| 82 | + " if (iteration % 1000 == 0):\n", |
| 83 | + " print (\"error after %s iterations: %s\" % (iteration, str(numpy.mean(numpy.abs(error)))))\n", |
| 84 | + "\n", |
| 85 | + " # The neural network thinks.\n", |
| 86 | + " def think(self, inputs):\n", |
| 87 | + " # Pass inputs through our neural network (our single neuron).\n", |
| 88 | + " return self.__sigmoid(np.dot(inputs, self.synaptic_weights))\n", |
| 89 | + "\n", |
| 90 | + "\n", |
| 91 | + "if __name__ == \"__main__\":\n", |
| 92 | + "\n", |
| 93 | + " #Intialise a single neuron neural network.\n", |
| 94 | + " neural_network = NeuralNetwork()\n", |
| 95 | + "\n", |
| 96 | + " print (\"Random starting synaptic weights: \")\n", |
| 97 | + " print (neural_network.synaptic_weights)\n", |
| 98 | + "\n", |
| 99 | + " # The training set. We have 4 examples, each consisting of 3 input values\n", |
| 100 | + " # and 1 output value.\n", |
| 101 | + " training_set_inputs = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 0]])\n", |
| 102 | + " training_set_outputs = np.array([[0, 1, 1, 0]]).T\n", |
| 103 | + "\n", |
| 104 | + " # Train the neural network using a training set.\n", |
| 105 | + " # Do it 10,000 times and make small adjustments each time.\n", |
| 106 | + " neural_network.train(training_set_inputs, training_set_outputs, 10000)\n", |
| 107 | + "\n", |
| 108 | + " print (\"New synaptic weights after training: \")\n", |
| 109 | + " print (neural_network.synaptic_weights)\n", |
| 110 | + "\n", |
| 111 | + " # Test the neural network with a new pattern\n", |
| 112 | + " test = [1, 0, 0]\n", |
| 113 | + " print (\"Considering new situation %s -> ?: \" % test )\n", |
| 114 | + " print (neural_network.think(np.array(test)))\n" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 14, |
| 120 | + "metadata": { |
| 121 | + "collapsed": false |
| 122 | + }, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "name": "stdout", |
| 126 | + "output_type": "stream", |
| 127 | + "text": [ |
| 128 | + "input:\n", |
| 129 | + "[[0 0 1]\n", |
| 130 | + " [1 1 1]\n", |
| 131 | + " [1 0 1]\n", |
| 132 | + " [0 1 0]]\n", |
| 133 | + "truth:\n", |
| 134 | + "[[0]\n", |
| 135 | + " [1]\n", |
| 136 | + " [1]\n", |
| 137 | + " [0]]\n", |
| 138 | + "dot-product:\n", |
| 139 | + "[[-0.99977125]\n", |
| 140 | + " [-0.72507825]\n", |
| 141 | + " [-1.16572724]\n", |
| 142 | + " [ 0.44064899]]\n", |
| 143 | + "output:\n", |
| 144 | + "[[ 0.2689864 ]\n", |
| 145 | + " [ 0.3262757 ]\n", |
| 146 | + " [ 0.23762817]\n", |
| 147 | + " [ 0.60841366]]\n", |
| 148 | + "error:\n", |
| 149 | + "[[ 0.2689864 ]\n", |
| 150 | + " [-0.6737243 ]\n", |
| 151 | + " [-0.76237183]\n", |
| 152 | + " [ 0.60841366]]\n", |
| 153 | + "derivative:\n", |
| 154 | + "[[-0.28621005]\n", |
| 155 | + " [-0.00314557]\n", |
| 156 | + " [-0.23331852]]\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "# a look inside one iteration\n", |
| 162 | + "def sigmoid(x):\n", |
| 163 | + " return 1 / (1 + np.exp(-x))\n", |
| 164 | + "\n", |
| 165 | + "def sigmoid_derivative(x):\n", |
| 166 | + " return x * (1 - x)\n", |
| 167 | + " \n", |
| 168 | + "sweights = [[-0.16595599],[ 0.44064899],[-0.99977125]]\n", |
| 169 | + "print (\"input:\")\n", |
| 170 | + "print (training_set_inputs)\n", |
| 171 | + "print (\"truth:\")\n", |
| 172 | + "print (training_set_outputs)\n", |
| 173 | + "print (\"dot-product:\")\n", |
| 174 | + "print (np.dot(training_set_inputs, sweights) )\n", |
| 175 | + "output = sigmoid(np.dot(training_set_inputs, sweights))\n", |
| 176 | + "print (\"output:\")\n", |
| 177 | + "print (output)\n", |
| 178 | + "error = sigmoid(np.dot(training_set_inputs, sweights)) - training_set_outputs\n", |
| 179 | + "print (\"error:\")\n", |
| 180 | + "print (error)\n", |
| 181 | + "print (\"derivative:\")\n", |
| 182 | + "print (np.dot(training_set_inputs.T, error * sigmoid_derivative(output)) )" |
| 183 | + ] |
| 184 | + } |
| 185 | + ], |
| 186 | + "metadata": { |
| 187 | + "kernelspec": { |
| 188 | + "display_name": "Python 3", |
| 189 | + "language": "python", |
| 190 | + "name": "python3" |
| 191 | + }, |
| 192 | + "language_info": { |
| 193 | + "codemirror_mode": { |
| 194 | + "name": "ipython", |
| 195 | + "version": 3 |
| 196 | + }, |
| 197 | + "file_extension": ".py", |
| 198 | + "mimetype": "text/x-python", |
| 199 | + "name": "python", |
| 200 | + "nbconvert_exporter": "python", |
| 201 | + "pygments_lexer": "ipython3", |
| 202 | + "version": "3.5.2" |
| 203 | + } |
| 204 | + }, |
| 205 | + "nbformat": 4, |
| 206 | + "nbformat_minor": 0 |
| 207 | +} |
0 commit comments