Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
akagam1 committed Apr 15, 2022
1 parent 140863a commit c018e5f
Showing 1 changed file with 213 additions and 0 deletions.
213 changes: 213 additions & 0 deletions XOR_NN.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "XOR-NN.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyP/dE4A5rYjsAy2fBvCAVOU",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/akagam1/Machine-Learning/blob/main/XOR_NN.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "65402TIidgZH"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import random\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"source": [
"x1 = []\n",
"x2 = []\n",
"y = []\n",
"A2 = 0\n",
"training = 100\n",
"for i in range(training):\n",
" temp1 = random.randint(0,1)\n",
" temp2 = random.randint(0,1)\n",
" x1.append(temp1)\n",
" x2.append(temp2)\n",
"\n",
"for i in range(training):\n",
" if x1[i] != x2[i]:\n",
" y.append(1)\n",
" else:\n",
" y.append(0)\n",
"\n",
"x = np.array([x1,x2])\n",
"y = np.array(y)\n",
"print(x.shape[1])"
],
"metadata": {
"id": "9xLxBQqwd-A_",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fb9606bb-18cb-4e78-8dad-dab9defc400f"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"100\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def sigmoid(z):\n",
" return 1/(1+np.exp(-z))\n",
"\n",
"def initParas(input_size, hidden_size, output_size):\n",
" w1 = np.random.randn(hidden_size, input_size)\n",
" w2 = np.random.randn(output_size, hidden_size)\n",
" b1 = np.zeros((hidden_size, 1))\n",
" b2 = np.zeros((output_size, 1))\n",
"\n",
" return (w1,w2, b1, b2)\n",
"\n",
"def forwardProp(x,y, parameters):\n",
" global A2\n",
" w1,w2,b1,b2 = parameters\n",
" m = x.shape[1]\n",
"\n",
" z1 = np.dot(w1,x) + b1\n",
" a1 = sigmoid(z1)\n",
" z2 = np.dot(w2,a1) + b2\n",
" a2 = sigmoid(z2)\n",
"\n",
" A2 = a2\n",
" return (z1,a1,z2,a2)\n",
"\n",
"def backProp(x,y, forward,parameters):\n",
" w1,w2,b1,b2 = parameters\n",
" z1,a1,z2,a2= forward\n",
" m = x.shape[1]\n",
"\n",
" dz2 = (y-a2)/m\n",
" dw2 = np.dot(dz2, a1.T)\n",
" db2 = np.sum(dz2, axis =1, keepdims = True) \n",
"\n",
" da1 = np.dot(w2.T, dz2)\n",
" dz1 = np.multiply(da1, a1 * (1-a1))\n",
" dw1 = np.dot(dz1, x.T)\n",
" db1 = np.sum(dz1, axis=1, keepdims = True) \n",
"\n",
" return (dz2, dw2, db2, dz1, dw1, db1)\n",
"\n",
"def paraUpdate(parameters, grads, alpha):\n",
" w1,w2,b1,b2 = parameters\n",
" w1 = w1 + alpha*grads[4]\n",
" w2 = w2 + alpha*grads[1]\n",
" b1 = b1 + alpha*grads[5]\n",
" b2 = b2 + alpha*grads[2]\n",
"\n",
" return (w1,w2,b1,b2)\n"
],
"metadata": {
"id": "SJUT6WxxpN1X"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"epochs = 10000\n",
"alpha = 0.1\n",
"input = 2\n",
"output = 1\n",
"hidden = 2 #can be any value\n",
"parameters = initParas(input,hidden,output)\n",
"\n",
"for i in range(epochs):\n",
" forward = forwardProp(x,y,parameters)\n",
" back = backProp(x,y, forward, parameters)\n",
" parameters = paraUpdate(parameters, back, alpha)\n"
],
"metadata": {
"id": "G66zH4W8zSlB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"x1 = []\n",
"x2 = []\n",
"ytest = []\n",
"tests=10\n",
"for i in range(tests):\n",
" temp1 = random.randint(0,1)\n",
" temp2 = random.randint(0,1)\n",
" x1.append(temp1)\n",
" x2.append(temp2)\n",
"\n",
"for i in range(tests):\n",
" if x1[i] != x2[i]:\n",
" ytest.append(1)\n",
" else:\n",
" ytest.append(0)\n",
"xtest = np.array([x1,x2])\n",
"ytest = np.array(ytest)\n",
"\n",
"correct = 0\n",
"temp = forwardProp(xtest,ytest,parameters)\n",
"for i in range(xtest.shape[1]):\n",
" A2[0][i] = round(A2[0][i])\n",
" if A2[0][i] == ytest[i]:\n",
" correct += 1\n",
"print(f'Percent Accuracy: {(correct/tests)*100}%')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QPSubX3c7Euz",
"outputId": "e5fd4581-35e5-480a-f7e3-8a28fdffbcf1"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Percent Accuracy: 100.0%\n"
]
}
]
}
]
}

0 comments on commit c018e5f

Please sign in to comment.