Skip to content

Commit 66af7dc

Browse files
committed
NeuralAdd
1 parent ba906fc commit 66af7dc

12 files changed

+960
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": []
9+
}
10+
],
11+
"metadata": {
12+
"kernelspec": {
13+
"display_name": "Python 3",
14+
"language": "python",
15+
"name": "python3"
16+
},
17+
"language_info": {
18+
"codemirror_mode": {
19+
"name": "ipython",
20+
"version": 3
21+
},
22+
"file_extension": ".py",
23+
"mimetype": "text/x-python",
24+
"name": "python",
25+
"nbconvert_exporter": "python",
26+
"pygments_lexer": "ipython3",
27+
"version": "3.6.4"
28+
}
29+
},
30+
"nbformat": 4,
31+
"nbformat_minor": 2
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## No Hidden layers"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 2,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 63,
22+
"metadata": {},
23+
"outputs": [
24+
{
25+
"data": {
26+
"text/plain": [
27+
"((4, 2), (4, 1))"
28+
]
29+
},
30+
"execution_count": 63,
31+
"metadata": {},
32+
"output_type": "execute_result"
33+
}
34+
],
35+
"source": [
36+
"X = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
37+
"Y = np.array([[0,0,0,1]]).T\n",
38+
"X.shape,Y.shape"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 7,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"def sig(z):\n",
48+
" return 1/(1+np.exp(-z))"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 17,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"def derivativeSig(z):\n",
58+
" return sig(z)*(1-sig(z))"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 42,
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"data": {
68+
"text/plain": [
69+
"(array([[ 0.19225879],\n",
70+
" [-0.68617919]]), array([-0.94748922]))"
71+
]
72+
},
73+
"execution_count": 42,
74+
"metadata": {},
75+
"output_type": "execute_result"
76+
}
77+
],
78+
"source": [
79+
"#no hidden layer\n",
80+
"# weights = 2 * np.random.random((2,1)) - 1\n",
81+
"# bais = 2 * np.random.random(1) - 1\n",
82+
"# lr = 0.01\n",
83+
"# weights,bais"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 72,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"data": {
93+
"text/plain": [
94+
"(array([[ 0.83508785],\n",
95+
" [-0.48865845]]), array([-0.34629789]))"
96+
]
97+
},
98+
"execution_count": 72,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"weights = 2* np.random.random((2, 1)) - 1\n",
105+
"bias = 2 * np.random.random(1) - 1\n",
106+
"lr = 0.1\n",
107+
"weights, bias"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 78,
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"data": {
117+
"text/plain": [
118+
"(array([[6.55847355],\n",
119+
" [6.55847355]]), array([-10.68602841]), array([[2.28616145e-05],\n",
120+
" [1.58664489e-02],\n",
121+
" [1.58664489e-02],\n",
122+
" [9.19154826e-01]]))"
123+
]
124+
},
125+
"execution_count": 78,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"#no hidden layer\n",
132+
"for iter in range(10000):\n",
133+
" output0 = X\n",
134+
" output = sig(np.dot(output0,weights)+bias)\n",
135+
"\n",
136+
"\n",
137+
" first_term = output-Y\n",
138+
" input_for_the_last_layer = np.dot(output0,weights)+bais\n",
139+
" second_term = DerivativeSig(input_for_the_last_layer)\n",
140+
" first_two = first_term * second_term\n",
141+
" \n",
142+
" first_two.shape\n",
143+
" changes = np.array([[0.0],[0.0]])\n",
144+
"\n",
145+
" for i in range(2):\n",
146+
" for j in range(4):\n",
147+
" changes[i][0] += first_two[j][0] * output0[j][i]\n",
148+
" weights = weights- lr*changes\n",
149+
" bais_change = 0.0\n",
150+
" for j in range(4):\n",
151+
" bais_change += first_two[j][0] *1\n",
152+
" bias = bias - lr*bais_change\n",
153+
"\n",
154+
"output = sig(np.dot(X,weights)+bias)\n",
155+
"weights,bias,output"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 79,
161+
"metadata": {},
162+
"outputs": [
163+
{
164+
"data": {
165+
"text/plain": [
166+
"(array([[6.50448825],\n",
167+
" [6.50448825]]), array([-10.79883995]), array([[2.04227644e-05],\n",
168+
" [1.34617243e-02],\n",
169+
" [1.34617243e-02],\n",
170+
" [9.01156090e-01]]))"
171+
]
172+
},
173+
"execution_count": 79,
174+
"metadata": {},
175+
"output_type": "execute_result"
176+
}
177+
],
178+
"source": [
179+
"#no hidden layer\n",
180+
"for iter in range(10000):\n",
181+
" output0 = X\n",
182+
" output = sig(np.dot(output0,weights)+bias)\n",
183+
"\n",
184+
"\n",
185+
" first_term = output-Y\n",
186+
" input_for_the_last_layer = np.dot(output0,weights)+bais\n",
187+
" second_term = DerivativeSig(input_for_the_last_layer)\n",
188+
" first_two = first_term * second_term\n",
189+
" \n",
190+
" first_two.shape\n",
191+
" changes = np.dot(output0.T,first_two)\n",
192+
"\n",
193+
" weights = weights- lr*changes\n",
194+
" bais_change = np.sum(first_two)\n",
195+
" bias = bias - lr*bais_change\n",
196+
"\n",
197+
"output = sig(np.dot(X,weights)+bias)\n",
198+
"weights,bias,output"
199+
]
200+
},
201+
{
202+
"cell_type": "markdown",
203+
"metadata": {},
204+
"source": [
205+
"## Hidden Layer Neural Network (XOR)"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 3,
211+
"metadata": {},
212+
"outputs": [
213+
{
214+
"data": {
215+
"text/plain": [
216+
"((4, 2), (4, 1))"
217+
]
218+
},
219+
"execution_count": 3,
220+
"metadata": {},
221+
"output_type": "execute_result"
222+
}
223+
],
224+
"source": [
225+
"Xh = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
226+
"Yh = np.array([[0,1,1,0]]).T\n",
227+
"Xh.shape,Yh.shape"
228+
]
229+
},
230+
{
231+
"cell_type": "code",
232+
"execution_count": 4,
233+
"metadata": {},
234+
"outputs": [],
235+
"source": [
236+
"wh = 2*np.random.random((2,2))-1\n",
237+
"bh = 2*np.random.random((1,2))-1\n",
238+
"wo = 2*np.random.random((2,1))-1\n",
239+
"bo = 2*np.random.random((1,1)) -1\n",
240+
"lr = 0.1"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 20,
246+
"metadata": {},
247+
"outputs": [
248+
{
249+
"data": {
250+
"text/plain": [
251+
"(array([[0.04218936],\n",
252+
" [0.95282516],\n",
253+
" [0.95996342],\n",
254+
" [0.03735394]]), array([[ 4.91616033, 5.62227738],\n",
255+
" [-5.14461895, -5.55010192]]), array([[-2.67450006, 2.85794844]]), array([[ 7.98928045],\n",
256+
" [-7.5259943 ]]), array([[3.47979419]]))"
257+
]
258+
},
259+
"execution_count": 20,
260+
"metadata": {},
261+
"output_type": "execute_result"
262+
}
263+
],
264+
"source": [
265+
"#forward propagation\n",
266+
"for iter in range(10000):\n",
267+
" output0 =Xh\n",
268+
" inputHidden = np.dot(output0,wh)+bh\n",
269+
" outputHidden = sig(inputHidden)\n",
270+
" inputforOutputLayer = np.dot(outputHidden,wo)+ bo \n",
271+
" output = sig(inputforOutputLayer)\n",
272+
" output\n",
273+
"\n",
274+
" first_term_for_output_layer = output-Yh\n",
275+
" second_term_output_layer = derivativeSig(inputforOutputLayer)\n",
276+
" first_two_term_output_layer = first_term_for_output_layer*second_term_output_layer\n",
277+
" first_term_hidden_layer = np.dot(first_two_term_output_layer,wo.T)\n",
278+
" second_term_hidden_layer = derivativeSig(inputHidden)\n",
279+
" first_two_hidden_layer = first_term_hidden_layer*second_term_hidden_layer\n",
280+
"\n",
281+
" changes_output = np.dot(outputHidden.T,first_two_term_output_layer)\n",
282+
" changes_output_bias = np.sum(first_two_term_output_layer,axis=0,keepdims=True)\n",
283+
"\n",
284+
" changes_hidden = np.dot(output0.T,first_two_hidden_layer)\n",
285+
" changes_hidden_bias = np.sum(first_two_hidden_layer,axis=0,keepdims=True)\n",
286+
"\n",
287+
" wo = wo-lr*changes_output\n",
288+
" bo = bo-lr*changes_output_bias\n",
289+
"\n",
290+
" wh = wh - lr*changes_hidden\n",
291+
" bh = bh - lr*changes_hidden_bias\n",
292+
"output0 =Xh\n",
293+
"inputHidden = np.dot(output0,wh)+bh\n",
294+
"outputHidden = sig(inputHidden)\n",
295+
"inputforOutputLayer = np.dot(outputHidden,wo)+ bo \n",
296+
"output = sig(inputforOutputLayer)\n",
297+
"output,wh,bh,wo,bo"
298+
]
299+
},
300+
{
301+
"cell_type": "code",
302+
"execution_count": null,
303+
"metadata": {},
304+
"outputs": [],
305+
"source": []
306+
}
307+
],
308+
"metadata": {
309+
"kernelspec": {
310+
"display_name": "Python 3",
311+
"language": "python",
312+
"name": "python3"
313+
},
314+
"language_info": {
315+
"codemirror_mode": {
316+
"name": "ipython",
317+
"version": 3
318+
},
319+
"file_extension": ".py",
320+
"mimetype": "text/x-python",
321+
"name": "python",
322+
"nbconvert_exporter": "python",
323+
"pygments_lexer": "ipython3",
324+
"version": "3.6.4"
325+
}
326+
},
327+
"nbformat": 4,
328+
"nbformat_minor": 2
329+
}

0 commit comments

Comments
 (0)