Skip to content

Instantly share code, notes, and snippets.

@deep5050
Last active January 4, 2020 20:55
Show Gist options
  • Save deep5050/c4dd7dce26e84d2a662c6eee6f8e0671 to your computer and use it in GitHub Desktop.
Save deep5050/c4dd7dce26e84d2a662c6eee6f8e0671 to your computer and use it in GitHub Desktop.
back propagation,GA based weight determination , generic GA, perceptron learning
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Untitled2.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/deep5050/c4dd7dce26e84d2a662c6eee6f8e0671/untitled2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "DNJ8SCgFQqlK",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GLPleZbEOeoZ",
"colab_type": "code",
"colab": {}
},
"source": [
"class layer:\n",
" def __init__(self,input_x,neuron):\n",
" self.neuron_count = neuron\n",
" self.input_ = input_x\n",
" self.weights = np.random.rand(self.input_,self.neuron_count)\n",
" self.bias = np.random.rand(self.neuron_count)\n",
" self.error = None\n",
" self.delta = None\n",
"\n",
"\n",
" def sigmoid(self,x):\n",
" return 1/(1 + np.exp(- x))\n",
"\n",
" def sigmoid_deriv(self,x):\n",
" return self.sigmoid(x) * (1 - self.sigmoid(x))\n",
" \n",
" def activate(self,x):\n",
" self.net_input = np.dot(x,self.weights) + self.bias\n",
" self.output = self.sigmoid(self.net_input)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "rEueXQx0SqmD",
"colab_type": "code",
"colab": {}
},
"source": [
"class network:\n",
" def __init__(self):\n",
" self.layers = []\n",
"\n",
"\n",
"\n",
"\n",
" def add_layer(self,layer):\n",
" self.layers.append(layer)\n",
"\n",
" def feedforward(self,x):\n",
" for layer in self.layers:\n",
" x = layer.activate(x)\n",
" return x\n",
"\n",
" def backprop(self):\n",
" self.output = self.feedforward(self.X)\n",
"\n",
" for i in reversed(range(len(self.layers))):\n",
" layer = self.layers[i]\n",
" if layer == self.layers[-1]:\n",
" layer.error = self.Y - self.output\n",
" layer.delta = self.error * layer.sigmoid_deriv(self.output)\n",
" else:\n",
" next_layer = self.layers[i+1]\n",
" layer.error = np.dot(next_layer.weights,next_layer.delta)\n",
" layer.delta = layer.error * layer.sigmoid_deriv(layer.output)\n",
"\n",
" # weights updation\n",
" for j in range(len(self.layers)):\n",
" layer = self.layers[j]\n",
" temp_input = None\n",
" if i == 0:\n",
" temp_input = self.X\n",
" else:\n",
" temp_input = np.atleast_2D(self.layers[i-1].output)\n",
" layer.weights += self.lr * layer.delta * temp_input.T\n",
"\n",
"\n",
" def fit(self,X,Y,lr,max_epoch):\n",
" self.lr = lr\n",
" self.max_epoch = max_epoch\n",
" \n",
" MSES = []\n",
" for j in range(0,self.max_epoch):\n",
" for k in range(len(X)):\n",
" self.X = X[j]\n",
" print(self.X)\n",
" self.Y = Y[j]\n",
" if j % 100 == 0:\n",
" error__ = np.mean(np.square(self.Y - self.backprop()))\n",
" MSES.append(error__)\n",
"\n",
" \n",
" plt.plot(MSES)\n",
" plt.show()\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "LsArdNJsYAiX",
"colab_type": "code",
"outputId": "1cc97a61-a4a6-4023-9dfd-ea8338a3d7a3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 374
}
},
"source": [
"nn = network()\n",
"nn.add_layer(layer(3, 5))\n",
"nn.add_layer(layer(5, 3))\n",
"nn.add_layer(layer(3, 2))\n",
"\n",
"\n",
"# Define dataset\n",
"X = np.array([[0, 0,0],[0,0,1], [0, 1,0], [0,1, 1], [1,0,0],[1, 1,1]])\n",
"y = np.array([[0], [0], [0],[0],[0],[1]])\n",
"\n",
"nn.fit(X,y,0.4,5000)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[0 0 0]\n"
],
"name": "stdout"
},
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-11-2a874fbd851a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0.4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-10-5517ad56f9d4>\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, X, Y, lr, max_epoch)\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mY\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mY\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mj\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;36m100\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 51\u001b[0;31m \u001b[0merror__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msquare\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mY\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackprop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 52\u001b[0m \u001b[0mMSES\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merror__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-10-5517ad56f9d4>\u001b[0m in \u001b[0;36mbackprop\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbackprop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfeedforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mreversed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-10-5517ad56f9d4>\u001b[0m in \u001b[0;36mfeedforward\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfeedforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlayer\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mactivate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-5-e1fb61f04612>\u001b[0m in \u001b[0;36mactivate\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mactivate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnet_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweights\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbias\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msigmoid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnet_input\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mdot\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'NoneType' and 'float'"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "J7ZZQDj3OKie",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "WJBWiALbgKlr",
"colab_type": "code",
"outputId": "066671a7-09f4-4da8-a606-4e6324df64d7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 884
}
},
"source": [
"# ga based weight\n",
"\n",
"import random\n",
"\n",
"#consider a 2-2-2(l-m-n) neural network\n",
"l=2\n",
"m=2\n",
"n=2\n",
"\n",
"#no of digit for each weight is 'd'(let)\n",
"d=5\n",
"\n",
"#no of genes (l+n)*m\n",
"#calculate length of the string\n",
"s=int(((l+n)*m)*d)\n",
"print(\"length of the generared chromosome is\",s)\n",
"#generate chromosome\n",
"chromosome=[]\n",
"for i in range (0,s):\n",
" val=random.randint(1,9)\n",
" chromosome.append(val)\n",
"print(chromosome)\n",
"\n",
"genes=[]\n",
"gene=[]\n",
"for i in range(0,s,d):\n",
" gene=chromosome[i:i+5]\n",
" genes.append(gene)\n",
"\n",
"print(\"genes in chromosome\")\n",
"for gene in genes:\n",
" print(gene)\n",
"\n",
"#function for calculate weight for each gene \n",
"def cal_weight(gene):\n",
" p=len(gene)-1\n",
" w=0.0\n",
" for i in range(1,len(gene)):\n",
" w+=gene[i]*pow(10,p-i)\n",
" print(w)\n",
" w=w/pow(10,p-1)\n",
" return(w)\n",
" \n",
" \n",
"for gene in genes:\n",
" if( 0<=gene[0]<5 ):\n",
" weight=-cal_weight(gene)\n",
" print(\"weigh of gene\",weight)\n",
" \n",
" elif( 5<=gene[0]<=9 ):\n",
" weight=+cal_weight(gene)\n",
" print(\"weigh of gene\",weight)\n",
" \n",
" \n",
" \n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"length of the generared chromosome is 40\n",
"[5, 4, 2, 5, 5, 5, 2, 6, 5, 4, 1, 2, 7, 2, 4, 4, 1, 4, 2, 3, 7, 7, 4, 1, 1, 9, 8, 3, 3, 6, 3, 2, 9, 1, 9, 5, 2, 9, 7, 9]\n",
"genes in chromosome\n",
"[5, 4, 2, 5, 5]\n",
"[5, 2, 6, 5, 4]\n",
"[1, 2, 7, 2, 4]\n",
"[4, 1, 4, 2, 3]\n",
"[7, 7, 4, 1, 1]\n",
"[9, 8, 3, 3, 6]\n",
"[3, 2, 9, 1, 9]\n",
"[5, 2, 9, 7, 9]\n",
"4000.0\n",
"4200.0\n",
"4250.0\n",
"4255.0\n",
"weigh of gene 4.255\n",
"2000.0\n",
"2600.0\n",
"2650.0\n",
"2654.0\n",
"weigh of gene 2.654\n",
"2000.0\n",
"2700.0\n",
"2720.0\n",
"2724.0\n",
"weigh of gene -2.724\n",
"1000.0\n",
"1400.0\n",
"1420.0\n",
"1423.0\n",
"weigh of gene -1.423\n",
"7000.0\n",
"7400.0\n",
"7410.0\n",
"7411.0\n",
"weigh of gene 7.411\n",
"8000.0\n",
"8300.0\n",
"8330.0\n",
"8336.0\n",
"weigh of gene 8.336\n",
"2000.0\n",
"2900.0\n",
"2910.0\n",
"2919.0\n",
"weigh of gene -2.919\n",
"2000.0\n",
"2900.0\n",
"2970.0\n",
"2979.0\n",
"weigh of gene 2.979\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "2ZMcGiM0th_z",
"colab_type": "code",
"outputId": "9f7900ab-7746-4c69-bb92-3f9458809db4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 323
}
},
"source": [
"##GA\n",
"\n",
"#Take n number of initial samples, then compute the fittest population\n",
"# Consider number of 1s in the string as fitness function.\n",
"import random\n",
"\n",
"population = [\"110001\",\"001000\",\"110110\",\"010101\",\"000010\",\"101011\"]\n",
"\n",
"def swap(a, b):\n",
"\treturn b, a\n",
"\n",
"def count_ones(chromosome):\n",
"\ttemp_ch = list(chromosome)\n",
"\tcount = 0\n",
"\tfor bit in temp_ch:\n",
"\t\tif bit == '1':\n",
"\t\t\tcount += 1\n",
"\treturn count\n",
"\n",
"def make_zipped_chromosomes(ch_list):\t#takes the population list and\n",
"\tfitness_list = [count_ones(element) for element in ch_list]\n",
"\t#zip the lists\n",
"\tzipped = zip(ch_list, fitness_list)\n",
"\tzipped_list = list(zipped)\n",
"\tzipped_list_sorted = sorted(zipped_list, key = lambda x : x[1], reverse = True)\n",
"\treturn zipped_list_sorted\n",
"\n",
"def roulette_wheel_select(ch_list):\n",
"\ttotalSum = 0\n",
"\tfor element in ch_list:\n",
"\t\ttotalSum += element[1]\n",
"\n",
"\trand_int = random.randint(0, totalSum)\n",
"\t# print(rand_int)\n",
"\tpartial_sum = 0\n",
"\tfor element in ch_list:\n",
"\t\tpartial_sum += element[1]\n",
"\t\tif partial_sum >= rand_int:\n",
"\t\t\treturn element\t#return the selected chromosome\n",
"\n",
"def crossover(mat1, mat2):\n",
"\tlength = len(mat1)\n",
"\t#find the crossover point in range (1 to n-1)\n",
"\tcross_point = random.randint(1, length-1)\n",
"\t# cross_point = 3\n",
"\n",
"\tmat1_list = list(mat1)\n",
"\tmat2_list = list(mat2)\n",
"\n",
"\t# print('cross point', cross_point)\n",
"\t# print('Chromosomes before swap', mat1_list, mat2_list)\n",
"\n",
"\tfor i in range(cross_point, length):\n",
"\t\tmat1_list[i], mat2_list[i] = swap(mat1_list[i], mat2_list[i])\n",
"\n",
"\t# print('Chromosomes after swap:', mat1_list, mat2_list)\n",
"\ttoss = random.randint(0, 4)\t\t\t#toss with a probability of 0.25\n",
"\t# if [list(mat1), list(mat2)] == [mat1_list, mat2_list] or [list(mat2), list(mat1)] == [mat1_list, mat2_list]:\t\t#mutation. In this one mutation will only occur if parent chromosomes match with offspring chromosomes.\n",
"\t\t# print(\"Yes\")\n",
"\t# print(\"Toss value: \"+str(toss))\n",
"\tif toss == 3:\n",
"\t\tlength = len(mat1_list) * 2\n",
"\t\tbit_changer = random.randint(0, length)\n",
"\t\tif bit_changer >= len(mat1_list):\n",
"\t\t\tswap_val = bit_changer - len(mat2_list) - 1\n",
"\t\t\t# print(swap_val)\n",
"\t\t\tmat2_list[swap_val] = str(1 - int(mat2_list[swap_val]))\n",
"\t\telse:\n",
"\t\t\t# print(bit_changer)\n",
"\t\t\tmat1_list[bit_changer] = str(1 - int(mat1_list[bit_changer]))\n",
"\t\t# print('Chromosomes after Mutation:', mat1_list, mat2_list)\n",
"\telse:\n",
"\t\tpass\n",
"\t\t# print(\"No\")\n",
"\n",
"\tnew_ch1 = ''.join([element for element in mat1_list])\n",
"\tnew_ch2 = ''.join([element for element in mat2_list])\n",
"\n",
"\treturn [new_ch1, new_ch2]\n",
"\n",
"def execute_ga(ch_list):\n",
"\tnew_ch_list = []\n",
"\tlength = len(ch_list) / 2\n",
"\tfor count in range(int(length)):\n",
"\t\titem1 = roulette_wheel_select(ch_list)\n",
"\t\tmat1 = item1[0]\n",
"\t\tch_list.remove(item1)\n",
"\t\titem2 = roulette_wheel_select(ch_list)\n",
"\t\tmat2 = item2[0]\n",
"\t\tch_list.remove(item2)\n",
"\t\ttemp_list = crossover(mat1, mat2)\n",
"\t\tfor item in temp_list:\n",
"\t\t\tnew_ch_list.append(item)\n",
"\treturn make_zipped_chromosomes(new_ch_list)\n",
"\n",
"\n",
"for chromosome in population:\n",
"\tprint(chromosome, 'Fitness count:', count_ones(chromosome))\n",
"\n",
"ch_zip_list = make_zipped_chromosomes(population)\n",
"print(\"before:\")\n",
"print(ch_zip_list)\n",
"\n",
"#we are targetting for 80% accuracy, so the value is close to 28.(80% of 36 as there are 36 total bits)\n",
"threshold_value = 30\n",
"gen_count = 0\n",
"print(\"please wait....\")\n",
"while(True):\n",
"\tch_zip_list = execute_ga(ch_zip_list)\n",
"\t#count maximum fitness of the offspring pool\n",
"\ttotal_fitness = 0\n",
"\tfor item in ch_zip_list:\n",
"\t\ttotal_fitness += int(item[1])\n",
"\t# print(\"Fitness of lot \"+str(gen_count)+\" is \" +str(total_fitness))\n",
"\tif total_fitness >= threshold_value :\n",
"\t\tprint(\"Fitness of lot \"+str(gen_count)+\" is \" +str(total_fitness))\n",
"\t\tbreak\n",
"\telse:\n",
"\t\tgen_count += 1\n",
"\t# print(\"New Pool:\")\n",
"\t# for chromosome in ch_zip_list:\n",
"\t# \tprint(chromosome, 'Fitness count:', count_ones(chromosome[0]))\n",
"\n",
"print(\"after:\")\n",
"print(ch_zip_list)\n",
"for chromosome in ch_zip_list:\n",
"\tprint(chromosome, 'Fitness count:', count_ones(chromosome[0]))\n",
"#crossover(\"010101\", \"110110\")\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"110001 Fitness count: 3\n",
"001000 Fitness count: 1\n",
"110110 Fitness count: 4\n",
"010101 Fitness count: 3\n",
"000010 Fitness count: 1\n",
"101011 Fitness count: 4\n",
"before:\n",
"[('110110', 4), ('101011', 4), ('110001', 3), ('010101', 3), ('001000', 1), ('000010', 1)]\n",
"please wait....\n",
"Fitness of lot 185255 is 30\n",
"after:\n",
"[('111111', 6), ('110111', 5), ('111110', 5), ('110111', 5), ('111101', 5), ('011011', 4)]\n",
"('111111', 6) Fitness count: 6\n",
"('110111', 5) Fitness count: 5\n",
"('111110', 5) Fitness count: 5\n",
"('110111', 5) Fitness count: 5\n",
"('111101', 5) Fitness count: 5\n",
"('011011', 4) Fitness count: 4\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "yVBoLOrPtj5j",
"colab_type": "code",
"outputId": "66a357e5-03ac-46f2-a701-b43890b69fcd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 2652
}
},
"source": [
"##perceptron\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"class Perceptron(object):\n",
" \"\"\"\n",
" This class defines a single layer perceptron with 3 inputs ;\n",
" initial weights will be set to 0;\n",
" one extra bias w[0] will be added;\n",
" Params\n",
" ------\n",
" eta : float\n",
" the learning rate to the model ( between 0 to 1)\n",
" max_iter : int\n",
" maximum number of iterration allowed for the prediction . default values is set to 50\n",
" Attributes:\n",
" ---------\n",
" weights : array\n",
" stores the weights to the percpetron\n",
" len(weoights) willl be 1 extra to the len(inputs)\n",
"\n",
" errors_ : array\n",
" stores the error in each epoch for simple ploting purpose\n",
" \"\"\"\n",
" def __init__(self,eta=0.1,max_iter = 50):\n",
" self.eta = eta\n",
" self.max_iter = max_iter\n",
"\n",
" def net_input(self,x):\n",
" \"\"\"\n",
" cal culate the net input\n",
" :return:\n",
" \"\"\"\n",
" return np.dot(x,self.weights[1:]) + self.weights[0]\n",
"\n",
" def predict(self,x):\n",
" # return np.where(self.net_input(x) >= 0.0 ,1 ,0)\n",
" # if traing set contains 0 the use this\n",
" # if traing set contain -1 instead of 0 use the below\n",
" return np.where(self.net_input(x) >= 0.0, 1, -1)\n",
" def fit(self,X,Y):\n",
" \"\"\"\n",
" :param X:\n",
" the input training set to the model\n",
" :param Y:\n",
" input output training set to the model\n",
" :return: self(object)\n",
" \"\"\"\n",
" # self.weights = np.array([0.001,0.5,0.9,0.3])\n",
" self.weights = np.zeros(1+ X.shape[1]) # initialze weights to 0 including the bias\n",
" self.errors_ = []\n",
" for iter in range(self.max_iter):\n",
" print(\"----------------------------------------------------\")\n",
" print(\" In iter no.:-> \",iter)\n",
" print()\n",
" errors = 0\n",
" for x_i,target in zip(X,Y):\n",
" predicted = self.predict(x_i)\n",
" update = self.eta * (target- predicted)\n",
" print(\"Target:->\",target,\" Predicted:->\",predicted,\" Update:->\",update)\n",
" self.weights[1:] += update * x_i\n",
" self.weights[0] += update\n",
" if update !=0.0:\n",
" errors += update\n",
"\n",
" print(\"Total error:->\",errors)\n",
" self.errors_.append(errors)\n",
"\n",
" print(\"Weights:->\", self.weights)\n",
" if errors == 0.0 :break\n",
" # plt.plot(self.errors_,c='r')\n",
" # plt.ylabel(\"Error in each epoch\")\n",
" # plt.xlabel(\"Epoch no.\")\n",
" # plt.show()\n",
"train_x = np.array([\n",
" [-1, -1, -1],\n",
" [-1, -1, 1],\n",
" [-1, 1, -1],\n",
" [-1, 1, 1],\n",
" [1, -1, -1],\n",
" [1, -1, 1],\n",
" [1, 1, -1],\n",
" [1, 1, 1]\n",
"])\n",
"And_Y = np.array([-1, -1, -1, -1, -1, -1, -1, 1])\n",
"OR_Y = np.array([-1, 1, 1, 1, 1, 1, 1, 1])\n",
"NAND_Y = np.array([1, 1, 1, 1, 1, 1, 1, -1])\n",
"NOR_y = np.array([1, -1, -1, -1, -1, -1, -1, -1])\n",
"\n",
"AND = Perceptron(0.3, 100)\n",
"NAND = Perceptron(0.3, 100)\n",
"OR = Perceptron(0.3, 100)\n",
"NOR = Perceptron(0.3, 100)\n",
"print(\"\\n############# LOGIC GATE : AND ####################\\n\")\n",
"AND.fit(train_x, And_Y)\n",
"print(\"\\n################# LOGIC GATE : NAND #####################\\n\")\n",
"NAND.fit(train_x,NAND_Y)\n",
"print(\"\\n################## LOGIC GATE : OR ########################\\n\")\n",
"OR.fit(train_x,OR_Y)\n",
"print(\"\\n################## LOGIC GATE : NOR ########################\\n\")\n",
"NOR.fit(train_x,NOR_y)\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"############# LOGIC GATE : AND ####################\n",
"\n",
"----------------------------------------------------\n",
" In iter no.:-> 0\n",
"\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Total error:-> -1.7999999999999998\n",
"Weights:-> [-1.8 0.6 0.6 0.6]\n",
"----------------------------------------------------\n",
" In iter no.:-> 1\n",
"\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Total error:-> 0\n",
"Weights:-> [-1.8 0.6 0.6 0.6]\n",
"\n",
"################# LOGIC GATE : NAND #####################\n",
"\n",
"----------------------------------------------------\n",
" In iter no.:-> 0\n",
"\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Total error:-> -0.6\n",
"Weights:-> [-0.6 -0.6 -0.6 -0.6]\n",
"----------------------------------------------------\n",
" In iter no.:-> 1\n",
"\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> -1 Update:-> 0.6\n",
"Target:-> 1 Predicted:-> -1 Update:-> 0.6\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Total error:-> 1.2\n",
"Weights:-> [ 0.6 -0.6 -0.6 -0.6]\n",
"----------------------------------------------------\n",
" In iter no.:-> 2\n",
"\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Total error:-> 0\n",
"Weights:-> [ 0.6 -0.6 -0.6 -0.6]\n",
"\n",
"################## LOGIC GATE : OR ########################\n",
"\n",
"----------------------------------------------------\n",
" In iter no.:-> 0\n",
"\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> 1 Predicted:-> -1 Update:-> 0.6\n",
"Target:-> 1 Predicted:-> -1 Update:-> 0.6\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> -1 Update:-> 0.6\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Total error:-> 1.2\n",
"Weights:-> [1.2 0. 0. 0. ]\n",
"----------------------------------------------------\n",
" In iter no.:-> 1\n",
"\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Total error:-> -0.6\n",
"Weights:-> [0.6 0.6 0.6 0.6]\n",
"----------------------------------------------------\n",
" In iter no.:-> 2\n",
"\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Total error:-> 0\n",
"Weights:-> [0.6 0.6 0.6 0.6]\n",
"\n",
"################## LOGIC GATE : NOR ########################\n",
"\n",
"----------------------------------------------------\n",
" In iter no.:-> 0\n",
"\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> 1 Update:-> -0.6\n",
"Total error:-> -2.4\n",
"Weights:-> [-2.4 0. 0. 0. ]\n",
"----------------------------------------------------\n",
" In iter no.:-> 1\n",
"\n",
"Target:-> 1 Predicted:-> -1 Update:-> 0.6\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Total error:-> 0.6\n",
"Weights:-> [-1.8 -0.6 -0.6 -0.6]\n",
"----------------------------------------------------\n",
" In iter no.:-> 2\n",
"\n",
"Target:-> 1 Predicted:-> 1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Target:-> -1 Predicted:-> -1 Update:-> 0.0\n",
"Total error:-> 0\n",
"Weights:-> [-1.8 -0.6 -0.6 -0.6]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "V4qZdhoEuWMI",
"colab_type": "code",
"outputId": "8735d900-a509-4de4-f8c3-350f0199385d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# PERCEPTRON PRACTICE\n",
"\n",
"\n",
"import numpy as np\n",
"\n",
"\n",
"class Perceptron:\n",
" def __init__(self,lr,max_iter):\n",
" self.lr = lr\n",
" self.max_iter = max_iter\n",
" self.weights = np.zeros(4)\n",
"\n",
" def activate(self,data):\n",
" if data >= 0.0 :\n",
" return 1\n",
" else: \n",
" return -1\n",
"\n",
"\n",
" \n",
" def fit(self,X,Y):\n",
" for iter in range(self.max_iter):\n",
" for x,y in zip(X,Y):\n",
" # make training predictions\n",
" net_input = np.dot(x,self.weights[1:]) + self.weights[0] # add bias\n",
" output = self.activate(net_input)\n",
"\n",
" # calculate error\n",
" error = y-output\n",
"\n",
" # weight changes\n",
" self.weights[1:] += x * self.lr * error\n",
" self.weights[0] += self.lr * error\n",
" \n",
"\n",
" print(self.weights)\n",
"\n",
"\n",
"\n",
"train_x = np.array([\n",
" [-1, -1, -1],\n",
" [-1, -1, 1],\n",
" [-1, 1, -1],\n",
" [-1, 1, 1],\n",
" [1, -1, -1],\n",
" [1, -1, 1],\n",
" [1, 1, -1],\n",
" [1, 1, 1]\n",
"])\n",
"And_Y = np.array([-1, -1, -1, -1, -1, -1, -1, 1])\n",
"# OR_Y = np.array([-1, 1, 1, 1, 1, 1, 1, 1])\n",
"# NAND_Y = np.array([1, 1, 1, 1, 1, 1, 1, -1])\n",
"# NOR_y = np.array([1, -1, -1, -1, -1, -1, -1, -1])\n",
"\n",
"AND = Perceptron(0.3, 100)\n",
"# NAND = Perceptron(0.3, 100)\n",
"# OR = Perceptron(0.3, 100)\n",
"\n",
"AND.fit(train_x,And_Y)\n",
" "
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[-1.8 0.6 0.6 0.6]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "uK3iHXiCf7dV",
"colab_type": "code",
"outputId": "9fc8e879-50aa-4b45-ebd4-a6ffaa277527",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
}
},
"source": [
"\n",
"\n",
"# GA weight practice\n",
"\n",
"# make a string 0f (l+n)*m*d of characters\n",
"# take d characters at a time ,consedring it as a chromosome\n",
"# take the first bit to determine the sign\n",
"# take the rest bits and make them decimal value\n",
"import json\n",
"\n",
"class GA_weight:\n",
" def __init__(self,input_layer,hidden_layer,output_layer,digits):\n",
" gene_length = (input_layer + output_layer) * hidden_layer * digits\n",
" self.gene = \"\"\n",
" self.chromosome_pool = []\n",
" self.digits = digits\n",
"\n",
" for i in range(gene_length):\n",
" self.gene+= str(np.random.randint(1,10))\n",
" \n",
" print(\"Gene Pattern:\", self.gene)\n",
"\n",
" def get_chromosome_pool(self):\n",
" \n",
" data = {}\n",
" for i in range(0,len(self.gene),self.digits):\n",
" temp_chromosome_pattern = self.gene[i:i+self.digits]\n",
" data['cromosome'] = temp_chromosome_pattern\n",
" temp_chromosome = list(map(int,temp_chromosome_pattern))\n",
"\n",
" # calculate decimal value\n",
" value = 0.0\n",
" power = self.digits - 2 \n",
" for j in range(1,len(temp_chromosome)):\n",
" value += temp_chromosome[j] * pow(10,power)\n",
" power -= 1\n",
"\n",
" value = value / pow(10,self.digits - 2)\n",
" # determine sign\n",
" if temp_chromosome[0] < 5:\n",
" value = - value\n",
" \n",
" data['weight'] = value\n",
" buff = json.dumps(data)\n",
" self.chromosome_pool.append(buff)\n",
"\n",
" print(self.chromosome_pool)\n",
"\n",
"ga = GA_weight(2,2,2,5)\n",
"ga.get_chromosome_pool()\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Gene Pattern: 5575493228444729578277619177391688342127\n",
"['{\"cromosome\": \"55754\", \"weight\": 5.754}', '{\"cromosome\": \"93228\", \"weight\": 3.228}', '{\"cromosome\": \"44472\", \"weight\": -4.472}', '{\"cromosome\": \"95782\", \"weight\": 5.782}', '{\"cromosome\": \"77619\", \"weight\": 7.619}', '{\"cromosome\": \"17739\", \"weight\": -7.739}', '{\"cromosome\": \"16883\", \"weight\": -6.883}', '{\"cromosome\": \"42127\", \"weight\": -2.127}']\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "5o-fN2dwi3mo",
"colab_type": "code",
"outputId": "a73331f5-e581-487a-ba00-8c7af17ee54e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 2162
}
},
"source": [
"# ga practice\n",
"\n",
"# get a chormosome pool\n",
"# count fitness\n",
"# selection \n",
"# mutation\n",
"# crossover\n",
"\n",
"\n",
"\n",
"import numpy as np\n",
"import random\n",
"import copy\n",
"\n",
"\n",
"class generic_GA:\n",
" def __init__(self,pop_count,chrom_len):\n",
" \n",
" self.initial_pop_count = pop_count\n",
" self.chrom_len = chrom_len\n",
" self.population =[]\n",
" self.fitness_list = []\n",
" self.make_initial_polplation()\n",
"\n",
" def make_initial_polplation(self):\n",
" pop = []\n",
" for count_ in range(0,self.initial_pop_count):\n",
" buff = []\n",
" for i in range(self.chrom_len):\n",
" buff.append(np.random.randint(0,2))\n",
" \n",
" pop.append(buff)\n",
" self.population = pop\n",
" print(pop)\n",
" self.calculate_fitness_list()\n",
"\n",
"\n",
" def swap(self,a,b):\n",
" return b,a\n",
"\n",
" def rearrange_pop(self):\n",
" self.calculate_fitness_list()\n",
" zipped = zip(self.population, self.fitness_list)\n",
" zipped_list = list(zipped)\n",
" zipped_list_sorted = sorted(zipped_list, key = lambda x : x[1], reverse = True)\n",
" temp = list(zipped_list_sorted)\n",
" print(\"temp \",temp)\n",
" # self.population = list(zipped_list)\n",
"\n",
"\n",
" def calculate_fitness_list(self):\n",
" \n",
" for i in range(0,len(self.population)):\n",
" chrom = self.population[i]\n",
" count_1 = 0\n",
" for j in range(0,len(chrom)):\n",
" if chrom[j] == 1:\n",
" count_1 += 1\n",
"\n",
" self.fitness_list.append(count_1)\n",
" \n",
" # self.fitness_list.sort(reverse = True)\n",
"\n",
"\n",
" def mutation(self,chromosome):\n",
" print(\" Mutation:\")\n",
" print(\" before: \",chromosome)\n",
" mp = np.random.randint(0,9) / 10\n",
" print(\" MP:\",mp)\n",
" if mp < 0.3:\n",
" # get mutaion point\n",
" mutation_point = np.random.randint(0,self.chrom_len)\n",
" \n",
" # toggle bit\n",
" if chromosome[mutation_point] == 0:\n",
" chromosome[mutation_point] = 1\n",
" # considering to increase fitness only\n",
" # else:\n",
" # chromosome[mutation_point] = 0\n",
" print(\" After : \",chromosome)\n",
"\n",
"\n",
"\n",
" def crossover(self,first,second):\n",
" a = copy.deepcopy(first)\n",
" b = copy.deepcopy(second)\n",
" cp = np.random.randint(0,9) / 10\n",
"\n",
" print(\" Crossover:\")\n",
" print(\" CP: \",cp)\n",
" print(\" parents: \",first,second)\n",
" if cp < 0.5:\n",
" crossover_point = np.random.randint(0,self.chrom_len)\n",
" for j in range(crossover_point,self.chrom_len):\n",
" a[j],b[j] = self.swap(first[j],second[j])\n",
" \n",
" print(\" Children: \",a,b)\n",
" self.population.append(a)\n",
" self.population.append(b)\n",
"\n",
"\n",
"\n",
"\n",
" def selection(self):\n",
" self.rearrange_pop()\n",
" # roulette wheel\n",
" total_fitness_sum = 0\n",
" for fitness in self.fitness_list:\n",
" total_fitness_sum += fitness\n",
"\n",
" # randomly choose a value between 0 to total sum\n",
"\n",
" sp = np.random.randint(0,total_fitness_sum)\n",
"\n",
" self.selected_chroms = []\n",
" temp_sum = 0\n",
"\n",
" for index in range(0,len(self.population)):\n",
" temp_sum += self.fitness_list[index]\n",
"\n",
" if temp_sum >= sp:\n",
" self.selected_chroms.append(self.population[index])\n",
" return \n",
"\n",
"\n",
"\n",
" def run(self,max_iter):\n",
" for i in range(max_iter):\n",
" self.selection()\n",
" chrom_for_crossover_1 = np.random.randint(0,len(self.population))\n",
" chrom_for_crossover_2 = np.random.randint(0,len(self.population))\n",
"\n",
" chrom_for_mutation = np.random.randint(0,len(self.population))\n",
"\n",
" self.crossover(self.population[chrom_for_crossover_1],self.population[chrom_for_crossover_2])\n",
"\n",
" self.mutation(self.population[chrom_for_mutation])\n",
" \n",
" print()\n",
" print(\"Iteration: \",i)\n",
" print(\"Population:\" ,self.population)\n",
" print()\n",
"\n",
"\n",
"\n",
"\n",
"net = generic_GA(8,6)\n",
"net.run(10)\n",
" "
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0]]\n",
"temp [([0, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.2\n",
" parents: [0, 1, 1, 1, 1, 1] [0, 1, 1, 1, 1, 1]\n",
" Children: [0, 1, 1, 1, 1, 1] [0, 1, 1, 1, 1, 1]\n",
" Mutation:\n",
" before: [0, 0, 1, 1, 1, 1]\n",
" MP: 0.2\n",
" After : [0, 0, 1, 1, 1, 1]\n",
"\n",
"Iteration: 0\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([0, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.6\n",
" parents: [0, 1, 1, 1, 1, 1] [0, 1, 1, 1, 1, 1]\n",
" Mutation:\n",
" before: [1, 1, 0, 0, 0, 1]\n",
" MP: 0.6\n",
" After : [1, 1, 0, 0, 0, 1]\n",
"\n",
"Iteration: 1\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([0, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.5\n",
" parents: [0, 0, 1, 1, 1, 1] [0, 1, 1, 1, 1, 1]\n",
" Mutation:\n",
" before: [1, 1, 0, 0, 0, 1]\n",
" MP: 0.8\n",
" After : [1, 1, 0, 0, 0, 1]\n",
"\n",
"Iteration: 2\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([0, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.5\n",
" parents: [0, 1, 0, 0, 0, 1] [0, 1, 0, 0, 0, 1]\n",
" Mutation:\n",
" before: [0, 1, 1, 1, 1, 1]\n",
" MP: 0.1\n",
" After : [0, 1, 1, 1, 1, 1]\n",
"\n",
"Iteration: 3\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([0, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.6\n",
" parents: [0, 1, 1, 1, 1, 1] [0, 1, 0, 0, 0, 0]\n",
" Mutation:\n",
" before: [0, 1, 1, 1, 1, 1]\n",
" MP: 0.1\n",
" After : [1, 1, 1, 1, 1, 1]\n",
"\n",
"Iteration: 4\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([1, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.7\n",
" parents: [0, 1, 0, 0, 1, 1] [0, 1, 1, 1, 1, 1]\n",
" Mutation:\n",
" before: [0, 1, 0, 1, 1, 1]\n",
" MP: 0.5\n",
" After : [0, 1, 0, 1, 1, 1]\n",
"\n",
"Iteration: 5\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([1, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.7\n",
" parents: [1, 0, 1, 0, 0, 0] [0, 0, 1, 1, 1, 1]\n",
" Mutation:\n",
" before: [0, 0, 1, 1, 1, 1]\n",
" MP: 0.5\n",
" After : [0, 0, 1, 1, 1, 1]\n",
"\n",
"Iteration: 6\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1]]\n",
"\n",
"temp [([1, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1)]\n",
" Crossover:\n",
" CP: 0.3\n",
" parents: [1, 1, 0, 0, 0, 1] [1, 1, 1, 1, 1, 1]\n",
" Children: [1, 1, 0, 1, 1, 1] [1, 1, 1, 0, 0, 1]\n",
" Mutation:\n",
" before: [1, 1, 1, 1, 1, 1]\n",
" MP: 0.3\n",
" After : [1, 1, 1, 1, 1, 1]\n",
"\n",
"Iteration: 7\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1], [1, 1, 1, 0, 0, 1]]\n",
"\n",
"temp [([1, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([1, 1, 1, 0, 0, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1), ([1, 1, 0, 1, 1, 1], 1)]\n",
" Crossover:\n",
" CP: 0.3\n",
" parents: [1, 1, 0, 1, 1, 1] [0, 1, 1, 1, 1, 1]\n",
" Children: [0, 1, 1, 1, 1, 1] [1, 1, 0, 1, 1, 1]\n",
" Mutation:\n",
" before: [1, 1, 1, 1, 1, 1]\n",
" MP: 0.1\n",
" After : [1, 1, 1, 1, 1, 1]\n",
"\n",
"Iteration: 8\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1], [1, 1, 1, 0, 0, 1], [0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1]]\n",
"\n",
"temp [([1, 1, 1, 1, 1, 1], 5), ([0, 1, 1, 1, 1, 1], 5), ([0, 1, 0, 1, 1, 1], 4), ([0, 0, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([0, 1, 1, 1, 1, 1], 4), ([1, 1, 0, 0, 0, 1], 3), ([0, 1, 0, 0, 1, 1], 3), ([1, 1, 1, 0, 0, 1], 3), ([1, 1, 0, 1, 1, 1], 3), ([0, 1, 0, 0, 0, 1], 2), ([1, 0, 1, 0, 0, 0], 2), ([0, 1, 0, 0, 0, 0], 1), ([1, 1, 0, 1, 1, 1], 1)]\n",
" Crossover:\n",
" CP: 0.2\n",
" parents: [0, 0, 1, 1, 1, 1] [1, 1, 0, 0, 0, 1]\n",
" Children: [0, 0, 1, 0, 0, 1] [1, 1, 0, 1, 1, 1]\n",
" Mutation:\n",
" before: [0, 0, 1, 1, 1, 1]\n",
" MP: 0.2\n",
" After : [0, 0, 1, 1, 1, 1]\n",
"\n",
"Iteration: 9\n",
"Population: [[0, 1, 0, 1, 1, 1], [0, 0, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1], [1, 1, 1, 0, 0, 1], [0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1], [1, 1, 0, 1, 1, 1]]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "t5HctASs2TBA",
"colab_type": "code",
"outputId": "5d08f99f-9a5c-4a77-fe32-2397999b6445",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 408
}
},
"source": [
"# backprop practice\n",
"\n",
"import numpy as np\n",
"import random\n",
"import matplotlib.pyplot as plt\n",
"\n",
"class layer:\n",
" def __init__(self,input_count,neurons):\n",
" self.input_count = input_count\n",
" self.neurons = neurons\n",
" self.weights = np.random.rand(input_count , neurons)\n",
" # print(len(self.weights))\n",
" self.bias = np.random.rand(neurons)\n",
" self.error = None\n",
" self.delta = None\n",
"\n",
"\n",
" def sigmoid(self,data):\n",
" return 1 / ( 1 + np.exp(- data))\n",
"\n",
" def sigmoid_deriv(self,data):\n",
" return self.sigmoid(data) * ( 1- self.sigmoid(data))\n",
"\n",
" def activate(self,x):\n",
" net_input = np.dot(x,self.weights) + self.bias\n",
" self.output = self.sigmoid(net_input)\n",
" return self.output\n",
"\n",
"\n",
"\n",
"######################################################\n",
"class network:\n",
" def __init__(self):\n",
" self.layers = []\n",
"\n",
"\n",
" def add_layer(self,layer):\n",
" self.layers.append(layer)\n",
"\n",
" def feedforward(self):\n",
" temp = self.x\n",
" for i in range(len(self.layers)):\n",
" temp = self.layers[i].activate(temp)\n",
" return temp \n",
"\n",
" def backprop(self):\n",
" output = self.feedforward()\n",
" error = self.y - output\n",
"\n",
" # changes through back prop\n",
"\n",
" for i in reversed(range(len(self.layers))):\n",
" layer = self.layers[i]\n",
" if layer == self.layers[-1]:\n",
" # if it is the last layer\n",
" layer.error = self.y - output\n",
" layer.delta = layer.error * layer.sigmoid_deriv(output)\n",
" \n",
" else:\n",
" next_layer = self.layers[i+1]\n",
" layer.error = np.dot(next_layer.weights , layer.delta)\n",
" layer.delta = layer.error * layer.sigmoid_deriv(layer.output)\n",
"\n",
" # weight changes\n",
"\n",
" for i in range(len(self.layers)):\n",
" layer = self.layers[i]\n",
" if i==0:\n",
" input_data = np.atleast_2D(self.x)\n",
" else:\n",
" input_data = np.atleast_2D(self.layers[i -1 ].output)\n",
"\n",
" layer.weights += self.lr * np.dot(layer.weights,input_data) * layer.delta\n",
"\n",
"\n",
"\n",
" def fit(self,X,Y,lr,max_iter):\n",
" self.lr = lr\n",
" mses = []\n",
" for i in range(max_iter):\n",
" for x,y in zip(X,Y):\n",
" self.x = x\n",
" self.y = y\n",
" self.backprop()\n",
" if i% 100 == 0:\n",
" mse.append(np.mean(n.squared(self.error)))\n",
"\n",
" plt.plot(mse)\n",
" \n",
"\n",
"\n",
"nn = network()\n",
"# input_layer = layer(3,5)\n",
"hidden_layer = layer(5,3)\n",
"output_layer = layer(3,2)\n",
"\n",
"nn.add_layer(input_layer)\n",
"nn.add_layer(hidden_layer)\n",
"nn.add_layer(output_layer)\n",
"\n",
"\n",
"# Define dataset\n",
"X = np.array([[0, 0,0],[0,0,1], [0, 1,0], [0,1, 1], [1,0,0],[1, 1,1]])\n",
"Y = np.array([[0], [0], [0],[0],[0],[1]])\n",
"\n",
"nn.fit(X,Y,0.4,5000)\n",
"\n",
" "
],
"execution_count": 0,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-20-03230908361b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0mY\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 105\u001b[0;31m \u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mY\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0.4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 106\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-20-03230908361b>\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, X, Y, lr, max_iter)\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 82\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 83\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackprop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 84\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m%\u001b[0m \u001b[0;36m100\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 85\u001b[0m \u001b[0mmse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msquared\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merror\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-20-03230908361b>\u001b[0m in \u001b[0;36mbackprop\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[0mnext_layer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 60\u001b[0;31m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merror\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_layer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweights\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdelta\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 61\u001b[0m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdelta\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merror\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msigmoid_deriv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mdot\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'float' and 'NoneType'"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "bzcazXjeYfMt",
"colab_type": "code",
"outputId": "f5873afb-9868-471e-eb95-8c4ab31958f3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"source": [
"import numpy as np\n",
"np.random.rand(3,5)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.1925213 , 0.19629503, 0.06590605, 0.32122734, 0.22023239,\n",
" 0.94504047])"
]
},
"metadata": {
"tags": []
},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "LmAGRGaeo6Zi",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment