|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python program to implement a Single Neuron Neural Network\n", |
| 8 | + "<br>\n", |
| 9 | + "--> Neural networks are artificial systems that were inspired by biological neural networks.<br><br>\n", |
| 10 | + "--> These systems learn to perform tasks by being exposed to various datasets and examples without any task-specific rules<br><br>.\n", |
| 11 | + "--> The idea is that the system generates identifying characteristics from the data they have been passed without being programmed with a pre-programmed understanding of these datasets.<br><br>\n", |
| 12 | + "--> Neural networks are based on computational models for threshold logic.<br><br>\n", |
| 13 | + "--> Threshold logic is a combination of algorithms and mathematics.<br><br>\n", |
| 14 | + "--> Neural networks are based either on the study of the brain or on the application of neural networks to artificial intelligence.<br><br>\n", |
| 15 | + "--> The work has led to improvements in finite automata theory. Components of a typical neural network involve neurons, connections which are known as synapses, weights, biases, propagation function, and a learning rule. <br><br>\n", |
| 16 | + "--> Neural network can be trained on both supervised and unsupervised learning.<br><br>" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 1, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "# Importing Necessary functions from Numpy Library\n", |
| 26 | + "# exp is for exponential\n", |
| 27 | + "# array is for making numpy array\n", |
| 28 | + "# random is to generate a collection of random numbers\n", |
| 29 | + "# The dot() function will return the dot product of a and b.\n", |
| 30 | + "# If both a and b are scalars or if both are 1-D arrays then a scalar value is returned, otherwise an array is returned.\n", |
| 31 | + "# Tanh, short for hyperbolic tangent, is one of several activation functions that can be used in neural networks.\n", |
| 32 | + "# It is a non-linear function that maps the input to an output in the range of -1 to +1.\n", |
| 33 | + "from numpy import exp, array, random, dot, tanh" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "# Elements of a Neural Network <br>\n", |
| 41 | + "\n", |
| 42 | + "--> Input Layer: This layer accepts input features. It provides information from the outside world to the network, no computation is performed at this layer, nodes here just pass on the information(features) to the hidden layer. <br><br>\n", |
| 43 | + "\n", |
| 44 | + "--> Hidden Layer: Nodes of this layer are not exposed to the outer world, they are part of the abstraction provided by any neural network. The hidden layer performs all sorts of computation on the features entered through the input layer and transfers the result to the output layer. <br><br>\n", |
| 45 | + "\n", |
| 46 | + "--> Output Layer: This layer bring up the information learned by the network to the outer world. <br><br>" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "## Class to create a Neural network with Single Neuron and training it using Supervised Learning" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 2, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "# Creating Class for Neural Network\n", |
| 63 | + "class NeuralNetwork():\n", |
| 64 | + " \n", |
| 65 | + " # Initializing using constructor or init function\n", |
| 66 | + " def __init__(self):\n", |
| 67 | + " \n", |
| 68 | + " # Using seed to make sure it will generate same weights in every run\n", |
| 69 | + " random.seed(1)\n", |
| 70 | + "\n", |
| 71 | + " # 3x1 Weight matrix generated using random\n", |
| 72 | + " # It will create a 3 rows one column matrix\n", |
| 73 | + " self.weight_matrix = 2 * random.random((3, 1)) - 1\n", |
| 74 | + "\n", |
| 75 | + " # An activation function is a mathematical function that is applied to the output of a neural network node.\n", |
| 76 | + " # The activation function decides whether a neuron should be activated or not by calculating the weighted sum and further adding bias to it. \n", |
| 77 | + " # tanh as activation function\n", |
| 78 | + " # Here tanh is used as activation function to activate the neural network\n", |
| 79 | + " def tanh(self, x):\n", |
| 80 | + " \n", |
| 81 | + " # Returning value from tanh function\n", |
| 82 | + " return tanh(x)\n", |
| 83 | + " \n", |
| 84 | + " # A gradient is just a way of quantifying the relationship between error and the weights of the neural network. The relationship between these two things can be graphed as a slope, with incorrect weights producing more error.\n", |
| 85 | + " # The steepness of the slope/gradient represents how fast the model is learning.\n", |
| 86 | + " # Derivative of tanh function.\n", |
| 87 | + " # Needed to calculate the gradients.\n", |
| 88 | + " def tanh_derivative(self, x):\n", |
| 89 | + " \n", |
| 90 | + " # Returning value from tanh_derivative function\n", |
| 91 | + " return 1.0 - tanh(x) ** 2\n", |
| 92 | + "\n", |
| 93 | + " # Forward propagation\n", |
| 94 | + " # As the name suggests, the input data is fed in the forward direction through the network.\n", |
| 95 | + " # Each hidden layer accepts the input data, processes it as per the activation function and passes to the successive layer.\n", |
| 96 | + " def forward_propagation(self, inputs):\n", |
| 97 | + " \n", |
| 98 | + " # Returning value from forward propagation function\n", |
| 99 | + " return self.tanh(dot(inputs, self.weight_matrix))\n", |
| 100 | + "\n", |
| 101 | + " # Training the Neural Network.\n", |
| 102 | + " # self is positional argument, train_inputs is the inputs given, train_outputs is the output received from the network and num_train_iterations is the number of iterations of the network\n", |
| 103 | + " def train(self, train_inputs, train_outputs,\n", |
| 104 | + " num_train_iterations):\n", |
| 105 | + " \n", |
| 106 | + " # Number of iterations we want to perform for this set of input.\n", |
| 107 | + " for iteration in range(num_train_iterations):\n", |
| 108 | + " \n", |
| 109 | + " # Calling forward_propagation function for output\n", |
| 110 | + " output = self.forward_propagation(train_inputs)\n", |
| 111 | + "\n", |
| 112 | + " # Calculating the error in the output\n", |
| 113 | + " error = train_outputs - output\n", |
| 114 | + "\n", |
| 115 | + " # A neural network is able to generalize and model a problem in the real world (which is nothing more than a mathematical function)\n", |
| 116 | + " # thanks to the constant adjustment of weights and bias, which modulate the output and the input of each single neuron\n", |
| 117 | + " # until the network does not approach an acceptable solution.\n", |
| 118 | + " # Multiplying the error by input and then by gradient of tanh function to calculate the adjustment needs to be made in weights\n", |
| 119 | + " adjustment = dot(train_inputs.T, error *\n", |
| 120 | + " self.tanh_derivative(output))\n", |
| 121 | + "\n", |
| 122 | + " # Adjusting the weight matrix\n", |
| 123 | + " self.weight_matrix += adjustment" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "# Driver Code of the program\n", |
| 131 | + "## The execution will begin from main function" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 3, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [ |
| 139 | + { |
| 140 | + "name": "stdout", |
| 141 | + "output_type": "stream", |
| 142 | + "text": [ |
| 143 | + "Random weights at the Start of Training are: \n", |
| 144 | + "[[-0.16595599]\n", |
| 145 | + " [ 0.44064899]\n", |
| 146 | + " [-0.99977125]]\n", |
| 147 | + "\n", |
| 148 | + "New Weights after Traininga are: \n", |
| 149 | + "[[5.39428067]\n", |
| 150 | + " [0.19482422]\n", |
| 151 | + " [0.34317086]]\n", |
| 152 | + "\n", |
| 153 | + "Testing Neural Network on New Example is: \n", |
| 154 | + "[0.99995873]\n" |
| 155 | + ] |
| 156 | + } |
| 157 | + ], |
| 158 | + "source": [ |
| 159 | + "# Main Function\n", |
| 160 | + "if __name__ == \"__main__\":\n", |
| 161 | + " \n", |
| 162 | + " # Creating object of Neural Network Class\n", |
| 163 | + " neural_network = NeuralNetwork()\n", |
| 164 | + " \n", |
| 165 | + " # Before Training or the start of training\n", |
| 166 | + " print('Random weights at the Start of Training are: ')\n", |
| 167 | + " print(neural_network.weight_matrix)\n", |
| 168 | + " \n", |
| 169 | + " # Giving inputs to Neural Network\n", |
| 170 | + " train_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])\n", |
| 171 | + " \n", |
| 172 | + " # Desired or Training Output\n", |
| 173 | + " train_outputs = array([[0, 1, 1, 0]]).T\n", |
| 174 | + " \n", |
| 175 | + " # Training Neural Network\n", |
| 176 | + " neural_network.train(train_inputs, train_outputs, 10000)\n", |
| 177 | + " \n", |
| 178 | + " # Weights after training\n", |
| 179 | + " print('\\nNew Weights after Traininga are: ')\n", |
| 180 | + " print(neural_network.weight_matrix)\n", |
| 181 | + "\n", |
| 182 | + " # Testing the Neural Network with a New Situation.\n", |
| 183 | + " print(\"\\nTesting Neural Network on New Example is: \")\n", |
| 184 | + " print(neural_network.forward_propagation(array([1, 0, 0])))" |
| 185 | + ] |
| 186 | + } |
| 187 | + ], |
| 188 | + "metadata": { |
| 189 | + "kernelspec": { |
| 190 | + "display_name": "Python 3 (ipykernel)", |
| 191 | + "language": "python", |
| 192 | + "name": "python3" |
| 193 | + }, |
| 194 | + "language_info": { |
| 195 | + "codemirror_mode": { |
| 196 | + "name": "ipython", |
| 197 | + "version": 3 |
| 198 | + }, |
| 199 | + "file_extension": ".py", |
| 200 | + "mimetype": "text/x-python", |
| 201 | + "name": "python", |
| 202 | + "nbconvert_exporter": "python", |
| 203 | + "pygments_lexer": "ipython3", |
| 204 | + "version": "3.9.12" |
| 205 | + } |
| 206 | + }, |
| 207 | + "nbformat": 4, |
| 208 | + "nbformat_minor": 2 |
| 209 | +} |
0 commit comments