Skip to content

Commit c018e5f

Browse files
committed
Created using Colaboratory
1 parent 140863a commit c018e5f

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

XOR_NN.ipynb

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "XOR-NN.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": [],
9+
"authorship_tag": "ABX9TyP/dE4A5rYjsAy2fBvCAVOU",
10+
"include_colab_link": true
11+
},
12+
"kernelspec": {
13+
"name": "python3",
14+
"display_name": "Python 3"
15+
},
16+
"language_info": {
17+
"name": "python"
18+
}
19+
},
20+
"cells": [
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {
24+
"id": "view-in-github",
25+
"colab_type": "text"
26+
},
27+
"source": [
28+
"<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>"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {
35+
"id": "65402TIidgZH"
36+
},
37+
"outputs": [],
38+
"source": [
39+
"import numpy as np\n",
40+
"import random\n",
41+
"import matplotlib.pyplot as plt"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"source": [
47+
"x1 = []\n",
48+
"x2 = []\n",
49+
"y = []\n",
50+
"A2 = 0\n",
51+
"training = 100\n",
52+
"for i in range(training):\n",
53+
" temp1 = random.randint(0,1)\n",
54+
" temp2 = random.randint(0,1)\n",
55+
" x1.append(temp1)\n",
56+
" x2.append(temp2)\n",
57+
"\n",
58+
"for i in range(training):\n",
59+
" if x1[i] != x2[i]:\n",
60+
" y.append(1)\n",
61+
" else:\n",
62+
" y.append(0)\n",
63+
"\n",
64+
"x = np.array([x1,x2])\n",
65+
"y = np.array(y)\n",
66+
"print(x.shape[1])"
67+
],
68+
"metadata": {
69+
"id": "9xLxBQqwd-A_",
70+
"colab": {
71+
"base_uri": "https://localhost:8080/"
72+
},
73+
"outputId": "fb9606bb-18cb-4e78-8dad-dab9defc400f"
74+
},
75+
"execution_count": null,
76+
"outputs": [
77+
{
78+
"output_type": "stream",
79+
"name": "stdout",
80+
"text": [
81+
"100\n"
82+
]
83+
}
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"source": [
89+
"def sigmoid(z):\n",
90+
" return 1/(1+np.exp(-z))\n",
91+
"\n",
92+
"def initParas(input_size, hidden_size, output_size):\n",
93+
" w1 = np.random.randn(hidden_size, input_size)\n",
94+
" w2 = np.random.randn(output_size, hidden_size)\n",
95+
" b1 = np.zeros((hidden_size, 1))\n",
96+
" b2 = np.zeros((output_size, 1))\n",
97+
"\n",
98+
" return (w1,w2, b1, b2)\n",
99+
"\n",
100+
"def forwardProp(x,y, parameters):\n",
101+
" global A2\n",
102+
" w1,w2,b1,b2 = parameters\n",
103+
" m = x.shape[1]\n",
104+
"\n",
105+
" z1 = np.dot(w1,x) + b1\n",
106+
" a1 = sigmoid(z1)\n",
107+
" z2 = np.dot(w2,a1) + b2\n",
108+
" a2 = sigmoid(z2)\n",
109+
"\n",
110+
" A2 = a2\n",
111+
" return (z1,a1,z2,a2)\n",
112+
"\n",
113+
"def backProp(x,y, forward,parameters):\n",
114+
" w1,w2,b1,b2 = parameters\n",
115+
" z1,a1,z2,a2= forward\n",
116+
" m = x.shape[1]\n",
117+
"\n",
118+
" dz2 = (y-a2)/m\n",
119+
" dw2 = np.dot(dz2, a1.T)\n",
120+
" db2 = np.sum(dz2, axis =1, keepdims = True) \n",
121+
"\n",
122+
" da1 = np.dot(w2.T, dz2)\n",
123+
" dz1 = np.multiply(da1, a1 * (1-a1))\n",
124+
" dw1 = np.dot(dz1, x.T)\n",
125+
" db1 = np.sum(dz1, axis=1, keepdims = True) \n",
126+
"\n",
127+
" return (dz2, dw2, db2, dz1, dw1, db1)\n",
128+
"\n",
129+
"def paraUpdate(parameters, grads, alpha):\n",
130+
" w1,w2,b1,b2 = parameters\n",
131+
" w1 = w1 + alpha*grads[4]\n",
132+
" w2 = w2 + alpha*grads[1]\n",
133+
" b1 = b1 + alpha*grads[5]\n",
134+
" b2 = b2 + alpha*grads[2]\n",
135+
"\n",
136+
" return (w1,w2,b1,b2)\n"
137+
],
138+
"metadata": {
139+
"id": "SJUT6WxxpN1X"
140+
},
141+
"execution_count": null,
142+
"outputs": []
143+
},
144+
{
145+
"cell_type": "code",
146+
"source": [
147+
"epochs = 10000\n",
148+
"alpha = 0.1\n",
149+
"input = 2\n",
150+
"output = 1\n",
151+
"hidden = 2 #can be any value\n",
152+
"parameters = initParas(input,hidden,output)\n",
153+
"\n",
154+
"for i in range(epochs):\n",
155+
" forward = forwardProp(x,y,parameters)\n",
156+
" back = backProp(x,y, forward, parameters)\n",
157+
" parameters = paraUpdate(parameters, back, alpha)\n"
158+
],
159+
"metadata": {
160+
"id": "G66zH4W8zSlB"
161+
},
162+
"execution_count": null,
163+
"outputs": []
164+
},
165+
{
166+
"cell_type": "code",
167+
"source": [
168+
"x1 = []\n",
169+
"x2 = []\n",
170+
"ytest = []\n",
171+
"tests=10\n",
172+
"for i in range(tests):\n",
173+
" temp1 = random.randint(0,1)\n",
174+
" temp2 = random.randint(0,1)\n",
175+
" x1.append(temp1)\n",
176+
" x2.append(temp2)\n",
177+
"\n",
178+
"for i in range(tests):\n",
179+
" if x1[i] != x2[i]:\n",
180+
" ytest.append(1)\n",
181+
" else:\n",
182+
" ytest.append(0)\n",
183+
"xtest = np.array([x1,x2])\n",
184+
"ytest = np.array(ytest)\n",
185+
"\n",
186+
"correct = 0\n",
187+
"temp = forwardProp(xtest,ytest,parameters)\n",
188+
"for i in range(xtest.shape[1]):\n",
189+
" A2[0][i] = round(A2[0][i])\n",
190+
" if A2[0][i] == ytest[i]:\n",
191+
" correct += 1\n",
192+
"print(f'Percent Accuracy: {(correct/tests)*100}%')"
193+
],
194+
"metadata": {
195+
"colab": {
196+
"base_uri": "https://localhost:8080/"
197+
},
198+
"id": "QPSubX3c7Euz",
199+
"outputId": "e5fd4581-35e5-480a-f7e3-8a28fdffbcf1"
200+
},
201+
"execution_count": null,
202+
"outputs": [
203+
{
204+
"output_type": "stream",
205+
"name": "stdout",
206+
"text": [
207+
"Percent Accuracy: 100.0%\n"
208+
]
209+
}
210+
]
211+
}
212+
]
213+
}

0 commit comments

Comments
 (0)