|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<h3>Implementation of different activation fucntion in learning rules</h3>" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 49, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [ |
| 15 | + { |
| 16 | + "name": "stdout", |
| 17 | + "output_type": "stream", |
| 18 | + "text": [ |
| 19 | + "Trained Weights for hebb network are : [1.5488135 1.71518937]\n", |
| 20 | + "Trained bias for hebb network are : [1.60276338]\n", |
| 21 | + "Trained Weights for hebb network are : [0.53143536 0.57216559]\n", |
| 22 | + "Trained bias for hebb network are : [0.51141955]\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "import numpy as np\n", |
| 28 | + "import pandas as pd\n", |
| 29 | + "\n", |
| 30 | + "# Problem --> OR- Gate\n", |
| 31 | + "input = np.array([[1, 1] ,[1,-1] , [-1,1] ,[-1,-1] ])\n", |
| 32 | + "output = np.array([1 ,1 , 1 ,-1 ])\n", |
| 33 | + "np.random.seed(0)\n", |
| 34 | + "weight = np.random.rand(2)\n", |
| 35 | + "bias = np.random.rand(1)\n", |
| 36 | + "learning_rate = 0.1\n", |
| 37 | + "\n", |
| 38 | + "# Hebbian Learning rule\n", |
| 39 | + "def hebbNetwork(input , output , b , w ,epoches, learning_rate = .1):\n", |
| 40 | + " for epoch in range(epoches):\n", |
| 41 | + " for i in range(len(input)):\n", |
| 42 | + " cal_output = (np.dot(input[i] , w) + b)\n", |
| 43 | + " # print(w)\n", |
| 44 | + " if cal_output != output[i]:\n", |
| 45 | + " w = w + (learning_rate*output[i]*input[i])\n", |
| 46 | + " b = b + (learning_rate* output[i])\n", |
| 47 | + " return w , b\n", |
| 48 | + "\n", |
| 49 | + "# Aderline Learning rule\n", |
| 50 | + "def AderlineNetwork(input , output , b , w ,epoches, learning_rate = .1):\n", |
| 51 | + " for epoch in range(epoches):\n", |
| 52 | + " for i in range(len(input)):\n", |
| 53 | + " cal_output = (np.dot(input[i] , w) + b)\n", |
| 54 | + " error = (output[i] - cal_output)**2\n", |
| 55 | + " # print(error)\n", |
| 56 | + " if cal_output != output[i]:\n", |
| 57 | + " w = w + (learning_rate*(output[i]- cal_output)*input[i])\n", |
| 58 | + " b = b + (learning_rate*(output[i]- cal_output))\n", |
| 59 | + " return w , b\n", |
| 60 | + "\n", |
| 61 | + "\n", |
| 62 | + "wh , bh = hebbNetwork(input , output , bias , weight , 5)\n", |
| 63 | + "\n", |
| 64 | + "print(\"Trained Weights for hebb network are : \" , wh)\n", |
| 65 | + "print(\"Trained bias for hebb network are : \" , bh)\n", |
| 66 | + "\n", |
| 67 | + "wa , ba = AderlineNetwork(input , output , bias , weight , 5)\n", |
| 68 | + "\n", |
| 69 | + "print(\"Trained Weights for hebb network are : \" , wa)\n", |
| 70 | + "print(\"Trained bias for hebb network are : \" , ba)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": 50, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "[4.86676625]\n", |
| 83 | + "[1.43638751]\n", |
| 84 | + "[1.76913924]\n", |
| 85 | + "[-1.66123949]\n" |
| 86 | + ] |
| 87 | + } |
| 88 | + ], |
| 89 | + "source": [ |
| 90 | + "for i in range(len(input)):\n", |
| 91 | + " cal_output = (np.dot(input[i] , wh) + bh)\n", |
| 92 | + " print(cal_output)" |
| 93 | + ] |
| 94 | + } |
| 95 | + ], |
| 96 | + "metadata": { |
| 97 | + "kernelspec": { |
| 98 | + "display_name": "Python 3", |
| 99 | + "language": "python", |
| 100 | + "name": "python3" |
| 101 | + }, |
| 102 | + "language_info": { |
| 103 | + "codemirror_mode": { |
| 104 | + "name": "ipython", |
| 105 | + "version": 3 |
| 106 | + }, |
| 107 | + "file_extension": ".py", |
| 108 | + "mimetype": "text/x-python", |
| 109 | + "name": "python", |
| 110 | + "nbconvert_exporter": "python", |
| 111 | + "pygments_lexer": "ipython3", |
| 112 | + "version": "3.8.10" |
| 113 | + } |
| 114 | + }, |
| 115 | + "nbformat": 4, |
| 116 | + "nbformat_minor": 2 |
| 117 | +} |
0 commit comments