MuzzammilShah commited on
Commit
525fbd4
·
verified ·
1 Parent(s): aa46144

Initial Uploads

Browse files
1-derivative-simple-function.ipynb ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import math\n",
10
+ "import numpy as np\n",
11
+ "import matplotlib.pyplot as plt\n",
12
+ "%matplotlib inline"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 2,
18
+ "metadata": {},
19
+ "outputs": [],
20
+ "source": [
21
+ "# Now define a function, a scaler value function f(x)\n",
22
+ "def f(x):\n",
23
+ " return 3*x**2 - 4*x +5"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 4,
29
+ "metadata": {},
30
+ "outputs": [
31
+ {
32
+ "data": {
33
+ "text/plain": [
34
+ "20.0"
35
+ ]
36
+ },
37
+ "execution_count": 4,
38
+ "metadata": {},
39
+ "output_type": "execute_result"
40
+ }
41
+ ],
42
+ "source": [
43
+ "# Now we can just pass in some value to check\n",
44
+ "f(3.0)"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 7,
50
+ "metadata": {},
51
+ "outputs": [
52
+ {
53
+ "data": {
54
+ "text/plain": [
55
+ "array([-5. , -4.75, -4.5 , -4.25, -4. , -3.75, -3.5 , -3.25, -3. ,\n",
56
+ " -2.75, -2.5 , -2.25, -2. , -1.75, -1.5 , -1.25, -1. , -0.75,\n",
57
+ " -0.5 , -0.25, 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 ,\n",
58
+ " 1.75, 2. , 2.25, 2.5 , 2.75, 3. , 3.25, 3.5 , 3.75,\n",
59
+ " 4. , 4.25, 4.5 , 4.75])"
60
+ ]
61
+ },
62
+ "execution_count": 7,
63
+ "metadata": {},
64
+ "output_type": "execute_result"
65
+ }
66
+ ],
67
+ "source": [
68
+ "# The f(x) equation as you can see is a Quadratic equation, precisely a Parabola\n",
69
+ "# So now, we try to plot it\n",
70
+ "\n",
71
+ "# Now, we'll just add like a range of values to feed in\n",
72
+ "\n",
73
+ "# We'll start with x-axis values so, from -5 to 5 (Not including 5) in the steps of 0.25\n",
74
+ "# Therefore creating a numpy array\n",
75
+ "xs = np.arange(-5,5,0.25)\n",
76
+ "xs"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": 8,
82
+ "metadata": {},
83
+ "outputs": [
84
+ {
85
+ "data": {
86
+ "text/plain": [
87
+ "array([100. , 91.6875, 83.75 , 76.1875, 69. , 62.1875,\n",
88
+ " 55.75 , 49.6875, 44. , 38.6875, 33.75 , 29.1875,\n",
89
+ " 25. , 21.1875, 17.75 , 14.6875, 12. , 9.6875,\n",
90
+ " 7.75 , 6.1875, 5. , 4.1875, 3.75 , 3.6875,\n",
91
+ " 4. , 4.6875, 5.75 , 7.1875, 9. , 11.1875,\n",
92
+ " 13.75 , 16.6875, 20. , 23.6875, 27.75 , 32.1875,\n",
93
+ " 37. , 42.1875, 47.75 , 53.6875])"
94
+ ]
95
+ },
96
+ "execution_count": 8,
97
+ "metadata": {},
98
+ "output_type": "execute_result"
99
+ }
100
+ ],
101
+ "source": [
102
+ "# Now for the y-axis values, we call each of those elements in the numpy array to the function f(x)\n",
103
+ "\n",
104
+ "# Therefore we create an another numpy array which containes the values after applying the function to each of the elements in xs\n",
105
+ "ys = f(xs)\n",
106
+ "ys"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 9,
112
+ "metadata": {},
113
+ "outputs": [
114
+ {
115
+ "data": {
116
+ "text/plain": [
117
+ "[<matplotlib.lines.Line2D at 0x2274508baf0>]"
118
+ ]
119
+ },
120
+ "execution_count": 9,
121
+ "metadata": {},
122
+ "output_type": "execute_result"
123
+ },
124
+ {
125
+ "data": {
126
+ "image/png": "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",
127
+ "text/plain": [
128
+ "<Figure size 640x480 with 1 Axes>"
129
+ ]
130
+ },
131
+ "metadata": {},
132
+ "output_type": "display_data"
133
+ }
134
+ ],
135
+ "source": [
136
+ "# And now we plot this using matplotlib\n",
137
+ "plt.plot(xs, ys)"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "markdown",
142
+ "metadata": {},
143
+ "source": [
144
+ "Now we need to see what is the derivative of this function f(x) at any single input point x\n",
145
+ "\\\n",
146
+ "\\\n",
147
+ "So what is the derivative at different point in the x-axis to the function f(x)"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "markdown",
152
+ "metadata": {},
153
+ "source": [
154
+ "![image.png](https://wikimedia.org/api/rest_v1/media/math/render/svg/aae79a56cdcbc44af1612a50f06169b07f02cbf3)"
155
+ ]
156
+ },
157
+ {
158
+ "cell_type": "markdown",
159
+ "metadata": {},
160
+ "source": [
161
+ "Now, we can check the derivative of the value by considering a very small value of h (almost close to zero)"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "code",
166
+ "execution_count": 10,
167
+ "metadata": {},
168
+ "outputs": [],
169
+ "source": [
170
+ "h = 0.001\n",
171
+ "x = 3.0"
172
+ ]
173
+ },
174
+ {
175
+ "cell_type": "code",
176
+ "execution_count": 12,
177
+ "metadata": {},
178
+ "outputs": [
179
+ {
180
+ "data": {
181
+ "text/plain": [
182
+ "20.014003000000002"
183
+ ]
184
+ },
185
+ "execution_count": 12,
186
+ "metadata": {},
187
+ "output_type": "execute_result"
188
+ }
189
+ ],
190
+ "source": [
191
+ "# Now, if we take the f(x) value directly, we know we get 20.0 (Already done in above cells)\n",
192
+ "\n",
193
+ "# Lets say what if we add the value of h to it, so we are nudging it to a slighly more positive direction\n",
194
+ "# So the value must be slightly more than 20\n",
195
+ "\n",
196
+ "f(x+h)"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": 14,
202
+ "metadata": {},
203
+ "outputs": [
204
+ {
205
+ "data": {
206
+ "text/plain": [
207
+ "0.01400300000000243"
208
+ ]
209
+ },
210
+ "execution_count": 14,
211
+ "metadata": {},
212
+ "output_type": "execute_result"
213
+ }
214
+ ],
215
+ "source": [
216
+ "# Now, by how much that above value has increased shows us the strength or the size of that slope\n",
217
+ "\n",
218
+ "# Therefore, Next we see how much the function has responded\n",
219
+ "\n",
220
+ "f(x+h) - f(x)"
221
+ ]
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "execution_count": 15,
226
+ "metadata": {},
227
+ "outputs": [
228
+ {
229
+ "data": {
230
+ "text/plain": [
231
+ "14.00300000000243"
232
+ ]
233
+ },
234
+ "execution_count": 15,
235
+ "metadata": {},
236
+ "output_type": "execute_result"
237
+ }
238
+ ],
239
+ "source": [
240
+ "# Next we have to normalise it by adding the value rise of the run i.e. h, to get the value of the slope\n",
241
+ "\n",
242
+ "(f(x+h) - f(x))/h"
243
+ ]
244
+ },
245
+ {
246
+ "cell_type": "markdown",
247
+ "metadata": {},
248
+ "source": [
249
+ "Therefore, at 3 i.e. when x=3, the slope is **14**.\n",
250
+ "\\\n",
251
+ "\\\n",
252
+ "You can see the same value if you calculate it manually using the derivative formula:\n",
253
+ "\\\n",
254
+ "=> Derivative of 3x^2 - 4x +5 \\\n",
255
+ "=> 6x-4 \\\n",
256
+ "=> 6(3) - 4 \\\n",
257
+ "=> 18 -4 \\\n",
258
+ "=> **14**"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": 18,
264
+ "metadata": {},
265
+ "outputs": [
266
+ {
267
+ "data": {
268
+ "text/plain": [
269
+ "-22.00000039920269"
270
+ ]
271
+ },
272
+ "execution_count": 18,
273
+ "metadata": {},
274
+ "output_type": "execute_result"
275
+ }
276
+ ],
277
+ "source": [
278
+ "# Now what if we add a negative value for x?\n",
279
+ "# Then even the function will become negative. Therefore, we will be getting a negative sign slope\n",
280
+ "\n",
281
+ "h = 0.00000001 # Cant make this too small, as unlike in theory, computer can handle only a finite amount. Therefore make it too small and it will directly return 0 :)\n",
282
+ "x = -3.0\n",
283
+ "(f(x+h) - f(x))/h\n"
284
+ ]
285
+ },
286
+ {
287
+ "cell_type": "code",
288
+ "execution_count": 19,
289
+ "metadata": {},
290
+ "outputs": [
291
+ {
292
+ "data": {
293
+ "text/plain": [
294
+ "0.0"
295
+ ]
296
+ },
297
+ "execution_count": 19,
298
+ "metadata": {},
299
+ "output_type": "execute_result"
300
+ }
301
+ ],
302
+ "source": [
303
+ "# Now at some point the slop must be 0, therefore nudging a small value either way from that point, it still remains 0\n",
304
+ "# In this case, for the function it is at around x = 2/3\n",
305
+ "\n",
306
+ "h = 0.00000001\n",
307
+ "x = 2/3\n",
308
+ "(f(x+h) - f(x))/h"
309
+ ]
310
+ }
311
+ ],
312
+ "metadata": {
313
+ "kernelspec": {
314
+ "display_name": "venv",
315
+ "language": "python",
316
+ "name": "python3"
317
+ },
318
+ "language_info": {
319
+ "codemirror_mode": {
320
+ "name": "ipython",
321
+ "version": 3
322
+ },
323
+ "file_extension": ".py",
324
+ "mimetype": "text/x-python",
325
+ "name": "python",
326
+ "nbconvert_exporter": "python",
327
+ "pygments_lexer": "ipython3",
328
+ "version": "3.10.0"
329
+ }
330
+ },
331
+ "nbformat": 4,
332
+ "nbformat_minor": 2
333
+ }
2-derivative-function-with-multiple-inputs.ipynb ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "# Now we get slighly more complex from the previous one\n",
10
+ "\n",
11
+ "# We are adding 3 scalar inputs\n",
12
+ "a = 2.0\n",
13
+ "b = -3.0\n",
14
+ "c = 10.0\n",
15
+ "\n",
16
+ "#Single ouput\n",
17
+ "d = a*b + c"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": 2,
23
+ "metadata": {},
24
+ "outputs": [
25
+ {
26
+ "name": "stdout",
27
+ "output_type": "stream",
28
+ "text": [
29
+ "4.0\n"
30
+ ]
31
+ }
32
+ ],
33
+ "source": [
34
+ "print(d)"
35
+ ]
36
+ },
37
+ {
38
+ "cell_type": "markdown",
39
+ "metadata": {},
40
+ "source": [
41
+ "Now we are gonna look at the derivitive of d wrt to a,b & c"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 3,
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "# We'll set the value of h\n",
51
+ "h = 0.0001"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": 11,
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "#inputs\n",
61
+ "a = 2.0\n",
62
+ "b = -3.0\n",
63
+ "c = 10.0\n",
64
+ "\n",
65
+ "d1 = a*b + c"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "markdown",
70
+ "metadata": {},
71
+ "source": [
72
+ "(i) wrt a"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": 6,
78
+ "metadata": {},
79
+ "outputs": [],
80
+ "source": [
81
+ "a += h\n",
82
+ "\n",
83
+ "d2 = a*b + c"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": 7,
89
+ "metadata": {},
90
+ "outputs": [
91
+ {
92
+ "name": "stdout",
93
+ "output_type": "stream",
94
+ "text": [
95
+ "d1 4.0\n",
96
+ "d2 3.999699999999999\n",
97
+ "Slope: -3.000000000010772\n"
98
+ ]
99
+ }
100
+ ],
101
+ "source": [
102
+ "print('d1', d1)\n",
103
+ "print('d2', d2)\n",
104
+ "\n",
105
+ "# Here (d2 - d1) is the amount of bump that was actually increased, after we had bump the value of the specific value that we are interested in by a tiny amount\n",
106
+ "# And that is normalised by dividing that value by h, to get the slope\n",
107
+ "print('Slope: ', (d2 - d1)/h)"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "markdown",
112
+ "metadata": {},
113
+ "source": [
114
+ "Now here with the ouput d2, it becomes slightly less than 4 after we had added a small value to a\n",
115
+ "\\\n",
116
+ "\\\n",
117
+ "As the negative value formed after the product is bigger\n",
118
+ "\\\n",
119
+ "\\\n",
120
+ "Now, since we know the overall value had reduced, the sign of the slope should also naturally be negative (as seen)"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "markdown",
125
+ "metadata": {},
126
+ "source": [
127
+ "Even Mathematically, if we take the derivative of the expression d2 wrt a \\\n",
128
+ "then only b is left \\\n",
129
+ "which is a negative value, hence the negative sign"
130
+ ]
131
+ },
132
+ {
133
+ "cell_type": "markdown",
134
+ "metadata": {},
135
+ "source": [
136
+ "(ii) wrt b"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": 10,
142
+ "metadata": {},
143
+ "outputs": [
144
+ {
145
+ "name": "stdout",
146
+ "output_type": "stream",
147
+ "text": [
148
+ "d1 4.0\n",
149
+ "d2 4.0002\n",
150
+ "Slope: 2.0000000000042206\n"
151
+ ]
152
+ }
153
+ ],
154
+ "source": [
155
+ "b += h\n",
156
+ "\n",
157
+ "d2 = a*b + c\n",
158
+ "\n",
159
+ "print('d1', d1)\n",
160
+ "print('d2', d2)\n",
161
+ "\n",
162
+ "print('Slope: ', (d2 - d1)/h)"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "markdown",
167
+ "metadata": {},
168
+ "source": [
169
+ "Similar explaination here\n",
170
+ "\\\n",
171
+ "\\\n",
172
+ "the value of b becomes slightly less after adding the small increament\n",
173
+ "\\\n",
174
+ "Therefore increasing the value of the final expression d2\n",
175
+ "\\\n",
176
+ "\\\n",
177
+ "Here, since its the derivative of d2 wrt b\n",
178
+ "\\\n",
179
+ "only a remains, therefore a positive slope value"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "markdown",
184
+ "metadata": {},
185
+ "source": [
186
+ "(iii) wrt c"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "code",
191
+ "execution_count": 12,
192
+ "metadata": {},
193
+ "outputs": [
194
+ {
195
+ "name": "stdout",
196
+ "output_type": "stream",
197
+ "text": [
198
+ "d1 4.0\n",
199
+ "d2 4.0001\n",
200
+ "Slope: 0.9999999999976694\n"
201
+ ]
202
+ }
203
+ ],
204
+ "source": [
205
+ "c += h\n",
206
+ "\n",
207
+ "d2 = a*b + c\n",
208
+ "\n",
209
+ "print('d1', d1)\n",
210
+ "print('d2', d2)\n",
211
+ "\n",
212
+ "print('Slope: ', (d2 - d1)/h)"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "markdown",
217
+ "metadata": {},
218
+ "source": [
219
+ "Here, we add a slightly bit higher value to c\n",
220
+ "\\\n",
221
+ "\\\n",
222
+ "For this derivative, a and b are not considered.\n",
223
+ "\\\n",
224
+ "The function or d2 becomes slightly bit higher because of c\n",
225
+ "\\\n",
226
+ "It becomes slightly bit higher by the exact amount that was added to c\n",
227
+ "\\\n",
228
+ "\\\n",
229
+ "Therefore cancelling out and the value of the slope is hence 1"
230
+ ]
231
+ },
232
+ {
233
+ "cell_type": "markdown",
234
+ "metadata": {},
235
+ "source": [
236
+ "**Therefore, this will be the derivative at which 'd' will increase, as we scale 'c'**"
237
+ ]
238
+ }
239
+ ],
240
+ "metadata": {
241
+ "kernelspec": {
242
+ "display_name": "venv",
243
+ "language": "python",
244
+ "name": "python3"
245
+ },
246
+ "language_info": {
247
+ "codemirror_mode": {
248
+ "name": "ipython",
249
+ "version": 3
250
+ },
251
+ "file_extension": ".py",
252
+ "mimetype": "text/x-python",
253
+ "name": "python",
254
+ "nbconvert_exporter": "python",
255
+ "pygments_lexer": "ipython3",
256
+ "version": "3.10.0"
257
+ }
258
+ },
259
+ "nbformat": 4,
260
+ "nbformat_minor": 2
261
+ }
3-value-object.ipynb ADDED
@@ -0,0 +1,335 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "#### Expression: d = a * b + c"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 6,
13
+ "metadata": {},
14
+ "outputs": [],
15
+ "source": [
16
+ "class Value:\n",
17
+ "\n",
18
+ " def __init__(self, data):\n",
19
+ " self.data = data\n",
20
+ "\n",
21
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
22
+ " return f\"Value(data={self.data})\"\n",
23
+ " \n",
24
+ " def __add__(self, other):\n",
25
+ " out = Value(self.data + other.data)\n",
26
+ " return out\n",
27
+ " \n",
28
+ " def __mul__(self, other):\n",
29
+ " out = Value(self.data * other.data)\n",
30
+ " return out"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 12,
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "a = Value(2.0)\n",
40
+ "b = Value(-3.0)\n",
41
+ "c = Value(10.0)"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 9,
47
+ "metadata": {},
48
+ "outputs": [
49
+ {
50
+ "data": {
51
+ "text/plain": [
52
+ "Value(data=4.0)"
53
+ ]
54
+ },
55
+ "execution_count": 9,
56
+ "metadata": {},
57
+ "output_type": "execute_result"
58
+ }
59
+ ],
60
+ "source": [
61
+ "a*b + c"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "markdown",
66
+ "metadata": {},
67
+ "source": [
68
+ "SAME THING!! (Up and Down cells)"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": 10,
74
+ "metadata": {},
75
+ "outputs": [
76
+ {
77
+ "data": {
78
+ "text/plain": [
79
+ "Value(data=4.0)"
80
+ ]
81
+ },
82
+ "execution_count": 10,
83
+ "metadata": {},
84
+ "output_type": "execute_result"
85
+ }
86
+ ],
87
+ "source": [
88
+ "(a.__mul__(b)).__add__(c)"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "markdown",
93
+ "metadata": {},
94
+ "source": [
95
+ "--------"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "markdown",
100
+ "metadata": {},
101
+ "source": [
102
+ "#### Visualization of the expression"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 3,
108
+ "metadata": {},
109
+ "outputs": [],
110
+ "source": [
111
+ "class Value:\n",
112
+ "\n",
113
+ " def __init__(self, data, _children=(), _op=''):\n",
114
+ " self.data = data\n",
115
+ " self._prev = set(_children)\n",
116
+ " self._op = _op\n",
117
+ "\n",
118
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
119
+ " return f\"Value(data={self.data})\"\n",
120
+ " \n",
121
+ " def __add__(self, other):\n",
122
+ " out = Value(self.data + other.data, (self, other), '+')\n",
123
+ " return out\n",
124
+ " \n",
125
+ " def __mul__(self, other):\n",
126
+ " out = Value(self.data * other.data, (self, other), '*')\n",
127
+ " return out"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": 13,
133
+ "metadata": {},
134
+ "outputs": [],
135
+ "source": [
136
+ "d = a*b + c"
137
+ ]
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": 14,
142
+ "metadata": {},
143
+ "outputs": [
144
+ {
145
+ "data": {
146
+ "text/plain": [
147
+ "{Value(data=-6.0), Value(data=10.0)}"
148
+ ]
149
+ },
150
+ "execution_count": 14,
151
+ "metadata": {},
152
+ "output_type": "execute_result"
153
+ }
154
+ ],
155
+ "source": [
156
+ "d._prev"
157
+ ]
158
+ },
159
+ {
160
+ "cell_type": "code",
161
+ "execution_count": 15,
162
+ "metadata": {},
163
+ "outputs": [
164
+ {
165
+ "data": {
166
+ "text/plain": [
167
+ "'+'"
168
+ ]
169
+ },
170
+ "execution_count": 15,
171
+ "metadata": {},
172
+ "output_type": "execute_result"
173
+ }
174
+ ],
175
+ "source": [
176
+ "d._op"
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "markdown",
181
+ "metadata": {},
182
+ "source": [
183
+ "--------------------"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "execution_count": 4,
189
+ "metadata": {},
190
+ "outputs": [
191
+ {
192
+ "data": {
193
+ "text/plain": [
194
+ "Value(data=4.0)"
195
+ ]
196
+ },
197
+ "execution_count": 4,
198
+ "metadata": {},
199
+ "output_type": "execute_result"
200
+ }
201
+ ],
202
+ "source": [
203
+ "a = Value(2.0)\n",
204
+ "b = Value(-3.0)\n",
205
+ "c = Value(10.0)\n",
206
+ "\n",
207
+ "d= a*b + c\n",
208
+ "d"
209
+ ]
210
+ },
211
+ {
212
+ "cell_type": "code",
213
+ "execution_count": 1,
214
+ "metadata": {},
215
+ "outputs": [
216
+ {
217
+ "name": "stdout",
218
+ "output_type": "stream",
219
+ "text": [
220
+ "Collecting graphviz\n",
221
+ " Downloading graphviz-0.20.3-py3-none-any.whl (47 kB)\n",
222
+ "Installing collected packages: graphviz\n",
223
+ "Successfully installed graphviz-0.20.3\n",
224
+ "Note: you may need to restart the kernel to use updated packages.\n"
225
+ ]
226
+ },
227
+ {
228
+ "name": "stderr",
229
+ "output_type": "stream",
230
+ "text": [
231
+ "WARNING: You are using pip version 21.2.3; however, version 24.2 is available.\n",
232
+ "You should consider upgrading via the 'c:\\Users\\imdiv\\OneDrive\\Desktop\\GitHub Portal\\Micrograd\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
233
+ ]
234
+ }
235
+ ],
236
+ "source": [
237
+ "%pip install graphviz"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "code",
242
+ "execution_count": 9,
243
+ "metadata": {},
244
+ "outputs": [],
245
+ "source": [
246
+ "from graphviz import Digraph\n",
247
+ "\n",
248
+ "def trace(root):\n",
249
+ " #Builds a set of all nodes and edges in a graph\n",
250
+ " nodes, edges = set(), set()\n",
251
+ " def build(v):\n",
252
+ " if v not in nodes:\n",
253
+ " nodes.add(v)\n",
254
+ " for child in v._prev:\n",
255
+ " edges.add((child, v))\n",
256
+ " build(child)\n",
257
+ " build(root)\n",
258
+ " return nodes, edges\n",
259
+ "\n",
260
+ "def draw_dot(root):\n",
261
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
262
+ "\n",
263
+ " nodes, edges = trace(root)\n",
264
+ " for n in nodes:\n",
265
+ " uid = str(id(n))\n",
266
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
267
+ " dot.node(name = uid, label = \"{ data %.4f }\" % ( n.data, ), shape='record')\n",
268
+ " if n._op:\n",
269
+ " #If this value is a result of some operation, then create an op node (in a circle/oval shape to distinguish) for it\n",
270
+ " dot.node(name = uid + n._op, label=n._op)\n",
271
+ " #and connect this node to it\n",
272
+ " dot.edge(uid + n._op, uid)\n",
273
+ "\n",
274
+ " for n1, n2 in edges:\n",
275
+ " #Connect n1 to the node of n2\n",
276
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
277
+ "\n",
278
+ " return dot"
279
+ ]
280
+ },
281
+ {
282
+ "cell_type": "code",
283
+ "execution_count": null,
284
+ "metadata": {},
285
+ "outputs": [],
286
+ "source": [
287
+ "draw_dot(d)"
288
+ ]
289
+ },
290
+ {
291
+ "cell_type": "markdown",
292
+ "metadata": {},
293
+ "source": [
294
+ "NOTE: The graph will only get generated if you have graphviz installed locally on your machine and the env variable path needs to be set. \n",
295
+ "\n",
296
+ "If you want to avoid downloading it locally, you can directly run this on Google Colab to view the output, as graphviz is already preinstalled for you"
297
+ ]
298
+ },
299
+ {
300
+ "cell_type": "markdown",
301
+ "metadata": {},
302
+ "source": [
303
+ "**Check 3_1-graph-visualisation.ipynb for viewing the outputs**"
304
+ ]
305
+ },
306
+ {
307
+ "cell_type": "markdown",
308
+ "metadata": {},
309
+ "source": [
310
+ "-------------------"
311
+ ]
312
+ }
313
+ ],
314
+ "metadata": {
315
+ "kernelspec": {
316
+ "display_name": "venv",
317
+ "language": "python",
318
+ "name": "python3"
319
+ },
320
+ "language_info": {
321
+ "codemirror_mode": {
322
+ "name": "ipython",
323
+ "version": 3
324
+ },
325
+ "file_extension": ".py",
326
+ "mimetype": "text/x-python",
327
+ "name": "python",
328
+ "nbconvert_exporter": "python",
329
+ "pygments_lexer": "ipython3",
330
+ "version": "3.10.0"
331
+ }
332
+ },
333
+ "nbformat": 4,
334
+ "nbformat_minor": 2
335
+ }
3_1-graph-visualisation.ipynb ADDED
@@ -0,0 +1,778 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "**Note: Start from 3-value-object.ipynb and then come back to this notebook**"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 1,
13
+ "metadata": {
14
+ "id": "a3vQTv60x2DL"
15
+ },
16
+ "outputs": [],
17
+ "source": [
18
+ "class Value:\n",
19
+ "\n",
20
+ " def __init__(self, data, _children=(), _op=''):\n",
21
+ " self.data = data\n",
22
+ " self._prev = set(_children)\n",
23
+ " self._op = _op\n",
24
+ "\n",
25
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
26
+ " return f\"Value(data={self.data})\"\n",
27
+ "\n",
28
+ " def __add__(self, other):\n",
29
+ " out = Value(self.data + other.data, (self, other), '+')\n",
30
+ " return out\n",
31
+ "\n",
32
+ " def __mul__(self, other):\n",
33
+ " out = Value(self.data * other.data, (self, other), '*')\n",
34
+ " return out"
35
+ ]
36
+ },
37
+ {
38
+ "cell_type": "code",
39
+ "execution_count": 2,
40
+ "metadata": {
41
+ "colab": {
42
+ "base_uri": "https://localhost:8080/"
43
+ },
44
+ "id": "mMs20Lgfx5T-",
45
+ "outputId": "75f6bb3c-b38b-43cd-8790-f4d533a9d786"
46
+ },
47
+ "outputs": [
48
+ {
49
+ "data": {
50
+ "text/plain": [
51
+ "Value(data=4.0)"
52
+ ]
53
+ },
54
+ "execution_count": 2,
55
+ "metadata": {},
56
+ "output_type": "execute_result"
57
+ }
58
+ ],
59
+ "source": [
60
+ "a = Value(2.0)\n",
61
+ "b = Value(-3.0)\n",
62
+ "c = Value(10.0)\n",
63
+ "\n",
64
+ "d= a*b + c\n",
65
+ "d"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "markdown",
70
+ "metadata": {
71
+ "id": "nw3EmXumzbrm"
72
+ },
73
+ "source": [
74
+ "------------------------------"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": 3,
80
+ "metadata": {
81
+ "id": "uFstZf41x7Y-"
82
+ },
83
+ "outputs": [],
84
+ "source": [
85
+ "from graphviz import Digraph\n",
86
+ "\n",
87
+ "def trace(root):\n",
88
+ " #Builds a set of all nodes and edges in a graph\n",
89
+ " nodes, edges = set(), set()\n",
90
+ " def build(v):\n",
91
+ " if v not in nodes:\n",
92
+ " nodes.add(v)\n",
93
+ " for child in v._prev:\n",
94
+ " edges.add((child, v))\n",
95
+ " build(child)\n",
96
+ " build(root)\n",
97
+ " return nodes, edges\n",
98
+ "\n",
99
+ "def draw_dot(root):\n",
100
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
101
+ "\n",
102
+ " nodes, edges = trace(root)\n",
103
+ " for n in nodes:\n",
104
+ " uid = str(id(n))\n",
105
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
106
+ " dot.node(name = uid, label = \"{ data %.4f }\" % ( n.data, ), shape='record')\n",
107
+ " if n._op:\n",
108
+ " #If this value is a result of some operation, then create an op node for it\n",
109
+ " dot.node(name = uid + n._op, label=n._op)\n",
110
+ " #and connect this node to it\n",
111
+ " dot.edge(uid + n._op, uid)\n",
112
+ "\n",
113
+ " for n1, n2 in edges:\n",
114
+ " #Connect n1 to the node of n2\n",
115
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
116
+ "\n",
117
+ " return dot"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": 4,
123
+ "metadata": {
124
+ "colab": {
125
+ "base_uri": "https://localhost:8080/",
126
+ "height": 191
127
+ },
128
+ "id": "ctAf9yvAx-Gd",
129
+ "outputId": "d2ec2ab2-6f50-495d-825f-13eb8fe9e11b"
130
+ },
131
+ "outputs": [
132
+ {
133
+ "data": {
134
+ "image/svg+xml": [
135
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
136
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
137
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
138
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
139
+ " -->\n",
140
+ "<!-- Title: %3 Pages: 1 -->\n",
141
+ "<svg width=\"512pt\" height=\"127pt\"\n",
142
+ " viewBox=\"0.00 0.00 512.00 127.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
143
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 123)\">\n",
144
+ "<title>%3</title>\n",
145
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-123 508,-123 508,4 -4,4\"/>\n",
146
+ "<!-- 132024466696192 -->\n",
147
+ "<g id=\"node1\" class=\"node\">\n",
148
+ "<title>132024466696192</title>\n",
149
+ "<polygon fill=\"none\" stroke=\"black\" points=\"212,-27.5 212,-63.5 297,-63.5 297,-27.5 212,-27.5\"/>\n",
150
+ "<text text-anchor=\"middle\" x=\"254.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
151
+ "</g>\n",
152
+ "<!-- 132024466692304+ -->\n",
153
+ "<g id=\"node5\" class=\"node\">\n",
154
+ "<title>132024466692304+</title>\n",
155
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"361\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n",
156
+ "<text text-anchor=\"middle\" x=\"361\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
157
+ "</g>\n",
158
+ "<!-- 132024466696192&#45;&gt;132024466692304+ -->\n",
159
+ "<g id=\"edge3\" class=\"edge\">\n",
160
+ "<title>132024466696192&#45;&gt;132024466692304+</title>\n",
161
+ "<path fill=\"none\" stroke=\"black\" d=\"M297.1,-56.25C306.61,-58.71 316.62,-61.29 325.78,-63.66\"/>\n",
162
+ "<polygon fill=\"black\" stroke=\"black\" points=\"325.13,-67.11 335.69,-66.22 326.88,-60.33 325.13,-67.11\"/>\n",
163
+ "</g>\n",
164
+ "<!-- 132024466696192* -->\n",
165
+ "<g id=\"node2\" class=\"node\">\n",
166
+ "<title>132024466696192*</title>\n",
167
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"148\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n",
168
+ "<text text-anchor=\"middle\" x=\"148\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
169
+ "</g>\n",
170
+ "<!-- 132024466696192*&#45;&gt;132024466696192 -->\n",
171
+ "<g id=\"edge1\" class=\"edge\">\n",
172
+ "<title>132024466696192*&#45;&gt;132024466696192</title>\n",
173
+ "<path fill=\"none\" stroke=\"black\" d=\"M175.1,-45.5C183.27,-45.5 192.59,-45.5 201.88,-45.5\"/>\n",
174
+ "<polygon fill=\"black\" stroke=\"black\" points=\"201.89,-49 211.89,-45.5 201.89,-42 201.89,-49\"/>\n",
175
+ "</g>\n",
176
+ "<!-- 132024466696240 -->\n",
177
+ "<g id=\"node3\" class=\"node\">\n",
178
+ "<title>132024466696240</title>\n",
179
+ "<polygon fill=\"none\" stroke=\"black\" points=\"211,-82.5 211,-118.5 298,-118.5 298,-82.5 211,-82.5\"/>\n",
180
+ "<text text-anchor=\"middle\" x=\"254.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n",
181
+ "</g>\n",
182
+ "<!-- 132024466696240&#45;&gt;132024466692304+ -->\n",
183
+ "<g id=\"edge6\" class=\"edge\">\n",
184
+ "<title>132024466696240&#45;&gt;132024466692304+</title>\n",
185
+ "<path fill=\"none\" stroke=\"black\" d=\"M298,-89.11C307.18,-86.65 316.78,-84.08 325.6,-81.72\"/>\n",
186
+ "<polygon fill=\"black\" stroke=\"black\" points=\"326.78,-85.02 335.53,-79.06 324.97,-78.26 326.78,-85.02\"/>\n",
187
+ "</g>\n",
188
+ "<!-- 132024466692304 -->\n",
189
+ "<g id=\"node4\" class=\"node\">\n",
190
+ "<title>132024466692304</title>\n",
191
+ "<polygon fill=\"none\" stroke=\"black\" points=\"424,-54.5 424,-90.5 504,-90.5 504,-54.5 424,-54.5\"/>\n",
192
+ "<text text-anchor=\"middle\" x=\"464\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n",
193
+ "</g>\n",
194
+ "<!-- 132024466692304+&#45;&gt;132024466692304 -->\n",
195
+ "<g id=\"edge2\" class=\"edge\">\n",
196
+ "<title>132024466692304+&#45;&gt;132024466692304</title>\n",
197
+ "<path fill=\"none\" stroke=\"black\" d=\"M388.01,-72.5C395.82,-72.5 404.67,-72.5 413.48,-72.5\"/>\n",
198
+ "<polygon fill=\"black\" stroke=\"black\" points=\"413.73,-76 423.73,-72.5 413.73,-69 413.73,-76\"/>\n",
199
+ "</g>\n",
200
+ "<!-- 132024466695424 -->\n",
201
+ "<g id=\"node6\" class=\"node\">\n",
202
+ "<title>132024466695424</title>\n",
203
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-55.5 2.5,-91.5 82.5,-91.5 82.5,-55.5 2.5,-55.5\"/>\n",
204
+ "<text text-anchor=\"middle\" x=\"42.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n",
205
+ "</g>\n",
206
+ "<!-- 132024466695424&#45;&gt;132024466696192* -->\n",
207
+ "<g id=\"edge4\" class=\"edge\">\n",
208
+ "<title>132024466695424&#45;&gt;132024466696192*</title>\n",
209
+ "<path fill=\"none\" stroke=\"black\" d=\"M82.63,-62.91C92.66,-60.2 103.36,-57.31 113.09,-54.67\"/>\n",
210
+ "<polygon fill=\"black\" stroke=\"black\" points=\"114.09,-58.03 122.83,-52.04 112.26,-51.27 114.09,-58.03\"/>\n",
211
+ "</g>\n",
212
+ "<!-- 132024466702288 -->\n",
213
+ "<g id=\"node7\" class=\"node\">\n",
214
+ "<title>132024466702288</title>\n",
215
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 85,-36.5 85,-0.5 0,-0.5\"/>\n",
216
+ "<text text-anchor=\"middle\" x=\"42.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
217
+ "</g>\n",
218
+ "<!-- 132024466702288&#45;&gt;132024466696192* -->\n",
219
+ "<g id=\"edge5\" class=\"edge\">\n",
220
+ "<title>132024466702288&#45;&gt;132024466696192*</title>\n",
221
+ "<path fill=\"none\" stroke=\"black\" d=\"M85,-29.33C94.14,-31.71 103.72,-34.21 112.54,-36.51\"/>\n",
222
+ "<polygon fill=\"black\" stroke=\"black\" points=\"111.91,-39.96 122.47,-39.1 113.68,-33.19 111.91,-39.96\"/>\n",
223
+ "</g>\n",
224
+ "</g>\n",
225
+ "</svg>\n"
226
+ ],
227
+ "text/plain": [
228
+ "<graphviz.graphs.Digraph at 0x781357706f50>"
229
+ ]
230
+ },
231
+ "execution_count": 4,
232
+ "metadata": {},
233
+ "output_type": "execute_result"
234
+ }
235
+ ],
236
+ "source": [
237
+ "draw_dot(d)"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "markdown",
242
+ "metadata": {
243
+ "id": "0jbMh4zuzdPe"
244
+ },
245
+ "source": [
246
+ "----------------------------"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "markdown",
251
+ "metadata": {
252
+ "id": "5hRXnSyp0AVJ"
253
+ },
254
+ "source": [
255
+ "Same graph, but we are just adding labels"
256
+ ]
257
+ },
258
+ {
259
+ "cell_type": "code",
260
+ "execution_count": 5,
261
+ "metadata": {
262
+ "id": "W4YOVlr3yDk1"
263
+ },
264
+ "outputs": [],
265
+ "source": [
266
+ "class Value:\n",
267
+ "\n",
268
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
269
+ " self.data = data\n",
270
+ " self._prev = set(_children)\n",
271
+ " self._op = _op\n",
272
+ " self.label = label\n",
273
+ "\n",
274
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
275
+ " return f\"Value(data={self.data})\"\n",
276
+ "\n",
277
+ " def __add__(self, other):\n",
278
+ " out = Value(self.data + other.data, (self, other), '+')\n",
279
+ " return out\n",
280
+ "\n",
281
+ " def __mul__(self, other):\n",
282
+ " out = Value(self.data * other.data, (self, other), '*')\n",
283
+ " return out"
284
+ ]
285
+ },
286
+ {
287
+ "cell_type": "code",
288
+ "execution_count": 6,
289
+ "metadata": {
290
+ "colab": {
291
+ "base_uri": "https://localhost:8080/"
292
+ },
293
+ "id": "N_Hfq2Ew0HDD",
294
+ "outputId": "4bac705a-5a44-41a0-958f-54529ac4ee28"
295
+ },
296
+ "outputs": [
297
+ {
298
+ "data": {
299
+ "text/plain": [
300
+ "Value(data=4.0)"
301
+ ]
302
+ },
303
+ "execution_count": 6,
304
+ "metadata": {},
305
+ "output_type": "execute_result"
306
+ }
307
+ ],
308
+ "source": [
309
+ "a = Value(2.0, label='a')\n",
310
+ "b = Value(-3.0, label='b')\n",
311
+ "c = Value(10.0, label='c')\n",
312
+ "e = a*b; e.label='e'\n",
313
+ "d= e + c; d.label='d'\n",
314
+ "d"
315
+ ]
316
+ },
317
+ {
318
+ "cell_type": "code",
319
+ "execution_count": 7,
320
+ "metadata": {
321
+ "id": "hLg_Kkmi0I-T"
322
+ },
323
+ "outputs": [],
324
+ "source": [
325
+ "from graphviz import Digraph\n",
326
+ "\n",
327
+ "def trace(root):\n",
328
+ " #Builds a set of all nodes and edges in a graph\n",
329
+ " nodes, edges = set(), set()\n",
330
+ " def build(v):\n",
331
+ " if v not in nodes:\n",
332
+ " nodes.add(v)\n",
333
+ " for child in v._prev:\n",
334
+ " edges.add((child, v))\n",
335
+ " build(child)\n",
336
+ " build(root)\n",
337
+ " return nodes, edges\n",
338
+ "\n",
339
+ "def draw_dot(root):\n",
340
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
341
+ "\n",
342
+ " nodes, edges = trace(root)\n",
343
+ " for n in nodes:\n",
344
+ " uid = str(id(n))\n",
345
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
346
+ " dot.node(name = uid, label = \"{ %s | data %.4f }\" % ( n.label, n.data), shape='record')\n",
347
+ " if n._op:\n",
348
+ " #If this value is a result of some operation, then create an op node for it\n",
349
+ " dot.node(name = uid + n._op, label=n._op)\n",
350
+ " #and connect this node to it\n",
351
+ " dot.edge(uid + n._op, uid)\n",
352
+ "\n",
353
+ " for n1, n2 in edges:\n",
354
+ " #Connect n1 to the node of n2\n",
355
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
356
+ "\n",
357
+ " return dot"
358
+ ]
359
+ },
360
+ {
361
+ "cell_type": "code",
362
+ "execution_count": 8,
363
+ "metadata": {
364
+ "colab": {
365
+ "base_uri": "https://localhost:8080/",
366
+ "height": 192
367
+ },
368
+ "id": "-sUwmERm0Khb",
369
+ "outputId": "ccbf71a3-95cd-4449-b35c-40534ad17f2f"
370
+ },
371
+ "outputs": [
372
+ {
373
+ "data": {
374
+ "image/svg+xml": [
375
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
376
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
377
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
378
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
379
+ " -->\n",
380
+ "<!-- Title: %3 Pages: 1 -->\n",
381
+ "<svg width=\"581pt\" height=\"128pt\"\n",
382
+ " viewBox=\"0.00 0.00 581.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
383
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n",
384
+ "<title>%3</title>\n",
385
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 577,-124 577,4 -4,4\"/>\n",
386
+ "<!-- 132023956475904 -->\n",
387
+ "<g id=\"node1\" class=\"node\">\n",
388
+ "<title>132023956475904</title>\n",
389
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-83.5 0,-119.5 108,-119.5 108,-83.5 0,-83.5\"/>\n",
390
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
391
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-83.5 23,-119.5 \"/>\n",
392
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
393
+ "</g>\n",
394
+ "<!-- 132023956476336* -->\n",
395
+ "<g id=\"node7\" class=\"node\">\n",
396
+ "<title>132023956476336*</title>\n",
397
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
398
+ "<text text-anchor=\"middle\" x=\"171\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
399
+ "</g>\n",
400
+ "<!-- 132023956475904&#45;&gt;132023956476336* -->\n",
401
+ "<g id=\"edge4\" class=\"edge\">\n",
402
+ "<title>132023956475904&#45;&gt;132023956476336*</title>\n",
403
+ "<path fill=\"none\" stroke=\"black\" d=\"M108.12,-88.57C117.51,-86.28 127.06,-83.95 135.76,-81.84\"/>\n",
404
+ "<polygon fill=\"black\" stroke=\"black\" points=\"136.62,-85.23 145.51,-79.46 134.97,-78.43 136.62,-85.23\"/>\n",
405
+ "</g>\n",
406
+ "<!-- 132023956474896 -->\n",
407
+ "<g id=\"node2\" class=\"node\">\n",
408
+ "<title>132023956474896</title>\n",
409
+ "<polygon fill=\"none\" stroke=\"black\" points=\"234,-0.5 234,-36.5 344,-36.5 344,-0.5 234,-0.5\"/>\n",
410
+ "<text text-anchor=\"middle\" x=\"245.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n",
411
+ "<polyline fill=\"none\" stroke=\"black\" points=\"257,-0.5 257,-36.5 \"/>\n",
412
+ "<text text-anchor=\"middle\" x=\"300.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n",
413
+ "</g>\n",
414
+ "<!-- 132023956471968+ -->\n",
415
+ "<g id=\"node4\" class=\"node\">\n",
416
+ "<title>132023956471968+</title>\n",
417
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"407\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n",
418
+ "<text text-anchor=\"middle\" x=\"407\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
419
+ "</g>\n",
420
+ "<!-- 132023956474896&#45;&gt;132023956471968+ -->\n",
421
+ "<g id=\"edge5\" class=\"edge\">\n",
422
+ "<title>132023956474896&#45;&gt;132023956471968+</title>\n",
423
+ "<path fill=\"none\" stroke=\"black\" d=\"M344.26,-31.13C353.49,-33.28 362.84,-35.45 371.37,-37.44\"/>\n",
424
+ "<polygon fill=\"black\" stroke=\"black\" points=\"370.79,-40.9 381.32,-39.76 372.38,-34.08 370.79,-40.9\"/>\n",
425
+ "</g>\n",
426
+ "<!-- 132023956471968 -->\n",
427
+ "<g id=\"node3\" class=\"node\">\n",
428
+ "<title>132023956471968</title>\n",
429
+ "<polygon fill=\"none\" stroke=\"black\" points=\"470,-27.5 470,-63.5 573,-63.5 573,-27.5 470,-27.5\"/>\n",
430
+ "<text text-anchor=\"middle\" x=\"481.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n",
431
+ "<polyline fill=\"none\" stroke=\"black\" points=\"493,-27.5 493,-63.5 \"/>\n",
432
+ "<text text-anchor=\"middle\" x=\"533\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n",
433
+ "</g>\n",
434
+ "<!-- 132023956471968+&#45;&gt;132023956471968 -->\n",
435
+ "<g id=\"edge1\" class=\"edge\">\n",
436
+ "<title>132023956471968+&#45;&gt;132023956471968</title>\n",
437
+ "<path fill=\"none\" stroke=\"black\" d=\"M434.09,-45.5C441.83,-45.5 450.64,-45.5 459.61,-45.5\"/>\n",
438
+ "<polygon fill=\"black\" stroke=\"black\" points=\"459.76,-49 469.76,-45.5 459.76,-42 459.76,-49\"/>\n",
439
+ "</g>\n",
440
+ "<!-- 132023956476144 -->\n",
441
+ "<g id=\"node5\" class=\"node\">\n",
442
+ "<title>132023956476144</title>\n",
443
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-28.5 2.5,-64.5 105.5,-64.5 105.5,-28.5 2.5,-28.5\"/>\n",
444
+ "<text text-anchor=\"middle\" x=\"14\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
445
+ "<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-28.5 25.5,-64.5 \"/>\n",
446
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n",
447
+ "</g>\n",
448
+ "<!-- 132023956476144&#45;&gt;132023956476336* -->\n",
449
+ "<g id=\"edge6\" class=\"edge\">\n",
450
+ "<title>132023956476144&#45;&gt;132023956476336*</title>\n",
451
+ "<path fill=\"none\" stroke=\"black\" d=\"M105.78,-58.42C115.74,-60.76 125.98,-63.17 135.28,-65.35\"/>\n",
452
+ "<polygon fill=\"black\" stroke=\"black\" points=\"134.77,-68.82 145.3,-67.7 136.37,-62.01 134.77,-68.82\"/>\n",
453
+ "</g>\n",
454
+ "<!-- 132023956476336 -->\n",
455
+ "<g id=\"node6\" class=\"node\">\n",
456
+ "<title>132023956476336</title>\n",
457
+ "<polygon fill=\"none\" stroke=\"black\" points=\"235,-55.5 235,-91.5 343,-91.5 343,-55.5 235,-55.5\"/>\n",
458
+ "<text text-anchor=\"middle\" x=\"246.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n",
459
+ "<polyline fill=\"none\" stroke=\"black\" points=\"258,-55.5 258,-91.5 \"/>\n",
460
+ "<text text-anchor=\"middle\" x=\"300.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
461
+ "</g>\n",
462
+ "<!-- 132023956476336&#45;&gt;132023956471968+ -->\n",
463
+ "<g id=\"edge3\" class=\"edge\">\n",
464
+ "<title>132023956476336&#45;&gt;132023956471968+</title>\n",
465
+ "<path fill=\"none\" stroke=\"black\" d=\"M343.25,-60.65C352.84,-58.33 362.6,-55.98 371.48,-53.83\"/>\n",
466
+ "<polygon fill=\"black\" stroke=\"black\" points=\"372.54,-57.18 381.43,-51.43 370.89,-50.37 372.54,-57.18\"/>\n",
467
+ "</g>\n",
468
+ "<!-- 132023956476336*&#45;&gt;132023956476336 -->\n",
469
+ "<g id=\"edge2\" class=\"edge\">\n",
470
+ "<title>132023956476336*&#45;&gt;132023956476336</title>\n",
471
+ "<path fill=\"none\" stroke=\"black\" d=\"M198.03,-73.5C206.08,-73.5 215.32,-73.5 224.74,-73.5\"/>\n",
472
+ "<polygon fill=\"black\" stroke=\"black\" points=\"224.99,-77 234.99,-73.5 224.99,-70 224.99,-77\"/>\n",
473
+ "</g>\n",
474
+ "</g>\n",
475
+ "</svg>\n"
476
+ ],
477
+ "text/plain": [
478
+ "<graphviz.graphs.Digraph at 0x781339071a80>"
479
+ ]
480
+ },
481
+ "execution_count": 8,
482
+ "metadata": {},
483
+ "output_type": "execute_result"
484
+ }
485
+ ],
486
+ "source": [
487
+ "draw_dot(d)"
488
+ ]
489
+ },
490
+ {
491
+ "cell_type": "markdown",
492
+ "metadata": {
493
+ "id": "vKd4M_c10-ww"
494
+ },
495
+ "source": [
496
+ "------------------------"
497
+ ]
498
+ },
499
+ {
500
+ "cell_type": "markdown",
501
+ "metadata": {
502
+ "id": "-NTWJR0Z0_kg"
503
+ },
504
+ "source": [
505
+ "Adding another layer to this NN"
506
+ ]
507
+ },
508
+ {
509
+ "cell_type": "code",
510
+ "execution_count": 9,
511
+ "metadata": {
512
+ "colab": {
513
+ "base_uri": "https://localhost:8080/"
514
+ },
515
+ "id": "5C6tTU9A0o4d",
516
+ "outputId": "a3992bc0-43ea-45fe-fca4-676a26b18e44"
517
+ },
518
+ "outputs": [
519
+ {
520
+ "data": {
521
+ "text/plain": [
522
+ "Value(data=-8.0)"
523
+ ]
524
+ },
525
+ "execution_count": 9,
526
+ "metadata": {},
527
+ "output_type": "execute_result"
528
+ }
529
+ ],
530
+ "source": [
531
+ "a = Value(2.0, label='a')\n",
532
+ "b = Value(-3.0, label='b')\n",
533
+ "c = Value(10.0, label='c')\n",
534
+ "e = a*b; e.label='e'\n",
535
+ "d= e + c; d.label='d'\n",
536
+ "f = Value(-2.0, label='f')\n",
537
+ "L = d*f; L.label='L'\n",
538
+ "L"
539
+ ]
540
+ },
541
+ {
542
+ "cell_type": "code",
543
+ "execution_count": 10,
544
+ "metadata": {
545
+ "id": "q968N1DP1Kgp"
546
+ },
547
+ "outputs": [],
548
+ "source": [
549
+ "from graphviz import Digraph\n",
550
+ "\n",
551
+ "def trace(root):\n",
552
+ " #Builds a set of all nodes and edges in a graph\n",
553
+ " nodes, edges = set(), set()\n",
554
+ " def build(v):\n",
555
+ " if v not in nodes:\n",
556
+ " nodes.add(v)\n",
557
+ " for child in v._prev:\n",
558
+ " edges.add((child, v))\n",
559
+ " build(child)\n",
560
+ " build(root)\n",
561
+ " return nodes, edges\n",
562
+ "\n",
563
+ "def draw_dot(root):\n",
564
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
565
+ "\n",
566
+ " nodes, edges = trace(root)\n",
567
+ " for n in nodes:\n",
568
+ " uid = str(id(n))\n",
569
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
570
+ " dot.node(name = uid, label = \"{ %s | data %.4f }\" % ( n.label, n.data), shape='record')\n",
571
+ " if n._op:\n",
572
+ " #If this value is a result of some operation, then create an op node for it\n",
573
+ " dot.node(name = uid + n._op, label=n._op)\n",
574
+ " #and connect this node to it\n",
575
+ " dot.edge(uid + n._op, uid)\n",
576
+ "\n",
577
+ " for n1, n2 in edges:\n",
578
+ " #Connect n1 to the node of n2\n",
579
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
580
+ "\n",
581
+ " return dot"
582
+ ]
583
+ },
584
+ {
585
+ "cell_type": "code",
586
+ "execution_count": 11,
587
+ "metadata": {
588
+ "colab": {
589
+ "base_uri": "https://localhost:8080/",
590
+ "height": 230
591
+ },
592
+ "id": "jirvOGVt1LHZ",
593
+ "outputId": "a3dcfab6-0663-4631-8465-02acfd219e19"
594
+ },
595
+ "outputs": [
596
+ {
597
+ "data": {
598
+ "image/svg+xml": [
599
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
600
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
601
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
602
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
603
+ " -->\n",
604
+ "<!-- Title: %3 Pages: 1 -->\n",
605
+ "<svg width=\"820pt\" height=\"156pt\"\n",
606
+ " viewBox=\"0.00 0.00 820.00 156.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
607
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 152)\">\n",
608
+ "<title>%3</title>\n",
609
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-152 816,-152 816,4 -4,4\"/>\n",
610
+ "<!-- 132023956477440 -->\n",
611
+ "<g id=\"node1\" class=\"node\">\n",
612
+ "<title>132023956477440</title>\n",
613
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-111.5 2.5,-147.5 105.5,-147.5 105.5,-111.5 2.5,-111.5\"/>\n",
614
+ "<text text-anchor=\"middle\" x=\"14\" y=\"-125.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
615
+ "<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-111.5 25.5,-147.5 \"/>\n",
616
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-125.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n",
617
+ "</g>\n",
618
+ "<!-- 132023956475136* -->\n",
619
+ "<g id=\"node3\" class=\"node\">\n",
620
+ "<title>132023956475136*</title>\n",
621
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-101.5\" rx=\"27\" ry=\"18\"/>\n",
622
+ "<text text-anchor=\"middle\" x=\"171\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
623
+ "</g>\n",
624
+ "<!-- 132023956477440&#45;&gt;132023956475136* -->\n",
625
+ "<g id=\"edge8\" class=\"edge\">\n",
626
+ "<title>132023956477440&#45;&gt;132023956475136*</title>\n",
627
+ "<path fill=\"none\" stroke=\"black\" d=\"M105.78,-117.14C115.85,-114.68 126.2,-112.16 135.58,-109.88\"/>\n",
628
+ "<polygon fill=\"black\" stroke=\"black\" points=\"136.41,-113.28 145.3,-107.51 134.76,-106.48 136.41,-113.28\"/>\n",
629
+ "</g>\n",
630
+ "<!-- 132023956475136 -->\n",
631
+ "<g id=\"node2\" class=\"node\">\n",
632
+ "<title>132023956475136</title>\n",
633
+ "<polygon fill=\"none\" stroke=\"black\" points=\"235,-83.5 235,-119.5 343,-119.5 343,-83.5 235,-83.5\"/>\n",
634
+ "<text text-anchor=\"middle\" x=\"246.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n",
635
+ "<polyline fill=\"none\" stroke=\"black\" points=\"258,-83.5 258,-119.5 \"/>\n",
636
+ "<text text-anchor=\"middle\" x=\"300.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
637
+ "</g>\n",
638
+ "<!-- 132023956479264+ -->\n",
639
+ "<g id=\"node5\" class=\"node\">\n",
640
+ "<title>132023956479264+</title>\n",
641
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"407\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
642
+ "<text text-anchor=\"middle\" x=\"407\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
643
+ "</g>\n",
644
+ "<!-- 132023956475136&#45;&gt;132023956479264+ -->\n",
645
+ "<g id=\"edge6\" class=\"edge\">\n",
646
+ "<title>132023956475136&#45;&gt;132023956479264+</title>\n",
647
+ "<path fill=\"none\" stroke=\"black\" d=\"M343.25,-88.65C352.84,-86.33 362.6,-83.98 371.48,-81.83\"/>\n",
648
+ "<polygon fill=\"black\" stroke=\"black\" points=\"372.54,-85.18 381.43,-79.43 370.89,-78.37 372.54,-85.18\"/>\n",
649
+ "</g>\n",
650
+ "<!-- 132023956475136*&#45;&gt;132023956475136 -->\n",
651
+ "<g id=\"edge1\" class=\"edge\">\n",
652
+ "<title>132023956475136*&#45;&gt;132023956475136</title>\n",
653
+ "<path fill=\"none\" stroke=\"black\" d=\"M198.03,-101.5C206.08,-101.5 215.32,-101.5 224.74,-101.5\"/>\n",
654
+ "<polygon fill=\"black\" stroke=\"black\" points=\"224.99,-105 234.99,-101.5 224.99,-98 224.99,-105\"/>\n",
655
+ "</g>\n",
656
+ "<!-- 132023956479264 -->\n",
657
+ "<g id=\"node4\" class=\"node\">\n",
658
+ "<title>132023956479264</title>\n",
659
+ "<polygon fill=\"none\" stroke=\"black\" points=\"471.5,-55.5 471.5,-91.5 574.5,-91.5 574.5,-55.5 471.5,-55.5\"/>\n",
660
+ "<text text-anchor=\"middle\" x=\"483\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n",
661
+ "<polyline fill=\"none\" stroke=\"black\" points=\"494.5,-55.5 494.5,-91.5 \"/>\n",
662
+ "<text text-anchor=\"middle\" x=\"534.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n",
663
+ "</g>\n",
664
+ "<!-- 132023956481472* -->\n",
665
+ "<g id=\"node10\" class=\"node\">\n",
666
+ "<title>132023956481472*</title>\n",
667
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"639\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n",
668
+ "<text text-anchor=\"middle\" x=\"639\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
669
+ "</g>\n",
670
+ "<!-- 132023956479264&#45;&gt;132023956481472* -->\n",
671
+ "<g id=\"edge9\" class=\"edge\">\n",
672
+ "<title>132023956479264&#45;&gt;132023956481472*</title>\n",
673
+ "<path fill=\"none\" stroke=\"black\" d=\"M574.67,-61.06C584.43,-58.66 594.45,-56.2 603.55,-53.96\"/>\n",
674
+ "<polygon fill=\"black\" stroke=\"black\" points=\"604.5,-57.33 613.37,-51.55 602.83,-50.54 604.5,-57.33\"/>\n",
675
+ "</g>\n",
676
+ "<!-- 132023956479264+&#45;&gt;132023956479264 -->\n",
677
+ "<g id=\"edge2\" class=\"edge\">\n",
678
+ "<title>132023956479264+&#45;&gt;132023956479264</title>\n",
679
+ "<path fill=\"none\" stroke=\"black\" d=\"M434.15,-73.5C442.26,-73.5 451.57,-73.5 461.02,-73.5\"/>\n",
680
+ "<polygon fill=\"black\" stroke=\"black\" points=\"461.28,-77 471.28,-73.5 461.28,-70 461.28,-77\"/>\n",
681
+ "</g>\n",
682
+ "<!-- 132023956484928 -->\n",
683
+ "<g id=\"node6\" class=\"node\">\n",
684
+ "<title>132023956484928</title>\n",
685
+ "<polygon fill=\"none\" stroke=\"black\" points=\"470,-0.5 470,-36.5 576,-36.5 576,-0.5 470,-0.5\"/>\n",
686
+ "<text text-anchor=\"middle\" x=\"480.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n",
687
+ "<polyline fill=\"none\" stroke=\"black\" points=\"491,-0.5 491,-36.5 \"/>\n",
688
+ "<text text-anchor=\"middle\" x=\"533.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n",
689
+ "</g>\n",
690
+ "<!-- 132023956484928&#45;&gt;132023956481472* -->\n",
691
+ "<g id=\"edge4\" class=\"edge\">\n",
692
+ "<title>132023956484928&#45;&gt;132023956481472*</title>\n",
693
+ "<path fill=\"none\" stroke=\"black\" d=\"M576.33,-30.89C585.48,-33.06 594.8,-35.27 603.33,-37.29\"/>\n",
694
+ "<polygon fill=\"black\" stroke=\"black\" points=\"602.73,-40.74 613.27,-39.64 604.34,-33.93 602.73,-40.74\"/>\n",
695
+ "</g>\n",
696
+ "<!-- 132023956483920 -->\n",
697
+ "<g id=\"node7\" class=\"node\">\n",
698
+ "<title>132023956483920</title>\n",
699
+ "<polygon fill=\"none\" stroke=\"black\" points=\"234,-28.5 234,-64.5 344,-64.5 344,-28.5 234,-28.5\"/>\n",
700
+ "<text text-anchor=\"middle\" x=\"245.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n",
701
+ "<polyline fill=\"none\" stroke=\"black\" points=\"257,-28.5 257,-64.5 \"/>\n",
702
+ "<text text-anchor=\"middle\" x=\"300.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n",
703
+ "</g>\n",
704
+ "<!-- 132023956483920&#45;&gt;132023956479264+ -->\n",
705
+ "<g id=\"edge5\" class=\"edge\">\n",
706
+ "<title>132023956483920&#45;&gt;132023956479264+</title>\n",
707
+ "<path fill=\"none\" stroke=\"black\" d=\"M344.26,-59.13C353.49,-61.28 362.84,-63.45 371.37,-65.44\"/>\n",
708
+ "<polygon fill=\"black\" stroke=\"black\" points=\"370.79,-68.9 381.32,-67.76 372.38,-62.08 370.79,-68.9\"/>\n",
709
+ "</g>\n",
710
+ "<!-- 132023956482432 -->\n",
711
+ "<g id=\"node8\" class=\"node\">\n",
712
+ "<title>132023956482432</title>\n",
713
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-56.5 0,-92.5 108,-92.5 108,-56.5 0,-56.5\"/>\n",
714
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
715
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-56.5 23,-92.5 \"/>\n",
716
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
717
+ "</g>\n",
718
+ "<!-- 132023956482432&#45;&gt;132023956475136* -->\n",
719
+ "<g id=\"edge7\" class=\"edge\">\n",
720
+ "<title>132023956482432&#45;&gt;132023956475136*</title>\n",
721
+ "<path fill=\"none\" stroke=\"black\" d=\"M108.12,-86.97C117.33,-89.13 126.68,-91.33 135.24,-93.34\"/>\n",
722
+ "<polygon fill=\"black\" stroke=\"black\" points=\"134.68,-96.8 145.21,-95.68 136.28,-89.99 134.68,-96.8\"/>\n",
723
+ "</g>\n",
724
+ "<!-- 132023956481472 -->\n",
725
+ "<g id=\"node9\" class=\"node\">\n",
726
+ "<title>132023956481472</title>\n",
727
+ "<polygon fill=\"none\" stroke=\"black\" points=\"702,-27.5 702,-63.5 812,-63.5 812,-27.5 702,-27.5\"/>\n",
728
+ "<text text-anchor=\"middle\" x=\"714.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n",
729
+ "<polyline fill=\"none\" stroke=\"black\" points=\"727,-27.5 727,-63.5 \"/>\n",
730
+ "<text text-anchor=\"middle\" x=\"769.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n",
731
+ "</g>\n",
732
+ "<!-- 132023956481472*&#45;&gt;132023956481472 -->\n",
733
+ "<g id=\"edge3\" class=\"edge\">\n",
734
+ "<title>132023956481472*&#45;&gt;132023956481472</title>\n",
735
+ "<path fill=\"none\" stroke=\"black\" d=\"M666.03,-45.5C673.7,-45.5 682.45,-45.5 691.42,-45.5\"/>\n",
736
+ "<polygon fill=\"black\" stroke=\"black\" points=\"691.58,-49 701.58,-45.5 691.58,-42 691.58,-49\"/>\n",
737
+ "</g>\n",
738
+ "</g>\n",
739
+ "</svg>\n"
740
+ ],
741
+ "text/plain": [
742
+ "<graphviz.graphs.Digraph at 0x781339072c20>"
743
+ ]
744
+ },
745
+ "execution_count": 11,
746
+ "metadata": {},
747
+ "output_type": "execute_result"
748
+ }
749
+ ],
750
+ "source": [
751
+ "draw_dot(L)"
752
+ ]
753
+ },
754
+ {
755
+ "cell_type": "markdown",
756
+ "metadata": {
757
+ "id": "7ZGzut4D1UXS"
758
+ },
759
+ "source": [
760
+ "----------------------"
761
+ ]
762
+ }
763
+ ],
764
+ "metadata": {
765
+ "colab": {
766
+ "provenance": []
767
+ },
768
+ "kernelspec": {
769
+ "display_name": "Python 3",
770
+ "name": "python3"
771
+ },
772
+ "language_info": {
773
+ "name": "python"
774
+ }
775
+ },
776
+ "nbformat": 4,
777
+ "nbformat_minor": 0
778
+ }
4_0_manual_backpropagation_simpleExpression.ipynb ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "#### **INITIAL TEMPLATE**\n",
8
+ "**Will be useful for the remaining lectures or calculations within this section**"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "metadata": {},
14
+ "source": [
15
+ "----------------"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": null,
21
+ "metadata": {
22
+ "id": "jtRAdDVT6jf2"
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "class Value:\n",
27
+ "\n",
28
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
29
+ " self.data = data\n",
30
+ " self.grad = 0.0\n",
31
+ " self._prev = set(_children)\n",
32
+ " self._op = _op\n",
33
+ " self.label = label\n",
34
+ "\n",
35
+ "\n",
36
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
37
+ " return f\"Value(data={self.data})\"\n",
38
+ "\n",
39
+ " def __add__(self, other):\n",
40
+ " out = Value(self.data + other.data, (self, other), '+')\n",
41
+ " return out\n",
42
+ "\n",
43
+ " def __mul__(self, other):\n",
44
+ " out = Value(self.data * other.data, (self, other), '*')\n",
45
+ " return out"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": null,
51
+ "metadata": {
52
+ "colab": {
53
+ "base_uri": "https://localhost:8080/"
54
+ },
55
+ "id": "AIP2sPDm6Los",
56
+ "outputId": "11771f7b-a039-4311-b173-bf656135506b"
57
+ },
58
+ "outputs": [
59
+ {
60
+ "data": {
61
+ "text/plain": [
62
+ "Value(data=-8.0)"
63
+ ]
64
+ },
65
+ "execution_count": 3,
66
+ "metadata": {},
67
+ "output_type": "execute_result"
68
+ }
69
+ ],
70
+ "source": [
71
+ "a = Value(2.0, label='a')\n",
72
+ "b = Value(-3.0, label='b')\n",
73
+ "c = Value(10.0, label='c')\n",
74
+ "e = a*b; e.label='e'\n",
75
+ "d= e + c; d.label='d'\n",
76
+ "f = Value(-2.0, label='f')\n",
77
+ "L = d*f; L.label='L'\n",
78
+ "L"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": null,
84
+ "metadata": {
85
+ "id": "T0rN8d146jvF"
86
+ },
87
+ "outputs": [],
88
+ "source": [
89
+ "from graphviz import Digraph\n",
90
+ "\n",
91
+ "def trace(root):\n",
92
+ " #Builds a set of all nodes and edges in a graph\n",
93
+ " nodes, edges = set(), set()\n",
94
+ " def build(v):\n",
95
+ " if v not in nodes:\n",
96
+ " nodes.add(v)\n",
97
+ " for child in v._prev:\n",
98
+ " edges.add((child, v))\n",
99
+ " build(child)\n",
100
+ " build(root)\n",
101
+ " return nodes, edges\n",
102
+ "\n",
103
+ "def draw_dot(root):\n",
104
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
105
+ "\n",
106
+ " nodes, edges = trace(root)\n",
107
+ " for n in nodes:\n",
108
+ " uid = str(id(n))\n",
109
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
110
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
111
+ " if n._op:\n",
112
+ " #If this value is a result of some operation, then create an op node for it\n",
113
+ " dot.node(name = uid + n._op, label=n._op)\n",
114
+ " #and connect this node to it\n",
115
+ " dot.edge(uid + n._op, uid)\n",
116
+ "\n",
117
+ " for n1, n2 in edges:\n",
118
+ " #Connect n1 to the node of n2\n",
119
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
120
+ "\n",
121
+ " return dot"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": null,
127
+ "metadata": {
128
+ "colab": {
129
+ "base_uri": "https://localhost:8080/",
130
+ "height": 247
131
+ },
132
+ "id": "k7wjwrfo6nUl",
133
+ "outputId": "d78c4618-6574-49f9-8e80-f2faa8dad69a"
134
+ },
135
+ "outputs": [
136
+ {
137
+ "data": {
138
+ "image/svg+xml": [
139
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
140
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
141
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
142
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
143
+ " -->\n",
144
+ "<!-- Title: %3 Pages: 1 -->\n",
145
+ "<svg width=\"1148pt\" height=\"154pt\"\n",
146
+ " viewBox=\"0.00 0.00 1148.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
147
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n",
148
+ "<title>%3</title>\n",
149
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1144,-150 1144,4 -4,4\"/>\n",
150
+ "<!-- 135449920624224 -->\n",
151
+ "<g id=\"node1\" class=\"node\">\n",
152
+ "<title>135449920624224</title>\n",
153
+ "<polygon fill=\"none\" stroke=\"black\" points=\"317,-27.5 317,-63.5 507,-63.5 507,-27.5 317,-27.5\"/>\n",
154
+ "<text text-anchor=\"middle\" x=\"328.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n",
155
+ "<polyline fill=\"none\" stroke=\"black\" points=\"340,-27.5 340,-63.5 \"/>\n",
156
+ "<text text-anchor=\"middle\" x=\"382.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
157
+ "<polyline fill=\"none\" stroke=\"black\" points=\"425,-27.5 425,-63.5 \"/>\n",
158
+ "<text text-anchor=\"middle\" x=\"466\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
159
+ "</g>\n",
160
+ "<!-- 135449920632192+ -->\n",
161
+ "<g id=\"node8\" class=\"node\">\n",
162
+ "<title>135449920632192+</title>\n",
163
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n",
164
+ "<text text-anchor=\"middle\" x=\"571\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
165
+ "</g>\n",
166
+ "<!-- 135449920624224&#45;&gt;135449920632192+ -->\n",
167
+ "<g id=\"edge9\" class=\"edge\">\n",
168
+ "<title>135449920624224&#45;&gt;135449920632192+</title>\n",
169
+ "<path fill=\"none\" stroke=\"black\" d=\"M507.05,-61.67C516.78,-63.35 526.18,-64.96 534.62,-66.42\"/>\n",
170
+ "<polygon fill=\"black\" stroke=\"black\" points=\"534.3,-69.91 544.75,-68.16 535.49,-63.01 534.3,-69.91\"/>\n",
171
+ "</g>\n",
172
+ "<!-- 135449920624224* -->\n",
173
+ "<g id=\"node2\" class=\"node\">\n",
174
+ "<title>135449920624224*</title>\n",
175
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n",
176
+ "<text text-anchor=\"middle\" x=\"253\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
177
+ "</g>\n",
178
+ "<!-- 135449920624224*&#45;&gt;135449920624224 -->\n",
179
+ "<g id=\"edge1\" class=\"edge\">\n",
180
+ "<title>135449920624224*&#45;&gt;135449920624224</title>\n",
181
+ "<path fill=\"none\" stroke=\"black\" d=\"M280.28,-45.5C288.05,-45.5 297.08,-45.5 306.68,-45.5\"/>\n",
182
+ "<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-49 316.88,-45.5 306.88,-42 306.88,-49\"/>\n",
183
+ "</g>\n",
184
+ "<!-- 135449920621248 -->\n",
185
+ "<g id=\"node3\" class=\"node\">\n",
186
+ "<title>135449920621248</title>\n",
187
+ "<polygon fill=\"none\" stroke=\"black\" points=\"948,-81.5 948,-117.5 1140,-117.5 1140,-81.5 948,-81.5\"/>\n",
188
+ "<text text-anchor=\"middle\" x=\"960.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n",
189
+ "<polyline fill=\"none\" stroke=\"black\" points=\"973,-81.5 973,-117.5 \"/>\n",
190
+ "<text text-anchor=\"middle\" x=\"1015.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n",
191
+ "<polyline fill=\"none\" stroke=\"black\" points=\"1058,-81.5 1058,-117.5 \"/>\n",
192
+ "<text text-anchor=\"middle\" x=\"1099\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
193
+ "</g>\n",
194
+ "<!-- 135449920621248* -->\n",
195
+ "<g id=\"node4\" class=\"node\">\n",
196
+ "<title>135449920621248*</title>\n",
197
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"885\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n",
198
+ "<text text-anchor=\"middle\" x=\"885\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
199
+ "</g>\n",
200
+ "<!-- 135449920621248*&#45;&gt;135449920621248 -->\n",
201
+ "<g id=\"edge2\" class=\"edge\">\n",
202
+ "<title>135449920621248*&#45;&gt;135449920621248</title>\n",
203
+ "<path fill=\"none\" stroke=\"black\" d=\"M912.28,-99.5C919.78,-99.5 928.44,-99.5 937.67,-99.5\"/>\n",
204
+ "<polygon fill=\"black\" stroke=\"black\" points=\"937.87,-103 947.87,-99.5 937.87,-96 937.87,-103\"/>\n",
205
+ "</g>\n",
206
+ "<!-- 135449920632624 -->\n",
207
+ "<g id=\"node5\" class=\"node\">\n",
208
+ "<title>135449920632624</title>\n",
209
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 190,-91.5 190,-55.5 0,-55.5\"/>\n",
210
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
211
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-55.5 23,-91.5 \"/>\n",
212
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
213
+ "<polyline fill=\"none\" stroke=\"black\" points=\"108,-55.5 108,-91.5 \"/>\n",
214
+ "<text text-anchor=\"middle\" x=\"149\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
215
+ "</g>\n",
216
+ "<!-- 135449920632624&#45;&gt;135449920624224* -->\n",
217
+ "<g id=\"edge5\" class=\"edge\">\n",
218
+ "<title>135449920632624&#45;&gt;135449920624224*</title>\n",
219
+ "<path fill=\"none\" stroke=\"black\" d=\"M190.34,-56.57C199.62,-54.9 208.58,-53.29 216.66,-51.84\"/>\n",
220
+ "<polygon fill=\"black\" stroke=\"black\" points=\"217.49,-55.25 226.72,-50.04 216.26,-48.36 217.49,-55.25\"/>\n",
221
+ "</g>\n",
222
+ "<!-- 135449920619856 -->\n",
223
+ "<g id=\"node6\" class=\"node\">\n",
224
+ "<title>135449920619856</title>\n",
225
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-0.5 2.5,-36.5 187.5,-36.5 187.5,-0.5 2.5,-0.5\"/>\n",
226
+ "<text text-anchor=\"middle\" x=\"14\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
227
+ "<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-0.5 25.5,-36.5 \"/>\n",
228
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n",
229
+ "<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-0.5 105.5,-36.5 \"/>\n",
230
+ "<text text-anchor=\"middle\" x=\"146.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
231
+ "</g>\n",
232
+ "<!-- 135449920619856&#45;&gt;135449920624224* -->\n",
233
+ "<g id=\"edge8\" class=\"edge\">\n",
234
+ "<title>135449920619856&#45;&gt;135449920624224*</title>\n",
235
+ "<path fill=\"none\" stroke=\"black\" d=\"M187.65,-34.36C197.94,-36.14 207.91,-37.87 216.81,-39.41\"/>\n",
236
+ "<polygon fill=\"black\" stroke=\"black\" points=\"216.26,-42.87 226.71,-41.12 217.45,-35.97 216.26,-42.87\"/>\n",
237
+ "</g>\n",
238
+ "<!-- 135449920632192 -->\n",
239
+ "<g id=\"node7\" class=\"node\">\n",
240
+ "<title>135449920632192</title>\n",
241
+ "<polygon fill=\"none\" stroke=\"black\" points=\"635.5,-54.5 635.5,-90.5 820.5,-90.5 820.5,-54.5 635.5,-54.5\"/>\n",
242
+ "<text text-anchor=\"middle\" x=\"647\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n",
243
+ "<polyline fill=\"none\" stroke=\"black\" points=\"658.5,-54.5 658.5,-90.5 \"/>\n",
244
+ "<text text-anchor=\"middle\" x=\"698.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n",
245
+ "<polyline fill=\"none\" stroke=\"black\" points=\"738.5,-54.5 738.5,-90.5 \"/>\n",
246
+ "<text text-anchor=\"middle\" x=\"779.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
247
+ "</g>\n",
248
+ "<!-- 135449920632192&#45;&gt;135449920621248* -->\n",
249
+ "<g id=\"edge7\" class=\"edge\">\n",
250
+ "<title>135449920632192&#45;&gt;135449920621248*</title>\n",
251
+ "<path fill=\"none\" stroke=\"black\" d=\"M820.51,-88.44C830.48,-90.18 840.13,-91.86 848.77,-93.36\"/>\n",
252
+ "<polygon fill=\"black\" stroke=\"black\" points=\"848.31,-96.84 858.77,-95.1 849.52,-89.94 848.31,-96.84\"/>\n",
253
+ "</g>\n",
254
+ "<!-- 135449920632192+&#45;&gt;135449920632192 -->\n",
255
+ "<g id=\"edge3\" class=\"edge\">\n",
256
+ "<title>135449920632192+&#45;&gt;135449920632192</title>\n",
257
+ "<path fill=\"none\" stroke=\"black\" d=\"M598.29,-72.5C606.26,-72.5 615.54,-72.5 625.39,-72.5\"/>\n",
258
+ "<polygon fill=\"black\" stroke=\"black\" points=\"625.41,-76 635.41,-72.5 625.41,-69 625.41,-76\"/>\n",
259
+ "</g>\n",
260
+ "<!-- 135449920619424 -->\n",
261
+ "<g id=\"node9\" class=\"node\">\n",
262
+ "<title>135449920619424</title>\n",
263
+ "<polygon fill=\"none\" stroke=\"black\" points=\"316,-82.5 316,-118.5 508,-118.5 508,-82.5 316,-82.5\"/>\n",
264
+ "<text text-anchor=\"middle\" x=\"327.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n",
265
+ "<polyline fill=\"none\" stroke=\"black\" points=\"339,-82.5 339,-118.5 \"/>\n",
266
+ "<text text-anchor=\"middle\" x=\"382.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n",
267
+ "<polyline fill=\"none\" stroke=\"black\" points=\"426,-82.5 426,-118.5 \"/>\n",
268
+ "<text text-anchor=\"middle\" x=\"467\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
269
+ "</g>\n",
270
+ "<!-- 135449920619424&#45;&gt;135449920632192+ -->\n",
271
+ "<g id=\"edge4\" class=\"edge\">\n",
272
+ "<title>135449920619424&#45;&gt;135449920632192+</title>\n",
273
+ "<path fill=\"none\" stroke=\"black\" d=\"M508.4,-83.49C517.69,-81.83 526.64,-80.23 534.71,-78.79\"/>\n",
274
+ "<polygon fill=\"black\" stroke=\"black\" points=\"535.53,-82.2 544.76,-77 534.31,-75.31 535.53,-82.2\"/>\n",
275
+ "</g>\n",
276
+ "<!-- 135449920632288 -->\n",
277
+ "<g id=\"node10\" class=\"node\">\n",
278
+ "<title>135449920632288</title>\n",
279
+ "<polygon fill=\"none\" stroke=\"black\" points=\"634,-109.5 634,-145.5 822,-145.5 822,-109.5 634,-109.5\"/>\n",
280
+ "<text text-anchor=\"middle\" x=\"644.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n",
281
+ "<polyline fill=\"none\" stroke=\"black\" points=\"655,-109.5 655,-145.5 \"/>\n",
282
+ "<text text-anchor=\"middle\" x=\"697.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n",
283
+ "<polyline fill=\"none\" stroke=\"black\" points=\"740,-109.5 740,-145.5 \"/>\n",
284
+ "<text text-anchor=\"middle\" x=\"781\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
285
+ "</g>\n",
286
+ "<!-- 135449920632288&#45;&gt;135449920621248* -->\n",
287
+ "<g id=\"edge6\" class=\"edge\">\n",
288
+ "<title>135449920632288&#45;&gt;135449920621248*</title>\n",
289
+ "<path fill=\"none\" stroke=\"black\" d=\"M822.29,-110.65C831.57,-108.97 840.52,-107.35 848.61,-105.89\"/>\n",
290
+ "<polygon fill=\"black\" stroke=\"black\" points=\"849.46,-109.3 858.68,-104.07 848.21,-102.41 849.46,-109.3\"/>\n",
291
+ "</g>\n",
292
+ "</g>\n",
293
+ "</svg>\n"
294
+ ],
295
+ "text/plain": [
296
+ "<graphviz.graphs.Digraph at 0x7b30e4675ae0>"
297
+ ]
298
+ },
299
+ "execution_count": 7,
300
+ "metadata": {},
301
+ "output_type": "execute_result"
302
+ }
303
+ ],
304
+ "source": [
305
+ "draw_dot(L)"
306
+ ]
307
+ }
308
+ ],
309
+ "metadata": {
310
+ "colab": {
311
+ "provenance": []
312
+ },
313
+ "kernelspec": {
314
+ "display_name": "Python 3",
315
+ "name": "python3"
316
+ },
317
+ "language_info": {
318
+ "name": "python"
319
+ }
320
+ },
321
+ "nbformat": 4,
322
+ "nbformat_minor": 0
323
+ }
4_1_manual_backpropagation_simpleExpression.ipynb ADDED
@@ -0,0 +1,502 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "class Value:\n",
21
+ "\n",
22
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
23
+ " self.data = data\n",
24
+ " self.grad = 0.0\n",
25
+ " self._prev = set(_children)\n",
26
+ " self._op = _op\n",
27
+ " self.label = label\n",
28
+ "\n",
29
+ "\n",
30
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
31
+ " return f\"Value(data={self.data})\"\n",
32
+ "\n",
33
+ " def __add__(self, other):\n",
34
+ " out = Value(self.data + other.data, (self, other), '+')\n",
35
+ " return out\n",
36
+ "\n",
37
+ " def __mul__(self, other):\n",
38
+ " out = Value(self.data * other.data, (self, other), '*')\n",
39
+ " return out"
40
+ ],
41
+ "metadata": {
42
+ "id": "jtRAdDVT6jf2"
43
+ },
44
+ "execution_count": 2,
45
+ "outputs": []
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 17,
50
+ "metadata": {
51
+ "colab": {
52
+ "base_uri": "https://localhost:8080/"
53
+ },
54
+ "id": "AIP2sPDm6Los",
55
+ "outputId": "685902a5-d209-4a47-90fc-97bf59a10af2"
56
+ },
57
+ "outputs": [
58
+ {
59
+ "output_type": "execute_result",
60
+ "data": {
61
+ "text/plain": [
62
+ "Value(data=-8.0)"
63
+ ]
64
+ },
65
+ "metadata": {},
66
+ "execution_count": 17
67
+ }
68
+ ],
69
+ "source": [
70
+ "a = Value(2.0, label='a')\n",
71
+ "b = Value(-3.0, label='b')\n",
72
+ "c = Value(10.0, label='c')\n",
73
+ "e = a*b; e.label='e'\n",
74
+ "d= e + c; d.label='d'\n",
75
+ "f = Value(-2.0, label='f')\n",
76
+ "L = d*f; L.label='L'\n",
77
+ "L"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "source": [
83
+ "from graphviz import Digraph\n",
84
+ "\n",
85
+ "def trace(root):\n",
86
+ " #Builds a set of all nodes and edges in a graph\n",
87
+ " nodes, edges = set(), set()\n",
88
+ " def build(v):\n",
89
+ " if v not in nodes:\n",
90
+ " nodes.add(v)\n",
91
+ " for child in v._prev:\n",
92
+ " edges.add((child, v))\n",
93
+ " build(child)\n",
94
+ " build(root)\n",
95
+ " return nodes, edges\n",
96
+ "\n",
97
+ "def draw_dot(root):\n",
98
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
99
+ "\n",
100
+ " nodes, edges = trace(root)\n",
101
+ " for n in nodes:\n",
102
+ " uid = str(id(n))\n",
103
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
104
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
105
+ " if n._op:\n",
106
+ " #If this value is a result of some operation, then create an op node for it\n",
107
+ " dot.node(name = uid + n._op, label=n._op)\n",
108
+ " #and connect this node to it\n",
109
+ " dot.edge(uid + n._op, uid)\n",
110
+ "\n",
111
+ " for n1, n2 in edges:\n",
112
+ " #Connect n1 to the node of n2\n",
113
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
114
+ "\n",
115
+ " return dot"
116
+ ],
117
+ "metadata": {
118
+ "id": "T0rN8d146jvF"
119
+ },
120
+ "execution_count": 7,
121
+ "outputs": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "draw_dot(L)"
127
+ ],
128
+ "metadata": {
129
+ "colab": {
130
+ "base_uri": "https://localhost:8080/",
131
+ "height": 247
132
+ },
133
+ "id": "k7wjwrfo6nUl",
134
+ "outputId": "d78c4618-6574-49f9-8e80-f2faa8dad69a"
135
+ },
136
+ "execution_count": null,
137
+ "outputs": [
138
+ {
139
+ "output_type": "execute_result",
140
+ "data": {
141
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1148pt\" height=\"154pt\"\n viewBox=\"0.00 0.00 1148.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1144,-150 1144,4 -4,4\"/>\n<!-- 135449920624224 -->\n<g id=\"node1\" class=\"node\">\n<title>135449920624224</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-27.5 317,-63.5 507,-63.5 507,-27.5 317,-27.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-27.5 340,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-27.5 425,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632192+ -->\n<g id=\"node8\" class=\"node\">\n<title>135449920632192+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 135449920624224&#45;&gt;135449920632192+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>135449920624224&#45;&gt;135449920632192+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-61.67C516.78,-63.35 526.18,-64.96 534.62,-66.42\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.3,-69.91 544.75,-68.16 535.49,-63.01 534.3,-69.91\"/>\n</g>\n<!-- 135449920624224* -->\n<g id=\"node2\" class=\"node\">\n<title>135449920624224*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135449920624224*&#45;&gt;135449920624224 -->\n<g id=\"edge1\" class=\"edge\">\n<title>135449920624224*&#45;&gt;135449920624224</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-45.5C288.05,-45.5 297.08,-45.5 306.68,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-49 316.88,-45.5 306.88,-42 306.88,-49\"/>\n</g>\n<!-- 135449920621248 -->\n<g id=\"node3\" class=\"node\">\n<title>135449920621248</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"948,-81.5 948,-117.5 1140,-117.5 1140,-81.5 948,-81.5\"/>\n<text text-anchor=\"middle\" x=\"960.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"973,-81.5 973,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1015.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1058,-81.5 1058,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1099\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920621248* -->\n<g id=\"node4\" class=\"node\">\n<title>135449920621248*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"885\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"885\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135449920621248*&#45;&gt;135449920621248 -->\n<g id=\"edge2\" class=\"edge\">\n<title>135449920621248*&#45;&gt;135449920621248</title>\n<path fill=\"none\" stroke=\"black\" d=\"M912.28,-99.5C919.78,-99.5 928.44,-99.5 937.67,-99.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"937.87,-103 947.87,-99.5 937.87,-96 937.87,-103\"/>\n</g>\n<!-- 135449920632624 -->\n<g id=\"node5\" class=\"node\">\n<title>135449920632624</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 190,-91.5 190,-55.5 0,-55.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-55.5 23,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-55.5 108,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632624&#45;&gt;135449920624224* -->\n<g id=\"edge5\" class=\"edge\">\n<title>135449920632624&#45;&gt;135449920624224*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-56.57C199.62,-54.9 208.58,-53.29 216.66,-51.84\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.49,-55.25 226.72,-50.04 216.26,-48.36 217.49,-55.25\"/>\n</g>\n<!-- 135449920619856 -->\n<g id=\"node6\" class=\"node\">\n<title>135449920619856</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-0.5 2.5,-36.5 187.5,-36.5 187.5,-0.5 2.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-0.5 25.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-0.5 105.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920619856&#45;&gt;135449920624224* -->\n<g id=\"edge8\" class=\"edge\">\n<title>135449920619856&#45;&gt;135449920624224*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-34.36C197.94,-36.14 207.91,-37.87 216.81,-39.41\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.26,-42.87 226.71,-41.12 217.45,-35.97 216.26,-42.87\"/>\n</g>\n<!-- 135449920632192 -->\n<g id=\"node7\" class=\"node\">\n<title>135449920632192</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"635.5,-54.5 635.5,-90.5 820.5,-90.5 820.5,-54.5 635.5,-54.5\"/>\n<text text-anchor=\"middle\" x=\"647\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"658.5,-54.5 658.5,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"698.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"738.5,-54.5 738.5,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"779.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632192&#45;&gt;135449920621248* -->\n<g id=\"edge7\" class=\"edge\">\n<title>135449920632192&#45;&gt;135449920621248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M820.51,-88.44C830.48,-90.18 840.13,-91.86 848.77,-93.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"848.31,-96.84 858.77,-95.1 849.52,-89.94 848.31,-96.84\"/>\n</g>\n<!-- 135449920632192+&#45;&gt;135449920632192 -->\n<g id=\"edge3\" class=\"edge\">\n<title>135449920632192+&#45;&gt;135449920632192</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.29,-72.5C606.26,-72.5 615.54,-72.5 625.39,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"625.41,-76 635.41,-72.5 625.41,-69 625.41,-76\"/>\n</g>\n<!-- 135449920619424 -->\n<g id=\"node9\" class=\"node\">\n<title>135449920619424</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-82.5 316,-118.5 508,-118.5 508,-82.5 316,-82.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-82.5 339,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-82.5 426,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920619424&#45;&gt;135449920632192+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>135449920619424&#45;&gt;135449920632192+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-83.49C517.69,-81.83 526.64,-80.23 534.71,-78.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.53,-82.2 544.76,-77 534.31,-75.31 535.53,-82.2\"/>\n</g>\n<!-- 135449920632288 -->\n<g id=\"node10\" class=\"node\">\n<title>135449920632288</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-109.5 634,-145.5 822,-145.5 822,-109.5 634,-109.5\"/>\n<text text-anchor=\"middle\" x=\"644.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655,-109.5 655,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"697.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740,-109.5 740,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"781\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632288&#45;&gt;135449920621248* -->\n<g id=\"edge6\" class=\"edge\">\n<title>135449920632288&#45;&gt;135449920621248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.29,-110.65C831.57,-108.97 840.52,-107.35 848.61,-105.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.46,-109.3 858.68,-104.07 848.21,-102.41 849.46,-109.3\"/>\n</g>\n</g>\n</svg>\n",
142
+ "text/plain": [
143
+ "<graphviz.graphs.Digraph at 0x7b30e4675ae0>"
144
+ ]
145
+ },
146
+ "metadata": {},
147
+ "execution_count": 7
148
+ }
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "markdown",
153
+ "source": [
154
+ "----------------------"
155
+ ],
156
+ "metadata": {
157
+ "id": "UO6I8Z-_CaNv"
158
+ }
159
+ },
160
+ {
161
+ "cell_type": "markdown",
162
+ "source": [
163
+ "### **Now, let's start to fill those grad values**"
164
+ ],
165
+ "metadata": {
166
+ "id": "wB-SONL3CltR"
167
+ }
168
+ },
169
+ {
170
+ "cell_type": "markdown",
171
+ "source": [
172
+ "--------------"
173
+ ],
174
+ "metadata": {
175
+ "id": "EhvqPDYqF50Z"
176
+ }
177
+ },
178
+ {
179
+ "cell_type": "markdown",
180
+ "source": [
181
+ "**Let's first find the derivative of L w.r.t L**"
182
+ ],
183
+ "metadata": {
184
+ "id": "dF0QlSFJCbsI"
185
+ }
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "source": [
190
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
191
+ "def lol():\n",
192
+ "\n",
193
+ " h = 0.001\n",
194
+ "\n",
195
+ " #Here we are basically making them as local variables, to not affect the global variables on top\n",
196
+ " a = Value(2.0, label='a')\n",
197
+ " b = Value(-3.0, label='b')\n",
198
+ " c = Value(10.0, label='c')\n",
199
+ " e = a*b; e.label='e'\n",
200
+ " d= e + c; d.label='d'\n",
201
+ " f = Value(-2.0, label='f')\n",
202
+ " L = d*f; L.label='L'\n",
203
+ " L1 = L.data #L is basically a node, so we need its data\n",
204
+ "\n",
205
+ " a = Value(2.0, label='a')\n",
206
+ " b = Value(-3.0, label='b')\n",
207
+ " c = Value(10.0, label='c')\n",
208
+ " e = a*b; e.label='e'\n",
209
+ " d= e + c; d.label='d'\n",
210
+ " f = Value(-2.0, label='f')\n",
211
+ " L = d*f; L.label='L'\n",
212
+ " L2 = L.data + h\n",
213
+ "\n",
214
+ " print((L2-L1)/h)\n",
215
+ "\n",
216
+ "lol()"
217
+ ],
218
+ "metadata": {
219
+ "colab": {
220
+ "base_uri": "https://localhost:8080/"
221
+ },
222
+ "id": "AWQsdevqCUks",
223
+ "outputId": "e08b83fd-b101-4fdc-a554-41561c00a08b"
224
+ },
225
+ "execution_count": 6,
226
+ "outputs": [
227
+ {
228
+ "output_type": "stream",
229
+ "name": "stdout",
230
+ "text": [
231
+ "1.000000000000334\n"
232
+ ]
233
+ }
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "markdown",
238
+ "source": [
239
+ "This was theoritically obvious as well. The derivitive of L wrt L will be one.\n",
240
+ "\n",
241
+ "&nbsp;\n",
242
+ "\n",
243
+ "So, lets add that value manually. (Remember to run the global variables for this)"
244
+ ],
245
+ "metadata": {
246
+ "id": "HbfBzVQ4EEHM"
247
+ }
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "source": [
252
+ "L.grad = 1.0"
253
+ ],
254
+ "metadata": {
255
+ "id": "3TCgz-n6DbzI"
256
+ },
257
+ "execution_count": 10,
258
+ "outputs": []
259
+ },
260
+ {
261
+ "cell_type": "code",
262
+ "source": [
263
+ "draw_dot(L)"
264
+ ],
265
+ "metadata": {
266
+ "colab": {
267
+ "base_uri": "https://localhost:8080/",
268
+ "height": 212
269
+ },
270
+ "id": "RS6YodRTEX43",
271
+ "outputId": "3b58ed12-b486-4452-b700-8048b1e03e3b"
272
+ },
273
+ "execution_count": 11,
274
+ "outputs": [
275
+ {
276
+ "output_type": "execute_result",
277
+ "data": {
278
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1148pt\" height=\"128pt\"\n viewBox=\"0.00 0.00 1148.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 1144,-124 1144,4 -4,4\"/>\n<!-- 139719306605568 -->\n<g id=\"node1\" class=\"node\">\n<title>139719306605568</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-82.5 634,-118.5 822,-118.5 822,-82.5 634,-82.5\"/>\n<text text-anchor=\"middle\" x=\"644.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655,-82.5 655,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"697.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740,-82.5 740,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"781\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306596928* -->\n<g id=\"node3\" class=\"node\">\n<title>139719306596928*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"885\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"885\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306605568&#45;&gt;139719306596928* -->\n<g id=\"edge5\" class=\"edge\">\n<title>139719306605568&#45;&gt;139719306596928*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.29,-83.65C831.57,-81.97 840.52,-80.35 848.61,-78.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.46,-82.3 858.68,-77.07 848.21,-75.41 849.46,-82.3\"/>\n</g>\n<!-- 139719306596928 -->\n<g id=\"node2\" class=\"node\">\n<title>139719306596928</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"948,-54.5 948,-90.5 1140,-90.5 1140,-54.5 948,-54.5\"/>\n<text text-anchor=\"middle\" x=\"960.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"973,-54.5 973,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1015.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1058,-54.5 1058,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1099\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 139719306596928*&#45;&gt;139719306596928 -->\n<g id=\"edge1\" class=\"edge\">\n<title>139719306596928*&#45;&gt;139719306596928</title>\n<path fill=\"none\" stroke=\"black\" d=\"M912.28,-72.5C919.78,-72.5 928.44,-72.5 937.67,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"937.87,-76 947.87,-72.5 937.87,-69 937.87,-76\"/>\n</g>\n<!-- 139719306594912 -->\n<g id=\"node4\" class=\"node\">\n<title>139719306594912</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-55.5 317,-91.5 507,-91.5 507,-55.5 317,-55.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-55.5 340,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-55.5 425,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306603120+ -->\n<g id=\"node7\" class=\"node\">\n<title>139719306603120+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 139719306594912&#45;&gt;139719306603120+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>139719306594912&#45;&gt;139719306603120+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-56.73C516.89,-54.97 526.39,-53.28 534.9,-51.76\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.52,-55.2 544.75,-50 534.29,-48.31 535.52,-55.2\"/>\n</g>\n<!-- 139719306594912* -->\n<g id=\"node5\" class=\"node\">\n<title>139719306594912*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306594912*&#45;&gt;139719306594912 -->\n<g id=\"edge2\" class=\"edge\">\n<title>139719306594912*&#45;&gt;139719306594912</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-73.5C288.05,-73.5 297.08,-73.5 306.68,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-77 316.88,-73.5 306.88,-70 306.88,-77\"/>\n</g>\n<!-- 139719306603120 -->\n<g id=\"node6\" class=\"node\">\n<title>139719306603120</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"635.5,-27.5 635.5,-63.5 820.5,-63.5 820.5,-27.5 635.5,-27.5\"/>\n<text text-anchor=\"middle\" x=\"647\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"658.5,-27.5 658.5,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"698.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"738.5,-27.5 738.5,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"779.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306603120&#45;&gt;139719306596928* -->\n<g id=\"edge8\" class=\"edge\">\n<title>139719306603120&#45;&gt;139719306596928*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M820.51,-61.44C830.48,-63.18 840.13,-64.86 848.77,-66.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"848.31,-69.84 858.77,-68.1 849.52,-62.94 848.31,-69.84\"/>\n</g>\n<!-- 139719306603120+&#45;&gt;139719306603120 -->\n<g id=\"edge3\" class=\"edge\">\n<title>139719306603120+&#45;&gt;139719306603120</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.29,-45.5C606.26,-45.5 615.54,-45.5 625.39,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"625.41,-49 635.41,-45.5 625.41,-42 625.41,-49\"/>\n</g>\n<!-- 139719306604320 -->\n<g id=\"node8\" class=\"node\">\n<title>139719306604320</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-0.5 316,-36.5 508,-36.5 508,-0.5 316,-0.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-0.5 339,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-0.5 426,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306604320&#45;&gt;139719306603120+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>139719306604320&#45;&gt;139719306603120+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-34.91C517.69,-36.5 526.64,-38.04 534.71,-39.43\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.32,-42.91 544.76,-41.16 535.5,-36.02 534.32,-42.91\"/>\n</g>\n<!-- 139719306603840 -->\n<g id=\"node9\" class=\"node\">\n<title>139719306603840</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-83.5 2.5,-119.5 187.5,-119.5 187.5,-83.5 2.5,-83.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-83.5 25.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-83.5 105.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306603840&#45;&gt;139719306594912* -->\n<g id=\"edge4\" class=\"edge\">\n<title>139719306603840&#45;&gt;139719306594912*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-85.05C197.94,-83.2 207.91,-81.41 216.81,-79.82\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.48,-83.25 226.71,-78.04 216.25,-76.36 217.48,-83.25\"/>\n</g>\n<!-- 139719306608016 -->\n<g id=\"node10\" class=\"node\">\n<title>139719306608016</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-28.5 0,-64.5 190,-64.5 190,-28.5 0,-28.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-28.5 23,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-28.5 108,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306608016&#45;&gt;139719306594912* -->\n<g id=\"edge9\" class=\"edge\">\n<title>139719306608016&#45;&gt;139719306594912*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-62.83C199.62,-64.44 208.58,-65.99 216.66,-67.38\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.27,-70.87 226.72,-69.12 217.46,-63.97 216.27,-70.87\"/>\n</g>\n</g>\n</svg>\n",
279
+ "text/plain": [
280
+ "<graphviz.graphs.Digraph at 0x7f12efa36800>"
281
+ ]
282
+ },
283
+ "metadata": {},
284
+ "execution_count": 11
285
+ }
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "markdown",
290
+ "source": [
291
+ "-----------"
292
+ ],
293
+ "metadata": {
294
+ "id": "4x1UEdrOHalT"
295
+ }
296
+ },
297
+ {
298
+ "cell_type": "markdown",
299
+ "source": [
300
+ "**Now, we find the derivative of L wrt to f and d**"
301
+ ],
302
+ "metadata": {
303
+ "id": "Hhj8DrcUF7fI"
304
+ }
305
+ },
306
+ {
307
+ "cell_type": "markdown",
308
+ "source": [
309
+ "So, mathematically:\n",
310
+ "\n",
311
+ "dL/dd = ?\n",
312
+ "\n",
313
+ "**L = d * f**\n",
314
+ "\n",
315
+ "Therefore, dL/dd = f\n",
316
+ "\n",
317
+ "If we do manual calculation to verify, \\\n",
318
+ "\n",
319
+ "=> f(x+h) - f(x) / h \\\n",
320
+ "\n",
321
+ "(Remember the f(x) is basically L here) \\\n",
322
+ "=> (d+h)*f - d*f / h \\\n",
323
+ "=> df + hf - df / h \\\n",
324
+ "=> hf/h \\\n",
325
+ "= f\n"
326
+ ],
327
+ "metadata": {
328
+ "id": "SUi_wdLTGCsq"
329
+ }
330
+ },
331
+ {
332
+ "cell_type": "markdown",
333
+ "source": [
334
+ "So here if you see,\n",
335
+ "\n",
336
+ "The derivative of L wrt f is the value in d \\\n",
337
+ "& \\\n",
338
+ "The derivative of L wrt d is the value in f\n",
339
+ "\n",
340
+ "So, grad f is 4.0 \\\n",
341
+ "and grad d is -2.0\n",
342
+ "\n",
343
+ "&nbsp;\n",
344
+ "\n",
345
+ "Lets check this in code!"
346
+ ],
347
+ "metadata": {
348
+ "id": "8ApC2l-HHfHi"
349
+ }
350
+ },
351
+ {
352
+ "cell_type": "code",
353
+ "source": [
354
+ "# STARTING WITH d\n",
355
+ "\n",
356
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
357
+ "def lol():\n",
358
+ "\n",
359
+ " h = 0.001\n",
360
+ "\n",
361
+ " a = Value(2.0, label='a')\n",
362
+ " b = Value(-3.0, label='b')\n",
363
+ " c = Value(10.0, label='c')\n",
364
+ " e = a*b; e.label='e'\n",
365
+ " d= e + c; d.label='d'\n",
366
+ " f = Value(-2.0, label='f')\n",
367
+ " L = d*f; L.label='L'\n",
368
+ " L1 = L.data #L is basically a node, so we need its data\n",
369
+ "\n",
370
+ " a = Value(2.0, label='a')\n",
371
+ " b = Value(-3.0, label='b')\n",
372
+ " c = Value(10.0, label='c')\n",
373
+ " e = a*b; e.label='e'\n",
374
+ " d= e + c; d.label='d'\n",
375
+ " d.data = d.data + h\n",
376
+ " f = Value(-2.0, label='f')\n",
377
+ " L = d*f; L.label='L'\n",
378
+ " L2 = L.data\n",
379
+ "\n",
380
+ " print((L2-L1)/h)\n",
381
+ "\n",
382
+ "lol()"
383
+ ],
384
+ "metadata": {
385
+ "colab": {
386
+ "base_uri": "https://localhost:8080/"
387
+ },
388
+ "id": "2wx02cE6EYOR",
389
+ "outputId": "f284e1ac-4c6f-490b-c8f3-e94dfbd9923e"
390
+ },
391
+ "execution_count": 12,
392
+ "outputs": [
393
+ {
394
+ "output_type": "stream",
395
+ "name": "stdout",
396
+ "text": [
397
+ "-2.000000000000668\n"
398
+ ]
399
+ }
400
+ ]
401
+ },
402
+ {
403
+ "cell_type": "code",
404
+ "source": [
405
+ "# NOW WITH f\n",
406
+ "\n",
407
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
408
+ "def lol():\n",
409
+ "\n",
410
+ " h = 0.00001\n",
411
+ "\n",
412
+ " a = Value(2.0, label='a')\n",
413
+ " b = Value(-3.0, label='b')\n",
414
+ " c = Value(10.0, label='c')\n",
415
+ " e = a*b; e.label='e'\n",
416
+ " d= e + c; d.label='d'\n",
417
+ " f = Value(-2.0, label='f')\n",
418
+ " L = d*f; L.label='L'\n",
419
+ " L1 = L.data #L is basically a node, so we need its data\n",
420
+ "\n",
421
+ " a = Value(2.0, label='a')\n",
422
+ " b = Value(-3.0, label='b')\n",
423
+ " c = Value(10.0, label='c')\n",
424
+ " e = a*b; e.label='e'\n",
425
+ " d= e + c; d.label='d'\n",
426
+ " f = Value(-2.0 + h, label='f')\n",
427
+ " L = d*f; L.label='L'\n",
428
+ " L2 = L.data\n",
429
+ "\n",
430
+ " print((L2-L1)/h)\n",
431
+ "\n",
432
+ "lol()"
433
+ ],
434
+ "metadata": {
435
+ "colab": {
436
+ "base_uri": "https://localhost:8080/"
437
+ },
438
+ "id": "5haMwvymIRxx",
439
+ "outputId": "7b372e31-8fa4-42d3-c591-371d3c49c78d"
440
+ },
441
+ "execution_count": 15,
442
+ "outputs": [
443
+ {
444
+ "output_type": "stream",
445
+ "name": "stdout",
446
+ "text": [
447
+ "4.000000000026205\n"
448
+ ]
449
+ }
450
+ ]
451
+ },
452
+ {
453
+ "cell_type": "markdown",
454
+ "source": [
455
+ "So, now that we have verified that mathematically and on our code. Lets manually add those variables to the graph"
456
+ ],
457
+ "metadata": {
458
+ "id": "EB8w0lF0IofD"
459
+ }
460
+ },
461
+ {
462
+ "cell_type": "code",
463
+ "source": [
464
+ "f.grad = 4.0\n",
465
+ "d.grad = -2.0"
466
+ ],
467
+ "metadata": {
468
+ "id": "pS4NnAZVIML9"
469
+ },
470
+ "execution_count": 19,
471
+ "outputs": []
472
+ },
473
+ {
474
+ "cell_type": "code",
475
+ "source": [
476
+ "draw_dot(L)"
477
+ ],
478
+ "metadata": {
479
+ "colab": {
480
+ "base_uri": "https://localhost:8080/",
481
+ "height": 212
482
+ },
483
+ "id": "ko5oltNPJDtc",
484
+ "outputId": "d78137fe-0afa-449f-db42-0a079ebfffa4"
485
+ },
486
+ "execution_count": 20,
487
+ "outputs": [
488
+ {
489
+ "output_type": "execute_result",
490
+ "data": {
491
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1149pt\" height=\"128pt\"\n viewBox=\"0.00 0.00 1149.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 1145,-124 1145,4 -4,4\"/>\n<!-- 139719306599472 -->\n<g id=\"node1\" class=\"node\">\n<title>139719306599472</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-83.5 316,-119.5 508,-119.5 508,-83.5 316,-83.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-83.5 339,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-83.5 426,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306607920+ -->\n<g id=\"node6\" class=\"node\">\n<title>139719306607920+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 139719306599472&#45;&gt;139719306607920+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>139719306599472&#45;&gt;139719306607920+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-84.49C517.69,-82.83 526.64,-81.23 534.71,-79.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.53,-83.2 544.76,-78 534.31,-76.31 535.53,-83.2\"/>\n</g>\n<!-- 139719306595392 -->\n<g id=\"node2\" class=\"node\">\n<title>139719306595392</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-28.5 317,-64.5 507,-64.5 507,-28.5 317,-28.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-28.5 340,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-28.5 425,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306595392&#45;&gt;139719306607920+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>139719306595392&#45;&gt;139719306607920+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-62.67C516.78,-64.35 526.18,-65.96 534.62,-67.42\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.3,-70.91 544.75,-69.16 535.49,-64.01 534.3,-70.91\"/>\n</g>\n<!-- 139719306595392* -->\n<g id=\"node3\" class=\"node\">\n<title>139719306595392*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-46.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306595392*&#45;&gt;139719306595392 -->\n<g id=\"edge1\" class=\"edge\">\n<title>139719306595392*&#45;&gt;139719306595392</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-46.5C288.05,-46.5 297.08,-46.5 306.68,-46.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-50 316.88,-46.5 306.88,-43 306.88,-50\"/>\n</g>\n<!-- 139719306594432 -->\n<g id=\"node4\" class=\"node\">\n<title>139719306594432</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-56.5 0,-92.5 190,-92.5 190,-56.5 0,-56.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-56.5 23,-92.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-56.5 108,-92.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306594432&#45;&gt;139719306595392* -->\n<g id=\"edge9\" class=\"edge\">\n<title>139719306594432&#45;&gt;139719306595392*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-57.57C199.62,-55.9 208.58,-54.29 216.66,-52.84\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.49,-56.25 226.72,-51.04 216.26,-49.36 217.49,-56.25\"/>\n</g>\n<!-- 139719306607920 -->\n<g id=\"node5\" class=\"node\">\n<title>139719306607920</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-55.5 634,-91.5 823,-91.5 823,-55.5 634,-55.5\"/>\n<text text-anchor=\"middle\" x=\"645.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"657,-55.5 657,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"697\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"737,-55.5 737,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"780\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 139719306602976* -->\n<g id=\"node9\" class=\"node\">\n<title>139719306602976*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"886\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"886\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306607920&#45;&gt;139719306602976* -->\n<g id=\"edge5\" class=\"edge\">\n<title>139719306607920&#45;&gt;139719306602976*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M823.1,-56.65C832.4,-54.97 841.38,-53.35 849.49,-51.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"850.37,-55.29 859.59,-50.07 849.13,-48.4 850.37,-55.29\"/>\n</g>\n<!-- 139719306607920+&#45;&gt;139719306607920 -->\n<g id=\"edge2\" class=\"edge\">\n<title>139719306607920+&#45;&gt;139719306607920</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.03,-73.5C605.66,-73.5 614.52,-73.5 623.94,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"623.94,-77 633.94,-73.5 623.94,-70 623.94,-77\"/>\n</g>\n<!-- 139719306602352 -->\n<g id=\"node7\" class=\"node\">\n<title>139719306602352</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634.5,-0.5 634.5,-36.5 822.5,-36.5 822.5,-0.5 634.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"645\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655.5,-0.5 655.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"698\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740.5,-0.5 740.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"781.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 139719306602352&#45;&gt;139719306602976* -->\n<g id=\"edge7\" class=\"edge\">\n<title>139719306602352&#45;&gt;139719306602976*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.65,-34.67C832.2,-36.33 841.43,-37.93 849.72,-39.37\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.24,-42.84 859.69,-41.11 850.44,-35.95 849.24,-42.84\"/>\n</g>\n<!-- 139719306602976 -->\n<g id=\"node8\" class=\"node\">\n<title>139719306602976</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"949,-27.5 949,-63.5 1141,-63.5 1141,-27.5 949,-27.5\"/>\n<text text-anchor=\"middle\" x=\"961.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"974,-27.5 974,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"1016.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1059,-27.5 1059,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"1100\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306602976*&#45;&gt;139719306602976 -->\n<g id=\"edge3\" class=\"edge\">\n<title>139719306602976*&#45;&gt;139719306602976</title>\n<path fill=\"none\" stroke=\"black\" d=\"M913.28,-45.5C920.78,-45.5 929.44,-45.5 938.67,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"938.87,-49 948.87,-45.5 938.87,-42 938.87,-49\"/>\n</g>\n<!-- 139719306601968 -->\n<g id=\"node10\" class=\"node\">\n<title>139719306601968</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-1.5 2.5,-37.5 187.5,-37.5 187.5,-1.5 2.5,-1.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-15.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-1.5 25.5,-37.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-15.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-1.5 105.5,-37.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-15.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306601968&#45;&gt;139719306595392* -->\n<g id=\"edge8\" class=\"edge\">\n<title>139719306601968&#45;&gt;139719306595392*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-35.36C197.94,-37.14 207.91,-38.87 216.81,-40.41\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.26,-43.87 226.71,-42.12 217.45,-36.97 216.26,-43.87\"/>\n</g>\n</g>\n</svg>\n",
492
+ "text/plain": [
493
+ "<graphviz.graphs.Digraph at 0x7f12ef9a3eb0>"
494
+ ]
495
+ },
496
+ "metadata": {},
497
+ "execution_count": 20
498
+ }
499
+ ]
500
+ }
501
+ ]
502
+ }
4_2_manual_backpropagation_simpleExpression.ipynb ADDED
@@ -0,0 +1,968 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "class Value:\n",
21
+ "\n",
22
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
23
+ " self.data = data\n",
24
+ " self.grad = 0.0\n",
25
+ " self._prev = set(_children)\n",
26
+ " self._op = _op\n",
27
+ " self.label = label\n",
28
+ "\n",
29
+ "\n",
30
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
31
+ " return f\"Value(data={self.data})\"\n",
32
+ "\n",
33
+ " def __add__(self, other):\n",
34
+ " out = Value(self.data + other.data, (self, other), '+')\n",
35
+ " return out\n",
36
+ "\n",
37
+ " def __mul__(self, other):\n",
38
+ " out = Value(self.data * other.data, (self, other), '*')\n",
39
+ " return out"
40
+ ],
41
+ "metadata": {
42
+ "id": "jtRAdDVT6jf2"
43
+ },
44
+ "execution_count": 20,
45
+ "outputs": []
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 21,
50
+ "metadata": {
51
+ "colab": {
52
+ "base_uri": "https://localhost:8080/"
53
+ },
54
+ "id": "AIP2sPDm6Los",
55
+ "outputId": "8e1d5665-fc27-4ddb-95ac-a9cf53f25d51"
56
+ },
57
+ "outputs": [
58
+ {
59
+ "output_type": "execute_result",
60
+ "data": {
61
+ "text/plain": [
62
+ "Value(data=-8.0)"
63
+ ]
64
+ },
65
+ "metadata": {},
66
+ "execution_count": 21
67
+ }
68
+ ],
69
+ "source": [
70
+ "a = Value(2.0, label='a')\n",
71
+ "b = Value(-3.0, label='b')\n",
72
+ "c = Value(10.0, label='c')\n",
73
+ "e = a*b; e.label='e'\n",
74
+ "d= e + c; d.label='d'\n",
75
+ "f = Value(-2.0, label='f')\n",
76
+ "L = d*f; L.label='L'\n",
77
+ "L"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "source": [
83
+ "from graphviz import Digraph\n",
84
+ "\n",
85
+ "def trace(root):\n",
86
+ " #Builds a set of all nodes and edges in a graph\n",
87
+ " nodes, edges = set(), set()\n",
88
+ " def build(v):\n",
89
+ " if v not in nodes:\n",
90
+ " nodes.add(v)\n",
91
+ " for child in v._prev:\n",
92
+ " edges.add((child, v))\n",
93
+ " build(child)\n",
94
+ " build(root)\n",
95
+ " return nodes, edges\n",
96
+ "\n",
97
+ "def draw_dot(root):\n",
98
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
99
+ "\n",
100
+ " nodes, edges = trace(root)\n",
101
+ " for n in nodes:\n",
102
+ " uid = str(id(n))\n",
103
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
104
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
105
+ " if n._op:\n",
106
+ " #If this value is a result of some operation, then create an op node for it\n",
107
+ " dot.node(name = uid + n._op, label=n._op)\n",
108
+ " #and connect this node to it\n",
109
+ " dot.edge(uid + n._op, uid)\n",
110
+ "\n",
111
+ " for n1, n2 in edges:\n",
112
+ " #Connect n1 to the node of n2\n",
113
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
114
+ "\n",
115
+ " return dot"
116
+ ],
117
+ "metadata": {
118
+ "id": "T0rN8d146jvF"
119
+ },
120
+ "execution_count": 22,
121
+ "outputs": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "draw_dot(L)"
127
+ ],
128
+ "metadata": {
129
+ "colab": {
130
+ "base_uri": "https://localhost:8080/",
131
+ "height": 247
132
+ },
133
+ "id": "k7wjwrfo6nUl",
134
+ "outputId": "d78c4618-6574-49f9-8e80-f2faa8dad69a"
135
+ },
136
+ "execution_count": null,
137
+ "outputs": [
138
+ {
139
+ "output_type": "execute_result",
140
+ "data": {
141
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1148pt\" height=\"154pt\"\n viewBox=\"0.00 0.00 1148.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1144,-150 1144,4 -4,4\"/>\n<!-- 135449920624224 -->\n<g id=\"node1\" class=\"node\">\n<title>135449920624224</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-27.5 317,-63.5 507,-63.5 507,-27.5 317,-27.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-27.5 340,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-27.5 425,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632192+ -->\n<g id=\"node8\" class=\"node\">\n<title>135449920632192+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 135449920624224&#45;&gt;135449920632192+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>135449920624224&#45;&gt;135449920632192+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-61.67C516.78,-63.35 526.18,-64.96 534.62,-66.42\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.3,-69.91 544.75,-68.16 535.49,-63.01 534.3,-69.91\"/>\n</g>\n<!-- 135449920624224* -->\n<g id=\"node2\" class=\"node\">\n<title>135449920624224*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135449920624224*&#45;&gt;135449920624224 -->\n<g id=\"edge1\" class=\"edge\">\n<title>135449920624224*&#45;&gt;135449920624224</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-45.5C288.05,-45.5 297.08,-45.5 306.68,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-49 316.88,-45.5 306.88,-42 306.88,-49\"/>\n</g>\n<!-- 135449920621248 -->\n<g id=\"node3\" class=\"node\">\n<title>135449920621248</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"948,-81.5 948,-117.5 1140,-117.5 1140,-81.5 948,-81.5\"/>\n<text text-anchor=\"middle\" x=\"960.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"973,-81.5 973,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1015.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1058,-81.5 1058,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1099\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920621248* -->\n<g id=\"node4\" class=\"node\">\n<title>135449920621248*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"885\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"885\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135449920621248*&#45;&gt;135449920621248 -->\n<g id=\"edge2\" class=\"edge\">\n<title>135449920621248*&#45;&gt;135449920621248</title>\n<path fill=\"none\" stroke=\"black\" d=\"M912.28,-99.5C919.78,-99.5 928.44,-99.5 937.67,-99.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"937.87,-103 947.87,-99.5 937.87,-96 937.87,-103\"/>\n</g>\n<!-- 135449920632624 -->\n<g id=\"node5\" class=\"node\">\n<title>135449920632624</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 190,-91.5 190,-55.5 0,-55.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-55.5 23,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-55.5 108,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632624&#45;&gt;135449920624224* -->\n<g id=\"edge5\" class=\"edge\">\n<title>135449920632624&#45;&gt;135449920624224*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-56.57C199.62,-54.9 208.58,-53.29 216.66,-51.84\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.49,-55.25 226.72,-50.04 216.26,-48.36 217.49,-55.25\"/>\n</g>\n<!-- 135449920619856 -->\n<g id=\"node6\" class=\"node\">\n<title>135449920619856</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-0.5 2.5,-36.5 187.5,-36.5 187.5,-0.5 2.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-0.5 25.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-0.5 105.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920619856&#45;&gt;135449920624224* -->\n<g id=\"edge8\" class=\"edge\">\n<title>135449920619856&#45;&gt;135449920624224*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-34.36C197.94,-36.14 207.91,-37.87 216.81,-39.41\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.26,-42.87 226.71,-41.12 217.45,-35.97 216.26,-42.87\"/>\n</g>\n<!-- 135449920632192 -->\n<g id=\"node7\" class=\"node\">\n<title>135449920632192</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"635.5,-54.5 635.5,-90.5 820.5,-90.5 820.5,-54.5 635.5,-54.5\"/>\n<text text-anchor=\"middle\" x=\"647\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"658.5,-54.5 658.5,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"698.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"738.5,-54.5 738.5,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"779.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632192&#45;&gt;135449920621248* -->\n<g id=\"edge7\" class=\"edge\">\n<title>135449920632192&#45;&gt;135449920621248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M820.51,-88.44C830.48,-90.18 840.13,-91.86 848.77,-93.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"848.31,-96.84 858.77,-95.1 849.52,-89.94 848.31,-96.84\"/>\n</g>\n<!-- 135449920632192+&#45;&gt;135449920632192 -->\n<g id=\"edge3\" class=\"edge\">\n<title>135449920632192+&#45;&gt;135449920632192</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.29,-72.5C606.26,-72.5 615.54,-72.5 625.39,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"625.41,-76 635.41,-72.5 625.41,-69 625.41,-76\"/>\n</g>\n<!-- 135449920619424 -->\n<g id=\"node9\" class=\"node\">\n<title>135449920619424</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-82.5 316,-118.5 508,-118.5 508,-82.5 316,-82.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-82.5 339,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-82.5 426,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920619424&#45;&gt;135449920632192+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>135449920619424&#45;&gt;135449920632192+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-83.49C517.69,-81.83 526.64,-80.23 534.71,-78.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.53,-82.2 544.76,-77 534.31,-75.31 535.53,-82.2\"/>\n</g>\n<!-- 135449920632288 -->\n<g id=\"node10\" class=\"node\">\n<title>135449920632288</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-109.5 634,-145.5 822,-145.5 822,-109.5 634,-109.5\"/>\n<text text-anchor=\"middle\" x=\"644.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655,-109.5 655,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"697.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740,-109.5 740,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"781\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632288&#45;&gt;135449920621248* -->\n<g id=\"edge6\" class=\"edge\">\n<title>135449920632288&#45;&gt;135449920621248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.29,-110.65C831.57,-108.97 840.52,-107.35 848.61,-105.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.46,-109.3 858.68,-104.07 848.21,-102.41 849.46,-109.3\"/>\n</g>\n</g>\n</svg>\n",
142
+ "text/plain": [
143
+ "<graphviz.graphs.Digraph at 0x7b30e4675ae0>"
144
+ ]
145
+ },
146
+ "metadata": {},
147
+ "execution_count": 7
148
+ }
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "markdown",
153
+ "source": [
154
+ "----------------------"
155
+ ],
156
+ "metadata": {
157
+ "id": "UO6I8Z-_CaNv"
158
+ }
159
+ },
160
+ {
161
+ "cell_type": "markdown",
162
+ "source": [
163
+ "### **Now, let's start to fill those grad values**"
164
+ ],
165
+ "metadata": {
166
+ "id": "wB-SONL3CltR"
167
+ }
168
+ },
169
+ {
170
+ "cell_type": "markdown",
171
+ "source": [
172
+ "--------------"
173
+ ],
174
+ "metadata": {
175
+ "id": "EhvqPDYqF50Z"
176
+ }
177
+ },
178
+ {
179
+ "cell_type": "markdown",
180
+ "source": [
181
+ "**Let's first find the derivative of L w.r.t L**"
182
+ ],
183
+ "metadata": {
184
+ "id": "dF0QlSFJCbsI"
185
+ }
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "source": [
190
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
191
+ "def lol():\n",
192
+ "\n",
193
+ " h = 0.001\n",
194
+ "\n",
195
+ " #Here we are basically making them as local variables, to not affect the global variables on top\n",
196
+ " a = Value(2.0, label='a')\n",
197
+ " b = Value(-3.0, label='b')\n",
198
+ " c = Value(10.0, label='c')\n",
199
+ " e = a*b; e.label='e'\n",
200
+ " d= e + c; d.label='d'\n",
201
+ " f = Value(-2.0, label='f')\n",
202
+ " L = d*f; L.label='L'\n",
203
+ " L1 = L.data #L is basically a node, so we need its data\n",
204
+ "\n",
205
+ " a = Value(2.0, label='a')\n",
206
+ " b = Value(-3.0, label='b')\n",
207
+ " c = Value(10.0, label='c')\n",
208
+ " e = a*b; e.label='e'\n",
209
+ " d= e + c; d.label='d'\n",
210
+ " f = Value(-2.0, label='f')\n",
211
+ " L = d*f; L.label='L'\n",
212
+ " L2 = L.data + h\n",
213
+ "\n",
214
+ " print((L2-L1)/h)\n",
215
+ "\n",
216
+ "lol()"
217
+ ],
218
+ "metadata": {
219
+ "colab": {
220
+ "base_uri": "https://localhost:8080/"
221
+ },
222
+ "id": "AWQsdevqCUks",
223
+ "outputId": "e08b83fd-b101-4fdc-a554-41561c00a08b"
224
+ },
225
+ "execution_count": null,
226
+ "outputs": [
227
+ {
228
+ "output_type": "stream",
229
+ "name": "stdout",
230
+ "text": [
231
+ "1.000000000000334\n"
232
+ ]
233
+ }
234
+ ]
235
+ },
236
+ {
237
+ "cell_type": "markdown",
238
+ "source": [
239
+ "This was theoritically obvious as well. The derivitive of L wrt L will be one.\n",
240
+ "\n",
241
+ "&nbsp;\n",
242
+ "\n",
243
+ "So, lets add that value manually. (Remember to run the global variables for this)"
244
+ ],
245
+ "metadata": {
246
+ "id": "HbfBzVQ4EEHM"
247
+ }
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "source": [
252
+ "L.grad = 1.0"
253
+ ],
254
+ "metadata": {
255
+ "id": "3TCgz-n6DbzI"
256
+ },
257
+ "execution_count": 23,
258
+ "outputs": []
259
+ },
260
+ {
261
+ "cell_type": "code",
262
+ "source": [
263
+ "draw_dot(L)"
264
+ ],
265
+ "metadata": {
266
+ "colab": {
267
+ "base_uri": "https://localhost:8080/",
268
+ "height": 212
269
+ },
270
+ "id": "RS6YodRTEX43",
271
+ "outputId": "3b58ed12-b486-4452-b700-8048b1e03e3b"
272
+ },
273
+ "execution_count": null,
274
+ "outputs": [
275
+ {
276
+ "output_type": "execute_result",
277
+ "data": {
278
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1148pt\" height=\"128pt\"\n viewBox=\"0.00 0.00 1148.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 1144,-124 1144,4 -4,4\"/>\n<!-- 139719306605568 -->\n<g id=\"node1\" class=\"node\">\n<title>139719306605568</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-82.5 634,-118.5 822,-118.5 822,-82.5 634,-82.5\"/>\n<text text-anchor=\"middle\" x=\"644.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655,-82.5 655,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"697.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740,-82.5 740,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"781\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306596928* -->\n<g id=\"node3\" class=\"node\">\n<title>139719306596928*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"885\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"885\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306605568&#45;&gt;139719306596928* -->\n<g id=\"edge5\" class=\"edge\">\n<title>139719306605568&#45;&gt;139719306596928*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.29,-83.65C831.57,-81.97 840.52,-80.35 848.61,-78.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.46,-82.3 858.68,-77.07 848.21,-75.41 849.46,-82.3\"/>\n</g>\n<!-- 139719306596928 -->\n<g id=\"node2\" class=\"node\">\n<title>139719306596928</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"948,-54.5 948,-90.5 1140,-90.5 1140,-54.5 948,-54.5\"/>\n<text text-anchor=\"middle\" x=\"960.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"973,-54.5 973,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1015.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1058,-54.5 1058,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1099\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 139719306596928*&#45;&gt;139719306596928 -->\n<g id=\"edge1\" class=\"edge\">\n<title>139719306596928*&#45;&gt;139719306596928</title>\n<path fill=\"none\" stroke=\"black\" d=\"M912.28,-72.5C919.78,-72.5 928.44,-72.5 937.67,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"937.87,-76 947.87,-72.5 937.87,-69 937.87,-76\"/>\n</g>\n<!-- 139719306594912 -->\n<g id=\"node4\" class=\"node\">\n<title>139719306594912</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-55.5 317,-91.5 507,-91.5 507,-55.5 317,-55.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-55.5 340,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-55.5 425,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306603120+ -->\n<g id=\"node7\" class=\"node\">\n<title>139719306603120+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 139719306594912&#45;&gt;139719306603120+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>139719306594912&#45;&gt;139719306603120+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-56.73C516.89,-54.97 526.39,-53.28 534.9,-51.76\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.52,-55.2 544.75,-50 534.29,-48.31 535.52,-55.2\"/>\n</g>\n<!-- 139719306594912* -->\n<g id=\"node5\" class=\"node\">\n<title>139719306594912*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306594912*&#45;&gt;139719306594912 -->\n<g id=\"edge2\" class=\"edge\">\n<title>139719306594912*&#45;&gt;139719306594912</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-73.5C288.05,-73.5 297.08,-73.5 306.68,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-77 316.88,-73.5 306.88,-70 306.88,-77\"/>\n</g>\n<!-- 139719306603120 -->\n<g id=\"node6\" class=\"node\">\n<title>139719306603120</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"635.5,-27.5 635.5,-63.5 820.5,-63.5 820.5,-27.5 635.5,-27.5\"/>\n<text text-anchor=\"middle\" x=\"647\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"658.5,-27.5 658.5,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"698.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"738.5,-27.5 738.5,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"779.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306603120&#45;&gt;139719306596928* -->\n<g id=\"edge8\" class=\"edge\">\n<title>139719306603120&#45;&gt;139719306596928*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M820.51,-61.44C830.48,-63.18 840.13,-64.86 848.77,-66.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"848.31,-69.84 858.77,-68.1 849.52,-62.94 848.31,-69.84\"/>\n</g>\n<!-- 139719306603120+&#45;&gt;139719306603120 -->\n<g id=\"edge3\" class=\"edge\">\n<title>139719306603120+&#45;&gt;139719306603120</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.29,-45.5C606.26,-45.5 615.54,-45.5 625.39,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"625.41,-49 635.41,-45.5 625.41,-42 625.41,-49\"/>\n</g>\n<!-- 139719306604320 -->\n<g id=\"node8\" class=\"node\">\n<title>139719306604320</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-0.5 316,-36.5 508,-36.5 508,-0.5 316,-0.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-0.5 339,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-0.5 426,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306604320&#45;&gt;139719306603120+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>139719306604320&#45;&gt;139719306603120+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-34.91C517.69,-36.5 526.64,-38.04 534.71,-39.43\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.32,-42.91 544.76,-41.16 535.5,-36.02 534.32,-42.91\"/>\n</g>\n<!-- 139719306603840 -->\n<g id=\"node9\" class=\"node\">\n<title>139719306603840</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-83.5 2.5,-119.5 187.5,-119.5 187.5,-83.5 2.5,-83.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-83.5 25.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-83.5 105.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306603840&#45;&gt;139719306594912* -->\n<g id=\"edge4\" class=\"edge\">\n<title>139719306603840&#45;&gt;139719306594912*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-85.05C197.94,-83.2 207.91,-81.41 216.81,-79.82\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.48,-83.25 226.71,-78.04 216.25,-76.36 217.48,-83.25\"/>\n</g>\n<!-- 139719306608016 -->\n<g id=\"node10\" class=\"node\">\n<title>139719306608016</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-28.5 0,-64.5 190,-64.5 190,-28.5 0,-28.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-28.5 23,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-28.5 108,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306608016&#45;&gt;139719306594912* -->\n<g id=\"edge9\" class=\"edge\">\n<title>139719306608016&#45;&gt;139719306594912*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-62.83C199.62,-64.44 208.58,-65.99 216.66,-67.38\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.27,-70.87 226.72,-69.12 217.46,-63.97 216.27,-70.87\"/>\n</g>\n</g>\n</svg>\n",
279
+ "text/plain": [
280
+ "<graphviz.graphs.Digraph at 0x7f12efa36800>"
281
+ ]
282
+ },
283
+ "metadata": {},
284
+ "execution_count": 11
285
+ }
286
+ ]
287
+ },
288
+ {
289
+ "cell_type": "markdown",
290
+ "source": [
291
+ "-----------"
292
+ ],
293
+ "metadata": {
294
+ "id": "4x1UEdrOHalT"
295
+ }
296
+ },
297
+ {
298
+ "cell_type": "markdown",
299
+ "source": [
300
+ "**Now, we find the derivative of L wrt to f and d**"
301
+ ],
302
+ "metadata": {
303
+ "id": "Hhj8DrcUF7fI"
304
+ }
305
+ },
306
+ {
307
+ "cell_type": "markdown",
308
+ "source": [
309
+ "So, mathematically:\n",
310
+ "\n",
311
+ "dL/dd = ?\n",
312
+ "\n",
313
+ "**L = d * f**\n",
314
+ "\n",
315
+ "Therefore, dL/dd = f\n",
316
+ "\n",
317
+ "If we do manual calculation to verify, \\\n",
318
+ "\n",
319
+ "=> f(x+h) - f(x) / h \\\n",
320
+ "\n",
321
+ "(Remember the f(x) is basically L here) \\\n",
322
+ "=> (d+h)*f - d*f / h \\\n",
323
+ "=> df + hf - df / h \\\n",
324
+ "=> hf/h \\\n",
325
+ "= f\n"
326
+ ],
327
+ "metadata": {
328
+ "id": "SUi_wdLTGCsq"
329
+ }
330
+ },
331
+ {
332
+ "cell_type": "markdown",
333
+ "source": [
334
+ "So here if you see,\n",
335
+ "\n",
336
+ "The derivative of L wrt f is the value in d \\\n",
337
+ "& \\\n",
338
+ "The derivative of L wrt d is the value in f\n",
339
+ "\n",
340
+ "So, grad f is 4.0 \\\n",
341
+ "and grad d is -2.0\n",
342
+ "\n",
343
+ "&nbsp;\n",
344
+ "\n",
345
+ "Lets check this in code!"
346
+ ],
347
+ "metadata": {
348
+ "id": "8ApC2l-HHfHi"
349
+ }
350
+ },
351
+ {
352
+ "cell_type": "code",
353
+ "source": [
354
+ "# STARTING WITH d\n",
355
+ "\n",
356
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
357
+ "def lol():\n",
358
+ "\n",
359
+ " h = 0.001\n",
360
+ "\n",
361
+ " a = Value(2.0, label='a')\n",
362
+ " b = Value(-3.0, label='b')\n",
363
+ " c = Value(10.0, label='c')\n",
364
+ " e = a*b; e.label='e'\n",
365
+ " d= e + c; d.label='d'\n",
366
+ " f = Value(-2.0, label='f')\n",
367
+ " L = d*f; L.label='L'\n",
368
+ " L1 = L.data #L is basically a node, so we need its data\n",
369
+ "\n",
370
+ " a = Value(2.0, label='a')\n",
371
+ " b = Value(-3.0, label='b')\n",
372
+ " c = Value(10.0, label='c')\n",
373
+ " e = a*b; e.label='e'\n",
374
+ " d= e + c; d.label='d'\n",
375
+ " d.data = d.data + h\n",
376
+ " f = Value(-2.0, label='f')\n",
377
+ " L = d*f; L.label='L'\n",
378
+ " L2 = L.data\n",
379
+ "\n",
380
+ " print((L2-L1)/h)\n",
381
+ "\n",
382
+ "lol()"
383
+ ],
384
+ "metadata": {
385
+ "colab": {
386
+ "base_uri": "https://localhost:8080/"
387
+ },
388
+ "id": "2wx02cE6EYOR",
389
+ "outputId": "f284e1ac-4c6f-490b-c8f3-e94dfbd9923e"
390
+ },
391
+ "execution_count": null,
392
+ "outputs": [
393
+ {
394
+ "output_type": "stream",
395
+ "name": "stdout",
396
+ "text": [
397
+ "-2.000000000000668\n"
398
+ ]
399
+ }
400
+ ]
401
+ },
402
+ {
403
+ "cell_type": "code",
404
+ "source": [
405
+ "# NOW WITH f\n",
406
+ "\n",
407
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
408
+ "def lol():\n",
409
+ "\n",
410
+ " h = 0.00001\n",
411
+ "\n",
412
+ " a = Value(2.0, label='a')\n",
413
+ " b = Value(-3.0, label='b')\n",
414
+ " c = Value(10.0, label='c')\n",
415
+ " e = a*b; e.label='e'\n",
416
+ " d= e + c; d.label='d'\n",
417
+ " f = Value(-2.0, label='f')\n",
418
+ " L = d*f; L.label='L'\n",
419
+ " L1 = L.data #L is basically a node, so we need its data\n",
420
+ "\n",
421
+ " a = Value(2.0, label='a')\n",
422
+ " b = Value(-3.0, label='b')\n",
423
+ " c = Value(10.0, label='c')\n",
424
+ " e = a*b; e.label='e'\n",
425
+ " d= e + c; d.label='d'\n",
426
+ " f = Value(-2.0 + h, label='f')\n",
427
+ " L = d*f; L.label='L'\n",
428
+ " L2 = L.data\n",
429
+ "\n",
430
+ " print((L2-L1)/h)\n",
431
+ "\n",
432
+ "lol()"
433
+ ],
434
+ "metadata": {
435
+ "colab": {
436
+ "base_uri": "https://localhost:8080/"
437
+ },
438
+ "id": "5haMwvymIRxx",
439
+ "outputId": "7b372e31-8fa4-42d3-c591-371d3c49c78d"
440
+ },
441
+ "execution_count": null,
442
+ "outputs": [
443
+ {
444
+ "output_type": "stream",
445
+ "name": "stdout",
446
+ "text": [
447
+ "4.000000000026205\n"
448
+ ]
449
+ }
450
+ ]
451
+ },
452
+ {
453
+ "cell_type": "markdown",
454
+ "source": [
455
+ "So, now that we have verified that mathematically and on our code. Lets manually add those variables to the graph"
456
+ ],
457
+ "metadata": {
458
+ "id": "EB8w0lF0IofD"
459
+ }
460
+ },
461
+ {
462
+ "cell_type": "code",
463
+ "source": [
464
+ "f.grad = 4.0\n",
465
+ "d.grad = -2.0"
466
+ ],
467
+ "metadata": {
468
+ "id": "pS4NnAZVIML9"
469
+ },
470
+ "execution_count": 24,
471
+ "outputs": []
472
+ },
473
+ {
474
+ "cell_type": "code",
475
+ "source": [
476
+ "draw_dot(L)"
477
+ ],
478
+ "metadata": {
479
+ "colab": {
480
+ "base_uri": "https://localhost:8080/",
481
+ "height": 212
482
+ },
483
+ "id": "ko5oltNPJDtc",
484
+ "outputId": "d78137fe-0afa-449f-db42-0a079ebfffa4"
485
+ },
486
+ "execution_count": null,
487
+ "outputs": [
488
+ {
489
+ "output_type": "execute_result",
490
+ "data": {
491
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1149pt\" height=\"128pt\"\n viewBox=\"0.00 0.00 1149.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 1145,-124 1145,4 -4,4\"/>\n<!-- 139719306599472 -->\n<g id=\"node1\" class=\"node\">\n<title>139719306599472</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-83.5 316,-119.5 508,-119.5 508,-83.5 316,-83.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-83.5 339,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-83.5 426,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306607920+ -->\n<g id=\"node6\" class=\"node\">\n<title>139719306607920+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 139719306599472&#45;&gt;139719306607920+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>139719306599472&#45;&gt;139719306607920+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-84.49C517.69,-82.83 526.64,-81.23 534.71,-79.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.53,-83.2 544.76,-78 534.31,-76.31 535.53,-83.2\"/>\n</g>\n<!-- 139719306595392 -->\n<g id=\"node2\" class=\"node\">\n<title>139719306595392</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-28.5 317,-64.5 507,-64.5 507,-28.5 317,-28.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-28.5 340,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-28.5 425,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306595392&#45;&gt;139719306607920+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>139719306595392&#45;&gt;139719306607920+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-62.67C516.78,-64.35 526.18,-65.96 534.62,-67.42\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.3,-70.91 544.75,-69.16 535.49,-64.01 534.3,-70.91\"/>\n</g>\n<!-- 139719306595392* -->\n<g id=\"node3\" class=\"node\">\n<title>139719306595392*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-46.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306595392*&#45;&gt;139719306595392 -->\n<g id=\"edge1\" class=\"edge\">\n<title>139719306595392*&#45;&gt;139719306595392</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-46.5C288.05,-46.5 297.08,-46.5 306.68,-46.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-50 316.88,-46.5 306.88,-43 306.88,-50\"/>\n</g>\n<!-- 139719306594432 -->\n<g id=\"node4\" class=\"node\">\n<title>139719306594432</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-56.5 0,-92.5 190,-92.5 190,-56.5 0,-56.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-56.5 23,-92.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-56.5 108,-92.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306594432&#45;&gt;139719306595392* -->\n<g id=\"edge9\" class=\"edge\">\n<title>139719306594432&#45;&gt;139719306595392*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-57.57C199.62,-55.9 208.58,-54.29 216.66,-52.84\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.49,-56.25 226.72,-51.04 216.26,-49.36 217.49,-56.25\"/>\n</g>\n<!-- 139719306607920 -->\n<g id=\"node5\" class=\"node\">\n<title>139719306607920</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-55.5 634,-91.5 823,-91.5 823,-55.5 634,-55.5\"/>\n<text text-anchor=\"middle\" x=\"645.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"657,-55.5 657,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"697\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"737,-55.5 737,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"780\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 139719306602976* -->\n<g id=\"node9\" class=\"node\">\n<title>139719306602976*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"886\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"886\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 139719306607920&#45;&gt;139719306602976* -->\n<g id=\"edge5\" class=\"edge\">\n<title>139719306607920&#45;&gt;139719306602976*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M823.1,-56.65C832.4,-54.97 841.38,-53.35 849.49,-51.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"850.37,-55.29 859.59,-50.07 849.13,-48.4 850.37,-55.29\"/>\n</g>\n<!-- 139719306607920+&#45;&gt;139719306607920 -->\n<g id=\"edge2\" class=\"edge\">\n<title>139719306607920+&#45;&gt;139719306607920</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.03,-73.5C605.66,-73.5 614.52,-73.5 623.94,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"623.94,-77 633.94,-73.5 623.94,-70 623.94,-77\"/>\n</g>\n<!-- 139719306602352 -->\n<g id=\"node7\" class=\"node\">\n<title>139719306602352</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634.5,-0.5 634.5,-36.5 822.5,-36.5 822.5,-0.5 634.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"645\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655.5,-0.5 655.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"698\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740.5,-0.5 740.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"781.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 139719306602352&#45;&gt;139719306602976* -->\n<g id=\"edge7\" class=\"edge\">\n<title>139719306602352&#45;&gt;139719306602976*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.65,-34.67C832.2,-36.33 841.43,-37.93 849.72,-39.37\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.24,-42.84 859.69,-41.11 850.44,-35.95 849.24,-42.84\"/>\n</g>\n<!-- 139719306602976 -->\n<g id=\"node8\" class=\"node\">\n<title>139719306602976</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"949,-27.5 949,-63.5 1141,-63.5 1141,-27.5 949,-27.5\"/>\n<text text-anchor=\"middle\" x=\"961.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"974,-27.5 974,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"1016.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1059,-27.5 1059,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"1100\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306602976*&#45;&gt;139719306602976 -->\n<g id=\"edge3\" class=\"edge\">\n<title>139719306602976*&#45;&gt;139719306602976</title>\n<path fill=\"none\" stroke=\"black\" d=\"M913.28,-45.5C920.78,-45.5 929.44,-45.5 938.67,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"938.87,-49 948.87,-45.5 938.87,-42 938.87,-49\"/>\n</g>\n<!-- 139719306601968 -->\n<g id=\"node10\" class=\"node\">\n<title>139719306601968</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-1.5 2.5,-37.5 187.5,-37.5 187.5,-1.5 2.5,-1.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-15.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-1.5 25.5,-37.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-15.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-1.5 105.5,-37.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-15.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 139719306601968&#45;&gt;139719306595392* -->\n<g id=\"edge8\" class=\"edge\">\n<title>139719306601968&#45;&gt;139719306595392*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-35.36C197.94,-37.14 207.91,-38.87 216.81,-40.41\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.26,-43.87 226.71,-42.12 217.45,-36.97 216.26,-43.87\"/>\n</g>\n</g>\n</svg>\n",
492
+ "text/plain": [
493
+ "<graphviz.graphs.Digraph at 0x7f12ef9a3eb0>"
494
+ ]
495
+ },
496
+ "metadata": {},
497
+ "execution_count": 20
498
+ }
499
+ ]
500
+ },
501
+ {
502
+ "cell_type": "markdown",
503
+ "source": [
504
+ "----------------------------"
505
+ ],
506
+ "metadata": {
507
+ "id": "39nOWCuEskoQ"
508
+ }
509
+ },
510
+ {
511
+ "cell_type": "markdown",
512
+ "source": [
513
+ "### **VERY IMPORTANT PART**"
514
+ ],
515
+ "metadata": {
516
+ "id": "UKflj5yEsl6i"
517
+ }
518
+ },
519
+ {
520
+ "cell_type": "markdown",
521
+ "source": [
522
+ "Now we'll be calculating the derivatives of the middle nodes"
523
+ ],
524
+ "metadata": {
525
+ "id": "AM3bFT0Eswmz"
526
+ }
527
+ },
528
+ {
529
+ "cell_type": "markdown",
530
+ "source": [
531
+ "##### **Starting with c & e**\n",
532
+ "\n",
533
+ "dL/dd had already been calculated (Check end of 4_1-manual-backpropagation notebook)\n",
534
+ "\n",
535
+ "d = c + e\n",
536
+ "\n",
537
+ "now,\n",
538
+ "Derivative of d wrt c, will be 1 \\\n",
539
+ "Derivative of d wrt e, will be 1 \\\n",
540
+ "\n",
541
+ "&nbsp;\n",
542
+ "\n",
543
+ "Because the derivative of '+' operation variables will lead to 1 (Calculus basics, it leads to constant, so 1)\n",
544
+ "\n",
545
+ "&nbsp;\n",
546
+ "\n",
547
+ "If we try to prove this mathematically:\n",
548
+ "\n",
549
+ "&nbsp;\n",
550
+ "\n",
551
+ "\td = c + e\n",
552
+ "\tf(x+h) - f(x) / h\n",
553
+ "\tNow, we'll calculate wrt c\n",
554
+ "\t=> ( ((c+h)+e) - (c+e) ) / h\n",
555
+ "\t=> c + h + e - c - e / h\n",
556
+ "\t=> h / h\n",
557
+ "\t=> 1\n",
558
+ "\tTherefore, dd/dc = 1\n",
559
+ "\n",
560
+ "&nbsp;\n",
561
+ "\n",
562
+ "Therefore, we can just substitute the value respectively.\n",
563
+ "\n",
564
+ "For node c:\n",
565
+ "\tdL/dc = dL/dd . dd/dc \\\n",
566
+ " So here, the values should be -> dL/dc = -2.0 * 1 = -2.0\n",
567
+ "\n",
568
+ "For node e:\n",
569
+ "\tdL/de = dL/dd . dd/de \\\n",
570
+ " So here, the values should be -> dL/de = -2.0 * 1 = -2.0"
571
+ ],
572
+ "metadata": {
573
+ "id": "Fu0K97wfs4m9"
574
+ }
575
+ },
576
+ {
577
+ "cell_type": "code",
578
+ "source": [
579
+ "# NOW WITH c\n",
580
+ "\n",
581
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
582
+ "def lol():\n",
583
+ "\n",
584
+ " h = 0.00001\n",
585
+ "\n",
586
+ " a = Value(2.0, label='a')\n",
587
+ " b = Value(-3.0, label='b')\n",
588
+ " c = Value(10.0, label='c')\n",
589
+ " e = a*b; e.label='e'\n",
590
+ " d= e + c; d.label='d'\n",
591
+ " f = Value(-2.0, label='f')\n",
592
+ " L = d*f; L.label='L'\n",
593
+ " L1 = L.data #L is basically a node, so we need its data\n",
594
+ "\n",
595
+ " a = Value(2.0, label='a')\n",
596
+ " b = Value(-3.0, label='b')\n",
597
+ " c = Value(10.0 + h, label='c')\n",
598
+ " e = a*b; e.label='e'\n",
599
+ " d= e + c; d.label='d'\n",
600
+ " f = Value(-2.0, label='f')\n",
601
+ " L = d*f; L.label='L'\n",
602
+ " L2 = L.data\n",
603
+ "\n",
604
+ " print((L2-L1)/h)\n",
605
+ "\n",
606
+ "lol()"
607
+ ],
608
+ "metadata": {
609
+ "colab": {
610
+ "base_uri": "https://localhost:8080/"
611
+ },
612
+ "id": "m_zBXwc6sgGA",
613
+ "outputId": "70b0d9cd-59a3-48eb-8a24-c69aeaf776de"
614
+ },
615
+ "execution_count": null,
616
+ "outputs": [
617
+ {
618
+ "output_type": "stream",
619
+ "name": "stdout",
620
+ "text": [
621
+ "-1.999999987845058\n"
622
+ ]
623
+ }
624
+ ]
625
+ },
626
+ {
627
+ "cell_type": "code",
628
+ "source": [
629
+ "# NOW WITH e\n",
630
+ "\n",
631
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
632
+ "def lol():\n",
633
+ "\n",
634
+ " h = 0.00001\n",
635
+ "\n",
636
+ " a = Value(2.0, label='a')\n",
637
+ " b = Value(-3.0, label='b')\n",
638
+ " c = Value(10.0, label='c')\n",
639
+ " e = a*b; e.label='e'\n",
640
+ " d= e + c; d.label='d'\n",
641
+ " f = Value(-2.0, label='f')\n",
642
+ " L = d*f; L.label='L'\n",
643
+ " L1 = L.data #L is basically a node, so we need its data\n",
644
+ "\n",
645
+ " a = Value(2.0, label='a')\n",
646
+ " b = Value(-3.0, label='b')\n",
647
+ " c = Value(10.0, label='c')\n",
648
+ " e = a*b; e.label='e'\n",
649
+ " e.data += h\n",
650
+ " d= e + c; d.label='d'\n",
651
+ " f = Value(-2.0, label='f')\n",
652
+ " L = d*f; L.label='L'\n",
653
+ " L2 = L.data\n",
654
+ "\n",
655
+ " print((L2-L1)/h)\n",
656
+ "\n",
657
+ "lol()"
658
+ ],
659
+ "metadata": {
660
+ "colab": {
661
+ "base_uri": "https://localhost:8080/"
662
+ },
663
+ "id": "R-EE419ftb_x",
664
+ "outputId": "26c102c8-5214-4f4c-f566-c0df6d13fb15"
665
+ },
666
+ "execution_count": null,
667
+ "outputs": [
668
+ {
669
+ "output_type": "stream",
670
+ "name": "stdout",
671
+ "text": [
672
+ "-1.9999999999242843\n"
673
+ ]
674
+ }
675
+ ]
676
+ },
677
+ {
678
+ "cell_type": "code",
679
+ "source": [
680
+ "# Therefore, we now add those values manually\n",
681
+ "c.grad = -2.0\n",
682
+ "e.grad = -2.0"
683
+ ],
684
+ "metadata": {
685
+ "id": "v6VKbkQAtw7H"
686
+ },
687
+ "execution_count": 25,
688
+ "outputs": []
689
+ },
690
+ {
691
+ "cell_type": "code",
692
+ "source": [
693
+ "draw_dot(L)"
694
+ ],
695
+ "metadata": {
696
+ "colab": {
697
+ "base_uri": "https://localhost:8080/",
698
+ "height": 247
699
+ },
700
+ "id": "QubwLaK6t6fb",
701
+ "outputId": "dbe3f5cf-1576-495a-9e24-9e16c9454d72"
702
+ },
703
+ "execution_count": null,
704
+ "outputs": [
705
+ {
706
+ "output_type": "execute_result",
707
+ "data": {
708
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1153pt\" height=\"154pt\"\n viewBox=\"0.00 0.00 1153.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1149,-150 1149,4 -4,4\"/>\n<!-- 135832571291264 -->\n<g id=\"node1\" class=\"node\">\n<title>135832571291264</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-27.5 317,-63.5 511,-63.5 511,-27.5 317,-27.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-27.5 340,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-27.5 425,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"468\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831741533536+ -->\n<g id=\"node10\" class=\"node\">\n<title>135831741533536+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"575\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"575\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 135832571291264&#45;&gt;135831741533536+ -->\n<g id=\"edge5\" class=\"edge\">\n<title>135832571291264&#45;&gt;135831741533536+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M511.16,-61.83C520.82,-63.47 530.13,-65.05 538.49,-66.47\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"538.09,-69.95 548.54,-68.18 539.26,-63.05 538.09,-69.95\"/>\n</g>\n<!-- 135832571291264* -->\n<g id=\"node2\" class=\"node\">\n<title>135832571291264*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135832571291264*&#45;&gt;135832571291264 -->\n<g id=\"edge1\" class=\"edge\">\n<title>135832571291264*&#45;&gt;135832571291264</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.26,-45.5C288.05,-45.5 297.1,-45.5 306.75,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.99,-49 316.99,-45.5 306.99,-42 306.99,-49\"/>\n</g>\n<!-- 135832571294864 -->\n<g id=\"node3\" class=\"node\">\n<title>135832571294864</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"953,-81.5 953,-117.5 1145,-117.5 1145,-81.5 953,-81.5\"/>\n<text text-anchor=\"middle\" x=\"965.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"978,-81.5 978,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1020.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1063,-81.5 1063,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1104\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135832571294864* -->\n<g id=\"node4\" class=\"node\">\n<title>135832571294864*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"890\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"890\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135832571294864*&#45;&gt;135832571294864 -->\n<g id=\"edge2\" class=\"edge\">\n<title>135832571294864*&#45;&gt;135832571294864</title>\n<path fill=\"none\" stroke=\"black\" d=\"M917.28,-99.5C924.78,-99.5 933.44,-99.5 942.67,-99.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"942.87,-103 952.87,-99.5 942.87,-96 942.87,-103\"/>\n</g>\n<!-- 135832571297456 -->\n<g id=\"node5\" class=\"node\">\n<title>135832571297456</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"638.5,-109.5 638.5,-145.5 826.5,-145.5 826.5,-109.5 638.5,-109.5\"/>\n<text text-anchor=\"middle\" x=\"649\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"659.5,-109.5 659.5,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"702\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"744.5,-109.5 744.5,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"785.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 135832571297456&#45;&gt;135832571294864* -->\n<g id=\"edge9\" class=\"edge\">\n<title>135832571297456&#45;&gt;135832571294864*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M826.65,-110.73C836.2,-109.01 845.43,-107.35 853.72,-105.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"854.47,-109.27 863.69,-104.06 853.23,-102.39 854.47,-109.27\"/>\n</g>\n<!-- 135832571295440 -->\n<g id=\"node6\" class=\"node\">\n<title>135832571295440</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-55.5 2.5,-91.5 187.5,-91.5 187.5,-55.5 2.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-55.5 25.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-55.5 105.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135832571295440&#45;&gt;135832571291264* -->\n<g id=\"edge6\" class=\"edge\">\n<title>135832571295440&#45;&gt;135832571291264*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-57.05C197.94,-55.2 207.91,-53.41 216.81,-51.82\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.48,-55.25 226.71,-50.04 216.25,-48.36 217.48,-55.25\"/>\n</g>\n<!-- 135832571297504 -->\n<g id=\"node7\" class=\"node\">\n<title>135832571297504</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 190,-36.5 190,-0.5 0,-0.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-0.5 23,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-0.5 108,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135832571297504&#45;&gt;135832571291264* -->\n<g id=\"edge4\" class=\"edge\">\n<title>135832571297504&#45;&gt;135832571291264*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-34.83C199.62,-36.44 208.58,-37.99 216.66,-39.38\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.27,-42.87 226.72,-41.12 217.46,-35.97 216.27,-42.87\"/>\n</g>\n<!-- 135832571288384 -->\n<g id=\"node8\" class=\"node\">\n<title>135832571288384</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-82.5 316,-118.5 512,-118.5 512,-82.5 316,-82.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-82.5 339,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-82.5 426,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"469\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135832571288384&#45;&gt;135831741533536+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>135832571288384&#45;&gt;135831741533536+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M512.07,-83.41C521.42,-81.76 530.43,-80.17 538.55,-78.74\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"539.41,-82.15 548.65,-76.96 538.2,-75.25 539.41,-82.15\"/>\n</g>\n<!-- 135831741533536 -->\n<g id=\"node9\" class=\"node\">\n<title>135831741533536</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"638,-54.5 638,-90.5 827,-90.5 827,-54.5 638,-54.5\"/>\n<text text-anchor=\"middle\" x=\"649.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"661,-54.5 661,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"701\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"741,-54.5 741,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"784\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831741533536&#45;&gt;135832571294864* -->\n<g id=\"edge8\" class=\"edge\">\n<title>135831741533536&#45;&gt;135832571294864*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M827.1,-88.75C836.4,-90.37 845.38,-91.93 853.49,-93.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"853.14,-96.83 863.59,-95.09 854.34,-89.93 853.14,-96.83\"/>\n</g>\n<!-- 135831741533536+&#45;&gt;135831741533536 -->\n<g id=\"edge3\" class=\"edge\">\n<title>135831741533536+&#45;&gt;135831741533536</title>\n<path fill=\"none\" stroke=\"black\" d=\"M602.03,-72.5C609.66,-72.5 618.52,-72.5 627.94,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"627.94,-76 637.94,-72.5 627.94,-69 627.94,-76\"/>\n</g>\n</g>\n</svg>\n",
709
+ "text/plain": [
710
+ "<graphviz.graphs.Digraph at 0x7b89fc291600>"
711
+ ]
712
+ },
713
+ "metadata": {},
714
+ "execution_count": 15
715
+ }
716
+ ]
717
+ },
718
+ {
719
+ "cell_type": "markdown",
720
+ "source": [
721
+ "--------------"
722
+ ],
723
+ "metadata": {
724
+ "id": "m8wvNP7tuH5_"
725
+ }
726
+ },
727
+ {
728
+ "cell_type": "markdown",
729
+ "source": [
730
+ "##### **Continuing with a & b**\n",
731
+ "\n",
732
+ "Same principle as above, but a different kind of equation here.\n",
733
+ "\n",
734
+ "&nbsp;\n",
735
+ "\n",
736
+ "Also remember here, derivative of L wrt e was just calculated above^ (dL/de)\n",
737
+ "\n",
738
+ "e = a * b\n",
739
+ "\n",
740
+ "Therefore,\n",
741
+ "Derivative of e wrt a, will be b\n",
742
+ "Derivative of e wrt b, will be a\n",
743
+ "\n",
744
+ "&nbsp;\n",
745
+ "\n",
746
+ "Because the derivative of the same variable at the denominator gets out, so the other variable in the product remains (Calculus derivative theory itself)\n",
747
+ "\td/da(a * b) = b\n",
748
+ "\n",
749
+ "&nbsp;\n",
750
+ "\n",
751
+ "If we try to prove this mathematically,\n",
752
+ "\n",
753
+ "&nbsp;\n",
754
+ "\n",
755
+ "\te = a * b\n",
756
+ "\tf(x+h) - f(x) / h\n",
757
+ "\tRemember, f(x) is equation here. So, finding wrt a, substituting the values\n",
758
+ "\t=> ( ((a + h) * b) - (a * b) ) / h\n",
759
+ "\t=> ab + hb - ab / h\n",
760
+ "\t=> hb / h\n",
761
+ "\t=> b\n",
762
+ "\tTherefore, de/da = b\n",
763
+ "\n",
764
+ "&nbsp;\n",
765
+ "\n",
766
+ "Therefore, we can just substitute the value respectively.\n",
767
+ "\n",
768
+ "For node a:\n",
769
+ "\tdL/da = dL/de . dd/da \\\n",
770
+ " So here, the values should be -> dL/da = -2.0 * -3.0 = 6.0\n",
771
+ "\n",
772
+ "For node b:\n",
773
+ "\tdL/db = dL/de . dd/db \\\n",
774
+ " So here, the values should be -> dL/db = -2.0 * 2.0 = -4.0"
775
+ ],
776
+ "metadata": {
777
+ "id": "O8n9U7ZJuIyd"
778
+ }
779
+ },
780
+ {
781
+ "cell_type": "code",
782
+ "source": [
783
+ "# NOW WITH a\n",
784
+ "\n",
785
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
786
+ "def lol():\n",
787
+ "\n",
788
+ " h = 0.00001\n",
789
+ "\n",
790
+ " a = Value(2.0, label='a')\n",
791
+ " b = Value(-3.0, label='b')\n",
792
+ " c = Value(10.0, label='c')\n",
793
+ " e = a*b; e.label='e'\n",
794
+ " d= e + c; d.label='d'\n",
795
+ " f = Value(-2.0, label='f')\n",
796
+ " L = d*f; L.label='L'\n",
797
+ " L1 = L.data #L is basically a node, so we need its data\n",
798
+ "\n",
799
+ " a = Value(2.0 + h, label='a')\n",
800
+ " b = Value(-3.0, label='b')\n",
801
+ " c = Value(10.0, label='c')\n",
802
+ " e = a*b; e.label='e'\n",
803
+ " d= e + c; d.label='d'\n",
804
+ " f = Value(-2.0, label='f')\n",
805
+ " L = d*f; L.label='L'\n",
806
+ " L2 = L.data\n",
807
+ "\n",
808
+ " print((L2-L1)/h)\n",
809
+ "\n",
810
+ "lol()"
811
+ ],
812
+ "metadata": {
813
+ "colab": {
814
+ "base_uri": "https://localhost:8080/"
815
+ },
816
+ "id": "YVDtrQyxuBet",
817
+ "outputId": "b41d3c16-53f3-4c65-9a8e-c8bd44d5084f"
818
+ },
819
+ "execution_count": null,
820
+ "outputs": [
821
+ {
822
+ "output_type": "stream",
823
+ "name": "stdout",
824
+ "text": [
825
+ "6.000000000128124\n"
826
+ ]
827
+ }
828
+ ]
829
+ },
830
+ {
831
+ "cell_type": "code",
832
+ "source": [
833
+ "# NOW WITH b\n",
834
+ "\n",
835
+ "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
836
+ "def lol():\n",
837
+ "\n",
838
+ " h = 0.00001\n",
839
+ "\n",
840
+ " a = Value(2.0, label='a')\n",
841
+ " b = Value(-3.0, label='b')\n",
842
+ " c = Value(10.0, label='c')\n",
843
+ " e = a*b; e.label='e'\n",
844
+ " d= e + c; d.label='d'\n",
845
+ " f = Value(-2.0, label='f')\n",
846
+ " L = d*f; L.label='L'\n",
847
+ " L1 = L.data #L is basically a node, so we need its data\n",
848
+ "\n",
849
+ " a = Value(2.0, label='a')\n",
850
+ " b = Value(-3.0 + h, label='b')\n",
851
+ " c = Value(10.0, label='c')\n",
852
+ " e = a*b; e.label='e'\n",
853
+ " d= e + c; d.label='d'\n",
854
+ " f = Value(-2.0, label='f')\n",
855
+ " L = d*f; L.label='L'\n",
856
+ " L2 = L.data\n",
857
+ "\n",
858
+ " print((L2-L1)/h)\n",
859
+ "\n",
860
+ "lol()"
861
+ ],
862
+ "metadata": {
863
+ "colab": {
864
+ "base_uri": "https://localhost:8080/"
865
+ },
866
+ "id": "sHZl0wacuaTI",
867
+ "outputId": "48107eca-924f-4546-8088-cdc78f93f0bf"
868
+ },
869
+ "execution_count": null,
870
+ "outputs": [
871
+ {
872
+ "output_type": "stream",
873
+ "name": "stdout",
874
+ "text": [
875
+ "-4.000000000026205\n"
876
+ ]
877
+ }
878
+ ]
879
+ },
880
+ {
881
+ "cell_type": "code",
882
+ "source": [
883
+ "#Now, we add those values manually\n",
884
+ "a.grad = 6.0\n",
885
+ "b.grad = -4.0"
886
+ ],
887
+ "metadata": {
888
+ "id": "9chK6a53ugZ1"
889
+ },
890
+ "execution_count": 26,
891
+ "outputs": []
892
+ },
893
+ {
894
+ "cell_type": "code",
895
+ "source": [
896
+ "draw_dot(L)"
897
+ ],
898
+ "metadata": {
899
+ "colab": {
900
+ "base_uri": "https://localhost:8080/",
901
+ "height": 247
902
+ },
903
+ "id": "UTIlEpQSundk",
904
+ "outputId": "434f0ad9-3ae0-4e38-b88f-0a9a5f87e8b1"
905
+ },
906
+ "execution_count": null,
907
+ "outputs": [
908
+ {
909
+ "output_type": "execute_result",
910
+ "data": {
911
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1157pt\" height=\"154pt\"\n viewBox=\"0.00 0.00 1157.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1153,-150 1153,4 -4,4\"/>\n<!-- 135832571291264 -->\n<g id=\"node1\" class=\"node\">\n<title>135832571291264</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"321,-27.5 321,-63.5 515,-63.5 515,-27.5 321,-27.5\"/>\n<text text-anchor=\"middle\" x=\"332.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"344,-27.5 344,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"429,-27.5 429,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"472\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831741533536+ -->\n<g id=\"node10\" class=\"node\">\n<title>135831741533536+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"579\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"579\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 135832571291264&#45;&gt;135831741533536+ -->\n<g id=\"edge5\" class=\"edge\">\n<title>135832571291264&#45;&gt;135831741533536+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M515.16,-61.83C524.82,-63.47 534.13,-65.05 542.49,-66.47\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"542.09,-69.95 552.54,-68.18 543.26,-63.05 542.09,-69.95\"/>\n</g>\n<!-- 135832571291264* -->\n<g id=\"node2\" class=\"node\">\n<title>135832571291264*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"257\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135832571291264*&#45;&gt;135832571291264 -->\n<g id=\"edge1\" class=\"edge\">\n<title>135832571291264*&#45;&gt;135832571291264</title>\n<path fill=\"none\" stroke=\"black\" d=\"M284.26,-45.5C292.05,-45.5 301.1,-45.5 310.75,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"310.99,-49 320.99,-45.5 310.99,-42 310.99,-49\"/>\n</g>\n<!-- 135832571294864 -->\n<g id=\"node3\" class=\"node\">\n<title>135832571294864</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"957,-81.5 957,-117.5 1149,-117.5 1149,-81.5 957,-81.5\"/>\n<text text-anchor=\"middle\" x=\"969.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"982,-81.5 982,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1024.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1067,-81.5 1067,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1108\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135832571294864* -->\n<g id=\"node4\" class=\"node\">\n<title>135832571294864*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"894\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"894\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135832571294864*&#45;&gt;135832571294864 -->\n<g id=\"edge2\" class=\"edge\">\n<title>135832571294864*&#45;&gt;135832571294864</title>\n<path fill=\"none\" stroke=\"black\" d=\"M921.28,-99.5C928.78,-99.5 937.44,-99.5 946.67,-99.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"946.87,-103 956.87,-99.5 946.87,-96 946.87,-103\"/>\n</g>\n<!-- 135832571297456 -->\n<g id=\"node5\" class=\"node\">\n<title>135832571297456</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642.5,-109.5 642.5,-145.5 830.5,-145.5 830.5,-109.5 642.5,-109.5\"/>\n<text text-anchor=\"middle\" x=\"653\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"663.5,-109.5 663.5,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"706\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"748.5,-109.5 748.5,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"789.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 135832571297456&#45;&gt;135832571294864* -->\n<g id=\"edge9\" class=\"edge\">\n<title>135832571297456&#45;&gt;135832571294864*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M830.65,-110.73C840.2,-109.01 849.43,-107.35 857.72,-105.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"858.47,-109.27 867.69,-104.06 857.23,-102.39 858.47,-109.27\"/>\n</g>\n<!-- 135832571295440 -->\n<g id=\"node6\" class=\"node\">\n<title>135832571295440</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4.5,-55.5 4.5,-91.5 189.5,-91.5 189.5,-55.5 4.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"16\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"27.5,-55.5 27.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"67.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"107.5,-55.5 107.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"148.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 6.0000</text>\n</g>\n<!-- 135832571295440&#45;&gt;135832571291264* -->\n<g id=\"edge6\" class=\"edge\">\n<title>135832571295440&#45;&gt;135832571291264*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M189.91,-57.21C200.76,-55.29 211.28,-53.43 220.61,-51.77\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"221.37,-55.19 230.6,-50 220.15,-48.3 221.37,-55.19\"/>\n</g>\n<!-- 135832571297504 -->\n<g id=\"node7\" class=\"node\">\n<title>135832571297504</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 194,-36.5 194,-0.5 0,-0.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-0.5 23,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-0.5 108,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"151\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;4.0000</text>\n</g>\n<!-- 135832571297504&#45;&gt;135832571291264* -->\n<g id=\"edge4\" class=\"edge\">\n<title>135832571297504&#45;&gt;135832571291264*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M194.01,-34.91C203.35,-36.5 212.37,-38.04 220.49,-39.43\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"220.16,-42.93 230.6,-41.16 221.34,-36.03 220.16,-42.93\"/>\n</g>\n<!-- 135832571288384 -->\n<g id=\"node8\" class=\"node\">\n<title>135832571288384</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"320,-82.5 320,-118.5 516,-118.5 516,-82.5 320,-82.5\"/>\n<text text-anchor=\"middle\" x=\"331.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"343,-82.5 343,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"430,-82.5 430,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"473\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135832571288384&#45;&gt;135831741533536+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>135832571288384&#45;&gt;135831741533536+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M516.07,-83.41C525.42,-81.76 534.43,-80.17 542.55,-78.74\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"543.41,-82.15 552.65,-76.96 542.2,-75.25 543.41,-82.15\"/>\n</g>\n<!-- 135831741533536 -->\n<g id=\"node9\" class=\"node\">\n<title>135831741533536</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642,-54.5 642,-90.5 831,-90.5 831,-54.5 642,-54.5\"/>\n<text text-anchor=\"middle\" x=\"653.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"665,-54.5 665,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"705\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"745,-54.5 745,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"788\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831741533536&#45;&gt;135832571294864* -->\n<g id=\"edge8\" class=\"edge\">\n<title>135831741533536&#45;&gt;135832571294864*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M831.1,-88.75C840.4,-90.37 849.38,-91.93 857.49,-93.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"857.14,-96.83 867.59,-95.09 858.34,-89.93 857.14,-96.83\"/>\n</g>\n<!-- 135831741533536+&#45;&gt;135831741533536 -->\n<g id=\"edge3\" class=\"edge\">\n<title>135831741533536+&#45;&gt;135831741533536</title>\n<path fill=\"none\" stroke=\"black\" d=\"M606.03,-72.5C613.66,-72.5 622.52,-72.5 631.94,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"631.94,-76 641.94,-72.5 631.94,-69 631.94,-76\"/>\n</g>\n</g>\n</svg>\n",
912
+ "text/plain": [
913
+ "<graphviz.graphs.Digraph at 0x7b89ba3d7f70>"
914
+ ]
915
+ },
916
+ "metadata": {},
917
+ "execution_count": 19
918
+ }
919
+ ]
920
+ },
921
+ {
922
+ "cell_type": "markdown",
923
+ "source": [
924
+ "----------------"
925
+ ],
926
+ "metadata": {
927
+ "id": "gzgHxzqZwEzZ"
928
+ }
929
+ },
930
+ {
931
+ "cell_type": "markdown",
932
+ "source": [
933
+ "### **Hence the FINAL GENERATED GRAPH AFTER MANUAL BACKPROPAGATION!!**"
934
+ ],
935
+ "metadata": {
936
+ "id": "1ucLHBf8uso3"
937
+ }
938
+ },
939
+ {
940
+ "cell_type": "code",
941
+ "source": [
942
+ "draw_dot(L)"
943
+ ],
944
+ "metadata": {
945
+ "colab": {
946
+ "base_uri": "https://localhost:8080/",
947
+ "height": 247
948
+ },
949
+ "id": "Witeo84_wF-p",
950
+ "outputId": "ad44a9d6-d533-475d-f80b-46c681a1e6de"
951
+ },
952
+ "execution_count": 27,
953
+ "outputs": [
954
+ {
955
+ "output_type": "execute_result",
956
+ "data": {
957
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1157pt\" height=\"154pt\"\n viewBox=\"0.00 0.00 1157.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1153,-150 1153,4 -4,4\"/>\n<!-- 135831465322064 -->\n<g id=\"node1\" class=\"node\">\n<title>135831465322064</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"321,-27.5 321,-63.5 515,-63.5 515,-27.5 321,-27.5\"/>\n<text text-anchor=\"middle\" x=\"332.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"344,-27.5 344,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"429,-27.5 429,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"472\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831465327584+ -->\n<g id=\"node10\" class=\"node\">\n<title>135831465327584+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"579\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"579\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 135831465322064&#45;&gt;135831465327584+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>135831465322064&#45;&gt;135831465327584+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M515.16,-61.83C524.82,-63.47 534.13,-65.05 542.49,-66.47\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"542.09,-69.95 552.54,-68.18 543.26,-63.05 542.09,-69.95\"/>\n</g>\n<!-- 135831465322064* -->\n<g id=\"node2\" class=\"node\">\n<title>135831465322064*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"257\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135831465322064*&#45;&gt;135831465322064 -->\n<g id=\"edge1\" class=\"edge\">\n<title>135831465322064*&#45;&gt;135831465322064</title>\n<path fill=\"none\" stroke=\"black\" d=\"M284.26,-45.5C292.05,-45.5 301.1,-45.5 310.75,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"310.99,-49 320.99,-45.5 310.99,-42 310.99,-49\"/>\n</g>\n<!-- 135831465327296 -->\n<g id=\"node3\" class=\"node\">\n<title>135831465327296</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"320,-82.5 320,-118.5 516,-118.5 516,-82.5 320,-82.5\"/>\n<text text-anchor=\"middle\" x=\"331.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"343,-82.5 343,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"430,-82.5 430,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"473\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831465327296&#45;&gt;135831465327584+ -->\n<g id=\"edge5\" class=\"edge\">\n<title>135831465327296&#45;&gt;135831465327584+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M516.07,-83.41C525.42,-81.76 534.43,-80.17 542.55,-78.74\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"543.41,-82.15 552.65,-76.96 542.2,-75.25 543.41,-82.15\"/>\n</g>\n<!-- 135831465327872 -->\n<g id=\"node4\" class=\"node\">\n<title>135831465327872</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 194,-91.5 194,-55.5 0,-55.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-55.5 23,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-55.5 108,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"151\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;4.0000</text>\n</g>\n<!-- 135831465327872&#45;&gt;135831465322064* -->\n<g id=\"edge8\" class=\"edge\">\n<title>135831465327872&#45;&gt;135831465322064*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M194.01,-56.49C203.35,-54.83 212.37,-53.23 220.49,-51.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"221.37,-55.19 230.6,-50 220.15,-48.3 221.37,-55.19\"/>\n</g>\n<!-- 135831465319232 -->\n<g id=\"node5\" class=\"node\">\n<title>135831465319232</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4.5,-0.5 4.5,-36.5 189.5,-36.5 189.5,-0.5 4.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"16\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"27.5,-0.5 27.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"67.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"107.5,-0.5 107.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"148.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 6.0000</text>\n</g>\n<!-- 135831465319232&#45;&gt;135831465322064* -->\n<g id=\"edge6\" class=\"edge\">\n<title>135831465319232&#45;&gt;135831465322064*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M189.91,-34.21C200.76,-36.06 211.28,-37.86 220.61,-39.45\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"220.16,-42.93 230.6,-41.16 221.34,-36.03 220.16,-42.93\"/>\n</g>\n<!-- 135831465327488 -->\n<g id=\"node6\" class=\"node\">\n<title>135831465327488</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642.5,-109.5 642.5,-145.5 830.5,-145.5 830.5,-109.5 642.5,-109.5\"/>\n<text text-anchor=\"middle\" x=\"653\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"663.5,-109.5 663.5,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"706\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"748.5,-109.5 748.5,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"789.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 135831465321920* -->\n<g id=\"node8\" class=\"node\">\n<title>135831465321920*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"894\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"894\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135831465327488&#45;&gt;135831465321920* -->\n<g id=\"edge9\" class=\"edge\">\n<title>135831465327488&#45;&gt;135831465321920*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M830.65,-110.73C840.2,-109.01 849.43,-107.35 857.72,-105.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"858.47,-109.27 867.69,-104.06 857.23,-102.39 858.47,-109.27\"/>\n</g>\n<!-- 135831465321920 -->\n<g id=\"node7\" class=\"node\">\n<title>135831465321920</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"957,-81.5 957,-117.5 1149,-117.5 1149,-81.5 957,-81.5\"/>\n<text text-anchor=\"middle\" x=\"969.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"982,-81.5 982,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1024.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1067,-81.5 1067,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1108\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 135831465321920*&#45;&gt;135831465321920 -->\n<g id=\"edge2\" class=\"edge\">\n<title>135831465321920*&#45;&gt;135831465321920</title>\n<path fill=\"none\" stroke=\"black\" d=\"M921.28,-99.5C928.78,-99.5 937.44,-99.5 946.67,-99.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"946.87,-103 956.87,-99.5 946.87,-96 946.87,-103\"/>\n</g>\n<!-- 135831465327584 -->\n<g id=\"node9\" class=\"node\">\n<title>135831465327584</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642,-54.5 642,-90.5 831,-90.5 831,-54.5 642,-54.5\"/>\n<text text-anchor=\"middle\" x=\"653.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"665,-54.5 665,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"705\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"745,-54.5 745,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"788\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 135831465327584&#45;&gt;135831465321920* -->\n<g id=\"edge4\" class=\"edge\">\n<title>135831465327584&#45;&gt;135831465321920*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M831.1,-88.75C840.4,-90.37 849.38,-91.93 857.49,-93.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"857.14,-96.83 867.59,-95.09 858.34,-89.93 857.14,-96.83\"/>\n</g>\n<!-- 135831465327584+&#45;&gt;135831465327584 -->\n<g id=\"edge3\" class=\"edge\">\n<title>135831465327584+&#45;&gt;135831465327584</title>\n<path fill=\"none\" stroke=\"black\" d=\"M606.03,-72.5C613.66,-72.5 622.52,-72.5 631.94,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"631.94,-76 641.94,-72.5 631.94,-69 631.94,-76\"/>\n</g>\n</g>\n</svg>\n",
958
+ "text/plain": [
959
+ "<graphviz.graphs.Digraph at 0x7b89ba3d7520>"
960
+ ]
961
+ },
962
+ "metadata": {},
963
+ "execution_count": 27
964
+ }
965
+ ]
966
+ }
967
+ ]
968
+ }
5_optimization_single_step_preview.ipynb ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "class Value:\n",
21
+ "\n",
22
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
23
+ " self.data = data\n",
24
+ " self.grad = 0.0\n",
25
+ " self._prev = set(_children)\n",
26
+ " self._op = _op\n",
27
+ " self.label = label\n",
28
+ "\n",
29
+ "\n",
30
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
31
+ " return f\"Value(data={self.data})\"\n",
32
+ "\n",
33
+ " def __add__(self, other):\n",
34
+ " out = Value(self.data + other.data, (self, other), '+')\n",
35
+ " return out\n",
36
+ "\n",
37
+ " def __mul__(self, other):\n",
38
+ " out = Value(self.data * other.data, (self, other), '*')\n",
39
+ " return out"
40
+ ],
41
+ "metadata": {
42
+ "id": "jtRAdDVT6jf2"
43
+ },
44
+ "execution_count": 1,
45
+ "outputs": []
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": 2,
50
+ "metadata": {
51
+ "colab": {
52
+ "base_uri": "https://localhost:8080/"
53
+ },
54
+ "id": "AIP2sPDm6Los",
55
+ "outputId": "f467edac-8c3a-4695-a651-5325a4ecea8f"
56
+ },
57
+ "outputs": [
58
+ {
59
+ "output_type": "execute_result",
60
+ "data": {
61
+ "text/plain": [
62
+ "Value(data=-8.0)"
63
+ ]
64
+ },
65
+ "metadata": {},
66
+ "execution_count": 2
67
+ }
68
+ ],
69
+ "source": [
70
+ "a = Value(2.0, label='a')\n",
71
+ "b = Value(-3.0, label='b')\n",
72
+ "c = Value(10.0, label='c')\n",
73
+ "e = a*b; e.label='e'\n",
74
+ "d= e + c; d.label='d'\n",
75
+ "f = Value(-2.0, label='f')\n",
76
+ "L = d*f; L.label='L'\n",
77
+ "L"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "source": [
83
+ "from graphviz import Digraph\n",
84
+ "\n",
85
+ "def trace(root):\n",
86
+ " #Builds a set of all nodes and edges in a graph\n",
87
+ " nodes, edges = set(), set()\n",
88
+ " def build(v):\n",
89
+ " if v not in nodes:\n",
90
+ " nodes.add(v)\n",
91
+ " for child in v._prev:\n",
92
+ " edges.add((child, v))\n",
93
+ " build(child)\n",
94
+ " build(root)\n",
95
+ " return nodes, edges\n",
96
+ "\n",
97
+ "def draw_dot(root):\n",
98
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
99
+ "\n",
100
+ " nodes, edges = trace(root)\n",
101
+ " for n in nodes:\n",
102
+ " uid = str(id(n))\n",
103
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
104
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
105
+ " if n._op:\n",
106
+ " #If this value is a result of some operation, then create an op node for it\n",
107
+ " dot.node(name = uid + n._op, label=n._op)\n",
108
+ " #and connect this node to it\n",
109
+ " dot.edge(uid + n._op, uid)\n",
110
+ "\n",
111
+ " for n1, n2 in edges:\n",
112
+ " #Connect n1 to the node of n2\n",
113
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
114
+ "\n",
115
+ " return dot"
116
+ ],
117
+ "metadata": {
118
+ "id": "T0rN8d146jvF"
119
+ },
120
+ "execution_count": 3,
121
+ "outputs": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "L.grad = 1.0\n",
127
+ "f.grad = 4.0\n",
128
+ "d.grad = -2.0\n",
129
+ "c.grad = -2.0\n",
130
+ "e.grad = -2.0\n",
131
+ "a.grad = 6.0\n",
132
+ "b.grad = -4.0"
133
+ ],
134
+ "metadata": {
135
+ "id": "3TCgz-n6DbzI"
136
+ },
137
+ "execution_count": 4,
138
+ "outputs": []
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "source": [
143
+ "draw_dot(L)"
144
+ ],
145
+ "metadata": {
146
+ "colab": {
147
+ "base_uri": "https://localhost:8080/",
148
+ "height": 212
149
+ },
150
+ "id": "k7wjwrfo6nUl",
151
+ "outputId": "b915567c-ba7b-44ec-fd34-97f35d258fd4"
152
+ },
153
+ "execution_count": 5,
154
+ "outputs": [
155
+ {
156
+ "output_type": "execute_result",
157
+ "data": {
158
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1157pt\" height=\"128pt\"\n viewBox=\"0.00 0.00 1157.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 1153,-124 1153,4 -4,4\"/>\n<!-- 132400623613456 -->\n<g id=\"node1\" class=\"node\">\n<title>132400623613456</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642,-27.5 642,-63.5 831,-63.5 831,-27.5 642,-27.5\"/>\n<text text-anchor=\"middle\" x=\"653.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"665,-27.5 665,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"705\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"745,-27.5 745,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"788\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 132400623605104* -->\n<g id=\"node9\" class=\"node\">\n<title>132400623605104*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"894\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"894\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132400623613456&#45;&gt;132400623605104* -->\n<g id=\"edge5\" class=\"edge\">\n<title>132400623613456&#45;&gt;132400623605104*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M831.1,-61.75C840.4,-63.37 849.38,-64.93 857.49,-66.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"857.14,-69.83 867.59,-68.09 858.34,-62.93 857.14,-69.83\"/>\n</g>\n<!-- 132400623613456+ -->\n<g id=\"node2\" class=\"node\">\n<title>132400623613456+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"579\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"579\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132400623613456+&#45;&gt;132400623613456 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132400623613456+&#45;&gt;132400623613456</title>\n<path fill=\"none\" stroke=\"black\" d=\"M606.03,-45.5C613.66,-45.5 622.52,-45.5 631.94,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"631.94,-49 641.94,-45.5 631.94,-42 631.94,-49\"/>\n</g>\n<!-- 132400623602320 -->\n<g id=\"node3\" class=\"node\">\n<title>132400623602320</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"321,-55.5 321,-91.5 515,-91.5 515,-55.5 321,-55.5\"/>\n<text text-anchor=\"middle\" x=\"332.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"344,-55.5 344,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"429,-55.5 429,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"472\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 132400623602320&#45;&gt;132400623613456+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>132400623602320&#45;&gt;132400623613456+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M515.16,-56.57C524.82,-54.87 534.13,-53.23 542.49,-51.75\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"543.3,-55.17 552.54,-49.98 542.08,-48.27 543.3,-55.17\"/>\n</g>\n<!-- 132400623602320* -->\n<g id=\"node4\" class=\"node\">\n<title>132400623602320*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"257\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132400623602320*&#45;&gt;132400623602320 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132400623602320*&#45;&gt;132400623602320</title>\n<path fill=\"none\" stroke=\"black\" d=\"M284.26,-73.5C292.05,-73.5 301.1,-73.5 310.75,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"310.99,-77 320.99,-73.5 310.99,-70 310.99,-77\"/>\n</g>\n<!-- 132400623613216 -->\n<g id=\"node5\" class=\"node\">\n<title>132400623613216</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642.5,-82.5 642.5,-118.5 830.5,-118.5 830.5,-82.5 642.5,-82.5\"/>\n<text text-anchor=\"middle\" x=\"653\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"663.5,-82.5 663.5,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"706\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"748.5,-82.5 748.5,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"789.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 132400623613216&#45;&gt;132400623605104* -->\n<g id=\"edge7\" class=\"edge\">\n<title>132400623613216&#45;&gt;132400623605104*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M830.65,-83.73C840.2,-82.01 849.43,-80.35 857.72,-78.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"858.47,-82.27 867.69,-77.06 857.23,-75.39 858.47,-82.27\"/>\n</g>\n<!-- 132400623615808 -->\n<g id=\"node6\" class=\"node\">\n<title>132400623615808</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4.5,-83.5 4.5,-119.5 189.5,-119.5 189.5,-83.5 4.5,-83.5\"/>\n<text text-anchor=\"middle\" x=\"16\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"27.5,-83.5 27.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"67.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"107.5,-83.5 107.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"148.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 6.0000</text>\n</g>\n<!-- 132400623615808&#45;&gt;132400623602320* -->\n<g id=\"edge6\" class=\"edge\">\n<title>132400623615808&#45;&gt;132400623602320*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M189.91,-85.21C200.76,-83.29 211.28,-81.43 220.61,-79.77\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"221.37,-83.19 230.6,-78 220.15,-76.3 221.37,-83.19\"/>\n</g>\n<!-- 132400623609664 -->\n<g id=\"node7\" class=\"node\">\n<title>132400623609664</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"320,-0.5 320,-36.5 516,-36.5 516,-0.5 320,-0.5\"/>\n<text text-anchor=\"middle\" x=\"331.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"343,-0.5 343,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"430,-0.5 430,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"473\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 132400623609664&#45;&gt;132400623613456+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>132400623609664&#45;&gt;132400623613456+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M516.07,-34.98C525.42,-36.57 534.43,-38.1 542.55,-39.48\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"542.21,-42.97 552.65,-41.2 543.38,-36.07 542.21,-42.97\"/>\n</g>\n<!-- 132400623605104 -->\n<g id=\"node8\" class=\"node\">\n<title>132400623605104</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"957,-54.5 957,-90.5 1149,-90.5 1149,-54.5 957,-54.5\"/>\n<text text-anchor=\"middle\" x=\"969.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"982,-54.5 982,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1024.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1067,-54.5 1067,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1108\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132400623605104*&#45;&gt;132400623605104 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132400623605104*&#45;&gt;132400623605104</title>\n<path fill=\"none\" stroke=\"black\" d=\"M921.28,-72.5C928.78,-72.5 937.44,-72.5 946.67,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"946.87,-76 956.87,-72.5 946.87,-69 946.87,-76\"/>\n</g>\n<!-- 132400623612880 -->\n<g id=\"node10\" class=\"node\">\n<title>132400623612880</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-28.5 0,-64.5 194,-64.5 194,-28.5 0,-28.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-28.5 23,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-28.5 108,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"151\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;4.0000</text>\n</g>\n<!-- 132400623612880&#45;&gt;132400623602320* -->\n<g id=\"edge8\" class=\"edge\">\n<title>132400623612880&#45;&gt;132400623602320*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M194.01,-62.91C203.35,-64.5 212.37,-66.04 220.49,-67.43\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"220.16,-70.93 230.6,-69.16 221.34,-64.03 220.16,-70.93\"/>\n</g>\n</g>\n</svg>\n",
159
+ "text/plain": [
160
+ "<graphviz.graphs.Digraph at 0x786aec236c20>"
161
+ ]
162
+ },
163
+ "metadata": {},
164
+ "execution_count": 5
165
+ }
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "markdown",
170
+ "source": [
171
+ "--------------------"
172
+ ],
173
+ "metadata": {
174
+ "id": "WqQ2p-U1eUnJ"
175
+ }
176
+ },
177
+ {
178
+ "cell_type": "markdown",
179
+ "source": [
180
+ "Now, we are going to try and nudge the leaf nodes (because those are usually what we have control over. In our example: a,b,c,f) slightly towards the gradient value, to nudge L towards a more positive direction."
181
+ ],
182
+ "metadata": {
183
+ "id": "cpx9Me4LeVfx"
184
+ }
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "source": [
189
+ "a.data += 0.01 * a.grad\n",
190
+ "b.data += 0.01 * b.grad\n",
191
+ "c.data += 0.01 * c.grad\n",
192
+ "f.data += 0.01 * f.grad\n",
193
+ "\n",
194
+ "e = a*b;\n",
195
+ "d= e + c;\n",
196
+ "L = d*f;\n",
197
+ "L\n",
198
+ "\n",
199
+ "print(L.data)"
200
+ ],
201
+ "metadata": {
202
+ "colab": {
203
+ "base_uri": "https://localhost:8080/"
204
+ },
205
+ "id": "_edHadeReTBn",
206
+ "outputId": "3631e3e6-5bbf-4ceb-d1e1-45599a5a2736"
207
+ },
208
+ "execution_count": 7,
209
+ "outputs": [
210
+ {
211
+ "output_type": "stream",
212
+ "name": "stdout",
213
+ "text": [
214
+ "-6.723584000000001\n"
215
+ ]
216
+ }
217
+ ]
218
+ },
219
+ {
220
+ "cell_type": "markdown",
221
+ "source": [
222
+ "Therefore the value of L was pushed to a more positive direction from -8.0 to -6.0"
223
+ ],
224
+ "metadata": {
225
+ "id": "aXwYpQKKgYGg"
226
+ }
227
+ }
228
+ ]
229
+ }
6_0_backpropagation_neuron.ipynb ADDED
@@ -0,0 +1,413 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "class Value:\n",
21
+ "\n",
22
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
23
+ " self.data = data\n",
24
+ " self.grad = 0.0\n",
25
+ " self._prev = set(_children)\n",
26
+ " self._op = _op\n",
27
+ " self.label = label\n",
28
+ "\n",
29
+ "\n",
30
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
31
+ " return f\"Value(data={self.data})\"\n",
32
+ "\n",
33
+ " def __add__(self, other):\n",
34
+ " out = Value(self.data + other.data, (self, other), '+')\n",
35
+ " return out\n",
36
+ "\n",
37
+ " def __mul__(self, other):\n",
38
+ " out = Value(self.data * other.data, (self, other), '*')\n",
39
+ " return out"
40
+ ],
41
+ "metadata": {
42
+ "id": "jtRAdDVT6jf2"
43
+ },
44
+ "execution_count": 1,
45
+ "outputs": []
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": null,
50
+ "metadata": {
51
+ "colab": {
52
+ "base_uri": "https://localhost:8080/"
53
+ },
54
+ "id": "AIP2sPDm6Los",
55
+ "outputId": "8e1d5665-fc27-4ddb-95ac-a9cf53f25d51"
56
+ },
57
+ "outputs": [
58
+ {
59
+ "output_type": "execute_result",
60
+ "data": {
61
+ "text/plain": [
62
+ "Value(data=-8.0)"
63
+ ]
64
+ },
65
+ "metadata": {},
66
+ "execution_count": 21
67
+ }
68
+ ],
69
+ "source": [
70
+ "a = Value(2.0, label='a')\n",
71
+ "b = Value(-3.0, label='b')\n",
72
+ "c = Value(10.0, label='c')\n",
73
+ "e = a*b; e.label='e'\n",
74
+ "d= e + c; d.label='d'\n",
75
+ "f = Value(-2.0, label='f')\n",
76
+ "L = d*f; L.label='L'\n",
77
+ "L"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "source": [
83
+ "from graphviz import Digraph\n",
84
+ "\n",
85
+ "def trace(root):\n",
86
+ " #Builds a set of all nodes and edges in a graph\n",
87
+ " nodes, edges = set(), set()\n",
88
+ " def build(v):\n",
89
+ " if v not in nodes:\n",
90
+ " nodes.add(v)\n",
91
+ " for child in v._prev:\n",
92
+ " edges.add((child, v))\n",
93
+ " build(child)\n",
94
+ " build(root)\n",
95
+ " return nodes, edges\n",
96
+ "\n",
97
+ "def draw_dot(root):\n",
98
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
99
+ "\n",
100
+ " nodes, edges = trace(root)\n",
101
+ " for n in nodes:\n",
102
+ " uid = str(id(n))\n",
103
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
104
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
105
+ " if n._op:\n",
106
+ " #If this value is a result of some operation, then create an op node for it\n",
107
+ " dot.node(name = uid + n._op, label=n._op)\n",
108
+ " #and connect this node to it\n",
109
+ " dot.edge(uid + n._op, uid)\n",
110
+ "\n",
111
+ " for n1, n2 in edges:\n",
112
+ " #Connect n1 to the node of n2\n",
113
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
114
+ "\n",
115
+ " return dot"
116
+ ],
117
+ "metadata": {
118
+ "id": "T0rN8d146jvF"
119
+ },
120
+ "execution_count": 2,
121
+ "outputs": []
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "source": [
126
+ "draw_dot(L)"
127
+ ],
128
+ "metadata": {
129
+ "colab": {
130
+ "base_uri": "https://localhost:8080/",
131
+ "height": 247
132
+ },
133
+ "id": "k7wjwrfo6nUl",
134
+ "outputId": "d78c4618-6574-49f9-8e80-f2faa8dad69a"
135
+ },
136
+ "execution_count": null,
137
+ "outputs": [
138
+ {
139
+ "output_type": "execute_result",
140
+ "data": {
141
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1148pt\" height=\"154pt\"\n viewBox=\"0.00 0.00 1148.00 154.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 150)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-150 1144,-150 1144,4 -4,4\"/>\n<!-- 135449920624224 -->\n<g id=\"node1\" class=\"node\">\n<title>135449920624224</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"317,-27.5 317,-63.5 507,-63.5 507,-27.5 317,-27.5\"/>\n<text text-anchor=\"middle\" x=\"328.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"340,-27.5 340,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"425,-27.5 425,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"466\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632192+ -->\n<g id=\"node8\" class=\"node\">\n<title>135449920632192+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"571\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"571\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 135449920624224&#45;&gt;135449920632192+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>135449920624224&#45;&gt;135449920632192+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M507.05,-61.67C516.78,-63.35 526.18,-64.96 534.62,-66.42\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"534.3,-69.91 544.75,-68.16 535.49,-63.01 534.3,-69.91\"/>\n</g>\n<!-- 135449920624224* -->\n<g id=\"node2\" class=\"node\">\n<title>135449920624224*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"253\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135449920624224*&#45;&gt;135449920624224 -->\n<g id=\"edge1\" class=\"edge\">\n<title>135449920624224*&#45;&gt;135449920624224</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.28,-45.5C288.05,-45.5 297.08,-45.5 306.68,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.88,-49 316.88,-45.5 306.88,-42 306.88,-49\"/>\n</g>\n<!-- 135449920621248 -->\n<g id=\"node3\" class=\"node\">\n<title>135449920621248</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"948,-81.5 948,-117.5 1140,-117.5 1140,-81.5 948,-81.5\"/>\n<text text-anchor=\"middle\" x=\"960.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"973,-81.5 973,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1015.5\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1058,-81.5 1058,-117.5 \"/>\n<text text-anchor=\"middle\" x=\"1099\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920621248* -->\n<g id=\"node4\" class=\"node\">\n<title>135449920621248*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"885\" cy=\"-99.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"885\" y=\"-95.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 135449920621248*&#45;&gt;135449920621248 -->\n<g id=\"edge2\" class=\"edge\">\n<title>135449920621248*&#45;&gt;135449920621248</title>\n<path fill=\"none\" stroke=\"black\" d=\"M912.28,-99.5C919.78,-99.5 928.44,-99.5 937.67,-99.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"937.87,-103 947.87,-99.5 937.87,-96 937.87,-103\"/>\n</g>\n<!-- 135449920632624 -->\n<g id=\"node5\" class=\"node\">\n<title>135449920632624</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 190,-91.5 190,-55.5 0,-55.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-55.5 23,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-55.5 108,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"149\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632624&#45;&gt;135449920624224* -->\n<g id=\"edge5\" class=\"edge\">\n<title>135449920632624&#45;&gt;135449920624224*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.34,-56.57C199.62,-54.9 208.58,-53.29 216.66,-51.84\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"217.49,-55.25 226.72,-50.04 216.26,-48.36 217.49,-55.25\"/>\n</g>\n<!-- 135449920619856 -->\n<g id=\"node6\" class=\"node\">\n<title>135449920619856</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-0.5 2.5,-36.5 187.5,-36.5 187.5,-0.5 2.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"14\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-0.5 25.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-0.5 105.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"146.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920619856&#45;&gt;135449920624224* -->\n<g id=\"edge8\" class=\"edge\">\n<title>135449920619856&#45;&gt;135449920624224*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.65,-34.36C197.94,-36.14 207.91,-37.87 216.81,-39.41\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"216.26,-42.87 226.71,-41.12 217.45,-35.97 216.26,-42.87\"/>\n</g>\n<!-- 135449920632192 -->\n<g id=\"node7\" class=\"node\">\n<title>135449920632192</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"635.5,-54.5 635.5,-90.5 820.5,-90.5 820.5,-54.5 635.5,-54.5\"/>\n<text text-anchor=\"middle\" x=\"647\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"658.5,-54.5 658.5,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"698.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"738.5,-54.5 738.5,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"779.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632192&#45;&gt;135449920621248* -->\n<g id=\"edge7\" class=\"edge\">\n<title>135449920632192&#45;&gt;135449920621248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M820.51,-88.44C830.48,-90.18 840.13,-91.86 848.77,-93.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"848.31,-96.84 858.77,-95.1 849.52,-89.94 848.31,-96.84\"/>\n</g>\n<!-- 135449920632192+&#45;&gt;135449920632192 -->\n<g id=\"edge3\" class=\"edge\">\n<title>135449920632192+&#45;&gt;135449920632192</title>\n<path fill=\"none\" stroke=\"black\" d=\"M598.29,-72.5C606.26,-72.5 615.54,-72.5 625.39,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"625.41,-76 635.41,-72.5 625.41,-69 625.41,-76\"/>\n</g>\n<!-- 135449920619424 -->\n<g id=\"node9\" class=\"node\">\n<title>135449920619424</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"316,-82.5 316,-118.5 508,-118.5 508,-82.5 316,-82.5\"/>\n<text text-anchor=\"middle\" x=\"327.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"339,-82.5 339,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"382.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"426,-82.5 426,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"467\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920619424&#45;&gt;135449920632192+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>135449920619424&#45;&gt;135449920632192+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M508.4,-83.49C517.69,-81.83 526.64,-80.23 534.71,-78.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"535.53,-82.2 544.76,-77 534.31,-75.31 535.53,-82.2\"/>\n</g>\n<!-- 135449920632288 -->\n<g id=\"node10\" class=\"node\">\n<title>135449920632288</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"634,-109.5 634,-145.5 822,-145.5 822,-109.5 634,-109.5\"/>\n<text text-anchor=\"middle\" x=\"644.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"655,-109.5 655,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"697.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"740,-109.5 740,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"781\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 135449920632288&#45;&gt;135449920621248* -->\n<g id=\"edge6\" class=\"edge\">\n<title>135449920632288&#45;&gt;135449920621248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M822.29,-110.65C831.57,-108.97 840.52,-107.35 848.61,-105.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"849.46,-109.3 858.68,-104.07 848.21,-102.41 849.46,-109.3\"/>\n</g>\n</g>\n</svg>\n",
142
+ "text/plain": [
143
+ "<graphviz.graphs.Digraph at 0x7b30e4675ae0>"
144
+ ]
145
+ },
146
+ "metadata": {},
147
+ "execution_count": 7
148
+ }
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "markdown",
153
+ "source": [
154
+ "------------------"
155
+ ],
156
+ "metadata": {
157
+ "id": "bcJ3Q97AutNy"
158
+ }
159
+ },
160
+ {
161
+ "cell_type": "markdown",
162
+ "source": [
163
+ "### **Implementing the Neuron Mathematical Mode**"
164
+ ],
165
+ "metadata": {
166
+ "id": "wFLtnVu1uuaz"
167
+ }
168
+ },
169
+ {
170
+ "cell_type": "markdown",
171
+ "source": [
172
+ "![Neuron Model](https://imgs.search.brave.com/eGlTokdhbINxZOKkAUnAz_Cu-grWMt7LkMKJoi66gdQ/rs:fit:500:0:0:0/g:ce/aHR0cHM6Ly9jczIz/MW4uZ2l0aHViLmlv/L2Fzc2V0cy9ubjEv/bmV1cm9uX21vZGVs/LmpwZWc)"
173
+ ],
174
+ "metadata": {
175
+ "id": "VhRE8DEMu3Qb"
176
+ }
177
+ },
178
+ {
179
+ "cell_type": "code",
180
+ "source": [
181
+ "#Inputs x1, x2 of the neuron\n",
182
+ "x1 = Value(2.0, label='x1')\n",
183
+ "x2 = Value(0.0, label='x2')\n",
184
+ "\n",
185
+ "#Weights w1, w2 of the neuron - The synaptic values\n",
186
+ "w1 = Value(-3.0, label='w1')\n",
187
+ "w2 = Value(1.0, label='w2')\n",
188
+ "\n",
189
+ "#The bias of the neuron\n",
190
+ "b = Value(6.7, label='b')"
191
+ ],
192
+ "metadata": {
193
+ "id": "3rGi-UXPu0lq"
194
+ },
195
+ "execution_count": 3,
196
+ "outputs": []
197
+ },
198
+ {
199
+ "cell_type": "code",
200
+ "source": [
201
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
202
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
203
+ "\n",
204
+ "#The summation\n",
205
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'"
206
+ ],
207
+ "metadata": {
208
+ "id": "RQHJZ77FvRz-"
209
+ },
210
+ "execution_count": 4,
211
+ "outputs": []
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "source": [
216
+ "#n is basically the cell body, but without the activation function\n",
217
+ "n = x1w1x2w2 + b; n.label = 'n'"
218
+ ],
219
+ "metadata": {
220
+ "id": "i33UWc5Yvh5e"
221
+ },
222
+ "execution_count": 5,
223
+ "outputs": []
224
+ },
225
+ {
226
+ "cell_type": "code",
227
+ "source": [
228
+ "draw_dot(n)"
229
+ ],
230
+ "metadata": {
231
+ "colab": {
232
+ "base_uri": "https://localhost:8080/",
233
+ "height": 322
234
+ },
235
+ "id": "aj871Eg0wVW_",
236
+ "outputId": "d4b424e3-0b7b-4857-b250-91b4e1b43626"
237
+ },
238
+ "execution_count": 6,
239
+ "outputs": [
240
+ {
241
+ "output_type": "execute_result",
242
+ "data": {
243
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1264pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1264.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1260,-206 1260,4 -4,4\"/>\n<!-- 137841230174752 -->\n<g id=\"node1\" class=\"node\">\n<title>137841230174752</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230173504+ -->\n<g id=\"node12\" class=\"node\">\n<title>137841230173504+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 137841230174752&#45;&gt;137841230173504+ -->\n<g id=\"edge10\" class=\"edge\">\n<title>137841230174752&#45;&gt;137841230173504+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M941.24,-118.51C952.12,-120 962.4,-121.4 971.45,-122.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.11,-126.13 981.49,-124.01 972.05,-119.19 971.11,-126.13\"/>\n</g>\n<!-- 137841230174752+ -->\n<g id=\"node2\" class=\"node\">\n<title>137841230174752+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 137841230174752+&#45;&gt;137841230174752 -->\n<g id=\"edge1\" class=\"edge\">\n<title>137841230174752+&#45;&gt;137841230174752</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n<!-- 137841230171248 -->\n<g id=\"node3\" class=\"node\">\n<title>137841230171248</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-110.5 328.5,-146.5 543.5,-146.5 543.5,-110.5 328.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-110.5 381.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-110.5 461.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230171248&#45;&gt;137841230174752+ -->\n<g id=\"edge12\" class=\"edge\">\n<title>137841230171248&#45;&gt;137841230174752+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-111.01C554.01,-109.34 563.76,-107.74 572.44,-106.32\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.17,-109.75 582.47,-104.68 572.04,-102.84 573.17,-109.75\"/>\n</g>\n<!-- 137841230171248* -->\n<g id=\"node4\" class=\"node\">\n<title>137841230171248*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 137841230171248*&#45;&gt;137841230171248 -->\n<g id=\"edge2\" class=\"edge\">\n<title>137841230171248*&#45;&gt;137841230171248</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C298.51,-128.5 308.08,-128.5 318.36,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-132 328.39,-128.5 318.39,-125 318.39,-132\"/>\n</g>\n<!-- 137841230178928 -->\n<g id=\"node5\" class=\"node\">\n<title>137841230178928</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-55.5 326,-91.5 546,-91.5 546,-55.5 326,-55.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-55.5 379,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-55.5 464,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230178928&#45;&gt;137841230174752+ -->\n<g id=\"edge5\" class=\"edge\">\n<title>137841230178928&#45;&gt;137841230174752+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-90.75C555.64,-92.23 564.6,-93.65 572.65,-94.92\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.23,-98.4 582.65,-96.5 573.32,-91.48 572.23,-98.4\"/>\n</g>\n<!-- 137841230178928* -->\n<g id=\"node6\" class=\"node\">\n<title>137841230178928*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 137841230178928*&#45;&gt;137841230178928 -->\n<g id=\"edge3\" class=\"edge\">\n<title>137841230178928*&#45;&gt;137841230178928</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C297.77,-73.5 306.37,-73.5 315.6,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-77 325.84,-73.5 315.84,-70 315.84,-77\"/>\n</g>\n<!-- 137841230177440 -->\n<g id=\"node7\" class=\"node\">\n<title>137841230177440</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-55.5 4,-91.5 196,-91.5 196,-55.5 4,-55.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-55.5 34,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-55.5 114,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230177440&#45;&gt;137841230178928* -->\n<g id=\"edge11\" class=\"edge\">\n<title>137841230177440&#45;&gt;137841230178928*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M196.05,-73.5C206.52,-73.5 216.65,-73.5 225.71,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.79,-77 235.79,-73.5 225.79,-70 225.79,-77\"/>\n</g>\n<!-- 137841230173888 -->\n<g id=\"node8\" class=\"node\">\n<title>137841230173888</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 200,-36.5 200,-0.5 0,-0.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-0.5 33,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-0.5 118,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230173888&#45;&gt;137841230178928* -->\n<g id=\"edge8\" class=\"edge\">\n<title>137841230173888&#45;&gt;137841230178928*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 137841230182144 -->\n<g id=\"node9\" class=\"node\">\n<title>137841230182144</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-137.5 716,-173.5 901,-173.5 901,-137.5 716,-137.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-137.5 739,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.7000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-137.5 819,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230182144&#45;&gt;137841230173504+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>137841230182144&#45;&gt;137841230173504+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-142.52C926,-138.98 951.59,-135.36 971.37,-132.55\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-135.99 981.43,-131.12 971.04,-129.06 972.03,-135.99\"/>\n</g>\n<!-- 137841230173456 -->\n<g id=\"node10\" class=\"node\">\n<title>137841230173456</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-165.5 2.5,-201.5 197.5,-201.5 197.5,-165.5 2.5,-165.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-165.5 35.5,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-165.5 115.5,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230173456&#45;&gt;137841230171248* -->\n<g id=\"edge6\" class=\"edge\">\n<title>137841230173456&#45;&gt;137841230171248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 137841230173504 -->\n<g id=\"node11\" class=\"node\">\n<title>137841230173504</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-109.5 1071,-145.5 1256,-145.5 1256,-109.5 1071,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-109.5 1094,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-109.5 1174,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230173504+&#45;&gt;137841230173504 -->\n<g id=\"edge4\" class=\"edge\">\n<title>137841230173504+&#45;&gt;137841230173504</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-127.5C1042.58,-127.5 1051.3,-127.5 1060.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-131 1070.81,-127.5 1060.81,-124 1060.81,-131\"/>\n</g>\n<!-- 137841230177152 -->\n<g id=\"node13\" class=\"node\">\n<title>137841230177152</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-110.5 4,-146.5 196,-146.5 196,-110.5 4,-110.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-110.5 34,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-110.5 114,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841230177152&#45;&gt;137841230171248* -->\n<g id=\"edge9\" class=\"edge\">\n<title>137841230177152&#45;&gt;137841230171248*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M196.05,-128.5C206.52,-128.5 216.65,-128.5 225.71,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.79,-132 235.79,-128.5 225.79,-125 225.79,-132\"/>\n</g>\n</g>\n</svg>\n",
244
+ "text/plain": [
245
+ "<graphviz.graphs.Digraph at 0x7d5da98f4f40>"
246
+ ]
247
+ },
248
+ "metadata": {},
249
+ "execution_count": 6
250
+ }
251
+ ]
252
+ },
253
+ {
254
+ "cell_type": "markdown",
255
+ "source": [
256
+ "Now, we have to get the output i.e. by having the dot product with the activation function.\n",
257
+ "\n",
258
+ "So we have to implement the tanh function"
259
+ ],
260
+ "metadata": {
261
+ "id": "TrUjY3Xrw0TP"
262
+ }
263
+ },
264
+ {
265
+ "cell_type": "markdown",
266
+ "source": [
267
+ "Now, tanh is a hyperbolic expression. So it doesnt just contain +, -, it also has exponetials. So we have to create that function first in our Value object.\n",
268
+ "\n",
269
+ "&nbsp;\n",
270
+ "\n",
271
+ "![tanh equation](https://wikimedia.org/api/rest_v1/media/math/render/svg/b8dc4c309a551cafc2ce5c883c924ecd87664b0f)\n"
272
+ ],
273
+ "metadata": {
274
+ "id": "fd6oiWGaxEIb"
275
+ }
276
+ },
277
+ {
278
+ "cell_type": "markdown",
279
+ "source": [
280
+ "So now, lets update our Value object"
281
+ ],
282
+ "metadata": {
283
+ "id": "hAXPXTquy8KN"
284
+ }
285
+ },
286
+ {
287
+ "cell_type": "code",
288
+ "source": [
289
+ "import math"
290
+ ],
291
+ "metadata": {
292
+ "id": "JlYxBvFK0AjA"
293
+ },
294
+ "execution_count": 7,
295
+ "outputs": []
296
+ },
297
+ {
298
+ "cell_type": "code",
299
+ "source": [
300
+ "class Value:\n",
301
+ "\n",
302
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
303
+ " self.data = data\n",
304
+ " self.grad = 0.0\n",
305
+ " self._prev = set(_children)\n",
306
+ " self._op = _op\n",
307
+ " self.label = label\n",
308
+ "\n",
309
+ "\n",
310
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
311
+ " return f\"Value(data={self.data})\"\n",
312
+ "\n",
313
+ " def __add__(self, other):\n",
314
+ " out = Value(self.data + other.data, (self, other), '+')\n",
315
+ " return out\n",
316
+ "\n",
317
+ " def __mul__(self, other):\n",
318
+ " out = Value(self.data * other.data, (self, other), '*')\n",
319
+ " return out\n",
320
+ "\n",
321
+ " def tanh(self):\n",
322
+ " x = self.data\n",
323
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
324
+ " out = Value(t, (self, ), 'tanh')\n",
325
+ " return out"
326
+ ],
327
+ "metadata": {
328
+ "id": "4XPxg_t3wl35"
329
+ },
330
+ "execution_count": 8,
331
+ "outputs": []
332
+ },
333
+ {
334
+ "cell_type": "code",
335
+ "source": [
336
+ "#Inputs x1, x2 of the neuron\n",
337
+ "x1 = Value(2.0, label='x1')\n",
338
+ "x2 = Value(0.0, label='x2')\n",
339
+ "\n",
340
+ "#Weights w1, w2 of the neuron - The synaptic values\n",
341
+ "w1 = Value(-3.0, label='w1')\n",
342
+ "w2 = Value(1.0, label='w2')\n",
343
+ "\n",
344
+ "#The bias of the neuron\n",
345
+ "b = Value(6.7, label='b')\n",
346
+ "\n",
347
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
348
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
349
+ "\n",
350
+ "#The summation\n",
351
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'"
352
+ ],
353
+ "metadata": {
354
+ "id": "S3HaLbW_zvne"
355
+ },
356
+ "execution_count": 9,
357
+ "outputs": []
358
+ },
359
+ {
360
+ "cell_type": "code",
361
+ "source": [
362
+ "#n is basically the cell body, but without the activation function\n",
363
+ "n = x1w1x2w2 + b; n.label = 'n'\n",
364
+ "\n",
365
+ "#Now we pass n to the activation function\n",
366
+ "\n",
367
+ "o = n.tanh(); o.label = 'o'"
368
+ ],
369
+ "metadata": {
370
+ "id": "UuUWxrHQzzYG"
371
+ },
372
+ "execution_count": 10,
373
+ "outputs": []
374
+ },
375
+ {
376
+ "cell_type": "code",
377
+ "source": [
378
+ "draw_dot(o)"
379
+ ],
380
+ "metadata": {
381
+ "colab": {
382
+ "base_uri": "https://localhost:8080/",
383
+ "height": 322
384
+ },
385
+ "id": "j8zlrUnLz8F4",
386
+ "outputId": "9ea436d3-3701-4bb8-9fad-7dd9e14cbbe9"
387
+ },
388
+ "execution_count": 11,
389
+ "outputs": [
390
+ {
391
+ "output_type": "execute_result",
392
+ "data": {
393
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1575pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n<!-- 137841228343824 -->\n<g id=\"node1\" class=\"node\">\n<title>137841228343824</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228338448+ -->\n<g id=\"node12\" class=\"node\">\n<title>137841228338448+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 137841228343824&#45;&gt;137841228338448+ -->\n<g id=\"edge12\" class=\"edge\">\n<title>137841228343824&#45;&gt;137841228338448+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M941.24,-118.51C952.12,-120 962.4,-121.4 971.45,-122.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.11,-126.13 981.49,-124.01 972.05,-119.19 971.11,-126.13\"/>\n</g>\n<!-- 137841228343824+ -->\n<g id=\"node2\" class=\"node\">\n<title>137841228343824+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 137841228343824+&#45;&gt;137841228343824 -->\n<g id=\"edge1\" class=\"edge\">\n<title>137841228343824+&#45;&gt;137841228343824</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n<!-- 137841228346992 -->\n<g id=\"node3\" class=\"node\">\n<title>137841228346992</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 200,-91.5 200,-55.5 0,-55.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-55.5 33,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-55.5 118,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228340464* -->\n<g id=\"node10\" class=\"node\">\n<title>137841228340464*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 137841228346992&#45;&gt;137841228340464* -->\n<g id=\"edge10\" class=\"edge\">\n<title>137841228346992&#45;&gt;137841228340464*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M200.21,-73.5C209.2,-73.5 217.86,-73.5 225.7,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.85,-77 235.85,-73.5 225.85,-70 225.85,-77\"/>\n</g>\n<!-- 137841228344448 -->\n<g id=\"node4\" class=\"node\">\n<title>137841228344448</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-110.5 328.5,-146.5 543.5,-146.5 543.5,-110.5 328.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-110.5 381.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-110.5 461.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228344448&#45;&gt;137841228343824+ -->\n<g id=\"edge11\" class=\"edge\">\n<title>137841228344448&#45;&gt;137841228343824+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-111.01C554.01,-109.34 563.76,-107.74 572.44,-106.32\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.17,-109.75 582.47,-104.68 572.04,-102.84 573.17,-109.75\"/>\n</g>\n<!-- 137841228344448* -->\n<g id=\"node5\" class=\"node\">\n<title>137841228344448*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 137841228344448*&#45;&gt;137841228344448 -->\n<g id=\"edge2\" class=\"edge\">\n<title>137841228344448*&#45;&gt;137841228344448</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C298.51,-128.5 308.08,-128.5 318.36,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-132 328.39,-128.5 318.39,-125 318.39,-132\"/>\n</g>\n<!-- 137841228336768 -->\n<g id=\"node6\" class=\"node\">\n<title>137841228336768</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-165.5 4,-201.5 196,-201.5 196,-165.5 4,-165.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-165.5 34,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-165.5 114,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228336768&#45;&gt;137841228344448* -->\n<g id=\"edge7\" class=\"edge\">\n<title>137841228336768&#45;&gt;137841228344448*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 137841228335280 -->\n<g id=\"node7\" class=\"node\">\n<title>137841228335280</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-109.5 1382,-145.5 1567,-145.5 1567,-109.5 1382,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1393.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1405,-109.5 1405,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1445\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.6044</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1485,-109.5 1485,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1526\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228335280tanh -->\n<g id=\"node8\" class=\"node\">\n<title>137841228335280tanh</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n</g>\n<!-- 137841228335280tanh&#45;&gt;137841228335280 -->\n<g id=\"edge3\" class=\"edge\">\n<title>137841228335280tanh&#45;&gt;137841228335280</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-127.5C1353.58,-127.5 1362.3,-127.5 1371.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-131 1381.81,-127.5 1371.81,-124 1371.81,-131\"/>\n</g>\n<!-- 137841228340464 -->\n<g id=\"node9\" class=\"node\">\n<title>137841228340464</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-55.5 326,-91.5 546,-91.5 546,-55.5 326,-55.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-55.5 379,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-55.5 464,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228340464&#45;&gt;137841228343824+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>137841228340464&#45;&gt;137841228343824+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-90.75C555.64,-92.23 564.6,-93.65 572.65,-94.92\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.23,-98.4 582.65,-96.5 573.32,-91.48 572.23,-98.4\"/>\n</g>\n<!-- 137841228340464*&#45;&gt;137841228340464 -->\n<g id=\"edge4\" class=\"edge\">\n<title>137841228340464*&#45;&gt;137841228340464</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C297.77,-73.5 306.37,-73.5 315.6,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-77 325.84,-73.5 315.84,-70 315.84,-77\"/>\n</g>\n<!-- 137841228338448 -->\n<g id=\"node11\" class=\"node\">\n<title>137841228338448</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-109.5 1071,-145.5 1256,-145.5 1256,-109.5 1071,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-109.5 1094,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-109.5 1174,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228338448&#45;&gt;137841228335280tanh -->\n<g id=\"edge8\" class=\"edge\">\n<title>137841228338448&#45;&gt;137841228335280tanh</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-127.5C1265.01,-127.5 1273.74,-127.5 1281.66,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-131 1291.91,-127.5 1281.91,-124 1281.91,-131\"/>\n</g>\n<!-- 137841228338448+&#45;&gt;137841228338448 -->\n<g id=\"edge5\" class=\"edge\">\n<title>137841228338448+&#45;&gt;137841228338448</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-127.5C1042.58,-127.5 1051.3,-127.5 1060.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-131 1070.81,-127.5 1060.81,-124 1060.81,-131\"/>\n</g>\n<!-- 137841228340560 -->\n<g id=\"node13\" class=\"node\">\n<title>137841228340560</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-0.5 4,-36.5 196,-36.5 196,-0.5 4,-0.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-0.5 34,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-0.5 114,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228340560&#45;&gt;137841228340464* -->\n<g id=\"edge13\" class=\"edge\">\n<title>137841228340560&#45;&gt;137841228340464*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 137841228348240 -->\n<g id=\"node14\" class=\"node\">\n<title>137841228348240</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-137.5 716,-173.5 901,-173.5 901,-137.5 716,-137.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-137.5 739,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.7000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-137.5 819,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228348240&#45;&gt;137841228338448+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>137841228348240&#45;&gt;137841228338448+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-142.52C926,-138.98 951.59,-135.36 971.37,-132.55\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-135.99 981.43,-131.12 971.04,-129.06 972.03,-135.99\"/>\n</g>\n<!-- 137841228344736 -->\n<g id=\"node15\" class=\"node\">\n<title>137841228344736</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-110.5 2.5,-146.5 197.5,-146.5 197.5,-110.5 2.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-110.5 35.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-110.5 115.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 137841228344736&#45;&gt;137841228344448* -->\n<g id=\"edge6\" class=\"edge\">\n<title>137841228344736&#45;&gt;137841228344448*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-128.5C207.65,-128.5 217.05,-128.5 225.52,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-132 235.7,-128.5 225.7,-125 225.7,-132\"/>\n</g>\n</g>\n</svg>\n",
394
+ "text/plain": [
395
+ "<graphviz.graphs.Digraph at 0x7d5da9737430>"
396
+ ]
397
+ },
398
+ "metadata": {},
399
+ "execution_count": 11
400
+ }
401
+ ]
402
+ },
403
+ {
404
+ "cell_type": "markdown",
405
+ "source": [
406
+ "We have recieved that output. So now, tanh is our little 'micrograd supported' node here, as an operation :)"
407
+ ],
408
+ "metadata": {
409
+ "id": "wMbP3alD0QYV"
410
+ }
411
+ }
412
+ ]
413
+ }
6_1_backpropagation_neuron_manual_calculation.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
7_0_backward_func_each_operation.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
7_1_backward_func_entire_graph.ipynb ADDED
@@ -0,0 +1,529 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "from graphviz import Digraph\n",
21
+ "\n",
22
+ "def trace(root):\n",
23
+ " #Builds a set of all nodes and edges in a graph\n",
24
+ " nodes, edges = set(), set()\n",
25
+ " def build(v):\n",
26
+ " if v not in nodes:\n",
27
+ " nodes.add(v)\n",
28
+ " for child in v._prev:\n",
29
+ " edges.add((child, v))\n",
30
+ " build(child)\n",
31
+ " build(root)\n",
32
+ " return nodes, edges\n",
33
+ "\n",
34
+ "def draw_dot(root):\n",
35
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
36
+ "\n",
37
+ " nodes, edges = trace(root)\n",
38
+ " for n in nodes:\n",
39
+ " uid = str(id(n))\n",
40
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
41
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
42
+ " if n._op:\n",
43
+ " #If this value is a result of some operation, then create an op node for it\n",
44
+ " dot.node(name = uid + n._op, label=n._op)\n",
45
+ " #and connect this node to it\n",
46
+ " dot.edge(uid + n._op, uid)\n",
47
+ "\n",
48
+ " for n1, n2 in edges:\n",
49
+ " #Connect n1 to the node of n2\n",
50
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
51
+ "\n",
52
+ " return dot"
53
+ ],
54
+ "metadata": {
55
+ "id": "T0rN8d146jvF"
56
+ },
57
+ "execution_count": 1,
58
+ "outputs": []
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "source": [
63
+ "import math"
64
+ ],
65
+ "metadata": {
66
+ "id": "JlYxBvFK0AjA"
67
+ },
68
+ "execution_count": 2,
69
+ "outputs": []
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "source": [
74
+ "class Value:\n",
75
+ "\n",
76
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
77
+ " self.data = data\n",
78
+ " self.grad = 0.0\n",
79
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
80
+ " self._prev = set(_children)\n",
81
+ " self._op = _op\n",
82
+ " self.label = label\n",
83
+ "\n",
84
+ "\n",
85
+ " def __repr__(self): # This basically allows us to print nicer looking expressions for the final output\n",
86
+ " return f\"Value(data={self.data})\"\n",
87
+ "\n",
88
+ " def __add__(self, other):\n",
89
+ " out = Value(self.data + other.data, (self, other), '+')\n",
90
+ "\n",
91
+ " def backward():\n",
92
+ " self.grad = 1.0 * out.grad #Remember we are doing chain rule here, hence the product with out.grad\n",
93
+ " other.grad = 1.0 * out.grad\n",
94
+ "\n",
95
+ " out._backward = backward\n",
96
+ " return out\n",
97
+ "\n",
98
+ " def __mul__(self, other):\n",
99
+ " out = Value(self.data * other.data, (self, other), '*')\n",
100
+ "\n",
101
+ " def backward():\n",
102
+ " self.grad = other.data * out.grad #Remember we are doing chain rule here, hence the product with out.grad\n",
103
+ " other.grad = self.data * out.grad\n",
104
+ "\n",
105
+ " out._backward = backward\n",
106
+ " return out\n",
107
+ "\n",
108
+ " def tanh(self):\n",
109
+ " x = self.data\n",
110
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
111
+ " out = Value(t, (self, ), 'tanh')\n",
112
+ "\n",
113
+ " def backward():\n",
114
+ " self.grad = 1 - (t**2) * out.grad #Remember we are doing chain rule here, hence the product with out.grad\n",
115
+ "\n",
116
+ " out._backward = backward\n",
117
+ " return out"
118
+ ],
119
+ "metadata": {
120
+ "id": "4XPxg_t3wl35"
121
+ },
122
+ "execution_count": 3,
123
+ "outputs": []
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "source": [
128
+ "#Inputs x1, x2 of the neuron\n",
129
+ "x1 = Value(2.0, label='x1')\n",
130
+ "x2 = Value(0.0, label='x2')\n",
131
+ "\n",
132
+ "#Weights w1, w2 of the neuron - The synaptic values\n",
133
+ "w1 = Value(-3.0, label='w1')\n",
134
+ "w2 = Value(1.0, label='w2')\n",
135
+ "\n",
136
+ "#The bias of the neuron\n",
137
+ "b = Value(6.8813735870195432, label='b')\n",
138
+ "\n",
139
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
140
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
141
+ "\n",
142
+ "#The summation\n",
143
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'\n",
144
+ "\n",
145
+ "#n is basically the cell body, but without the activation function\n",
146
+ "n = x1w1x2w2 + b; n.label = 'n'\n",
147
+ "\n",
148
+ "#Now we pass n to the activation function\n",
149
+ "o = n.tanh(); o.label = 'o'"
150
+ ],
151
+ "metadata": {
152
+ "id": "S3HaLbW_zvne"
153
+ },
154
+ "execution_count": 14,
155
+ "outputs": []
156
+ },
157
+ {
158
+ "cell_type": "code",
159
+ "source": [
160
+ "o.grad = 1.0 #This is the base case, we set this"
161
+ ],
162
+ "metadata": {
163
+ "id": "WJyfRZOvCBlp"
164
+ },
165
+ "execution_count": 5,
166
+ "outputs": []
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "source": [
171
+ "draw_dot(o)"
172
+ ],
173
+ "metadata": {
174
+ "colab": {
175
+ "base_uri": "https://localhost:8080/",
176
+ "height": 322
177
+ },
178
+ "id": "QhJO9xrb-Ius",
179
+ "outputId": "c923ed21-13d0-4a4d-d470-89b0d767e822"
180
+ },
181
+ "execution_count": 6,
182
+ "outputs": [
183
+ {
184
+ "output_type": "execute_result",
185
+ "data": {
186
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1575pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n<!-- 132397001389632 -->\n<g id=\"node1\" class=\"node\">\n<title>132397001389632</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-165.5 0,-201.5 200,-201.5 200,-165.5 0,-165.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-165.5 33,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-165.5 118,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003754704* -->\n<g id=\"node7\" class=\"node\">\n<title>132397003754704*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397001389632&#45;&gt;132397003754704* -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397001389632&#45;&gt;132397003754704*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 132397004820576 -->\n<g id=\"node2\" class=\"node\">\n<title>132397004820576</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-54.5 1071,-90.5 1256,-90.5 1256,-54.5 1071,-54.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-54.5 1094,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-54.5 1174,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397004827872tanh -->\n<g id=\"node9\" class=\"node\">\n<title>132397004827872tanh</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n</g>\n<!-- 132397004820576&#45;&gt;132397004827872tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397004820576&#45;&gt;132397004827872tanh</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-72.5C1265.01,-72.5 1273.74,-72.5 1281.66,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-76 1291.91,-72.5 1281.91,-69 1281.91,-76\"/>\n</g>\n<!-- 132397004820576+ -->\n<g id=\"node3\" class=\"node\">\n<title>132397004820576+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397004820576+&#45;&gt;132397004820576 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397004820576+&#45;&gt;132397004820576</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-72.5C1042.58,-72.5 1051.3,-72.5 1060.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-76 1070.81,-72.5 1060.81,-69 1060.81,-76\"/>\n</g>\n<!-- 132397001386128 -->\n<g id=\"node4\" class=\"node\">\n<title>132397001386128</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-55.5 2.5,-91.5 197.5,-91.5 197.5,-55.5 2.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-55.5 35.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-55.5 115.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003754752* -->\n<g id=\"node11\" class=\"node\">\n<title>132397003754752*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397001386128&#45;&gt;132397003754752* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397001386128&#45;&gt;132397003754752*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-73.5C207.65,-73.5 217.05,-73.5 225.52,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-77 235.7,-73.5 225.7,-70 225.7,-77\"/>\n</g>\n<!-- 132397003754176 -->\n<g id=\"node5\" class=\"node\">\n<title>132397003754176</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-27.5 716,-63.5 901,-63.5 901,-27.5 716,-27.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-27.5 739,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-27.5 819,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003754176&#45;&gt;132397004820576+ -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003754176&#45;&gt;132397004820576+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-58.01C926,-61.43 951.59,-64.93 971.37,-67.63\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.05,-71.12 981.43,-69.01 972,-64.18 971.05,-71.12\"/>\n</g>\n<!-- 132397003754704 -->\n<g id=\"node6\" class=\"node\">\n<title>132397003754704</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-110.5 326,-146.5 546,-146.5 546,-110.5 326,-110.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-110.5 379,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-110.5 464,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003766752+ -->\n<g id=\"node15\" class=\"node\">\n<title>132397003766752+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397003754704&#45;&gt;132397003766752+ -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003754704&#45;&gt;132397003766752+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-110.61C555.64,-109.07 564.6,-107.61 572.65,-106.29\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.35,-109.72 582.65,-104.65 572.22,-102.81 573.35,-109.72\"/>\n</g>\n<!-- 132397003754704*&#45;&gt;132397003754704 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397003754704*&#45;&gt;132397003754704</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C297.77,-128.5 306.37,-128.5 315.6,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-132 325.84,-128.5 315.84,-125 315.84,-132\"/>\n</g>\n<!-- 132397004827872 -->\n<g id=\"node8\" class=\"node\">\n<title>132397004827872</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-54.5 1382,-90.5 1567,-90.5 1567,-54.5 1382,-54.5\"/>\n<text text-anchor=\"middle\" x=\"1393.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1405,-54.5 1405,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1445\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1485,-54.5 1485,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1526\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132397004827872tanh&#45;&gt;132397004827872 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397004827872tanh&#45;&gt;132397004827872</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-72.5C1353.58,-72.5 1362.3,-72.5 1371.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-76 1381.81,-72.5 1371.81,-69 1371.81,-76\"/>\n</g>\n<!-- 132397003754752 -->\n<g id=\"node10\" class=\"node\">\n<title>132397003754752</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-55.5 328.5,-91.5 543.5,-91.5 543.5,-55.5 328.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-55.5 381.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-55.5 461.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003754752&#45;&gt;132397003766752+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003754752&#45;&gt;132397003766752+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-90.37C554.01,-91.98 563.76,-93.51 572.44,-94.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.05,-98.37 582.47,-96.47 573.14,-91.45 572.05,-98.37\"/>\n</g>\n<!-- 132397003754752*&#45;&gt;132397003754752 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397003754752*&#45;&gt;132397003754752</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C298.51,-73.5 308.08,-73.5 318.36,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-77 328.39,-73.5 318.39,-70 318.39,-77\"/>\n</g>\n<!-- 132397001390400 -->\n<g id=\"node12\" class=\"node\">\n<title>132397001390400</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-0.5 4,-36.5 196,-36.5 196,-0.5 4,-0.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-0.5 34,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-0.5 114,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397001390400&#45;&gt;132397003754752* -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397001390400&#45;&gt;132397003754752*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 132397001385312 -->\n<g id=\"node13\" class=\"node\">\n<title>132397001385312</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-110.5 4,-146.5 196,-146.5 196,-110.5 4,-110.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-110.5 34,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-110.5 114,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397001385312&#45;&gt;132397003754704* -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397001385312&#45;&gt;132397003754704*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M196.05,-128.5C206.52,-128.5 216.65,-128.5 225.71,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.79,-132 235.79,-128.5 225.79,-125 225.79,-132\"/>\n</g>\n<!-- 132397003766752 -->\n<g id=\"node14\" class=\"node\">\n<title>132397003766752</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003766752&#45;&gt;132397004820576+ -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003766752&#45;&gt;132397004820576+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M936.53,-82.49C949.09,-80.71 960.99,-79.02 971.3,-77.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-80.99 981.44,-76.12 971.05,-74.06 972.03,-80.99\"/>\n</g>\n<!-- 132397003766752+&#45;&gt;132397003766752 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397003766752+&#45;&gt;132397003766752</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n</g>\n</svg>\n",
187
+ "text/plain": [
188
+ "<graphviz.graphs.Digraph at 0x786a1460c040>"
189
+ ]
190
+ },
191
+ "metadata": {},
192
+ "execution_count": 6
193
+ }
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "markdown",
198
+ "source": [
199
+ "-----------"
200
+ ],
201
+ "metadata": {
202
+ "id": "lbX2sHA9AN67"
203
+ }
204
+ },
205
+ {
206
+ "cell_type": "markdown",
207
+ "source": [
208
+ "**Instead of calling the '_ backward' function each time, we are creating it as a function in the Value object itself.**"
209
+ ],
210
+ "metadata": {
211
+ "id": "UapnDEisAcZF"
212
+ }
213
+ },
214
+ {
215
+ "cell_type": "markdown",
216
+ "source": [
217
+ "Now, we also need to make sure that all the nodes have been accessed and forward pass through. So, we have used a concept called 'topological sort' where all the nodes are accessed/traversed in the same/one direction (either left-to-right or vice-versa)\n",
218
+ "![See the image here](https://miro.medium.com/v2/resize:fit:828/format:webp/1*uMg_ojFXts2WZSjcZe4oRQ.png)\n",
219
+ "\n",
220
+ "Therefore, we are adding the code for it, where it ensures that all the nodes are accessed at least once (and only stored once) and the node is only stored after/when all of it's child nodes are accessed and stored. This way we know we have traversed through the entire graph."
221
+ ],
222
+ "metadata": {
223
+ "id": "33aDIzRvAOm0"
224
+ }
225
+ },
226
+ {
227
+ "cell_type": "code",
228
+ "source": [
229
+ "topo = []\n",
230
+ "visited = set() #We are maintaining a set of visited nodes\n",
231
+ "\n",
232
+ "def build_topo(v):\n",
233
+ "\n",
234
+ " if v not in visited:\n",
235
+ " visited.add(v)\n",
236
+ "\n",
237
+ " for child in v._prev:\n",
238
+ " build_topo(child)\n",
239
+ "\n",
240
+ " topo.append(v) #Adds itself, only after all its children have been added\n",
241
+ "\n",
242
+ "build_topo(o) #We are starting from the root node, for us 'o'\n",
243
+ "topo"
244
+ ],
245
+ "metadata": {
246
+ "colab": {
247
+ "base_uri": "https://localhost:8080/"
248
+ },
249
+ "id": "GyqAKGDrATCU",
250
+ "outputId": "e1502c86-e5d8-4173-bb60-f1cd1f008cec"
251
+ },
252
+ "execution_count": 7,
253
+ "outputs": [
254
+ {
255
+ "output_type": "execute_result",
256
+ "data": {
257
+ "text/plain": [
258
+ "[Value(data=6.881373587019543),\n",
259
+ " Value(data=1.0),\n",
260
+ " Value(data=0.0),\n",
261
+ " Value(data=0.0),\n",
262
+ " Value(data=-3.0),\n",
263
+ " Value(data=2.0),\n",
264
+ " Value(data=-6.0),\n",
265
+ " Value(data=-6.0),\n",
266
+ " Value(data=0.8813735870195432),\n",
267
+ " Value(data=0.7071067811865476)]"
268
+ ]
269
+ },
270
+ "metadata": {},
271
+ "execution_count": 7
272
+ }
273
+ ]
274
+ },
275
+ {
276
+ "cell_type": "markdown",
277
+ "source": [
278
+ "Once all the nodes have been topologically sorted, we then reverse the nodes order (Since we are traversing it from left to right i.e. input to output, we are reversing it, so that the gradients are calculated. As we have done previously in our examples) call the '_ backward' function to perform backpropagation from the output."
279
+ ],
280
+ "metadata": {
281
+ "id": "YAxUnrVQAX6c"
282
+ }
283
+ },
284
+ {
285
+ "cell_type": "code",
286
+ "source": [
287
+ "for node in reversed(topo):\n",
288
+ " node._backward()"
289
+ ],
290
+ "metadata": {
291
+ "id": "drPd-zukAYtV"
292
+ },
293
+ "execution_count": 8,
294
+ "outputs": []
295
+ },
296
+ {
297
+ "cell_type": "markdown",
298
+ "source": [
299
+ "And now, we will add all the calculated gradients to the graph!"
300
+ ],
301
+ "metadata": {
302
+ "id": "66r_NGglCW-j"
303
+ }
304
+ },
305
+ {
306
+ "cell_type": "code",
307
+ "source": [
308
+ "draw_dot(o)"
309
+ ],
310
+ "metadata": {
311
+ "colab": {
312
+ "base_uri": "https://localhost:8080/",
313
+ "height": 322
314
+ },
315
+ "id": "G5IE00IVCVyJ",
316
+ "outputId": "fa67f40b-9803-4036-ede2-49094d23afa8"
317
+ },
318
+ "execution_count": 9,
319
+ "outputs": [
320
+ {
321
+ "output_type": "execute_result",
322
+ "data": {
323
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1575pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n<!-- 132397001389632 -->\n<g id=\"node1\" class=\"node\">\n<title>132397001389632</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-165.5 0,-201.5 200,-201.5 200,-165.5 0,-165.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-165.5 33,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-165.5 118,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132397003754704* -->\n<g id=\"node7\" class=\"node\">\n<title>132397003754704*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397001389632&#45;&gt;132397003754704* -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397001389632&#45;&gt;132397003754704*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 132397004820576 -->\n<g id=\"node2\" class=\"node\">\n<title>132397004820576</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-54.5 1071,-90.5 1256,-90.5 1256,-54.5 1071,-54.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-54.5 1094,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-54.5 1174,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397004827872tanh -->\n<g id=\"node9\" class=\"node\">\n<title>132397004827872tanh</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n</g>\n<!-- 132397004820576&#45;&gt;132397004827872tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397004820576&#45;&gt;132397004827872tanh</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-72.5C1265.01,-72.5 1273.74,-72.5 1281.66,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-76 1291.91,-72.5 1281.91,-69 1281.91,-76\"/>\n</g>\n<!-- 132397004820576+ -->\n<g id=\"node3\" class=\"node\">\n<title>132397004820576+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397004820576+&#45;&gt;132397004820576 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397004820576+&#45;&gt;132397004820576</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-72.5C1042.58,-72.5 1051.3,-72.5 1060.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-76 1070.81,-72.5 1060.81,-69 1060.81,-76\"/>\n</g>\n<!-- 132397001386128 -->\n<g id=\"node4\" class=\"node\">\n<title>132397001386128</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-55.5 2.5,-91.5 197.5,-91.5 197.5,-55.5 2.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-55.5 35.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-55.5 115.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003754752* -->\n<g id=\"node11\" class=\"node\">\n<title>132397003754752*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397001386128&#45;&gt;132397003754752* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397001386128&#45;&gt;132397003754752*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-73.5C207.65,-73.5 217.05,-73.5 225.52,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-77 235.7,-73.5 225.7,-70 225.7,-77\"/>\n</g>\n<!-- 132397003754176 -->\n<g id=\"node5\" class=\"node\">\n<title>132397003754176</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-27.5 716,-63.5 901,-63.5 901,-27.5 716,-27.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-27.5 739,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-27.5 819,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397003754176&#45;&gt;132397004820576+ -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003754176&#45;&gt;132397004820576+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-58.01C926,-61.43 951.59,-64.93 971.37,-67.63\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.05,-71.12 981.43,-69.01 972,-64.18 971.05,-71.12\"/>\n</g>\n<!-- 132397003754704 -->\n<g id=\"node6\" class=\"node\">\n<title>132397003754704</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-110.5 326,-146.5 546,-146.5 546,-110.5 326,-110.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-110.5 379,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-110.5 464,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397003766752+ -->\n<g id=\"node15\" class=\"node\">\n<title>132397003766752+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397003754704&#45;&gt;132397003766752+ -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003754704&#45;&gt;132397003766752+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-110.61C555.64,-109.07 564.6,-107.61 572.65,-106.29\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.35,-109.72 582.65,-104.65 572.22,-102.81 573.35,-109.72\"/>\n</g>\n<!-- 132397003754704*&#45;&gt;132397003754704 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397003754704*&#45;&gt;132397003754704</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C297.77,-128.5 306.37,-128.5 315.6,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-132 325.84,-128.5 315.84,-125 315.84,-132\"/>\n</g>\n<!-- 132397004827872 -->\n<g id=\"node8\" class=\"node\">\n<title>132397004827872</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-54.5 1382,-90.5 1567,-90.5 1567,-54.5 1382,-54.5\"/>\n<text text-anchor=\"middle\" x=\"1393.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1405,-54.5 1405,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1445\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1485,-54.5 1485,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1526\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132397004827872tanh&#45;&gt;132397004827872 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397004827872tanh&#45;&gt;132397004827872</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-72.5C1353.58,-72.5 1362.3,-72.5 1371.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-76 1381.81,-72.5 1371.81,-69 1371.81,-76\"/>\n</g>\n<!-- 132397003754752 -->\n<g id=\"node10\" class=\"node\">\n<title>132397003754752</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-55.5 328.5,-91.5 543.5,-91.5 543.5,-55.5 328.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-55.5 381.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-55.5 461.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397003754752&#45;&gt;132397003766752+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003754752&#45;&gt;132397003766752+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-90.37C554.01,-91.98 563.76,-93.51 572.44,-94.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.05,-98.37 582.47,-96.47 573.14,-91.45 572.05,-98.37\"/>\n</g>\n<!-- 132397003754752*&#45;&gt;132397003754752 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397003754752*&#45;&gt;132397003754752</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C298.51,-73.5 308.08,-73.5 318.36,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-77 328.39,-73.5 318.39,-70 318.39,-77\"/>\n</g>\n<!-- 132397001390400 -->\n<g id=\"node12\" class=\"node\">\n<title>132397001390400</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-0.5 4,-36.5 196,-36.5 196,-0.5 4,-0.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-0.5 34,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-0.5 114,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397001390400&#45;&gt;132397003754752* -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397001390400&#45;&gt;132397003754752*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 132397001385312 -->\n<g id=\"node13\" class=\"node\">\n<title>132397001385312</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2,-110.5 2,-146.5 198,-146.5 198,-110.5 2,-110.5\"/>\n<text text-anchor=\"middle\" x=\"17\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"32,-110.5 32,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"72\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"112,-110.5 112,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;1.5000</text>\n</g>\n<!-- 132397001385312&#45;&gt;132397003754704* -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397001385312&#45;&gt;132397003754704*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M198.37,-128.5C208.05,-128.5 217.4,-128.5 225.8,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.91,-132 235.91,-128.5 225.91,-125 225.91,-132\"/>\n</g>\n<!-- 132397003766752 -->\n<g id=\"node14\" class=\"node\">\n<title>132397003766752</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397003766752&#45;&gt;132397004820576+ -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003766752&#45;&gt;132397004820576+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M936.53,-82.49C949.09,-80.71 960.99,-79.02 971.3,-77.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-80.99 981.44,-76.12 971.05,-74.06 972.03,-80.99\"/>\n</g>\n<!-- 132397003766752+&#45;&gt;132397003766752 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397003766752+&#45;&gt;132397003766752</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n</g>\n</svg>\n",
324
+ "text/plain": [
325
+ "<graphviz.graphs.Digraph at 0x786a4a853580>"
326
+ ]
327
+ },
328
+ "metadata": {},
329
+ "execution_count": 9
330
+ }
331
+ ]
332
+ },
333
+ {
334
+ "cell_type": "markdown",
335
+ "source": [
336
+ "---------------------------------"
337
+ ],
338
+ "metadata": {
339
+ "id": "ZO28CHT6CgBt"
340
+ }
341
+ },
342
+ {
343
+ "cell_type": "markdown",
344
+ "source": [
345
+ "## **Now, we add this functionality to our value object**"
346
+ ],
347
+ "metadata": {
348
+ "id": "Ti2oC9OQCg_d"
349
+ }
350
+ },
351
+ {
352
+ "cell_type": "code",
353
+ "source": [
354
+ "import math"
355
+ ],
356
+ "metadata": {
357
+ "id": "pWY4aqA8CocW"
358
+ },
359
+ "execution_count": null,
360
+ "outputs": []
361
+ },
362
+ {
363
+ "cell_type": "code",
364
+ "source": [
365
+ "class Value:\n",
366
+ "\n",
367
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
368
+ " self.data = data\n",
369
+ " self.grad = 0.0\n",
370
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
371
+ " self._prev = set(_children)\n",
372
+ " self._op = _op\n",
373
+ " self.label = label\n",
374
+ "\n",
375
+ "\n",
376
+ " def __repr__(self):\n",
377
+ " return f\"Value(data={self.data})\"\n",
378
+ "\n",
379
+ " def __add__(self, other):\n",
380
+ " out = Value(self.data + other.data, (self, other), '+')\n",
381
+ "\n",
382
+ " def backward():\n",
383
+ " self.grad = 1.0 * out.grad\n",
384
+ " other.grad = 1.0 * out.grad\n",
385
+ "\n",
386
+ " out._backward = backward\n",
387
+ " return out\n",
388
+ "\n",
389
+ " def __mul__(self, other):\n",
390
+ " out = Value(self.data * other.data, (self, other), '*')\n",
391
+ "\n",
392
+ " def backward():\n",
393
+ " self.grad = other.data * out.grad\n",
394
+ " other.grad = self.data * out.grad\n",
395
+ "\n",
396
+ " out._backward = backward\n",
397
+ " return out\n",
398
+ "\n",
399
+ " def tanh(self):\n",
400
+ " x = self.data\n",
401
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
402
+ " out = Value(t, (self, ), 'tanh')\n",
403
+ "\n",
404
+ " def backward():\n",
405
+ " self.grad = 1 - (t**2) * out.grad\n",
406
+ "\n",
407
+ " out._backward = backward\n",
408
+ " return out\n",
409
+ "\n",
410
+ " def backward(self):\n",
411
+ "\n",
412
+ " topo = []\n",
413
+ " visited = set()\n",
414
+ " def build_topo(v):\n",
415
+ " if v not in visited:\n",
416
+ " visited.add(v)\n",
417
+ " for child in v._prev:\n",
418
+ " build_topo(child)\n",
419
+ " topo.append(v)\n",
420
+ "\n",
421
+ " build_topo(self)\n",
422
+ "\n",
423
+ " self.grad = 1.0\n",
424
+ " for node in reversed(topo):\n",
425
+ " node._backward()"
426
+ ],
427
+ "metadata": {
428
+ "id": "5g8aT0yjCeY1"
429
+ },
430
+ "execution_count": 16,
431
+ "outputs": []
432
+ },
433
+ {
434
+ "cell_type": "markdown",
435
+ "source": [
436
+ "**This is the updated value object^**"
437
+ ],
438
+ "metadata": {
439
+ "id": "aj0FopmoDf4M"
440
+ }
441
+ },
442
+ {
443
+ "cell_type": "markdown",
444
+ "source": [
445
+ "---------"
446
+ ],
447
+ "metadata": {
448
+ "id": "iUJHQaBXDlz8"
449
+ }
450
+ },
451
+ {
452
+ "cell_type": "markdown",
453
+ "source": [
454
+ "Cross-verifying it once"
455
+ ],
456
+ "metadata": {
457
+ "id": "k1LaYr_oDjic"
458
+ }
459
+ },
460
+ {
461
+ "cell_type": "code",
462
+ "source": [
463
+ "#I re-run the cells containing the node values as well\n",
464
+ "draw_dot(o)"
465
+ ],
466
+ "metadata": {
467
+ "colab": {
468
+ "base_uri": "https://localhost:8080/",
469
+ "height": 322
470
+ },
471
+ "id": "SPvXbZ-zDmsE",
472
+ "outputId": "285075bc-a1ce-4ad7-bf2f-00e524611495"
473
+ },
474
+ "execution_count": 17,
475
+ "outputs": [
476
+ {
477
+ "output_type": "execute_result",
478
+ "data": {
479
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1575pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n<!-- 132397006959184 -->\n<g id=\"node1\" class=\"node\">\n<title>132397006959184</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-109.5 1382,-145.5 1567,-145.5 1567,-109.5 1382,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1393.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1405,-109.5 1405,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1445\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1485,-109.5 1485,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1526\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397006959184tanh -->\n<g id=\"node2\" class=\"node\">\n<title>132397006959184tanh</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n</g>\n<!-- 132397006959184tanh&#45;&gt;132397006959184 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397006959184tanh&#45;&gt;132397006959184</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-127.5C1353.58,-127.5 1362.3,-127.5 1371.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-131 1381.81,-127.5 1371.81,-124 1371.81,-131\"/>\n</g>\n<!-- 132397003209840 -->\n<g id=\"node3\" class=\"node\">\n<title>132397003209840</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-165.5 4,-201.5 196,-201.5 196,-165.5 4,-165.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-165.5 34,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-165.5 114,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397006954336* -->\n<g id=\"node8\" class=\"node\">\n<title>132397006954336*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397003209840&#45;&gt;132397006954336* -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003209840&#45;&gt;132397006954336*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 132397003210368 -->\n<g id=\"node4\" class=\"node\">\n<title>132397003210368</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-110.5 2.5,-146.5 197.5,-146.5 197.5,-110.5 2.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-110.5 35.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-110.5 115.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003210368&#45;&gt;132397006954336* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397003210368&#45;&gt;132397006954336*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-128.5C207.65,-128.5 217.05,-128.5 225.52,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-132 235.7,-128.5 225.7,-125 225.7,-132\"/>\n</g>\n<!-- 132397003199232 -->\n<g id=\"node5\" class=\"node\">\n<title>132397003199232</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-137.5 716,-173.5 901,-173.5 901,-137.5 716,-137.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-137.5 739,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-137.5 819,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397006954480+ -->\n<g id=\"node15\" class=\"node\">\n<title>132397006954480+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397003199232&#45;&gt;132397006954480+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003199232&#45;&gt;132397006954480+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-142.52C926,-138.98 951.59,-135.36 971.37,-132.55\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-135.99 981.43,-131.12 971.04,-129.06 972.03,-135.99\"/>\n</g>\n<!-- 132397003199280 -->\n<g id=\"node6\" class=\"node\">\n<title>132397003199280</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-55.5 4,-91.5 196,-91.5 196,-55.5 4,-55.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-55.5 34,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-55.5 114,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397004831184* -->\n<g id=\"node13\" class=\"node\">\n<title>132397004831184*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397003199280&#45;&gt;132397004831184* -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003199280&#45;&gt;132397004831184*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M196.05,-73.5C206.52,-73.5 216.65,-73.5 225.71,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.79,-77 235.79,-73.5 225.79,-70 225.79,-77\"/>\n</g>\n<!-- 132397006954336 -->\n<g id=\"node7\" class=\"node\">\n<title>132397006954336</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-110.5 328.5,-146.5 543.5,-146.5 543.5,-110.5 328.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-110.5 381.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-110.5 461.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397006953856+ -->\n<g id=\"node10\" class=\"node\">\n<title>132397006953856+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397006954336&#45;&gt;132397006953856+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397006954336&#45;&gt;132397006953856+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-111.01C554.01,-109.34 563.76,-107.74 572.44,-106.32\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.17,-109.75 582.47,-104.68 572.04,-102.84 573.17,-109.75\"/>\n</g>\n<!-- 132397006954336*&#45;&gt;132397006954336 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397006954336*&#45;&gt;132397006954336</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C298.51,-128.5 308.08,-128.5 318.36,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-132 328.39,-128.5 318.39,-125 318.39,-132\"/>\n</g>\n<!-- 132397006953856 -->\n<g id=\"node9\" class=\"node\">\n<title>132397006953856</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397006953856&#45;&gt;132397006954480+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397006953856&#45;&gt;132397006954480+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M941.24,-118.51C952.12,-120 962.4,-121.4 971.45,-122.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.11,-126.13 981.49,-124.01 972.05,-119.19 971.11,-126.13\"/>\n</g>\n<!-- 132397006953856+&#45;&gt;132397006953856 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397006953856+&#45;&gt;132397006953856</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n<!-- 132397003200912 -->\n<g id=\"node11\" class=\"node\">\n<title>132397003200912</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 200,-36.5 200,-0.5 0,-0.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-0.5 33,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-0.5 118,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003200912&#45;&gt;132397004831184* -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003200912&#45;&gt;132397004831184*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 132397004831184 -->\n<g id=\"node12\" class=\"node\">\n<title>132397004831184</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-55.5 326,-91.5 546,-91.5 546,-55.5 326,-55.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-55.5 379,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-55.5 464,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397004831184&#45;&gt;132397006953856+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397004831184&#45;&gt;132397006953856+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-90.75C555.64,-92.23 564.6,-93.65 572.65,-94.92\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.23,-98.4 582.65,-96.5 573.32,-91.48 572.23,-98.4\"/>\n</g>\n<!-- 132397004831184*&#45;&gt;132397004831184 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397004831184*&#45;&gt;132397004831184</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C297.77,-73.5 306.37,-73.5 315.6,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-77 325.84,-73.5 315.84,-70 315.84,-77\"/>\n</g>\n<!-- 132397006954480 -->\n<g id=\"node14\" class=\"node\">\n<title>132397006954480</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-109.5 1071,-145.5 1256,-145.5 1256,-109.5 1071,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-109.5 1094,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-109.5 1174,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397006954480&#45;&gt;132397006959184tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397006954480&#45;&gt;132397006959184tanh</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-127.5C1265.01,-127.5 1273.74,-127.5 1281.66,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-131 1291.91,-127.5 1281.91,-124 1281.91,-131\"/>\n</g>\n<!-- 132397006954480+&#45;&gt;132397006954480 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397006954480+&#45;&gt;132397006954480</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-127.5C1042.58,-127.5 1051.3,-127.5 1060.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-131 1070.81,-127.5 1060.81,-124 1060.81,-131\"/>\n</g>\n</g>\n</svg>\n",
480
+ "text/plain": [
481
+ "<graphviz.graphs.Digraph at 0x786a142ad240>"
482
+ ]
483
+ },
484
+ "metadata": {},
485
+ "execution_count": 17
486
+ }
487
+ ]
488
+ },
489
+ {
490
+ "cell_type": "code",
491
+ "source": [
492
+ "o.backward()"
493
+ ],
494
+ "metadata": {
495
+ "id": "vcJmDVMxEPFJ"
496
+ },
497
+ "execution_count": 18,
498
+ "outputs": []
499
+ },
500
+ {
501
+ "cell_type": "code",
502
+ "source": [
503
+ "draw_dot(o)"
504
+ ],
505
+ "metadata": {
506
+ "colab": {
507
+ "base_uri": "https://localhost:8080/",
508
+ "height": 322
509
+ },
510
+ "id": "weL9YNfTD09O",
511
+ "outputId": "3d648d4e-c890-4347-8f23-9cd193d960d9"
512
+ },
513
+ "execution_count": 19,
514
+ "outputs": [
515
+ {
516
+ "output_type": "execute_result",
517
+ "data": {
518
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1575pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n<!-- 132397006959184 -->\n<g id=\"node1\" class=\"node\">\n<title>132397006959184</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-109.5 1382,-145.5 1567,-145.5 1567,-109.5 1382,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1393.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1405,-109.5 1405,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1445\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1485,-109.5 1485,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1526\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132397006959184tanh -->\n<g id=\"node2\" class=\"node\">\n<title>132397006959184tanh</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n</g>\n<!-- 132397006959184tanh&#45;&gt;132397006959184 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397006959184tanh&#45;&gt;132397006959184</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-127.5C1353.58,-127.5 1362.3,-127.5 1371.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-131 1381.81,-127.5 1371.81,-124 1371.81,-131\"/>\n</g>\n<!-- 132397003209840 -->\n<g id=\"node3\" class=\"node\">\n<title>132397003209840</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-165.5 4,-201.5 196,-201.5 196,-165.5 4,-165.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-165.5 34,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-165.5 114,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397006954336* -->\n<g id=\"node8\" class=\"node\">\n<title>132397006954336*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397003209840&#45;&gt;132397006954336* -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003209840&#45;&gt;132397006954336*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 132397003210368 -->\n<g id=\"node4\" class=\"node\">\n<title>132397003210368</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-110.5 2.5,-146.5 197.5,-146.5 197.5,-110.5 2.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-110.5 35.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-110.5 115.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 132397003210368&#45;&gt;132397006954336* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397003210368&#45;&gt;132397006954336*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-128.5C207.65,-128.5 217.05,-128.5 225.52,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-132 235.7,-128.5 225.7,-125 225.7,-132\"/>\n</g>\n<!-- 132397003199232 -->\n<g id=\"node5\" class=\"node\">\n<title>132397003199232</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-137.5 716,-173.5 901,-173.5 901,-137.5 716,-137.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-137.5 739,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-137.5 819,-173.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-151.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397006954480+ -->\n<g id=\"node15\" class=\"node\">\n<title>132397006954480+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397003199232&#45;&gt;132397006954480+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003199232&#45;&gt;132397006954480+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-142.52C926,-138.98 951.59,-135.36 971.37,-132.55\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-135.99 981.43,-131.12 971.04,-129.06 972.03,-135.99\"/>\n</g>\n<!-- 132397003199280 -->\n<g id=\"node6\" class=\"node\">\n<title>132397003199280</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2,-55.5 2,-91.5 198,-91.5 198,-55.5 2,-55.5\"/>\n<text text-anchor=\"middle\" x=\"17\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"32,-55.5 32,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"72\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"112,-55.5 112,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;1.5000</text>\n</g>\n<!-- 132397004831184* -->\n<g id=\"node13\" class=\"node\">\n<title>132397004831184*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132397003199280&#45;&gt;132397004831184* -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003199280&#45;&gt;132397004831184*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M198.37,-73.5C208.05,-73.5 217.4,-73.5 225.8,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.91,-77 235.91,-73.5 225.91,-70 225.91,-77\"/>\n</g>\n<!-- 132397006954336 -->\n<g id=\"node7\" class=\"node\">\n<title>132397006954336</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-110.5 328.5,-146.5 543.5,-146.5 543.5,-110.5 328.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-110.5 381.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-110.5 461.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397006953856+ -->\n<g id=\"node10\" class=\"node\">\n<title>132397006953856+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132397006954336&#45;&gt;132397006953856+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397006954336&#45;&gt;132397006953856+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-111.01C554.01,-109.34 563.76,-107.74 572.44,-106.32\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.17,-109.75 582.47,-104.68 572.04,-102.84 573.17,-109.75\"/>\n</g>\n<!-- 132397006954336*&#45;&gt;132397006954336 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397006954336*&#45;&gt;132397006954336</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C298.51,-128.5 308.08,-128.5 318.36,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-132 328.39,-128.5 318.39,-125 318.39,-132\"/>\n</g>\n<!-- 132397006953856 -->\n<g id=\"node9\" class=\"node\">\n<title>132397006953856</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397006953856&#45;&gt;132397006954480+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397006953856&#45;&gt;132397006954480+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M941.24,-118.51C952.12,-120 962.4,-121.4 971.45,-122.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.11,-126.13 981.49,-124.01 972.05,-119.19 971.11,-126.13\"/>\n</g>\n<!-- 132397006953856+&#45;&gt;132397006953856 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397006953856+&#45;&gt;132397006953856</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n<!-- 132397003200912 -->\n<g id=\"node11\" class=\"node\">\n<title>132397003200912</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 200,-36.5 200,-0.5 0,-0.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-0.5 33,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-0.5 118,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132397003200912&#45;&gt;132397004831184* -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003200912&#45;&gt;132397004831184*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 132397004831184 -->\n<g id=\"node12\" class=\"node\">\n<title>132397004831184</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-55.5 326,-91.5 546,-91.5 546,-55.5 326,-55.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-55.5 379,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-55.5 464,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397004831184&#45;&gt;132397006953856+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397004831184&#45;&gt;132397006953856+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-90.75C555.64,-92.23 564.6,-93.65 572.65,-94.92\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.23,-98.4 582.65,-96.5 573.32,-91.48 572.23,-98.4\"/>\n</g>\n<!-- 132397004831184*&#45;&gt;132397004831184 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397004831184*&#45;&gt;132397004831184</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C297.77,-73.5 306.37,-73.5 315.6,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-77 325.84,-73.5 315.84,-70 315.84,-77\"/>\n</g>\n<!-- 132397006954480 -->\n<g id=\"node14\" class=\"node\">\n<title>132397006954480</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-109.5 1071,-145.5 1256,-145.5 1256,-109.5 1071,-109.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-109.5 1094,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-109.5 1174,-145.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-123.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 132397006954480&#45;&gt;132397006959184tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397006954480&#45;&gt;132397006959184tanh</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-127.5C1265.01,-127.5 1273.74,-127.5 1281.66,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-131 1291.91,-127.5 1281.91,-124 1281.91,-131\"/>\n</g>\n<!-- 132397006954480+&#45;&gt;132397006954480 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397006954480+&#45;&gt;132397006954480</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-127.5C1042.58,-127.5 1051.3,-127.5 1060.57,-127.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-131 1070.81,-127.5 1060.81,-124 1060.81,-131\"/>\n</g>\n</g>\n</svg>\n",
519
+ "text/plain": [
520
+ "<graphviz.graphs.Digraph at 0x786a142ad630>"
521
+ ]
522
+ },
523
+ "metadata": {},
524
+ "execution_count": 19
525
+ }
526
+ ]
527
+ }
528
+ ]
529
+ }
8_handling_onenode_used_multiple_times.ipynb ADDED
@@ -0,0 +1,1169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {
7
+ "id": "T0rN8d146jvF"
8
+ },
9
+ "outputs": [],
10
+ "source": [
11
+ "from graphviz import Digraph\n",
12
+ "\n",
13
+ "def trace(root):\n",
14
+ " #Builds a set of all nodes and edges in a graph\n",
15
+ " nodes, edges = set(), set()\n",
16
+ " def build(v):\n",
17
+ " if v not in nodes:\n",
18
+ " nodes.add(v)\n",
19
+ " for child in v._prev:\n",
20
+ " edges.add((child, v))\n",
21
+ " build(child)\n",
22
+ " build(root)\n",
23
+ " return nodes, edges\n",
24
+ "\n",
25
+ "def draw_dot(root):\n",
26
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
27
+ "\n",
28
+ " nodes, edges = trace(root)\n",
29
+ " for n in nodes:\n",
30
+ " uid = str(id(n))\n",
31
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
32
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
33
+ " if n._op:\n",
34
+ " #If this value is a result of some operation, then create an op node for it\n",
35
+ " dot.node(name = uid + n._op, label=n._op)\n",
36
+ " #and connect this node to it\n",
37
+ " dot.edge(uid + n._op, uid)\n",
38
+ "\n",
39
+ " for n1, n2 in edges:\n",
40
+ " #Connect n1 to the node of n2\n",
41
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
42
+ "\n",
43
+ " return dot"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": 2,
49
+ "metadata": {
50
+ "id": "JlYxBvFK0AjA"
51
+ },
52
+ "outputs": [],
53
+ "source": [
54
+ "import math"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": 3,
60
+ "metadata": {
61
+ "id": "4XPxg_t3wl35"
62
+ },
63
+ "outputs": [],
64
+ "source": [
65
+ "class Value:\n",
66
+ "\n",
67
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
68
+ " self.data = data\n",
69
+ " self.grad = 0.0\n",
70
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
71
+ " self._prev = set(_children)\n",
72
+ " self._op = _op\n",
73
+ " self.label = label\n",
74
+ "\n",
75
+ "\n",
76
+ " def __repr__(self):\n",
77
+ " return f\"Value(data={self.data})\"\n",
78
+ "\n",
79
+ " def __add__(self, other):\n",
80
+ " out = Value(self.data + other.data, (self, other), '+')\n",
81
+ "\n",
82
+ " def backward():\n",
83
+ " self.grad = 1.0 * out.grad\n",
84
+ " other.grad = 1.0 * out.grad\n",
85
+ "\n",
86
+ " out._backward = backward\n",
87
+ " return out\n",
88
+ "\n",
89
+ " def __mul__(self, other):\n",
90
+ " out = Value(self.data * other.data, (self, other), '*')\n",
91
+ "\n",
92
+ " def backward():\n",
93
+ " self.grad = other.data * out.grad\n",
94
+ " other.grad = self.data * out.grad\n",
95
+ "\n",
96
+ " out._backward = backward\n",
97
+ " return out\n",
98
+ "\n",
99
+ " def tanh(self):\n",
100
+ " x = self.data\n",
101
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
102
+ " out = Value(t, (self, ), 'tanh')\n",
103
+ "\n",
104
+ " def backward():\n",
105
+ " self.grad = 1 - (t**2) * out.grad\n",
106
+ "\n",
107
+ " out._backward = backward\n",
108
+ " return out\n",
109
+ "\n",
110
+ " def backward(self):\n",
111
+ "\n",
112
+ " topo = []\n",
113
+ " visited = set()\n",
114
+ " def build_topo(v):\n",
115
+ " if v not in visited:\n",
116
+ " visited.add(v)\n",
117
+ " for child in v._prev:\n",
118
+ " build_topo(child)\n",
119
+ " topo.append(v)\n",
120
+ "\n",
121
+ " build_topo(self)\n",
122
+ "\n",
123
+ " self.grad = 1.0\n",
124
+ " for node in reversed(topo):\n",
125
+ " node._backward()"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": 13,
131
+ "metadata": {
132
+ "id": "S3HaLbW_zvne"
133
+ },
134
+ "outputs": [],
135
+ "source": [
136
+ "#Inputs x1, x2 of the neuron\n",
137
+ "x1 = Value(2.0, label='x1')\n",
138
+ "x2 = Value(0.0, label='x2')\n",
139
+ "\n",
140
+ "#Weights w1, w2 of the neuron - The synaptic values\n",
141
+ "w1 = Value(-3.0, label='w1')\n",
142
+ "w2 = Value(1.0, label='w2')\n",
143
+ "\n",
144
+ "#The bias of the neuron\n",
145
+ "b = Value(6.8813735870195432, label='b')\n",
146
+ "\n",
147
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
148
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
149
+ "\n",
150
+ "#The summation\n",
151
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'\n",
152
+ "\n",
153
+ "#n is basically the cell body, but without the activation function\n",
154
+ "n = x1w1x2w2 + b; n.label = 'n'\n",
155
+ "\n",
156
+ "#Now we pass n to the activation function\n",
157
+ "o = n.tanh(); o.label = 'o'"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": 5,
163
+ "metadata": {
164
+ "id": "WJyfRZOvCBlp"
165
+ },
166
+ "outputs": [],
167
+ "source": [
168
+ "o.grad = 1.0 #This is the base case, we set this"
169
+ ]
170
+ },
171
+ {
172
+ "cell_type": "code",
173
+ "execution_count": 8,
174
+ "metadata": {
175
+ "id": "KlnwtijqHFFC"
176
+ },
177
+ "outputs": [],
178
+ "source": [
179
+ "o.backward()"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "execution_count": 9,
185
+ "metadata": {
186
+ "colab": {
187
+ "base_uri": "https://localhost:8080/",
188
+ "height": 322
189
+ },
190
+ "id": "Dx6qKpg6HIrR",
191
+ "outputId": "46953515-8202-4a52-be89-46586374d2ee"
192
+ },
193
+ "outputs": [
194
+ {
195
+ "data": {
196
+ "image/svg+xml": [
197
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
198
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
199
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
200
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
201
+ " -->\n",
202
+ "<!-- Title: %3 Pages: 1 -->\n",
203
+ "<svg width=\"1575pt\" height=\"210pt\"\n",
204
+ " viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
205
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n",
206
+ "<title>%3</title>\n",
207
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n",
208
+ "<!-- 140166148584448 -->\n",
209
+ "<g id=\"node1\" class=\"node\">\n",
210
+ "<title>140166148584448</title>\n",
211
+ "<polygon fill=\"none\" stroke=\"black\" points=\"4,-165.5 4,-201.5 196,-201.5 196,-165.5 4,-165.5\"/>\n",
212
+ "<text text-anchor=\"middle\" x=\"19\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n",
213
+ "<polyline fill=\"none\" stroke=\"black\" points=\"34,-165.5 34,-201.5 \"/>\n",
214
+ "<text text-anchor=\"middle\" x=\"74\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n",
215
+ "<polyline fill=\"none\" stroke=\"black\" points=\"114,-165.5 114,-201.5 \"/>\n",
216
+ "<text text-anchor=\"middle\" x=\"155\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
217
+ "</g>\n",
218
+ "<!-- 140166147244480* -->\n",
219
+ "<g id=\"node8\" class=\"node\">\n",
220
+ "<title>140166147244480*</title>\n",
221
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n",
222
+ "<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
223
+ "</g>\n",
224
+ "<!-- 140166148584448&#45;&gt;140166147244480* -->\n",
225
+ "<g id=\"edge12\" class=\"edge\">\n",
226
+ "<title>140166148584448&#45;&gt;140166147244480*</title>\n",
227
+ "<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n",
228
+ "<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n",
229
+ "</g>\n",
230
+ "<!-- 140166148585072 -->\n",
231
+ "<g id=\"node2\" class=\"node\">\n",
232
+ "<title>140166148585072</title>\n",
233
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2,-55.5 2,-91.5 198,-91.5 198,-55.5 2,-55.5\"/>\n",
234
+ "<text text-anchor=\"middle\" x=\"17\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n",
235
+ "<polyline fill=\"none\" stroke=\"black\" points=\"32,-55.5 32,-91.5 \"/>\n",
236
+ "<text text-anchor=\"middle\" x=\"72\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n",
237
+ "<polyline fill=\"none\" stroke=\"black\" points=\"112,-55.5 112,-91.5 \"/>\n",
238
+ "<text text-anchor=\"middle\" x=\"155\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;1.5000</text>\n",
239
+ "</g>\n",
240
+ "<!-- 140166148583872* -->\n",
241
+ "<g id=\"node14\" class=\"node\">\n",
242
+ "<title>140166148583872*</title>\n",
243
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
244
+ "<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
245
+ "</g>\n",
246
+ "<!-- 140166148585072&#45;&gt;140166148583872* -->\n",
247
+ "<g id=\"edge9\" class=\"edge\">\n",
248
+ "<title>140166148585072&#45;&gt;140166148583872*</title>\n",
249
+ "<path fill=\"none\" stroke=\"black\" d=\"M198.37,-73.5C208.05,-73.5 217.4,-73.5 225.8,-73.5\"/>\n",
250
+ "<polygon fill=\"black\" stroke=\"black\" points=\"225.91,-77 235.91,-73.5 225.91,-70 225.91,-77\"/>\n",
251
+ "</g>\n",
252
+ "<!-- 140166147258016 -->\n",
253
+ "<g id=\"node3\" class=\"node\">\n",
254
+ "<title>140166147258016</title>\n",
255
+ "<polygon fill=\"none\" stroke=\"black\" points=\"1382,-54.5 1382,-90.5 1567,-90.5 1567,-54.5 1382,-54.5\"/>\n",
256
+ "<text text-anchor=\"middle\" x=\"1393.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n",
257
+ "<polyline fill=\"none\" stroke=\"black\" points=\"1405,-54.5 1405,-90.5 \"/>\n",
258
+ "<text text-anchor=\"middle\" x=\"1445\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n",
259
+ "<polyline fill=\"none\" stroke=\"black\" points=\"1485,-54.5 1485,-90.5 \"/>\n",
260
+ "<text text-anchor=\"middle\" x=\"1526\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
261
+ "</g>\n",
262
+ "<!-- 140166147258016tanh -->\n",
263
+ "<g id=\"node4\" class=\"node\">\n",
264
+ "<title>140166147258016tanh</title>\n",
265
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n",
266
+ "<text text-anchor=\"middle\" x=\"1319\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n",
267
+ "</g>\n",
268
+ "<!-- 140166147258016tanh&#45;&gt;140166147258016 -->\n",
269
+ "<g id=\"edge1\" class=\"edge\">\n",
270
+ "<title>140166147258016tanh&#45;&gt;140166147258016</title>\n",
271
+ "<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-72.5C1353.58,-72.5 1362.3,-72.5 1371.57,-72.5\"/>\n",
272
+ "<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-76 1381.81,-72.5 1371.81,-69 1371.81,-76\"/>\n",
273
+ "</g>\n",
274
+ "<!-- 140166147252928 -->\n",
275
+ "<g id=\"node5\" class=\"node\">\n",
276
+ "<title>140166147252928</title>\n",
277
+ "<polygon fill=\"none\" stroke=\"black\" points=\"1071,-54.5 1071,-90.5 1256,-90.5 1256,-54.5 1071,-54.5\"/>\n",
278
+ "<text text-anchor=\"middle\" x=\"1082.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n",
279
+ "<polyline fill=\"none\" stroke=\"black\" points=\"1094,-54.5 1094,-90.5 \"/>\n",
280
+ "<text text-anchor=\"middle\" x=\"1134\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n",
281
+ "<polyline fill=\"none\" stroke=\"black\" points=\"1174,-54.5 1174,-90.5 \"/>\n",
282
+ "<text text-anchor=\"middle\" x=\"1215\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
283
+ "</g>\n",
284
+ "<!-- 140166147252928&#45;&gt;140166147258016tanh -->\n",
285
+ "<g id=\"edge14\" class=\"edge\">\n",
286
+ "<title>140166147252928&#45;&gt;140166147258016tanh</title>\n",
287
+ "<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-72.5C1265.01,-72.5 1273.74,-72.5 1281.66,-72.5\"/>\n",
288
+ "<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-76 1291.91,-72.5 1281.91,-69 1281.91,-76\"/>\n",
289
+ "</g>\n",
290
+ "<!-- 140166147252928+ -->\n",
291
+ "<g id=\"node6\" class=\"node\">\n",
292
+ "<title>140166147252928+</title>\n",
293
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n",
294
+ "<text text-anchor=\"middle\" x=\"1008\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
295
+ "</g>\n",
296
+ "<!-- 140166147252928+&#45;&gt;140166147252928 -->\n",
297
+ "<g id=\"edge2\" class=\"edge\">\n",
298
+ "<title>140166147252928+&#45;&gt;140166147252928</title>\n",
299
+ "<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-72.5C1042.58,-72.5 1051.3,-72.5 1060.57,-72.5\"/>\n",
300
+ "<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-76 1070.81,-72.5 1060.81,-69 1060.81,-76\"/>\n",
301
+ "</g>\n",
302
+ "<!-- 140166147244480 -->\n",
303
+ "<g id=\"node7\" class=\"node\">\n",
304
+ "<title>140166147244480</title>\n",
305
+ "<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-110.5 328.5,-146.5 543.5,-146.5 543.5,-110.5 328.5,-110.5\"/>\n",
306
+ "<text text-anchor=\"middle\" x=\"355\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n",
307
+ "<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-110.5 381.5,-146.5 \"/>\n",
308
+ "<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n",
309
+ "<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-110.5 461.5,-146.5 \"/>\n",
310
+ "<text text-anchor=\"middle\" x=\"502.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
311
+ "</g>\n",
312
+ "<!-- 140166147257776+ -->\n",
313
+ "<g id=\"node12\" class=\"node\">\n",
314
+ "<title>140166147257776+</title>\n",
315
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n",
316
+ "<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
317
+ "</g>\n",
318
+ "<!-- 140166147244480&#45;&gt;140166147257776+ -->\n",
319
+ "<g id=\"edge13\" class=\"edge\">\n",
320
+ "<title>140166147244480&#45;&gt;140166147257776+</title>\n",
321
+ "<path fill=\"none\" stroke=\"black\" d=\"M543.84,-111.01C554.01,-109.34 563.76,-107.74 572.44,-106.32\"/>\n",
322
+ "<polygon fill=\"black\" stroke=\"black\" points=\"573.17,-109.75 582.47,-104.68 572.04,-102.84 573.17,-109.75\"/>\n",
323
+ "</g>\n",
324
+ "<!-- 140166147244480*&#45;&gt;140166147244480 -->\n",
325
+ "<g id=\"edge3\" class=\"edge\">\n",
326
+ "<title>140166147244480*&#45;&gt;140166147244480</title>\n",
327
+ "<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C298.51,-128.5 308.08,-128.5 318.36,-128.5\"/>\n",
328
+ "<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-132 328.39,-128.5 318.39,-125 318.39,-132\"/>\n",
329
+ "</g>\n",
330
+ "<!-- 140166148580080 -->\n",
331
+ "<g id=\"node9\" class=\"node\">\n",
332
+ "<title>140166148580080</title>\n",
333
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-110.5 2.5,-146.5 197.5,-146.5 197.5,-110.5 2.5,-110.5\"/>\n",
334
+ "<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n",
335
+ "<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-110.5 35.5,-146.5 \"/>\n",
336
+ "<text text-anchor=\"middle\" x=\"75.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n",
337
+ "<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-110.5 115.5,-146.5 \"/>\n",
338
+ "<text text-anchor=\"middle\" x=\"156.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
339
+ "</g>\n",
340
+ "<!-- 140166148580080&#45;&gt;140166147244480* -->\n",
341
+ "<g id=\"edge8\" class=\"edge\">\n",
342
+ "<title>140166148580080&#45;&gt;140166147244480*</title>\n",
343
+ "<path fill=\"none\" stroke=\"black\" d=\"M197.91,-128.5C207.65,-128.5 217.05,-128.5 225.52,-128.5\"/>\n",
344
+ "<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-132 235.7,-128.5 225.7,-125 225.7,-132\"/>\n",
345
+ "</g>\n",
346
+ "<!-- 140166148585792 -->\n",
347
+ "<g id=\"node10\" class=\"node\">\n",
348
+ "<title>140166148585792</title>\n",
349
+ "<polygon fill=\"none\" stroke=\"black\" points=\"716,-27.5 716,-63.5 901,-63.5 901,-27.5 716,-27.5\"/>\n",
350
+ "<text text-anchor=\"middle\" x=\"727.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
351
+ "<polyline fill=\"none\" stroke=\"black\" points=\"739,-27.5 739,-63.5 \"/>\n",
352
+ "<text text-anchor=\"middle\" x=\"779\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n",
353
+ "<polyline fill=\"none\" stroke=\"black\" points=\"819,-27.5 819,-63.5 \"/>\n",
354
+ "<text text-anchor=\"middle\" x=\"860\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
355
+ "</g>\n",
356
+ "<!-- 140166148585792&#45;&gt;140166147252928+ -->\n",
357
+ "<g id=\"edge11\" class=\"edge\">\n",
358
+ "<title>140166148585792&#45;&gt;140166147252928+</title>\n",
359
+ "<path fill=\"none\" stroke=\"black\" d=\"M901.02,-58.01C926,-61.43 951.59,-64.93 971.37,-67.63\"/>\n",
360
+ "<polygon fill=\"black\" stroke=\"black\" points=\"971.05,-71.12 981.43,-69.01 972,-64.18 971.05,-71.12\"/>\n",
361
+ "</g>\n",
362
+ "<!-- 140166147257776 -->\n",
363
+ "<g id=\"node11\" class=\"node\">\n",
364
+ "<title>140166147257776</title>\n",
365
+ "<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n",
366
+ "<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n",
367
+ "<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n",
368
+ "<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
369
+ "<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n",
370
+ "<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
371
+ "</g>\n",
372
+ "<!-- 140166147257776&#45;&gt;140166147252928+ -->\n",
373
+ "<g id=\"edge6\" class=\"edge\">\n",
374
+ "<title>140166147257776&#45;&gt;140166147252928+</title>\n",
375
+ "<path fill=\"none\" stroke=\"black\" d=\"M936.53,-82.49C949.09,-80.71 960.99,-79.02 971.3,-77.56\"/>\n",
376
+ "<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-80.99 981.44,-76.12 971.05,-74.06 972.03,-80.99\"/>\n",
377
+ "</g>\n",
378
+ "<!-- 140166147257776+&#45;&gt;140166147257776 -->\n",
379
+ "<g id=\"edge4\" class=\"edge\">\n",
380
+ "<title>140166147257776+&#45;&gt;140166147257776</title>\n",
381
+ "<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n",
382
+ "<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n",
383
+ "</g>\n",
384
+ "<!-- 140166148583872 -->\n",
385
+ "<g id=\"node13\" class=\"node\">\n",
386
+ "<title>140166148583872</title>\n",
387
+ "<polygon fill=\"none\" stroke=\"black\" points=\"326,-55.5 326,-91.5 546,-91.5 546,-55.5 326,-55.5\"/>\n",
388
+ "<text text-anchor=\"middle\" x=\"352.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n",
389
+ "<polyline fill=\"none\" stroke=\"black\" points=\"379,-55.5 379,-91.5 \"/>\n",
390
+ "<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
391
+ "<polyline fill=\"none\" stroke=\"black\" points=\"464,-55.5 464,-91.5 \"/>\n",
392
+ "<text text-anchor=\"middle\" x=\"505\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
393
+ "</g>\n",
394
+ "<!-- 140166148583872&#45;&gt;140166147257776+ -->\n",
395
+ "<g id=\"edge10\" class=\"edge\">\n",
396
+ "<title>140166148583872&#45;&gt;140166147257776+</title>\n",
397
+ "<path fill=\"none\" stroke=\"black\" d=\"M546.27,-90.75C555.64,-92.23 564.6,-93.65 572.65,-94.92\"/>\n",
398
+ "<polygon fill=\"black\" stroke=\"black\" points=\"572.23,-98.4 582.65,-96.5 573.32,-91.48 572.23,-98.4\"/>\n",
399
+ "</g>\n",
400
+ "<!-- 140166148583872*&#45;&gt;140166148583872 -->\n",
401
+ "<g id=\"edge5\" class=\"edge\">\n",
402
+ "<title>140166148583872*&#45;&gt;140166148583872</title>\n",
403
+ "<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C297.77,-73.5 306.37,-73.5 315.6,-73.5\"/>\n",
404
+ "<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-77 325.84,-73.5 315.84,-70 315.84,-77\"/>\n",
405
+ "</g>\n",
406
+ "<!-- 140166148580320 -->\n",
407
+ "<g id=\"node15\" class=\"node\">\n",
408
+ "<title>140166148580320</title>\n",
409
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 200,-36.5 200,-0.5 0,-0.5\"/>\n",
410
+ "<text text-anchor=\"middle\" x=\"16.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n",
411
+ "<polyline fill=\"none\" stroke=\"black\" points=\"33,-0.5 33,-36.5 \"/>\n",
412
+ "<text text-anchor=\"middle\" x=\"75.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
413
+ "<polyline fill=\"none\" stroke=\"black\" points=\"118,-0.5 118,-36.5 \"/>\n",
414
+ "<text text-anchor=\"middle\" x=\"159\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
415
+ "</g>\n",
416
+ "<!-- 140166148580320&#45;&gt;140166148583872* -->\n",
417
+ "<g id=\"edge7\" class=\"edge\">\n",
418
+ "<title>140166148580320&#45;&gt;140166148583872*</title>\n",
419
+ "<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n",
420
+ "<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n",
421
+ "</g>\n",
422
+ "</g>\n",
423
+ "</svg>\n"
424
+ ],
425
+ "text/plain": [
426
+ "<graphviz.graphs.Digraph at 0x7f7af97ed6c0>"
427
+ ]
428
+ },
429
+ "execution_count": 9,
430
+ "metadata": {},
431
+ "output_type": "execute_result"
432
+ }
433
+ ],
434
+ "source": [
435
+ "draw_dot(o)"
436
+ ]
437
+ },
438
+ {
439
+ "cell_type": "markdown",
440
+ "metadata": {
441
+ "id": "CLPxjkX2HYNz"
442
+ },
443
+ "source": [
444
+ "-----------------"
445
+ ]
446
+ },
447
+ {
448
+ "cell_type": "markdown",
449
+ "metadata": {
450
+ "id": "Vrd5eDUiHZIL"
451
+ },
452
+ "source": [
453
+ "Now, here we have noticed a bug. Which we can see in these following examples"
454
+ ]
455
+ },
456
+ {
457
+ "cell_type": "markdown",
458
+ "metadata": {
459
+ "id": "1ChWhGlhHkE-"
460
+ },
461
+ "source": [
462
+ "Example 1"
463
+ ]
464
+ },
465
+ {
466
+ "cell_type": "code",
467
+ "execution_count": 10,
468
+ "metadata": {
469
+ "colab": {
470
+ "base_uri": "https://localhost:8080/",
471
+ "height": 82
472
+ },
473
+ "id": "5zcaSA-JHPP0",
474
+ "outputId": "486d76c0-d5dd-4cbc-8cb5-522579b268c7"
475
+ },
476
+ "outputs": [
477
+ {
478
+ "data": {
479
+ "image/svg+xml": [
480
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
481
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
482
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
483
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
484
+ " -->\n",
485
+ "<!-- Title: %3 Pages: 1 -->\n",
486
+ "<svg width=\"504pt\" height=\"45pt\"\n",
487
+ " viewBox=\"0.00 0.00 504.00 45.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
488
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 41)\">\n",
489
+ "<title>%3</title>\n",
490
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-41 500,-41 500,4 -4,4\"/>\n",
491
+ "<!-- 140165250666496 -->\n",
492
+ "<g id=\"node1\" class=\"node\">\n",
493
+ "<title>140165250666496</title>\n",
494
+ "<polygon fill=\"none\" stroke=\"black\" points=\"311,-0.5 311,-36.5 496,-36.5 496,-0.5 311,-0.5\"/>\n",
495
+ "<text text-anchor=\"middle\" x=\"322.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
496
+ "<polyline fill=\"none\" stroke=\"black\" points=\"334,-0.5 334,-36.5 \"/>\n",
497
+ "<text text-anchor=\"middle\" x=\"374\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.0000</text>\n",
498
+ "<polyline fill=\"none\" stroke=\"black\" points=\"414,-0.5 414,-36.5 \"/>\n",
499
+ "<text text-anchor=\"middle\" x=\"455\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
500
+ "</g>\n",
501
+ "<!-- 140165250666496+ -->\n",
502
+ "<g id=\"node2\" class=\"node\">\n",
503
+ "<title>140165250666496+</title>\n",
504
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"248\" cy=\"-18.5\" rx=\"27\" ry=\"18\"/>\n",
505
+ "<text text-anchor=\"middle\" x=\"248\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
506
+ "</g>\n",
507
+ "<!-- 140165250666496+&#45;&gt;140165250666496 -->\n",
508
+ "<g id=\"edge1\" class=\"edge\">\n",
509
+ "<title>140165250666496+&#45;&gt;140165250666496</title>\n",
510
+ "<path fill=\"none\" stroke=\"black\" d=\"M275.04,-18.5C282.58,-18.5 291.3,-18.5 300.57,-18.5\"/>\n",
511
+ "<polygon fill=\"black\" stroke=\"black\" points=\"300.81,-22 310.81,-18.5 300.81,-15 300.81,-22\"/>\n",
512
+ "</g>\n",
513
+ "<!-- 140165250678304 -->\n",
514
+ "<g id=\"node3\" class=\"node\">\n",
515
+ "<title>140165250678304</title>\n",
516
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 185,-36.5 185,-0.5 0,-0.5\"/>\n",
517
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
518
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-0.5 23,-36.5 \"/>\n",
519
+ "<text text-anchor=\"middle\" x=\"63\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 3.0000</text>\n",
520
+ "<polyline fill=\"none\" stroke=\"black\" points=\"103,-0.5 103,-36.5 \"/>\n",
521
+ "<text text-anchor=\"middle\" x=\"144\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
522
+ "</g>\n",
523
+ "<!-- 140165250678304&#45;&gt;140165250666496+ -->\n",
524
+ "<g id=\"edge2\" class=\"edge\">\n",
525
+ "<title>140165250678304&#45;&gt;140165250666496+</title>\n",
526
+ "<path fill=\"none\" stroke=\"black\" d=\"M185.01,-18.5C194.01,-18.5 202.74,-18.5 210.66,-18.5\"/>\n",
527
+ "<polygon fill=\"black\" stroke=\"black\" points=\"210.91,-22 220.91,-18.5 210.91,-15 210.91,-22\"/>\n",
528
+ "</g>\n",
529
+ "</g>\n",
530
+ "</svg>\n"
531
+ ],
532
+ "text/plain": [
533
+ "<graphviz.graphs.Digraph at 0x7f7ac3f9ffa0>"
534
+ ]
535
+ },
536
+ "execution_count": 10,
537
+ "metadata": {},
538
+ "output_type": "execute_result"
539
+ }
540
+ ],
541
+ "source": [
542
+ "a = Value(3.0, label='a')\n",
543
+ "b = a + a ; b.label = 'b'\n",
544
+ "b.backward()\n",
545
+ "draw_dot(b)"
546
+ ]
547
+ },
548
+ {
549
+ "cell_type": "markdown",
550
+ "metadata": {
551
+ "id": "SZsYstFrH1gj"
552
+ },
553
+ "source": [
554
+ "So here, the data value is correct, but the grad value is wrong. \\\n",
555
+ "\\\n",
556
+ "The derivative of b wrt a, in this equation b = a + a, is 2. Because it's 1+1, therefore 2 \\\n",
557
+ "\\\n",
558
+ "In the graph the second arrow is right on top of the first one, so we only see one arrow. \\\n",
559
+ "\\\n",
560
+ "So it seems like one value is stored and when another same one comes, the first one gets over-written, and therefore only one of those values is considered."
561
+ ]
562
+ },
563
+ {
564
+ "cell_type": "markdown",
565
+ "metadata": {
566
+ "id": "3jzOlZN2IhX0"
567
+ },
568
+ "source": [
569
+ "Example 2"
570
+ ]
571
+ },
572
+ {
573
+ "cell_type": "code",
574
+ "execution_count": 11,
575
+ "metadata": {
576
+ "colab": {
577
+ "base_uri": "https://localhost:8080/",
578
+ "height": 155
579
+ },
580
+ "id": "7PNymHbLIiw8",
581
+ "outputId": "3ce2021d-f55b-4fe0-d34a-f50c415e6576"
582
+ },
583
+ "outputs": [
584
+ {
585
+ "data": {
586
+ "image/svg+xml": [
587
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
588
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
589
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
590
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
591
+ " -->\n",
592
+ "<!-- Title: %3 Pages: 1 -->\n",
593
+ "<svg width=\"828pt\" height=\"100pt\"\n",
594
+ " viewBox=\"0.00 0.00 828.00 100.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
595
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 96)\">\n",
596
+ "<title>%3</title>\n",
597
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-96 824,-96 824,4 -4,4\"/>\n",
598
+ "<!-- 140165252446240 -->\n",
599
+ "<g id=\"node1\" class=\"node\">\n",
600
+ "<title>140165252446240</title>\n",
601
+ "<polygon fill=\"none\" stroke=\"black\" points=\"632,-27.5 632,-63.5 820,-63.5 820,-27.5 632,-27.5\"/>\n",
602
+ "<text text-anchor=\"middle\" x=\"642.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n",
603
+ "<polyline fill=\"none\" stroke=\"black\" points=\"653,-27.5 653,-63.5 \"/>\n",
604
+ "<text text-anchor=\"middle\" x=\"695.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
605
+ "<polyline fill=\"none\" stroke=\"black\" points=\"738,-27.5 738,-63.5 \"/>\n",
606
+ "<text text-anchor=\"middle\" x=\"779\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
607
+ "</g>\n",
608
+ "<!-- 140165252446240* -->\n",
609
+ "<g id=\"node2\" class=\"node\">\n",
610
+ "<title>140165252446240*</title>\n",
611
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"569\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n",
612
+ "<text text-anchor=\"middle\" x=\"569\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
613
+ "</g>\n",
614
+ "<!-- 140165252446240*&#45;&gt;140165252446240 -->\n",
615
+ "<g id=\"edge1\" class=\"edge\">\n",
616
+ "<title>140165252446240*&#45;&gt;140165252446240</title>\n",
617
+ "<path fill=\"none\" stroke=\"black\" d=\"M596.29,-45.5C603.78,-45.5 612.42,-45.5 621.6,-45.5\"/>\n",
618
+ "<polygon fill=\"black\" stroke=\"black\" points=\"621.76,-49 631.76,-45.5 621.76,-42 621.76,-49\"/>\n",
619
+ "</g>\n",
620
+ "<!-- 140165252443216 -->\n",
621
+ "<g id=\"node3\" class=\"node\">\n",
622
+ "<title>140165252443216</title>\n",
623
+ "<polygon fill=\"none\" stroke=\"black\" points=\"316.5,-55.5 316.5,-91.5 505.5,-91.5 505.5,-55.5 316.5,-55.5\"/>\n",
624
+ "<text text-anchor=\"middle\" x=\"328\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n",
625
+ "<polyline fill=\"none\" stroke=\"black\" points=\"339.5,-55.5 339.5,-91.5 \"/>\n",
626
+ "<text text-anchor=\"middle\" x=\"379.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n",
627
+ "<polyline fill=\"none\" stroke=\"black\" points=\"419.5,-55.5 419.5,-91.5 \"/>\n",
628
+ "<text text-anchor=\"middle\" x=\"462.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;6.0000</text>\n",
629
+ "</g>\n",
630
+ "<!-- 140165252443216&#45;&gt;140165252446240* -->\n",
631
+ "<g id=\"edge9\" class=\"edge\">\n",
632
+ "<title>140165252443216&#45;&gt;140165252446240*</title>\n",
633
+ "<path fill=\"none\" stroke=\"black\" d=\"M505.9,-56.65C515.42,-54.94 524.62,-53.29 532.89,-51.8\"/>\n",
634
+ "<polygon fill=\"black\" stroke=\"black\" points=\"533.59,-55.23 542.81,-50.02 532.35,-48.34 533.59,-55.23\"/>\n",
635
+ "</g>\n",
636
+ "<!-- 140165252443216+ -->\n",
637
+ "<g id=\"node4\" class=\"node\">\n",
638
+ "<title>140165252443216+</title>\n",
639
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
640
+ "<text text-anchor=\"middle\" x=\"253\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
641
+ "</g>\n",
642
+ "<!-- 140165252443216+&#45;&gt;140165252443216 -->\n",
643
+ "<g id=\"edge2\" class=\"edge\">\n",
644
+ "<title>140165252443216+&#45;&gt;140165252443216</title>\n",
645
+ "<path fill=\"none\" stroke=\"black\" d=\"M280.11,-73.5C287.84,-73.5 296.8,-73.5 306.35,-73.5\"/>\n",
646
+ "<polygon fill=\"black\" stroke=\"black\" points=\"306.48,-77 316.48,-73.5 306.48,-70 306.48,-77\"/>\n",
647
+ "</g>\n",
648
+ "<!-- 140165250911536 -->\n",
649
+ "<g id=\"node5\" class=\"node\">\n",
650
+ "<title>140165250911536</title>\n",
651
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 190,-91.5 190,-55.5 0,-55.5\"/>\n",
652
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
653
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-55.5 23,-91.5 \"/>\n",
654
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n",
655
+ "<polyline fill=\"none\" stroke=\"black\" points=\"108,-55.5 108,-91.5 \"/>\n",
656
+ "<text text-anchor=\"middle\" x=\"149\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 3.0000</text>\n",
657
+ "</g>\n",
658
+ "<!-- 140165250911536&#45;&gt;140165252443216+ -->\n",
659
+ "<g id=\"edge5\" class=\"edge\">\n",
660
+ "<title>140165250911536&#45;&gt;140165252443216+</title>\n",
661
+ "<path fill=\"none\" stroke=\"black\" d=\"M190.34,-73.5C199.26,-73.5 207.87,-73.5 215.69,-73.5\"/>\n",
662
+ "<polygon fill=\"black\" stroke=\"black\" points=\"215.82,-77 225.82,-73.5 215.82,-70 215.82,-77\"/>\n",
663
+ "</g>\n",
664
+ "<!-- 140165250910096* -->\n",
665
+ "<g id=\"node8\" class=\"node\">\n",
666
+ "<title>140165250910096*</title>\n",
667
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"253\" cy=\"-18.5\" rx=\"27\" ry=\"18\"/>\n",
668
+ "<text text-anchor=\"middle\" x=\"253\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
669
+ "</g>\n",
670
+ "<!-- 140165250911536&#45;&gt;140165250910096* -->\n",
671
+ "<g id=\"edge8\" class=\"edge\">\n",
672
+ "<title>140165250911536&#45;&gt;140165250910096*</title>\n",
673
+ "<path fill=\"none\" stroke=\"black\" d=\"M163.39,-55.42C172.4,-52.63 181.47,-49.63 190,-46.5 200.5,-42.64 211.72,-37.81 221.69,-33.24\"/>\n",
674
+ "<polygon fill=\"black\" stroke=\"black\" points=\"223.21,-36.39 230.8,-28.99 220.26,-30.05 223.21,-36.39\"/>\n",
675
+ "</g>\n",
676
+ "<!-- 140165250910576 -->\n",
677
+ "<g id=\"node6\" class=\"node\">\n",
678
+ "<title>140165250910576</title>\n",
679
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0.5,-0.5 0.5,-36.5 189.5,-36.5 189.5,-0.5 0.5,-0.5\"/>\n",
680
+ "<text text-anchor=\"middle\" x=\"12\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
681
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23.5,-0.5 23.5,-36.5 \"/>\n",
682
+ "<text text-anchor=\"middle\" x=\"63.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 3.0000</text>\n",
683
+ "<polyline fill=\"none\" stroke=\"black\" points=\"103.5,-0.5 103.5,-36.5 \"/>\n",
684
+ "<text text-anchor=\"middle\" x=\"146.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n",
685
+ "</g>\n",
686
+ "<!-- 140165250910576&#45;&gt;140165252443216+ -->\n",
687
+ "<g id=\"edge4\" class=\"edge\">\n",
688
+ "<title>140165250910576&#45;&gt;140165252443216+</title>\n",
689
+ "<path fill=\"none\" stroke=\"black\" d=\"M160.31,-36.52C170.31,-39.65 180.48,-43.02 190,-46.5 200.26,-50.25 211.24,-54.83 221.08,-59.16\"/>\n",
690
+ "<polygon fill=\"black\" stroke=\"black\" points=\"219.9,-62.46 230.45,-63.35 222.75,-56.07 219.9,-62.46\"/>\n",
691
+ "</g>\n",
692
+ "<!-- 140165250910576&#45;&gt;140165250910096* -->\n",
693
+ "<g id=\"edge7\" class=\"edge\">\n",
694
+ "<title>140165250910576&#45;&gt;140165250910096*</title>\n",
695
+ "<path fill=\"none\" stroke=\"black\" d=\"M189.9,-18.5C199.05,-18.5 207.9,-18.5 215.91,-18.5\"/>\n",
696
+ "<polygon fill=\"black\" stroke=\"black\" points=\"215.91,-22 225.91,-18.5 215.91,-15 215.91,-22\"/>\n",
697
+ "</g>\n",
698
+ "<!-- 140165250910096 -->\n",
699
+ "<g id=\"node7\" class=\"node\">\n",
700
+ "<title>140165250910096</title>\n",
701
+ "<polygon fill=\"none\" stroke=\"black\" points=\"316,-0.5 316,-36.5 506,-36.5 506,-0.5 316,-0.5\"/>\n",
702
+ "<text text-anchor=\"middle\" x=\"327.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n",
703
+ "<polyline fill=\"none\" stroke=\"black\" points=\"339,-0.5 339,-36.5 \"/>\n",
704
+ "<text text-anchor=\"middle\" x=\"381.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
705
+ "<polyline fill=\"none\" stroke=\"black\" points=\"424,-0.5 424,-36.5 \"/>\n",
706
+ "<text text-anchor=\"middle\" x=\"465\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
707
+ "</g>\n",
708
+ "<!-- 140165250910096&#45;&gt;140165252446240* -->\n",
709
+ "<g id=\"edge6\" class=\"edge\">\n",
710
+ "<title>140165250910096&#45;&gt;140165252446240*</title>\n",
711
+ "<path fill=\"none\" stroke=\"black\" d=\"M506.34,-34.83C515.62,-36.44 524.58,-37.99 532.66,-39.38\"/>\n",
712
+ "<polygon fill=\"black\" stroke=\"black\" points=\"532.27,-42.87 542.72,-41.12 533.46,-35.97 532.27,-42.87\"/>\n",
713
+ "</g>\n",
714
+ "<!-- 140165250910096*&#45;&gt;140165250910096 -->\n",
715
+ "<g id=\"edge3\" class=\"edge\">\n",
716
+ "<title>140165250910096*&#45;&gt;140165250910096</title>\n",
717
+ "<path fill=\"none\" stroke=\"black\" d=\"M280.11,-18.5C287.63,-18.5 296.32,-18.5 305.58,-18.5\"/>\n",
718
+ "<polygon fill=\"black\" stroke=\"black\" points=\"305.81,-22 315.81,-18.5 305.81,-15 305.81,-22\"/>\n",
719
+ "</g>\n",
720
+ "</g>\n",
721
+ "</svg>\n"
722
+ ],
723
+ "text/plain": [
724
+ "<graphviz.graphs.Digraph at 0x7f7ac3fd89d0>"
725
+ ]
726
+ },
727
+ "execution_count": 11,
728
+ "metadata": {},
729
+ "output_type": "execute_result"
730
+ }
731
+ ],
732
+ "source": [
733
+ "a = Value(-2.0, label='a')\n",
734
+ "b = Value(3.0, label='b')\n",
735
+ "d = a * b ; d.label = 'd'\n",
736
+ "e = a + b ; e.label = 'e'\n",
737
+ "f = d * e ; f.label = 'f'\n",
738
+ "\n",
739
+ "f.backward()\n",
740
+ "draw_dot(f)"
741
+ ]
742
+ },
743
+ {
744
+ "cell_type": "markdown",
745
+ "metadata": {
746
+ "id": "8P7HExRtJByq"
747
+ },
748
+ "source": [
749
+ "Okay so here, the derivative of f wrt a, b have two more cases: through e and d. \\\n",
750
+ "\\\n",
751
+ "So now through e: \\\n",
752
+ "a and b grads are -6 respectively \\\n",
753
+ "\\\n",
754
+ "And through d: \\\n",
755
+ "a is 3 and b is -2 \\\n",
756
+ "\\\n",
757
+ "So if you notice, through e was calculated first and then overwirtten by the calculations through d."
758
+ ]
759
+ },
760
+ {
761
+ "cell_type": "markdown",
762
+ "metadata": {
763
+ "id": "OIejUTQWJud9"
764
+ },
765
+ "source": [
766
+ "----------------"
767
+ ]
768
+ },
769
+ {
770
+ "cell_type": "markdown",
771
+ "metadata": {
772
+ "id": "TaNNwHNUJvdt"
773
+ },
774
+ "source": [
775
+ "So, the solution is simple. We just need to ensure that the gradient values get accumelated if the same variable is used again. \\\n",
776
+ "\\\n",
777
+ "(Mathematical explaination or proof will be to use the **Multivariable case rule** of the chain rule.)\n",
778
+ "\\\n",
779
+ "So basically we'll add the new value with the prev added one"
780
+ ]
781
+ },
782
+ {
783
+ "cell_type": "markdown",
784
+ "metadata": {
785
+ "id": "17VS6floKCln"
786
+ },
787
+ "source": [
788
+ "So, let's update the Value object"
789
+ ]
790
+ },
791
+ {
792
+ "cell_type": "code",
793
+ "execution_count": 12,
794
+ "metadata": {
795
+ "id": "XBafkiKxJ-in"
796
+ },
797
+ "outputs": [],
798
+ "source": [
799
+ "class Value:\n",
800
+ "\n",
801
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
802
+ " self.data = data\n",
803
+ " self.grad = 0.0\n",
804
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
805
+ " self._prev = set(_children)\n",
806
+ " self._op = _op\n",
807
+ " self.label = label\n",
808
+ "\n",
809
+ "\n",
810
+ " def __repr__(self):\n",
811
+ " return f\"Value(data={self.data})\"\n",
812
+ "\n",
813
+ " def __add__(self, other):\n",
814
+ " out = Value(self.data + other.data, (self, other), '+')\n",
815
+ "\n",
816
+ " def backward():\n",
817
+ " self.grad += 1.0 * out.grad #Adding it on\n",
818
+ " other.grad += 1.0 * out.grad #Adding it on\n",
819
+ "\n",
820
+ " out._backward = backward\n",
821
+ " return out\n",
822
+ "\n",
823
+ " def __mul__(self, other):\n",
824
+ " out = Value(self.data * other.data, (self, other), '*')\n",
825
+ "\n",
826
+ " def backward():\n",
827
+ " self.grad += other.data * out.grad #Adding it on\n",
828
+ " other.grad += self.data * out.grad #Adding it on\n",
829
+ "\n",
830
+ " out._backward = backward\n",
831
+ " return out\n",
832
+ "\n",
833
+ " def tanh(self):\n",
834
+ " x = self.data\n",
835
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
836
+ " out = Value(t, (self, ), 'tanh')\n",
837
+ "\n",
838
+ " def backward():\n",
839
+ " self.grad += 1 - (t**2) * out.grad #Adding it on\n",
840
+ "\n",
841
+ " out._backward = backward\n",
842
+ " return out\n",
843
+ "\n",
844
+ " def backward(self):\n",
845
+ "\n",
846
+ " topo = []\n",
847
+ " visited = set()\n",
848
+ " def build_topo(v):\n",
849
+ " if v not in visited:\n",
850
+ " visited.add(v)\n",
851
+ " for child in v._prev:\n",
852
+ " build_topo(child)\n",
853
+ " topo.append(v)\n",
854
+ "\n",
855
+ " build_topo(self)\n",
856
+ "\n",
857
+ " self.grad = 1.0\n",
858
+ " for node in reversed(topo):\n",
859
+ " node._backward()"
860
+ ]
861
+ },
862
+ {
863
+ "cell_type": "markdown",
864
+ "metadata": {
865
+ "id": "uCsFhTH7KxQl"
866
+ },
867
+ "source": [
868
+ "------------"
869
+ ]
870
+ },
871
+ {
872
+ "cell_type": "markdown",
873
+ "metadata": {
874
+ "id": "CTcVTEwEKyIl"
875
+ },
876
+ "source": [
877
+ "Now lets check those examples again"
878
+ ]
879
+ },
880
+ {
881
+ "cell_type": "code",
882
+ "execution_count": 14,
883
+ "metadata": {
884
+ "colab": {
885
+ "base_uri": "https://localhost:8080/",
886
+ "height": 82
887
+ },
888
+ "id": "0ilTK_nCKr4z",
889
+ "outputId": "892c9e37-e2e5-4f77-d76f-5b298058fc94"
890
+ },
891
+ "outputs": [
892
+ {
893
+ "data": {
894
+ "image/svg+xml": [
895
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
896
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
897
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
898
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
899
+ " -->\n",
900
+ "<!-- Title: %3 Pages: 1 -->\n",
901
+ "<svg width=\"504pt\" height=\"45pt\"\n",
902
+ " viewBox=\"0.00 0.00 504.00 45.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
903
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 41)\">\n",
904
+ "<title>%3</title>\n",
905
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-41 500,-41 500,4 -4,4\"/>\n",
906
+ "<!-- 140165248834176 -->\n",
907
+ "<g id=\"node1\" class=\"node\">\n",
908
+ "<title>140165248834176</title>\n",
909
+ "<polygon fill=\"none\" stroke=\"black\" points=\"311,-0.5 311,-36.5 496,-36.5 496,-0.5 311,-0.5\"/>\n",
910
+ "<text text-anchor=\"middle\" x=\"322.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
911
+ "<polyline fill=\"none\" stroke=\"black\" points=\"334,-0.5 334,-36.5 \"/>\n",
912
+ "<text text-anchor=\"middle\" x=\"374\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.0000</text>\n",
913
+ "<polyline fill=\"none\" stroke=\"black\" points=\"414,-0.5 414,-36.5 \"/>\n",
914
+ "<text text-anchor=\"middle\" x=\"455\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
915
+ "</g>\n",
916
+ "<!-- 140165248834176+ -->\n",
917
+ "<g id=\"node2\" class=\"node\">\n",
918
+ "<title>140165248834176+</title>\n",
919
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"248\" cy=\"-18.5\" rx=\"27\" ry=\"18\"/>\n",
920
+ "<text text-anchor=\"middle\" x=\"248\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
921
+ "</g>\n",
922
+ "<!-- 140165248834176+&#45;&gt;140165248834176 -->\n",
923
+ "<g id=\"edge1\" class=\"edge\">\n",
924
+ "<title>140165248834176+&#45;&gt;140165248834176</title>\n",
925
+ "<path fill=\"none\" stroke=\"black\" d=\"M275.04,-18.5C282.58,-18.5 291.3,-18.5 300.57,-18.5\"/>\n",
926
+ "<polygon fill=\"black\" stroke=\"black\" points=\"300.81,-22 310.81,-18.5 300.81,-15 300.81,-22\"/>\n",
927
+ "</g>\n",
928
+ "<!-- 140165248834368 -->\n",
929
+ "<g id=\"node3\" class=\"node\">\n",
930
+ "<title>140165248834368</title>\n",
931
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 185,-36.5 185,-0.5 0,-0.5\"/>\n",
932
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
933
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-0.5 23,-36.5 \"/>\n",
934
+ "<text text-anchor=\"middle\" x=\"63\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 3.0000</text>\n",
935
+ "<polyline fill=\"none\" stroke=\"black\" points=\"103,-0.5 103,-36.5 \"/>\n",
936
+ "<text text-anchor=\"middle\" x=\"144\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 2.0000</text>\n",
937
+ "</g>\n",
938
+ "<!-- 140165248834368&#45;&gt;140165248834176+ -->\n",
939
+ "<g id=\"edge2\" class=\"edge\">\n",
940
+ "<title>140165248834368&#45;&gt;140165248834176+</title>\n",
941
+ "<path fill=\"none\" stroke=\"black\" d=\"M185.01,-18.5C194.01,-18.5 202.74,-18.5 210.66,-18.5\"/>\n",
942
+ "<polygon fill=\"black\" stroke=\"black\" points=\"210.91,-22 220.91,-18.5 210.91,-15 210.91,-22\"/>\n",
943
+ "</g>\n",
944
+ "</g>\n",
945
+ "</svg>\n"
946
+ ],
947
+ "text/plain": [
948
+ "<graphviz.graphs.Digraph at 0x7f7ac3ddcac0>"
949
+ ]
950
+ },
951
+ "execution_count": 14,
952
+ "metadata": {},
953
+ "output_type": "execute_result"
954
+ }
955
+ ],
956
+ "source": [
957
+ "a = Value(3.0, label='a')\n",
958
+ "b = a + a ; b.label = 'b'\n",
959
+ "b.backward()\n",
960
+ "draw_dot(b)"
961
+ ]
962
+ },
963
+ {
964
+ "cell_type": "code",
965
+ "execution_count": 15,
966
+ "metadata": {
967
+ "colab": {
968
+ "base_uri": "https://localhost:8080/",
969
+ "height": 155
970
+ },
971
+ "id": "wg8RotOhK1DF",
972
+ "outputId": "43aeb659-02b3-44f3-f4d0-2130761f0358"
973
+ },
974
+ "outputs": [
975
+ {
976
+ "data": {
977
+ "image/svg+xml": [
978
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
979
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
980
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
981
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
982
+ " -->\n",
983
+ "<!-- Title: %3 Pages: 1 -->\n",
984
+ "<svg width=\"832pt\" height=\"100pt\"\n",
985
+ " viewBox=\"0.00 0.00 832.00 100.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
986
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 96)\">\n",
987
+ "<title>%3</title>\n",
988
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-96 828,-96 828,4 -4,4\"/>\n",
989
+ "<!-- 140165248832640 -->\n",
990
+ "<g id=\"node1\" class=\"node\">\n",
991
+ "<title>140165248832640</title>\n",
992
+ "<polygon fill=\"none\" stroke=\"black\" points=\"320.5,-55.5 320.5,-91.5 509.5,-91.5 509.5,-55.5 320.5,-55.5\"/>\n",
993
+ "<text text-anchor=\"middle\" x=\"332\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n",
994
+ "<polyline fill=\"none\" stroke=\"black\" points=\"343.5,-55.5 343.5,-91.5 \"/>\n",
995
+ "<text text-anchor=\"middle\" x=\"383.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n",
996
+ "<polyline fill=\"none\" stroke=\"black\" points=\"423.5,-55.5 423.5,-91.5 \"/>\n",
997
+ "<text text-anchor=\"middle\" x=\"466.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;6.0000</text>\n",
998
+ "</g>\n",
999
+ "<!-- 140165248834416* -->\n",
1000
+ "<g id=\"node5\" class=\"node\">\n",
1001
+ "<title>140165248834416*</title>\n",
1002
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"573\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n",
1003
+ "<text text-anchor=\"middle\" x=\"573\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
1004
+ "</g>\n",
1005
+ "<!-- 140165248832640&#45;&gt;140165248834416* -->\n",
1006
+ "<g id=\"edge9\" class=\"edge\">\n",
1007
+ "<title>140165248832640&#45;&gt;140165248834416*</title>\n",
1008
+ "<path fill=\"none\" stroke=\"black\" d=\"M509.9,-56.65C519.42,-54.94 528.62,-53.29 536.89,-51.8\"/>\n",
1009
+ "<polygon fill=\"black\" stroke=\"black\" points=\"537.59,-55.23 546.81,-50.02 536.35,-48.34 537.59,-55.23\"/>\n",
1010
+ "</g>\n",
1011
+ "<!-- 140165248832640+ -->\n",
1012
+ "<g id=\"node2\" class=\"node\">\n",
1013
+ "<title>140165248832640+</title>\n",
1014
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"257\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
1015
+ "<text text-anchor=\"middle\" x=\"257\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n",
1016
+ "</g>\n",
1017
+ "<!-- 140165248832640+&#45;&gt;140165248832640 -->\n",
1018
+ "<g id=\"edge1\" class=\"edge\">\n",
1019
+ "<title>140165248832640+&#45;&gt;140165248832640</title>\n",
1020
+ "<path fill=\"none\" stroke=\"black\" d=\"M284.11,-73.5C291.84,-73.5 300.8,-73.5 310.35,-73.5\"/>\n",
1021
+ "<polygon fill=\"black\" stroke=\"black\" points=\"310.48,-77 320.48,-73.5 310.48,-70 310.48,-77\"/>\n",
1022
+ "</g>\n",
1023
+ "<!-- 140165248829088 -->\n",
1024
+ "<g id=\"node3\" class=\"node\">\n",
1025
+ "<title>140165248829088</title>\n",
1026
+ "<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-55.5 2.5,-91.5 191.5,-91.5 191.5,-55.5 2.5,-55.5\"/>\n",
1027
+ "<text text-anchor=\"middle\" x=\"14\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n",
1028
+ "<polyline fill=\"none\" stroke=\"black\" points=\"25.5,-55.5 25.5,-91.5 \"/>\n",
1029
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 3.0000</text>\n",
1030
+ "<polyline fill=\"none\" stroke=\"black\" points=\"105.5,-55.5 105.5,-91.5 \"/>\n",
1031
+ "<text text-anchor=\"middle\" x=\"148.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;8.0000</text>\n",
1032
+ "</g>\n",
1033
+ "<!-- 140165248829088&#45;&gt;140165248832640+ -->\n",
1034
+ "<g id=\"edge8\" class=\"edge\">\n",
1035
+ "<title>140165248829088&#45;&gt;140165248832640+</title>\n",
1036
+ "<path fill=\"none\" stroke=\"black\" d=\"M191.74,-73.5C201.55,-73.5 211.04,-73.5 219.59,-73.5\"/>\n",
1037
+ "<polygon fill=\"black\" stroke=\"black\" points=\"219.87,-77 229.87,-73.5 219.87,-70 219.87,-77\"/>\n",
1038
+ "</g>\n",
1039
+ "<!-- 140165248833408* -->\n",
1040
+ "<g id=\"node7\" class=\"node\">\n",
1041
+ "<title>140165248833408*</title>\n",
1042
+ "<ellipse fill=\"none\" stroke=\"black\" cx=\"257\" cy=\"-18.5\" rx=\"27\" ry=\"18\"/>\n",
1043
+ "<text text-anchor=\"middle\" x=\"257\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n",
1044
+ "</g>\n",
1045
+ "<!-- 140165248829088&#45;&gt;140165248833408* -->\n",
1046
+ "<g id=\"edge7\" class=\"edge\">\n",
1047
+ "<title>140165248829088&#45;&gt;140165248833408*</title>\n",
1048
+ "<path fill=\"none\" stroke=\"black\" d=\"M166.84,-55.49C176.04,-52.69 185.3,-49.67 194,-46.5 204.51,-42.67 215.73,-37.84 225.7,-33.28\"/>\n",
1049
+ "<polygon fill=\"black\" stroke=\"black\" points=\"227.23,-36.43 234.81,-29.02 224.27,-30.09 227.23,-36.43\"/>\n",
1050
+ "</g>\n",
1051
+ "<!-- 140165248834416 -->\n",
1052
+ "<g id=\"node4\" class=\"node\">\n",
1053
+ "<title>140165248834416</title>\n",
1054
+ "<polygon fill=\"none\" stroke=\"black\" points=\"636,-27.5 636,-63.5 824,-63.5 824,-27.5 636,-27.5\"/>\n",
1055
+ "<text text-anchor=\"middle\" x=\"646.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n",
1056
+ "<polyline fill=\"none\" stroke=\"black\" points=\"657,-27.5 657,-63.5 \"/>\n",
1057
+ "<text text-anchor=\"middle\" x=\"699.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
1058
+ "<polyline fill=\"none\" stroke=\"black\" points=\"742,-27.5 742,-63.5 \"/>\n",
1059
+ "<text text-anchor=\"middle\" x=\"783\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
1060
+ "</g>\n",
1061
+ "<!-- 140165248834416*&#45;&gt;140165248834416 -->\n",
1062
+ "<g id=\"edge2\" class=\"edge\">\n",
1063
+ "<title>140165248834416*&#45;&gt;140165248834416</title>\n",
1064
+ "<path fill=\"none\" stroke=\"black\" d=\"M600.29,-45.5C607.78,-45.5 616.42,-45.5 625.6,-45.5\"/>\n",
1065
+ "<polygon fill=\"black\" stroke=\"black\" points=\"625.76,-49 635.76,-45.5 625.76,-42 625.76,-49\"/>\n",
1066
+ "</g>\n",
1067
+ "<!-- 140165248833408 -->\n",
1068
+ "<g id=\"node6\" class=\"node\">\n",
1069
+ "<title>140165248833408</title>\n",
1070
+ "<polygon fill=\"none\" stroke=\"black\" points=\"320,-0.5 320,-36.5 510,-36.5 510,-0.5 320,-0.5\"/>\n",
1071
+ "<text text-anchor=\"middle\" x=\"331.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n",
1072
+ "<polyline fill=\"none\" stroke=\"black\" points=\"343,-0.5 343,-36.5 \"/>\n",
1073
+ "<text text-anchor=\"middle\" x=\"385.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
1074
+ "<polyline fill=\"none\" stroke=\"black\" points=\"428,-0.5 428,-36.5 \"/>\n",
1075
+ "<text text-anchor=\"middle\" x=\"469\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
1076
+ "</g>\n",
1077
+ "<!-- 140165248833408&#45;&gt;140165248834416* -->\n",
1078
+ "<g id=\"edge6\" class=\"edge\">\n",
1079
+ "<title>140165248833408&#45;&gt;140165248834416*</title>\n",
1080
+ "<path fill=\"none\" stroke=\"black\" d=\"M510.34,-34.83C519.62,-36.44 528.58,-37.99 536.66,-39.38\"/>\n",
1081
+ "<polygon fill=\"black\" stroke=\"black\" points=\"536.27,-42.87 546.72,-41.12 537.46,-35.97 536.27,-42.87\"/>\n",
1082
+ "</g>\n",
1083
+ "<!-- 140165248833408*&#45;&gt;140165248833408 -->\n",
1084
+ "<g id=\"edge3\" class=\"edge\">\n",
1085
+ "<title>140165248833408*&#45;&gt;140165248833408</title>\n",
1086
+ "<path fill=\"none\" stroke=\"black\" d=\"M284.11,-18.5C291.63,-18.5 300.32,-18.5 309.58,-18.5\"/>\n",
1087
+ "<polygon fill=\"black\" stroke=\"black\" points=\"309.81,-22 319.81,-18.5 309.81,-15 309.81,-22\"/>\n",
1088
+ "</g>\n",
1089
+ "<!-- 140165248833984 -->\n",
1090
+ "<g id=\"node8\" class=\"node\">\n",
1091
+ "<title>140165248833984</title>\n",
1092
+ "<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 194,-36.5 194,-0.5 0,-0.5\"/>\n",
1093
+ "<text text-anchor=\"middle\" x=\"11.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n",
1094
+ "<polyline fill=\"none\" stroke=\"black\" points=\"23,-0.5 23,-36.5 \"/>\n",
1095
+ "<text text-anchor=\"middle\" x=\"65.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n",
1096
+ "<polyline fill=\"none\" stroke=\"black\" points=\"108,-0.5 108,-36.5 \"/>\n",
1097
+ "<text text-anchor=\"middle\" x=\"151\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;3.0000</text>\n",
1098
+ "</g>\n",
1099
+ "<!-- 140165248833984&#45;&gt;140165248832640+ -->\n",
1100
+ "<g id=\"edge5\" class=\"edge\">\n",
1101
+ "<title>140165248833984&#45;&gt;140165248832640+</title>\n",
1102
+ "<path fill=\"none\" stroke=\"black\" d=\"M163.94,-36.53C174.08,-39.65 184.37,-43.01 194,-46.5 204.27,-50.22 215.26,-54.8 225.09,-59.12\"/>\n",
1103
+ "<polygon fill=\"black\" stroke=\"black\" points=\"223.91,-62.43 234.47,-63.32 226.77,-56.04 223.91,-62.43\"/>\n",
1104
+ "</g>\n",
1105
+ "<!-- 140165248833984&#45;&gt;140165248833408* -->\n",
1106
+ "<g id=\"edge4\" class=\"edge\">\n",
1107
+ "<title>140165248833984&#45;&gt;140165248833408*</title>\n",
1108
+ "<path fill=\"none\" stroke=\"black\" d=\"M194.01,-18.5C202.98,-18.5 211.65,-18.5 219.51,-18.5\"/>\n",
1109
+ "<polygon fill=\"black\" stroke=\"black\" points=\"219.7,-22 229.7,-18.5 219.7,-15 219.7,-22\"/>\n",
1110
+ "</g>\n",
1111
+ "</g>\n",
1112
+ "</svg>\n"
1113
+ ],
1114
+ "text/plain": [
1115
+ "<graphviz.graphs.Digraph at 0x7f7ac3ddce20>"
1116
+ ]
1117
+ },
1118
+ "execution_count": 15,
1119
+ "metadata": {},
1120
+ "output_type": "execute_result"
1121
+ }
1122
+ ],
1123
+ "source": [
1124
+ "a = Value(-2.0, label='a')\n",
1125
+ "b = Value(3.0, label='b')\n",
1126
+ "d = a * b ; d.label = 'd'\n",
1127
+ "e = a + b ; e.label = 'e'\n",
1128
+ "f = d * e ; f.label = 'f'\n",
1129
+ "\n",
1130
+ "f.backward()\n",
1131
+ "draw_dot(f)"
1132
+ ]
1133
+ },
1134
+ {
1135
+ "cell_type": "markdown",
1136
+ "metadata": {
1137
+ "id": "tYNyI5tWK7_-"
1138
+ },
1139
+ "source": [
1140
+ "And now we've got the correct gradient values! \\\n",
1141
+ "\\\n",
1142
+ "So after the values have been deposited the first time, if it is called again, then it deposites it again on top it next. \\\n",
1143
+ "\\\n",
1144
+ "Example 1: \\\n",
1145
+ "1 + 1 = **2** \\\n",
1146
+ "\\\n",
1147
+ "Example 2: \\\n",
1148
+ "a -> -6 + 3 = **-3** \\\n",
1149
+ "b -> -6 + (- 2) = **-8**\n",
1150
+ "\n",
1151
+ "Hence, resolving the issue :)"
1152
+ ]
1153
+ }
1154
+ ],
1155
+ "metadata": {
1156
+ "colab": {
1157
+ "provenance": []
1158
+ },
1159
+ "kernelspec": {
1160
+ "display_name": "Python 3",
1161
+ "name": "python3"
1162
+ },
1163
+ "language_info": {
1164
+ "name": "python"
1165
+ }
1166
+ },
1167
+ "nbformat": 4,
1168
+ "nbformat_minor": 0
1169
+ }
9_expanding_tanh_into_more_operations.ipynb ADDED
@@ -0,0 +1,410 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "from graphviz import Digraph\n",
21
+ "\n",
22
+ "def trace(root):\n",
23
+ " #Builds a set of all nodes and edges in a graph\n",
24
+ " nodes, edges = set(), set()\n",
25
+ " def build(v):\n",
26
+ " if v not in nodes:\n",
27
+ " nodes.add(v)\n",
28
+ " for child in v._prev:\n",
29
+ " edges.add((child, v))\n",
30
+ " build(child)\n",
31
+ " build(root)\n",
32
+ " return nodes, edges\n",
33
+ "\n",
34
+ "def draw_dot(root):\n",
35
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
36
+ "\n",
37
+ " nodes, edges = trace(root)\n",
38
+ " for n in nodes:\n",
39
+ " uid = str(id(n))\n",
40
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
41
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
42
+ " if n._op:\n",
43
+ " #If this value is a result of some operation, then create an op node for it\n",
44
+ " dot.node(name = uid + n._op, label=n._op)\n",
45
+ " #and connect this node to it\n",
46
+ " dot.edge(uid + n._op, uid)\n",
47
+ "\n",
48
+ " for n1, n2 in edges:\n",
49
+ " #Connect n1 to the node of n2\n",
50
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
51
+ "\n",
52
+ " return dot"
53
+ ],
54
+ "metadata": {
55
+ "id": "T0rN8d146jvF"
56
+ },
57
+ "execution_count": 1,
58
+ "outputs": []
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "source": [
63
+ "import math"
64
+ ],
65
+ "metadata": {
66
+ "id": "JlYxBvFK0AjA"
67
+ },
68
+ "execution_count": 2,
69
+ "outputs": []
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "source": [
74
+ "class Value:\n",
75
+ "\n",
76
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
77
+ " self.data = data\n",
78
+ " self.grad = 0.0\n",
79
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
80
+ " self._prev = set(_children)\n",
81
+ " self._op = _op\n",
82
+ " self.label = label\n",
83
+ "\n",
84
+ "\n",
85
+ " def __repr__(self):\n",
86
+ " return f\"Value(data={self.data})\"\n",
87
+ "\n",
88
+ " def __add__(self, other):\n",
89
+ " out = Value(self.data + other.data, (self, other), '+')\n",
90
+ "\n",
91
+ " def backward():\n",
92
+ " self.grad += 1.0 * out.grad #Adding it on\n",
93
+ " other.grad += 1.0 * out.grad #Adding it on\n",
94
+ "\n",
95
+ " out._backward = backward\n",
96
+ " return out\n",
97
+ "\n",
98
+ " def __mul__(self, other):\n",
99
+ " out = Value(self.data * other.data, (self, other), '*')\n",
100
+ "\n",
101
+ " def backward():\n",
102
+ " self.grad += other.data * out.grad #Adding it on\n",
103
+ " other.grad += self.data * out.grad #Adding it on\n",
104
+ "\n",
105
+ " out._backward = backward\n",
106
+ " return out\n",
107
+ "\n",
108
+ " def tanh(self):\n",
109
+ " x = self.data\n",
110
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
111
+ " out = Value(t, (self, ), 'tanh')\n",
112
+ "\n",
113
+ " def backward():\n",
114
+ " self.grad += 1 - (t**2) * out.grad #Adding it on\n",
115
+ "\n",
116
+ " out._backward = backward\n",
117
+ " return out\n",
118
+ "\n",
119
+ " def backward(self):\n",
120
+ "\n",
121
+ " topo = []\n",
122
+ " visited = set()\n",
123
+ " def build_topo(v):\n",
124
+ " if v not in visited:\n",
125
+ " visited.add(v)\n",
126
+ " for child in v._prev:\n",
127
+ " build_topo(child)\n",
128
+ " topo.append(v)\n",
129
+ "\n",
130
+ " build_topo(self)\n",
131
+ "\n",
132
+ " self.grad = 1.0\n",
133
+ " for node in reversed(topo):\n",
134
+ " node._backward()"
135
+ ],
136
+ "metadata": {
137
+ "id": "XBafkiKxJ-in"
138
+ },
139
+ "execution_count": 3,
140
+ "outputs": []
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "source": [
145
+ "#Inputs x1, x2 of the neuron\n",
146
+ "x1 = Value(2.0, label='x1')\n",
147
+ "x2 = Value(0.0, label='x2')\n",
148
+ "\n",
149
+ "#Weights w1, w2 of the neuron - The synaptic values\n",
150
+ "w1 = Value(-3.0, label='w1')\n",
151
+ "w2 = Value(1.0, label='w2')\n",
152
+ "\n",
153
+ "#The bias of the neuron\n",
154
+ "b = Value(6.8813735870195432, label='b')\n",
155
+ "\n",
156
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
157
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
158
+ "\n",
159
+ "#The summation\n",
160
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'\n",
161
+ "\n",
162
+ "#n is basically the cell body, but without the activation function\n",
163
+ "n = x1w1x2w2 + b; n.label = 'n'\n",
164
+ "\n",
165
+ "#Now we pass n to the activation function\n",
166
+ "o = n.tanh(); o.label = 'o'"
167
+ ],
168
+ "metadata": {
169
+ "id": "S3HaLbW_zvne"
170
+ },
171
+ "execution_count": 8,
172
+ "outputs": []
173
+ },
174
+ {
175
+ "cell_type": "code",
176
+ "source": [
177
+ "#o.grad = 1.0\n",
178
+ "o.backward()\n",
179
+ "\n",
180
+ "draw_dot(o)"
181
+ ],
182
+ "metadata": {
183
+ "colab": {
184
+ "base_uri": "https://localhost:8080/",
185
+ "height": 322
186
+ },
187
+ "id": "P6OmwdXGGoJZ",
188
+ "outputId": "b5f3e9c2-c28f-4272-c328-db0c0536f5b5"
189
+ },
190
+ "execution_count": 9,
191
+ "outputs": [
192
+ {
193
+ "output_type": "execute_result",
194
+ "data": {
195
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"1575pt\" height=\"210pt\"\n viewBox=\"0.00 0.00 1575.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-206 1571,-206 1571,4 -4,4\"/>\n<!-- 134557530771504 -->\n<g id=\"node1\" class=\"node\">\n<title>134557530771504</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-165.5 4,-201.5 196,-201.5 196,-165.5 4,-165.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-165.5 34,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-165.5 114,-201.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-179.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557530506528* -->\n<g id=\"node12\" class=\"node\">\n<title>134557530506528*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 134557530771504&#45;&gt;134557530506528* -->\n<g id=\"edge6\" class=\"edge\">\n<title>134557530771504&#45;&gt;134557530506528*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-165.44C181.84,-162.67 191.2,-159.67 200,-156.5 210.53,-152.71 221.75,-147.9 231.72,-143.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-146.48 240.82,-139.07 230.28,-140.14 233.25,-146.48\"/>\n</g>\n<!-- 134557530774768 -->\n<g id=\"node2\" class=\"node\">\n<title>134557530774768</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 200,-91.5 200,-55.5 0,-55.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-55.5 33,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-55.5 118,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 134557530518288* -->\n<g id=\"node5\" class=\"node\">\n<title>134557530518288*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 134557530774768&#45;&gt;134557530518288* -->\n<g id=\"edge9\" class=\"edge\">\n<title>134557530774768&#45;&gt;134557530518288*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M200.21,-73.5C209.2,-73.5 217.86,-73.5 225.7,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.85,-77 235.85,-73.5 225.85,-70 225.85,-77\"/>\n</g>\n<!-- 134557530779376 -->\n<g id=\"node3\" class=\"node\">\n<title>134557530779376</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-27.5 716,-63.5 901,-63.5 901,-27.5 716,-27.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-27.5 739,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-27.5 819,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531013424+ -->\n<g id=\"node9\" class=\"node\">\n<title>134557531013424+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 134557530779376&#45;&gt;134557531013424+ -->\n<g id=\"edge12\" class=\"edge\">\n<title>134557530779376&#45;&gt;134557531013424+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-58.01C926,-61.43 951.59,-64.93 971.37,-67.63\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.05,-71.12 981.43,-69.01 972,-64.18 971.05,-71.12\"/>\n</g>\n<!-- 134557530518288 -->\n<g id=\"node4\" class=\"node\">\n<title>134557530518288</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-55.5 326,-91.5 546,-91.5 546,-55.5 326,-55.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-55.5 379,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-55.5 464,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531020672+ -->\n<g id=\"node15\" class=\"node\">\n<title>134557531020672+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 134557530518288&#45;&gt;134557531020672+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>134557530518288&#45;&gt;134557531020672+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-90.75C555.64,-92.23 564.6,-93.65 572.65,-94.92\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.23,-98.4 582.65,-96.5 573.32,-91.48 572.23,-98.4\"/>\n</g>\n<!-- 134557530518288*&#45;&gt;134557530518288 -->\n<g id=\"edge1\" class=\"edge\">\n<title>134557530518288*&#45;&gt;134557530518288</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-73.5C297.77,-73.5 306.37,-73.5 315.6,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-77 325.84,-73.5 315.84,-70 315.84,-77\"/>\n</g>\n<!-- 134557531017504 -->\n<g id=\"node6\" class=\"node\">\n<title>134557531017504</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-54.5 1382,-90.5 1567,-90.5 1567,-54.5 1382,-54.5\"/>\n<text text-anchor=\"middle\" x=\"1393.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1405,-54.5 1405,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1445\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1485,-54.5 1485,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1526\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 134557531017504tanh -->\n<g id=\"node7\" class=\"node\">\n<title>134557531017504tanh</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">tanh</text>\n</g>\n<!-- 134557531017504tanh&#45;&gt;134557531017504 -->\n<g id=\"edge2\" class=\"edge\">\n<title>134557531017504tanh&#45;&gt;134557531017504</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.04,-72.5C1353.58,-72.5 1362.3,-72.5 1371.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.81,-76 1381.81,-72.5 1371.81,-69 1371.81,-76\"/>\n</g>\n<!-- 134557531013424 -->\n<g id=\"node8\" class=\"node\">\n<title>134557531013424</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-54.5 1071,-90.5 1256,-90.5 1256,-54.5 1071,-54.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-54.5 1094,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-54.5 1174,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531013424&#45;&gt;134557531017504tanh -->\n<g id=\"edge14\" class=\"edge\">\n<title>134557531013424&#45;&gt;134557531017504tanh</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-72.5C1265.01,-72.5 1273.74,-72.5 1281.66,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1281.91,-76 1291.91,-72.5 1281.91,-69 1281.91,-76\"/>\n</g>\n<!-- 134557531013424+&#45;&gt;134557531013424 -->\n<g id=\"edge3\" class=\"edge\">\n<title>134557531013424+&#45;&gt;134557531013424</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-72.5C1042.58,-72.5 1051.3,-72.5 1060.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-76 1070.81,-72.5 1060.81,-69 1060.81,-76\"/>\n</g>\n<!-- 134557530768192 -->\n<g id=\"node10\" class=\"node\">\n<title>134557530768192</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2,-0.5 2,-36.5 198,-36.5 198,-0.5 2,-0.5\"/>\n<text text-anchor=\"middle\" x=\"17\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"32,-0.5 32,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"72\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"112,-0.5 112,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;1.5000</text>\n</g>\n<!-- 134557530768192&#45;&gt;134557530518288* -->\n<g id=\"edge8\" class=\"edge\">\n<title>134557530768192&#45;&gt;134557530518288*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-36.5C179.65,-39.61 190.16,-42.98 200,-46.5 210.28,-50.17 221.28,-54.74 231.11,-59.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-62.37 240.48,-63.27 232.79,-55.99 229.93,-62.37\"/>\n</g>\n<!-- 134557530506528 -->\n<g id=\"node11\" class=\"node\">\n<title>134557530506528</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-110.5 328.5,-146.5 543.5,-146.5 543.5,-110.5 328.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-110.5 381.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-110.5 461.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557530506528&#45;&gt;134557531020672+ -->\n<g id=\"edge13\" class=\"edge\">\n<title>134557530506528&#45;&gt;134557531020672+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-111.01C554.01,-109.34 563.76,-107.74 572.44,-106.32\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.17,-109.75 582.47,-104.68 572.04,-102.84 573.17,-109.75\"/>\n</g>\n<!-- 134557530506528*&#45;&gt;134557530506528 -->\n<g id=\"edge4\" class=\"edge\">\n<title>134557530506528*&#45;&gt;134557530506528</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-128.5C298.51,-128.5 308.08,-128.5 318.36,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-132 328.39,-128.5 318.39,-125 318.39,-132\"/>\n</g>\n<!-- 134557530782592 -->\n<g id=\"node13\" class=\"node\">\n<title>134557530782592</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-110.5 2.5,-146.5 197.5,-146.5 197.5,-110.5 2.5,-110.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-110.5 35.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-110.5 115.5,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 134557530782592&#45;&gt;134557530506528* -->\n<g id=\"edge11\" class=\"edge\">\n<title>134557530782592&#45;&gt;134557530506528*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-128.5C207.65,-128.5 217.05,-128.5 225.52,-128.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-132 235.7,-128.5 225.7,-125 225.7,-132\"/>\n</g>\n<!-- 134557531020672 -->\n<g id=\"node14\" class=\"node\">\n<title>134557531020672</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-82.5 672,-118.5 945,-118.5 945,-82.5 672,-82.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-82.5 778,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-82.5 863,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531020672&#45;&gt;134557531013424+ -->\n<g id=\"edge10\" class=\"edge\">\n<title>134557531020672&#45;&gt;134557531013424+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M936.53,-82.49C949.09,-80.71 960.99,-79.02 971.3,-77.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-80.99 981.44,-76.12 971.05,-74.06 972.03,-80.99\"/>\n</g>\n<!-- 134557531020672+&#45;&gt;134557531020672 -->\n<g id=\"edge5\" class=\"edge\">\n<title>134557531020672+&#45;&gt;134557531020672</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-100.5C643.7,-100.5 652.41,-100.5 661.87,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-104 671.98,-100.5 661.98,-97 661.98,-104\"/>\n</g>\n</g>\n</svg>\n",
196
+ "text/plain": [
197
+ "<graphviz.graphs.Digraph at 0x7a611dd6a6b0>"
198
+ ]
199
+ },
200
+ "metadata": {},
201
+ "execution_count": 9
202
+ }
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "markdown",
207
+ "source": [
208
+ "--------------------"
209
+ ],
210
+ "metadata": {
211
+ "id": "476LQxU3FXL4"
212
+ }
213
+ },
214
+ {
215
+ "cell_type": "markdown",
216
+ "source": [
217
+ "### **Updating the Value object**"
218
+ ],
219
+ "metadata": {
220
+ "id": "vP0ZtM9HFX9p"
221
+ }
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "source": [
226
+ "class Value:\n",
227
+ "\n",
228
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
229
+ " self.data = data\n",
230
+ " self.grad = 0.0\n",
231
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
232
+ " self._prev = set(_children)\n",
233
+ " self._op = _op\n",
234
+ " self.label = label\n",
235
+ "\n",
236
+ "\n",
237
+ " def __repr__(self):\n",
238
+ " return f\"Value(data={self.data})\"\n",
239
+ "\n",
240
+ " def __add__(self, other):\n",
241
+ " other = other if isinstance(other, Value) else Value(other)\n",
242
+ " out = Value(self.data + other.data, (self, other), '+')\n",
243
+ "\n",
244
+ " def backward():\n",
245
+ " self.grad += 1.0 * out.grad\n",
246
+ " other.grad += 1.0 * out.grad\n",
247
+ "\n",
248
+ " out._backward = backward\n",
249
+ " return out\n",
250
+ "\n",
251
+ " def __mul__(self, other):\n",
252
+ " other = other if isinstance(other, Value) else Value(other)\n",
253
+ " out = Value(self.data * other.data, (self, other), '*')\n",
254
+ "\n",
255
+ " def backward():\n",
256
+ " self.grad += other.data * out.grad\n",
257
+ " other.grad += self.data * out.grad\n",
258
+ " out._backward = backward\n",
259
+ " return out\n",
260
+ "\n",
261
+ " def __rmul__(self, other): #other * self\n",
262
+ " return self * other\n",
263
+ "\n",
264
+ " def __truediv__(self, other): #self/other\n",
265
+ " return self * other**-1\n",
266
+ "\n",
267
+ " def __neg__(self):\n",
268
+ " return self * -1\n",
269
+ "\n",
270
+ " def __sub__(self, other): #self - other\n",
271
+ " return self + (-other)\n",
272
+ "\n",
273
+ " def __pow__(self, other):\n",
274
+ " assert isinstance(other, (int, float)), \"only supporting int/float powers for now\"\n",
275
+ " out = Value(self.data ** other, (self, ), f\"**{other}\")\n",
276
+ "\n",
277
+ " def backward():\n",
278
+ " self.grad += (other * (self.data ** (other - 1))) * out.grad\n",
279
+ "\n",
280
+ " out._backward = backward\n",
281
+ " return out\n",
282
+ "\n",
283
+ " def tanh(self):\n",
284
+ " x = self.data\n",
285
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
286
+ " out = Value(t, (self, ), 'tanh')\n",
287
+ "\n",
288
+ " def backward():\n",
289
+ " self.grad += 1 - (t**2) * out.grad\n",
290
+ "\n",
291
+ " out._backward = backward\n",
292
+ " return out\n",
293
+ "\n",
294
+ " def exp(self):\n",
295
+ " x = self.data\n",
296
+ " out = Value(math.exp(x), (self, ), 'exp') #We merged t and out, into just out\n",
297
+ "\n",
298
+ " def backward():\n",
299
+ " self.grad += out.data * out.grad\n",
300
+ "\n",
301
+ " out._backward = backward\n",
302
+ " return out\n",
303
+ "\n",
304
+ " def backward(self):\n",
305
+ "\n",
306
+ " topo = []\n",
307
+ " visited = set()\n",
308
+ " def build_topo(v):\n",
309
+ " if v not in visited:\n",
310
+ " visited.add(v)\n",
311
+ " for child in v._prev:\n",
312
+ " build_topo(child)\n",
313
+ " topo.append(v)\n",
314
+ "\n",
315
+ " build_topo(self)\n",
316
+ "\n",
317
+ " self.grad = 1.0\n",
318
+ " for node in reversed(topo):\n",
319
+ " node._backward()"
320
+ ],
321
+ "metadata": {
322
+ "id": "tA0zbyEwFbD5"
323
+ },
324
+ "execution_count": 10,
325
+ "outputs": []
326
+ },
327
+ {
328
+ "cell_type": "code",
329
+ "source": [
330
+ "x1 = Value(2.0, label='x1')\n",
331
+ "x2 = Value(0.0, label='x2')\n",
332
+ "\n",
333
+ "w1 = Value(-3.0, label='w1')\n",
334
+ "w2 = Value(1.0, label='w2')\n",
335
+ "\n",
336
+ "b = Value(6.8813735870195432, label='b')\n",
337
+ "\n",
338
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
339
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
340
+ "\n",
341
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'\n",
342
+ "\n",
343
+ "n = x1w1x2w2 + b; n.label = 'n'\n",
344
+ "\n",
345
+ "#o = n.tanh(); o.label = 'o'\n",
346
+ "\n",
347
+ "#Spliting up of the tanh function\n",
348
+ "\n",
349
+ "e = (2*n).exp()\n",
350
+ "o = (e - 1) / (e + 1)\n",
351
+ "o.label = 'o'"
352
+ ],
353
+ "metadata": {
354
+ "id": "Ounbj2XwHSZ1"
355
+ },
356
+ "execution_count": 11,
357
+ "outputs": []
358
+ },
359
+ {
360
+ "cell_type": "code",
361
+ "source": [
362
+ "o.backward()\n",
363
+ "\n",
364
+ "draw_dot(o)"
365
+ ],
366
+ "metadata": {
367
+ "colab": {
368
+ "base_uri": "https://localhost:8080/",
369
+ "height": 360
370
+ },
371
+ "id": "FomTAJvaIFcb",
372
+ "outputId": "c1de2563-fad8-4049-9b37-84254d4b45c1"
373
+ },
374
+ "execution_count": 13,
375
+ "outputs": [
376
+ {
377
+ "output_type": "execute_result",
378
+ "data": {
379
+ "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.43.0 (0)\n -->\n<!-- Title: %3 Pages: 1 -->\n<svg width=\"2944pt\" height=\"239pt\"\n viewBox=\"0.00 0.00 2944.00 239.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 235)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-235 2940,-235 2940,4 -4,4\"/>\n<!-- 134557531141120 -->\n<g id=\"node1\" class=\"node\">\n<title>134557531141120</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-194.5 0,-230.5 200,-230.5 200,-194.5 0,-194.5\"/>\n<text text-anchor=\"middle\" x=\"16.5\" y=\"-208.8\" font-family=\"Times,serif\" font-size=\"14.00\">w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"33,-194.5 33,-230.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-208.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"118,-194.5 118,-230.5 \"/>\n<text text-anchor=\"middle\" x=\"159\" y=\"-208.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 134557531129936* -->\n<g id=\"node5\" class=\"node\">\n<title>134557531129936*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-157.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 134557531141120&#45;&gt;134557531129936* -->\n<g id=\"edge19\" class=\"edge\">\n<title>134557531141120&#45;&gt;134557531129936*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M172.53,-194.44C181.84,-191.67 191.2,-188.67 200,-185.5 210.53,-181.71 221.75,-176.9 231.72,-172.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.25,-175.48 240.82,-168.07 230.28,-169.14 233.25,-175.48\"/>\n</g>\n<!-- 134557531141168 -->\n<g id=\"node2\" class=\"node\">\n<title>134557531141168</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2443,-31.5 2443,-67.5 2625,-67.5 2625,-31.5 2443,-31.5\"/>\n<text text-anchor=\"middle\" x=\"2453\" y=\"-45.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2463,-31.5 2463,-67.5 \"/>\n<text text-anchor=\"middle\" x=\"2503\" y=\"-45.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.1464</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2543,-31.5 2543,-67.5 \"/>\n<text text-anchor=\"middle\" x=\"2584\" y=\"-45.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.8284</text>\n</g>\n<!-- 134557531141312* -->\n<g id=\"node11\" class=\"node\">\n<title>134557531141312*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"2688\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"2688\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 134557531141168&#45;&gt;134557531141312* -->\n<g id=\"edge16\" class=\"edge\">\n<title>134557531141168&#45;&gt;134557531141312*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M2625.18,-63.15C2634.42,-64.54 2643.38,-65.9 2651.47,-67.12\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"2651.15,-70.61 2661.56,-68.65 2652.19,-63.69 2651.15,-70.61\"/>\n</g>\n<!-- 134557531141168**&#45;1 -->\n<g id=\"node3\" class=\"node\">\n<title>134557531141168**&#45;1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"2316\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"2316\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">**&#45;1</text>\n</g>\n<!-- 134557531141168**&#45;1&#45;&gt;134557531141168 -->\n<g id=\"edge1\" class=\"edge\">\n<title>134557531141168**&#45;1&#45;&gt;134557531141168</title>\n<path fill=\"none\" stroke=\"black\" d=\"M2343.05,-45.98C2365.53,-46.4 2399.48,-47.03 2432.5,-47.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"2432.64,-51.14 2442.7,-47.83 2432.77,-44.14 2432.64,-51.14\"/>\n</g>\n<!-- 134557531129936 -->\n<g id=\"node4\" class=\"node\">\n<title>134557531129936</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"326,-139.5 326,-175.5 546,-175.5 546,-139.5 326,-139.5\"/>\n<text text-anchor=\"middle\" x=\"352.5\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"379,-139.5 379,-175.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"464,-139.5 464,-175.5 \"/>\n<text text-anchor=\"middle\" x=\"505\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531131184+ -->\n<g id=\"node18\" class=\"node\">\n<title>134557531131184+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"609\" cy=\"-129.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"609\" y=\"-125.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 134557531129936&#45;&gt;134557531131184+ -->\n<g id=\"edge15\" class=\"edge\">\n<title>134557531129936&#45;&gt;134557531131184+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M546.27,-139.61C555.64,-138.07 564.6,-136.61 572.65,-135.29\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"573.35,-138.72 582.65,-133.65 572.22,-131.81 573.35,-138.72\"/>\n</g>\n<!-- 134557531129936*&#45;&gt;134557531129936 -->\n<g id=\"edge2\" class=\"edge\">\n<title>134557531129936*&#45;&gt;134557531129936</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-157.5C297.77,-157.5 306.37,-157.5 315.6,-157.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"315.84,-161 325.84,-157.5 315.84,-154 315.84,-161\"/>\n</g>\n<!-- 134557531132576 -->\n<g id=\"node6\" class=\"node\">\n<title>134557531132576</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2225,-82.5 2225,-118.5 2407,-118.5 2407,-82.5 2225,-82.5\"/>\n<text text-anchor=\"middle\" x=\"2235\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2245,-82.5 2245,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"2285\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.8284</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2325,-82.5 2325,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"2366\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.1464</text>\n</g>\n<!-- 134557531132576&#45;&gt;134557531141312* -->\n<g id=\"edge17\" class=\"edge\">\n<title>134557531132576&#45;&gt;134557531141312*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M2407.31,-93.9C2468.7,-89.37 2551.76,-83.19 2625,-77.5 2633.42,-76.85 2642.48,-76.13 2651.01,-75.44\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"2651.37,-78.92 2661.06,-74.63 2650.81,-71.94 2651.37,-78.92\"/>\n</g>\n<!-- 134557531132576+ -->\n<g id=\"node7\" class=\"node\">\n<title>134557531132576+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1940\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1940\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 134557531132576+&#45;&gt;134557531132576 -->\n<g id=\"edge3\" class=\"edge\">\n<title>134557531132576+&#45;&gt;134557531132576</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1967,-100.5C2017.46,-100.5 2131.06,-100.5 2214.81,-100.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"2214.87,-104 2224.87,-100.5 2214.87,-97 2214.87,-104\"/>\n</g>\n<!-- 134557531133104 -->\n<g id=\"node8\" class=\"node\">\n<title>134557531133104</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1382,-55.5 1382,-91.5 1564,-91.5 1564,-55.5 1382,-55.5\"/>\n<text text-anchor=\"middle\" x=\"1392\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1402,-55.5 1402,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"1442\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.7627</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1482,-55.5 1482,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"1523\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.2500</text>\n</g>\n<!-- 134557531132816exp -->\n<g id=\"node23\" class=\"node\">\n<title>134557531132816exp</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1627\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1627\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">exp</text>\n</g>\n<!-- 134557531133104&#45;&gt;134557531132816exp -->\n<g id=\"edge21\" class=\"edge\">\n<title>134557531133104&#45;&gt;134557531132816exp</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1564.18,-73.5C1573.14,-73.5 1581.83,-73.5 1589.73,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1589.96,-77 1599.96,-73.5 1589.96,-70 1589.96,-77\"/>\n</g>\n<!-- 134557531133104* -->\n<g id=\"node9\" class=\"node\">\n<title>134557531133104*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1319\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1319\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 134557531133104*&#45;&gt;134557531133104 -->\n<g id=\"edge4\" class=\"edge\">\n<title>134557531133104*&#45;&gt;134557531133104</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1346.13,-73.5C1353.67,-73.5 1362.39,-73.5 1371.64,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1371.87,-77 1381.87,-73.5 1371.87,-70 1371.87,-77\"/>\n</g>\n<!-- 134557531141312 -->\n<g id=\"node10\" class=\"node\">\n<title>134557531141312</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2751,-54.5 2751,-90.5 2936,-90.5 2936,-54.5 2751,-54.5\"/>\n<text text-anchor=\"middle\" x=\"2762.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">o</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2774,-54.5 2774,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"2814\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.7071</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2854,-54.5 2854,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"2895\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 134557531141312*&#45;&gt;134557531141312 -->\n<g id=\"edge5\" class=\"edge\">\n<title>134557531141312*&#45;&gt;134557531141312</title>\n<path fill=\"none\" stroke=\"black\" d=\"M2715.04,-72.5C2722.58,-72.5 2731.3,-72.5 2740.57,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"2740.81,-76 2750.81,-72.5 2740.81,-69 2740.81,-76\"/>\n</g>\n<!-- 134557531131568 -->\n<g id=\"node12\" class=\"node\">\n<title>134557531131568</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"716,-56.5 716,-92.5 901,-92.5 901,-56.5 716,-56.5\"/>\n<text text-anchor=\"middle\" x=\"727.5\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"739,-56.5 739,-92.5 \"/>\n<text text-anchor=\"middle\" x=\"779\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"819,-56.5 819,-92.5 \"/>\n<text text-anchor=\"middle\" x=\"860\" y=\"-70.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531129648+ -->\n<g id=\"node16\" class=\"node\">\n<title>134557531129648+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1008\" cy=\"-101.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1008\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 134557531131568&#45;&gt;134557531129648+ -->\n<g id=\"edge27\" class=\"edge\">\n<title>134557531131568&#45;&gt;134557531129648+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M901.02,-87.01C926,-90.43 951.59,-93.93 971.37,-96.63\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"971.05,-100.12 981.43,-98.01 972,-93.18 971.05,-100.12\"/>\n</g>\n<!-- 134557531132624 -->\n<g id=\"node13\" class=\"node\">\n<title>134557531132624</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1690,-110.5 1690,-146.5 1877,-146.5 1877,-110.5 1690,-110.5\"/>\n<text text-anchor=\"middle\" x=\"1700\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1710,-110.5 1710,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"1752.5\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1795,-110.5 1795,-146.5 \"/>\n<text text-anchor=\"middle\" x=\"1836\" y=\"-124.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.1464</text>\n</g>\n<!-- 134557531132624&#45;&gt;134557531132576+ -->\n<g id=\"edge18\" class=\"edge\">\n<title>134557531132624&#45;&gt;134557531132576+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1877.05,-111.73C1886.54,-110.01 1895.71,-108.35 1903.95,-106.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1904.64,-110.28 1913.85,-105.06 1903.39,-103.4 1904.64,-110.28\"/>\n</g>\n<!-- 134557531142944 -->\n<g id=\"node14\" class=\"node\">\n<title>134557531142944</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2,-139.5 2,-175.5 198,-175.5 198,-139.5 2,-139.5\"/>\n<text text-anchor=\"middle\" x=\"17\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"32,-139.5 32,-175.5 \"/>\n<text text-anchor=\"middle\" x=\"72\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"112,-139.5 112,-175.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-153.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;1.5000</text>\n</g>\n<!-- 134557531142944&#45;&gt;134557531129936* -->\n<g id=\"edge24\" class=\"edge\">\n<title>134557531142944&#45;&gt;134557531129936*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M198.37,-157.5C208.05,-157.5 217.4,-157.5 225.8,-157.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.91,-161 235.91,-157.5 225.91,-154 225.91,-161\"/>\n</g>\n<!-- 134557531129648 -->\n<g id=\"node15\" class=\"node\">\n<title>134557531129648</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1071,-83.5 1071,-119.5 1256,-119.5 1256,-83.5 1071,-83.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">n</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1094,-83.5 1094,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"1134\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.8814</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1174,-83.5 1174,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"1215\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531129648&#45;&gt;134557531133104* -->\n<g id=\"edge12\" class=\"edge\">\n<title>134557531129648&#45;&gt;134557531133104*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1256.01,-84.81C1265.49,-83.08 1274.66,-81.41 1282.9,-79.9\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1283.6,-83.33 1292.81,-78.09 1282.35,-76.45 1283.6,-83.33\"/>\n</g>\n<!-- 134557531129648+&#45;&gt;134557531129648 -->\n<g id=\"edge6\" class=\"edge\">\n<title>134557531129648+&#45;&gt;134557531129648</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1035.04,-101.5C1042.58,-101.5 1051.3,-101.5 1060.57,-101.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1060.81,-105 1070.81,-101.5 1060.81,-98 1060.81,-105\"/>\n</g>\n<!-- 134557531131184 -->\n<g id=\"node17\" class=\"node\">\n<title>134557531131184</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"672,-111.5 672,-147.5 945,-147.5 945,-111.5 672,-111.5\"/>\n<text text-anchor=\"middle\" x=\"725\" y=\"-125.8\" font-family=\"Times,serif\" font-size=\"14.00\">x1*w1 + x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"778,-111.5 778,-147.5 \"/>\n<text text-anchor=\"middle\" x=\"820.5\" y=\"-125.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"863,-111.5 863,-147.5 \"/>\n<text text-anchor=\"middle\" x=\"904\" y=\"-125.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531131184&#45;&gt;134557531129648+ -->\n<g id=\"edge23\" class=\"edge\">\n<title>134557531131184&#45;&gt;134557531129648+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M936.53,-111.49C949.09,-109.71 960.99,-108.02 971.3,-106.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"972.03,-109.99 981.44,-105.12 971.05,-103.06 972.03,-109.99\"/>\n</g>\n<!-- 134557531131184+&#45;&gt;134557531131184 -->\n<g id=\"edge7\" class=\"edge\">\n<title>134557531131184+&#45;&gt;134557531131184</title>\n<path fill=\"none\" stroke=\"black\" d=\"M636.23,-129.5C643.7,-129.5 652.41,-129.5 661.87,-129.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"661.98,-133 671.98,-129.5 661.98,-126 661.98,-133\"/>\n</g>\n<!-- 134557531142992 -->\n<g id=\"node19\" class=\"node\">\n<title>134557531142992</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2.5,-84.5 2.5,-120.5 197.5,-120.5 197.5,-84.5 2.5,-84.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"35.5,-84.5 35.5,-120.5 \"/>\n<text text-anchor=\"middle\" x=\"75.5\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"115.5,-84.5 115.5,-120.5 \"/>\n<text text-anchor=\"middle\" x=\"156.5\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0000</text>\n</g>\n<!-- 134557531135936* -->\n<g id=\"node25\" class=\"node\">\n<title>134557531135936*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"263\" cy=\"-102.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"263\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 134557531142992&#45;&gt;134557531135936* -->\n<g id=\"edge26\" class=\"edge\">\n<title>134557531142992&#45;&gt;134557531135936*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M197.91,-102.5C207.65,-102.5 217.05,-102.5 225.52,-102.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.7,-106 235.7,-102.5 225.7,-99 225.7,-106\"/>\n</g>\n<!-- 134557531139392 -->\n<g id=\"node20\" class=\"node\">\n<title>134557531139392</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1690.5,-0.5 1690.5,-36.5 1876.5,-36.5 1876.5,-0.5 1690.5,-0.5\"/>\n<text text-anchor=\"middle\" x=\"1700.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1710.5,-0.5 1710.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"1750.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 1.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1790.5,-0.5 1790.5,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"1833.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;0.1036</text>\n</g>\n<!-- 134557531131856+ -->\n<g id=\"node27\" class=\"node\">\n<title>134557531131856+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"1940\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"1940\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 134557531139392&#45;&gt;134557531131856+ -->\n<g id=\"edge11\" class=\"edge\">\n<title>134557531139392&#45;&gt;134557531131856+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1876.6,-34.6C1886.15,-36.26 1895.37,-37.88 1903.68,-39.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1903.2,-42.8 1913.65,-41.07 1904.4,-35.9 1903.2,-42.8\"/>\n</g>\n<!-- 134557531140976 -->\n<g id=\"node21\" class=\"node\">\n<title>134557531140976</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4,-29.5 4,-65.5 196,-65.5 196,-29.5 4,-29.5\"/>\n<text text-anchor=\"middle\" x=\"19\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"34,-29.5 34,-65.5 \"/>\n<text text-anchor=\"middle\" x=\"74\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"114,-29.5 114,-65.5 \"/>\n<text text-anchor=\"middle\" x=\"155\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531140976&#45;&gt;134557531135936* -->\n<g id=\"edge25\" class=\"edge\">\n<title>134557531140976&#45;&gt;134557531135936*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M169.28,-65.5C179.65,-68.61 190.16,-71.98 200,-75.5 210.28,-79.17 221.28,-83.74 231.11,-88.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"229.93,-91.37 240.48,-92.27 232.79,-84.99 229.93,-91.37\"/>\n</g>\n<!-- 134557531132816 -->\n<g id=\"node22\" class=\"node\">\n<title>134557531132816</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1692.5,-55.5 1692.5,-91.5 1874.5,-91.5 1874.5,-55.5 1692.5,-55.5\"/>\n<text text-anchor=\"middle\" x=\"1702.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1712.5,-55.5 1712.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"1752.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 5.8284</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1792.5,-55.5 1792.5,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"1833.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.0429</text>\n</g>\n<!-- 134557531132816&#45;&gt;134557531132576+ -->\n<g id=\"edge13\" class=\"edge\">\n<title>134557531132816&#45;&gt;134557531132576+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1874.82,-89.28C1885.07,-91.08 1895.01,-92.81 1903.88,-94.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1903.3,-97.81 1913.75,-96.09 1904.5,-90.92 1903.3,-97.81\"/>\n</g>\n<!-- 134557531132816&#45;&gt;134557531131856+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>134557531132816&#45;&gt;134557531131856+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1874.82,-57.13C1885.07,-55.27 1895.01,-53.47 1903.88,-51.87\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1904.54,-55.3 1913.75,-50.08 1903.29,-48.42 1904.54,-55.3\"/>\n</g>\n<!-- 134557531132816exp&#45;&gt;134557531132816 -->\n<g id=\"edge8\" class=\"edge\">\n<title>134557531132816exp&#45;&gt;134557531132816</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1654.21,-73.5C1662.34,-73.5 1671.85,-73.5 1681.94,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1682.19,-77 1692.19,-73.5 1682.19,-70 1682.19,-77\"/>\n</g>\n<!-- 134557531135936 -->\n<g id=\"node24\" class=\"node\">\n<title>134557531135936</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"328.5,-84.5 328.5,-120.5 543.5,-120.5 543.5,-84.5 328.5,-84.5\"/>\n<text text-anchor=\"middle\" x=\"355\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">x2*w2</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"381.5,-84.5 381.5,-120.5 \"/>\n<text text-anchor=\"middle\" x=\"421.5\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 0.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"461.5,-84.5 461.5,-120.5 \"/>\n<text text-anchor=\"middle\" x=\"502.5\" y=\"-98.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.5000</text>\n</g>\n<!-- 134557531135936&#45;&gt;134557531131184+ -->\n<g id=\"edge20\" class=\"edge\">\n<title>134557531135936&#45;&gt;134557531131184+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.84,-119.37C554.01,-120.98 563.76,-122.51 572.44,-123.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"572.05,-127.37 582.47,-125.47 573.14,-120.45 572.05,-127.37\"/>\n</g>\n<!-- 134557531135936*&#45;&gt;134557531135936 -->\n<g id=\"edge9\" class=\"edge\">\n<title>134557531135936*&#45;&gt;134557531135936</title>\n<path fill=\"none\" stroke=\"black\" d=\"M290.34,-102.5C298.51,-102.5 308.08,-102.5 318.36,-102.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"318.39,-106 328.39,-102.5 318.39,-99 318.39,-106\"/>\n</g>\n<!-- 134557531131856 -->\n<g id=\"node26\" class=\"node\">\n<title>134557531131856</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"2003,-27.5 2003,-63.5 2189,-63.5 2189,-27.5 2003,-27.5\"/>\n<text text-anchor=\"middle\" x=\"2013\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2023,-27.5 2023,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"2063\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 6.8284</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"2103,-27.5 2103,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"2146\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;0.1036</text>\n</g>\n<!-- 134557531131856&#45;&gt;134557531141168**&#45;1 -->\n<g id=\"edge22\" class=\"edge\">\n<title>134557531131856&#45;&gt;134557531141168**&#45;1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M2189.14,-45.5C2220.72,-45.5 2254.24,-45.5 2278.74,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"2278.98,-49 2288.98,-45.5 2278.98,-42 2278.98,-49\"/>\n</g>\n<!-- 134557531131856+&#45;&gt;134557531131856 -->\n<g id=\"edge10\" class=\"edge\">\n<title>134557531131856+&#45;&gt;134557531131856</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1967.12,-45.5C1974.62,-45.5 1983.29,-45.5 1992.5,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1992.69,-49 2002.69,-45.5 1992.69,-42 1992.69,-49\"/>\n</g>\n<!-- 134557531132912 -->\n<g id=\"node28\" class=\"node\">\n<title>134557531132912</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"1072.5,-28.5 1072.5,-64.5 1254.5,-64.5 1254.5,-28.5 1072.5,-28.5\"/>\n<text text-anchor=\"middle\" x=\"1082.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\"> </text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1092.5,-28.5 1092.5,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"1132.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1172.5,-28.5 1172.5,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"1213.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 0.2203</text>\n</g>\n<!-- 134557531132912&#45;&gt;134557531133104* -->\n<g id=\"edge28\" class=\"edge\">\n<title>134557531132912&#45;&gt;134557531133104*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M1254.68,-62.36C1264.61,-64.11 1274.23,-65.8 1282.84,-67.32\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"1282.36,-70.78 1292.81,-69.07 1283.57,-63.89 1282.36,-70.78\"/>\n</g>\n</g>\n</svg>\n",
380
+ "text/plain": [
381
+ "<graphviz.graphs.Digraph at 0x7a611dd86e60>"
382
+ ]
383
+ },
384
+ "metadata": {},
385
+ "execution_count": 13
386
+ }
387
+ ]
388
+ },
389
+ {
390
+ "cell_type": "markdown",
391
+ "source": [
392
+ "---------------"
393
+ ],
394
+ "metadata": {
395
+ "id": "5kMAOl3gIn0w"
396
+ }
397
+ },
398
+ {
399
+ "cell_type": "markdown",
400
+ "source": [
401
+ "**Hence, we have now split the tanh function and the additional operations have also been added!** \\\n",
402
+ "\\\n",
403
+ "**Plus, we are also getting the same output as before: o and the inputs & weights :)**"
404
+ ],
405
+ "metadata": {
406
+ "id": "tfCbeRFQIou_"
407
+ }
408
+ }
409
+ ]
410
+ }
README.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## SET 1 - MICROGRAD 🔗
2
+
3
+ [![Documentation](https://img.shields.io/badge/Documentation-Available-blue)](https://muzzammilshah.github.io/Road-to-GPT/Micrograd/)
4
+ ![Number of Commits](https://img.shields.io/github/commit-activity/m/MuzzammilShah/NeuralNetworks-Micrograd-Implementation?label=Commits)
5
+ [![Last Commit](https://img.shields.io/github/last-commit/MuzzammilShah/NeuralNetworks-Micrograd-Implementation.svg?style=flat)](https://github.com/MuzzammilShah/NeuralNetworks-Micrograd-Implementation/commits/main)
6
+ ![Project Status](https://img.shields.io/badge/Status-Done-success)
7
+
8
+ &nbsp;
9
+
10
+ ### **Overview**
11
+ This repository contains the implementation of **Backpropagation** using an **AutoGrad Engine**, inspired by the **Micrograd** video by Andrej Karpathy. It explores the foundations of training neural networks and implementing key operations from scratch.
12
+
13
+ The repository contains:
14
+
15
+ - **Manual Backpropagation**: Building intuition and understanding of the gradient calculation process.
16
+ - **Interactive Site Version**: A pilot version of an interactive site that visualizes the functionality, currently under development.
17
+
18
+ ✍🏻 Notes: Follow the notebooks in order for a structured learning path. Each notebook and note corresponds to a particular concept or milestone in the implementation.
19
+
20
+ &nbsp;
21
+
22
+ ### **🗂️Repository Structure**
23
+
24
+ ```plaintext
25
+ ├── .gitignore
26
+ ├── README.md
27
+ ├── notes/
28
+ │ ├── A-main-video-lecture-notes.md
29
+ │ ├── chatgpt-motivation.md
30
+ │ ├── crux-node-backpropagation.md
31
+ │ ├── expanding-tanh-and-adding-more-operations.md
32
+ │ ├── micrograd-functionality.md
33
+ │ ├── multi-layer-perceptron.md
34
+ │ ├── neurons-explanation.md
35
+ │ ├── pytorch-comparision.md
36
+ │ └── value-object-creation.md
37
+ ├── site/
38
+ │ ├── interactive_site_pilot_v1.2/
39
+ ├── 1-derivative-simple-function.ipynb
40
+ ├── 2-derivative-function-with-multiple-inputs.ipynb
41
+ ├── 3-value-object.ipynb
42
+ ├── 3_1-graph-visualisation.ipynb
43
+ ├── 4_0-manual-backpropagation_simpleExpression.ipynb
44
+ ├── ... (more implementation notebooks, there are a lot lol)
45
+ ```
46
+
47
+ - **Notes Directory**: Contains Markdown files with notes and explanations for each topic.
48
+ - **Interactive Site Directory**: Contains files for the pilot version of the interactive visualization tool.
49
+ - **Implementation Notebooks**: Step-by-step code for implementing and understanding backpropagation and related concepts.
50
+
51
+ &nbsp;
52
+
53
+ ### **📄Instructions**
54
+
55
+ 1. Start by reading the notes in the `notes/` directory for a theoretical understanding.
56
+ 2. Proceed with the notebooks in the root directory in order to build up the implementation step by step.
57
+ 3. Explore the `site/` directory for the pilot interactive version of the AutoGrad Engine visualization (Idea concept, not yet implemented)
58
+
59
+ &nbsp;
60
+
61
+ ### **⭐Documentation**
62
+
63
+ For a better reading experience and detailed notes, visit my **[Road to GPT Documentation Site](https://muzzammilshah.github.io/Road-to-GPT/Micrograd/)**.
64
+
65
+ > **💡Pro Tip**: This site provides an interactive and visually rich explanation of the notes and code. It is highly recommended you view this project from there.
66
+
67
+ &nbsp;
68
+
69
+ ### **✍🏻Acknowledgments**
70
+ Notes and implementations inspired by the **Micrograd** video by [Andrej Karpathy](https://karpathy.ai/).
71
+
72
+ For more of my projects, visit my [Portfolio Site](https://muhammedshah.com).
x10_implementing_in_pytorch.ipynb ADDED
@@ -0,0 +1,420 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "import math"
21
+ ],
22
+ "metadata": {
23
+ "id": "JlYxBvFK0AjA"
24
+ },
25
+ "execution_count": 1,
26
+ "outputs": []
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "source": [
31
+ "class Value:\n",
32
+ "\n",
33
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
34
+ " self.data = data\n",
35
+ " self.grad = 0.0\n",
36
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
37
+ " self._prev = set(_children)\n",
38
+ " self._op = _op\n",
39
+ " self.label = label\n",
40
+ "\n",
41
+ "\n",
42
+ " def __repr__(self):\n",
43
+ " return f\"Value(data={self.data})\"\n",
44
+ "\n",
45
+ " def __add__(self, other):\n",
46
+ " other = other if isinstance(other, Value) else Value(other)\n",
47
+ " out = Value(self.data + other.data, (self, other), '+')\n",
48
+ "\n",
49
+ " def backward():\n",
50
+ " self.grad += 1.0 * out.grad\n",
51
+ " other.grad += 1.0 * out.grad\n",
52
+ "\n",
53
+ " out._backward = backward\n",
54
+ " return out\n",
55
+ "\n",
56
+ " def __mul__(self, other):\n",
57
+ " other = other if isinstance(other, Value) else Value(other)\n",
58
+ " out = Value(self.data * other.data, (self, other), '*')\n",
59
+ "\n",
60
+ " def backward():\n",
61
+ " self.grad += other.data * out.grad\n",
62
+ " other.grad += self.data * out.grad\n",
63
+ " out._backward = backward\n",
64
+ " return out\n",
65
+ "\n",
66
+ " def __rmul__(self, other): #other * self\n",
67
+ " return self * other\n",
68
+ "\n",
69
+ " def __truediv__(self, other): #self/other\n",
70
+ " return self * other**-1\n",
71
+ "\n",
72
+ " def __neg__(self):\n",
73
+ " return self * -1\n",
74
+ "\n",
75
+ " def __sub__(self, other): #self - other\n",
76
+ " return self + (-other)\n",
77
+ "\n",
78
+ " def __pow__(self, other):\n",
79
+ " assert isinstance(other, (int, float)), \"only supporting int/float powers for now\"\n",
80
+ " out = Value(self.data ** other, (self, ), f\"**{other}\")\n",
81
+ "\n",
82
+ " def backward():\n",
83
+ " self.grad += (other * (self.data ** (other - 1))) * out.grad\n",
84
+ "\n",
85
+ " out._backward = backward\n",
86
+ " return out\n",
87
+ "\n",
88
+ " def tanh(self):\n",
89
+ " x = self.data\n",
90
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
91
+ " out = Value(t, (self, ), 'tanh')\n",
92
+ "\n",
93
+ " def backward():\n",
94
+ " self.grad += 1 - (t**2) * out.grad\n",
95
+ "\n",
96
+ " out._backward = backward\n",
97
+ " return out\n",
98
+ "\n",
99
+ " def exp(self):\n",
100
+ " x = self.data\n",
101
+ " out = Value(math.exp(x), (self, ), 'exp') #We merged t and out, into just out\n",
102
+ "\n",
103
+ " def backward():\n",
104
+ " self.grad += out.data * out.grad\n",
105
+ "\n",
106
+ " out._backward = backward\n",
107
+ " return out\n",
108
+ "\n",
109
+ " def backward(self):\n",
110
+ "\n",
111
+ " topo = []\n",
112
+ " visited = set()\n",
113
+ " def build_topo(v):\n",
114
+ " if v not in visited:\n",
115
+ " visited.add(v)\n",
116
+ " for child in v._prev:\n",
117
+ " build_topo(child)\n",
118
+ " topo.append(v)\n",
119
+ "\n",
120
+ " build_topo(self)\n",
121
+ "\n",
122
+ " self.grad = 1.0\n",
123
+ " for node in reversed(topo):\n",
124
+ " node._backward()"
125
+ ],
126
+ "metadata": {
127
+ "id": "tA0zbyEwFbD5"
128
+ },
129
+ "execution_count": 2,
130
+ "outputs": []
131
+ },
132
+ {
133
+ "cell_type": "code",
134
+ "source": [
135
+ "x1 = Value(2.0, label='x1')\n",
136
+ "x2 = Value(0.0, label='x2')\n",
137
+ "\n",
138
+ "w1 = Value(-3.0, label='w1')\n",
139
+ "w2 = Value(1.0, label='w2')\n",
140
+ "\n",
141
+ "b = Value(6.8813735870195432, label='b')\n",
142
+ "\n",
143
+ "x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
144
+ "x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
145
+ "\n",
146
+ "x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'\n",
147
+ "\n",
148
+ "n = x1w1x2w2 + b; n.label = 'n'\n",
149
+ "\n",
150
+ "#o = n.tanh(); o.label = 'o'\n",
151
+ "\n",
152
+ "#Spliting up of the tanh function\n",
153
+ "\n",
154
+ "e = (2*n).exp()\n",
155
+ "o = (e - 1) / (e + 1)\n",
156
+ "o.label = 'o'"
157
+ ],
158
+ "metadata": {
159
+ "id": "Ounbj2XwHSZ1"
160
+ },
161
+ "execution_count": null,
162
+ "outputs": []
163
+ },
164
+ {
165
+ "cell_type": "markdown",
166
+ "source": [
167
+ "--------------"
168
+ ],
169
+ "metadata": {
170
+ "id": "mBTYZa8wzbi0"
171
+ }
172
+ },
173
+ {
174
+ "cell_type": "markdown",
175
+ "source": [
176
+ "### **Now we'll how the same thing can be written in PyTorch syntax**"
177
+ ],
178
+ "metadata": {
179
+ "id": "QY-LvbPlzcwK"
180
+ }
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "source": [
185
+ "import torch"
186
+ ],
187
+ "metadata": {
188
+ "id": "1dfm4XaizroF"
189
+ },
190
+ "execution_count": 3,
191
+ "outputs": []
192
+ },
193
+ {
194
+ "cell_type": "code",
195
+ "source": [
196
+ "x1 = torch.Tensor([2.0]).double() ; x1.requires_grad = True\n",
197
+ "x2 = torch.Tensor([0.0]).double() ; x2.requires_grad = True\n",
198
+ "w1 = torch.Tensor([-3.0]).double() ; w1.requires_grad = True\n",
199
+ "w2 = torch.Tensor([1.0]).double() ; w2.requires_grad = True\n",
200
+ "b = torch.Tensor([6.8813735870195432]).double() ; b.requires_grad = True\n",
201
+ "\n",
202
+ "n = x1*w1 + x2*w2 + b\n",
203
+ "o = torch.tanh(n)\n",
204
+ "\n",
205
+ "print(o.data.item())\n",
206
+ "o.backward()\n",
207
+ "\n",
208
+ "print('x2', x2.grad.item())\n",
209
+ "print('w2', w2.grad.item())\n",
210
+ "print('x1', x1.grad.item())\n",
211
+ "print('w1', w1.grad.item())"
212
+ ],
213
+ "metadata": {
214
+ "colab": {
215
+ "base_uri": "https://localhost:8080/"
216
+ },
217
+ "id": "CoMLfm5lzkEh",
218
+ "outputId": "bddcf726-6830-4a2e-ea9a-bec32266dc8a"
219
+ },
220
+ "execution_count": 4,
221
+ "outputs": [
222
+ {
223
+ "output_type": "stream",
224
+ "name": "stdout",
225
+ "text": [
226
+ "0.7071066904050358\n",
227
+ "x2 0.5000001283844369\n",
228
+ "w2 0.0\n",
229
+ "x1 -1.5000003851533106\n",
230
+ "w1 1.0000002567688737\n"
231
+ ]
232
+ }
233
+ ]
234
+ },
235
+ {
236
+ "cell_type": "markdown",
237
+ "source": [
238
+ "-------------"
239
+ ],
240
+ "metadata": {
241
+ "id": "FvpWP3RjzxD1"
242
+ }
243
+ },
244
+ {
245
+ "cell_type": "markdown",
246
+ "source": [
247
+ "Some PyTorch syntax review"
248
+ ],
249
+ "metadata": {
250
+ "id": "_WqfN608zx9O"
251
+ }
252
+ },
253
+ {
254
+ "cell_type": "code",
255
+ "source": [
256
+ "torch.Tensor([[1,2,3], [4,5,6]])"
257
+ ],
258
+ "metadata": {
259
+ "colab": {
260
+ "base_uri": "https://localhost:8080/"
261
+ },
262
+ "id": "JWLvfH_Jztpj",
263
+ "outputId": "c9209290-c43c-4cc8-c3b7-36ba90b6fddc"
264
+ },
265
+ "execution_count": 5,
266
+ "outputs": [
267
+ {
268
+ "output_type": "execute_result",
269
+ "data": {
270
+ "text/plain": [
271
+ "tensor([[1., 2., 3.],\n",
272
+ " [4., 5., 6.]])"
273
+ ]
274
+ },
275
+ "metadata": {},
276
+ "execution_count": 5
277
+ }
278
+ ]
279
+ },
280
+ {
281
+ "cell_type": "code",
282
+ "source": [
283
+ "torch.Tensor([[1,2,3], [4,5,6]]).shape"
284
+ ],
285
+ "metadata": {
286
+ "colab": {
287
+ "base_uri": "https://localhost:8080/"
288
+ },
289
+ "id": "4lG9Zz5fz1zu",
290
+ "outputId": "14f98a9e-1cd1-4cf8-d429-db387ec3fc5e"
291
+ },
292
+ "execution_count": 6,
293
+ "outputs": [
294
+ {
295
+ "output_type": "execute_result",
296
+ "data": {
297
+ "text/plain": [
298
+ "torch.Size([2, 3])"
299
+ ]
300
+ },
301
+ "metadata": {},
302
+ "execution_count": 6
303
+ }
304
+ ]
305
+ },
306
+ {
307
+ "cell_type": "code",
308
+ "source": [
309
+ "torch.Tensor([2.0]).dtype"
310
+ ],
311
+ "metadata": {
312
+ "colab": {
313
+ "base_uri": "https://localhost:8080/"
314
+ },
315
+ "id": "gcaF9qLpz3Ah",
316
+ "outputId": "633bccd0-c61f-4f17-b81d-3f88e39574a3"
317
+ },
318
+ "execution_count": 7,
319
+ "outputs": [
320
+ {
321
+ "output_type": "execute_result",
322
+ "data": {
323
+ "text/plain": [
324
+ "torch.float32"
325
+ ]
326
+ },
327
+ "metadata": {},
328
+ "execution_count": 7
329
+ }
330
+ ]
331
+ },
332
+ {
333
+ "cell_type": "code",
334
+ "source": [
335
+ "torch.Tensor([2.0]).double().dtype"
336
+ ],
337
+ "metadata": {
338
+ "colab": {
339
+ "base_uri": "https://localhost:8080/"
340
+ },
341
+ "id": "JCeS4262z5Ks",
342
+ "outputId": "f7fcb909-6e7f-46ab-fb4f-ff1cee88a96d"
343
+ },
344
+ "execution_count": 8,
345
+ "outputs": [
346
+ {
347
+ "output_type": "execute_result",
348
+ "data": {
349
+ "text/plain": [
350
+ "torch.float64"
351
+ ]
352
+ },
353
+ "metadata": {},
354
+ "execution_count": 8
355
+ }
356
+ ]
357
+ },
358
+ {
359
+ "cell_type": "code",
360
+ "source": [
361
+ "x2.data"
362
+ ],
363
+ "metadata": {
364
+ "colab": {
365
+ "base_uri": "https://localhost:8080/"
366
+ },
367
+ "id": "F8HuYRzXz7H9",
368
+ "outputId": "284a5801-c7b1-4dbb-e950-545b161e190d"
369
+ },
370
+ "execution_count": 9,
371
+ "outputs": [
372
+ {
373
+ "output_type": "execute_result",
374
+ "data": {
375
+ "text/plain": [
376
+ "tensor([0.], dtype=torch.float64)"
377
+ ]
378
+ },
379
+ "metadata": {},
380
+ "execution_count": 9
381
+ }
382
+ ]
383
+ },
384
+ {
385
+ "cell_type": "code",
386
+ "source": [
387
+ "x2.data.item()"
388
+ ],
389
+ "metadata": {
390
+ "colab": {
391
+ "base_uri": "https://localhost:8080/"
392
+ },
393
+ "id": "9EVQmyZfz-GQ",
394
+ "outputId": "31f2d9c7-2fc2-4f2d-8590-c13d399a343c"
395
+ },
396
+ "execution_count": 10,
397
+ "outputs": [
398
+ {
399
+ "output_type": "execute_result",
400
+ "data": {
401
+ "text/plain": [
402
+ "0.0"
403
+ ]
404
+ },
405
+ "metadata": {},
406
+ "execution_count": 10
407
+ }
408
+ ]
409
+ },
410
+ {
411
+ "cell_type": "markdown",
412
+ "source": [
413
+ "------------"
414
+ ],
415
+ "metadata": {
416
+ "id": "pSGfEq290B4q"
417
+ }
418
+ }
419
+ ]
420
+ }
x11_creating_a_multi_layer_perceptron.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
x12_creating_a_loss_function.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
x13_collecting_all_parameters_in_NN.ipynb ADDED
@@ -0,0 +1,465 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "from graphviz import Digraph\n",
21
+ "\n",
22
+ "def trace(root):\n",
23
+ " #Builds a set of all nodes and edges in a graph\n",
24
+ " nodes, edges = set(), set()\n",
25
+ " def build(v):\n",
26
+ " if v not in nodes:\n",
27
+ " nodes.add(v)\n",
28
+ " for child in v._prev:\n",
29
+ " edges.add((child, v))\n",
30
+ " build(child)\n",
31
+ " build(root)\n",
32
+ " return nodes, edges\n",
33
+ "\n",
34
+ "def draw_dot(root):\n",
35
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
36
+ "\n",
37
+ " nodes, edges = trace(root)\n",
38
+ " for n in nodes:\n",
39
+ " uid = str(id(n))\n",
40
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
41
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
42
+ " if n._op:\n",
43
+ " #If this value is a result of some operation, then create an op node for it\n",
44
+ " dot.node(name = uid + n._op, label=n._op)\n",
45
+ " #and connect this node to it\n",
46
+ " dot.edge(uid + n._op, uid)\n",
47
+ "\n",
48
+ " for n1, n2 in edges:\n",
49
+ " #Connect n1 to the node of n2\n",
50
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
51
+ "\n",
52
+ " return dot"
53
+ ],
54
+ "metadata": {
55
+ "id": "T0rN8d146jvF"
56
+ },
57
+ "execution_count": 1,
58
+ "outputs": []
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "source": [
63
+ "import math"
64
+ ],
65
+ "metadata": {
66
+ "id": "JlYxBvFK0AjA"
67
+ },
68
+ "execution_count": 2,
69
+ "outputs": []
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "source": [
74
+ "class Value:\n",
75
+ "\n",
76
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
77
+ " self.data = data\n",
78
+ " self.grad = 0.0\n",
79
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
80
+ " self._prev = set(_children)\n",
81
+ " self._op = _op\n",
82
+ " self.label = label\n",
83
+ "\n",
84
+ "\n",
85
+ " def __repr__(self):\n",
86
+ " return f\"Value(data={self.data})\"\n",
87
+ "\n",
88
+ " def __add__(self, other):\n",
89
+ " other = other if isinstance(other, Value) else Value(other)\n",
90
+ " out = Value(self.data + other.data, (self, other), '+')\n",
91
+ "\n",
92
+ " def backward():\n",
93
+ " self.grad += 1.0 * out.grad\n",
94
+ " other.grad += 1.0 * out.grad\n",
95
+ "\n",
96
+ " out._backward = backward\n",
97
+ " return out\n",
98
+ "\n",
99
+ " def __radd__(self, other): #here\n",
100
+ " return self + other\n",
101
+ "\n",
102
+ " def __mul__(self, other):\n",
103
+ " other = other if isinstance(other, Value) else Value(other)\n",
104
+ " out = Value(self.data * other.data, (self, other), '*')\n",
105
+ "\n",
106
+ " def backward():\n",
107
+ " self.grad += other.data * out.grad\n",
108
+ " other.grad += self.data * out.grad\n",
109
+ " out._backward = backward\n",
110
+ " return out\n",
111
+ "\n",
112
+ " def __rmul__(self, other): #other * self\n",
113
+ " return self * other\n",
114
+ "\n",
115
+ " def __truediv__(self, other): #self/other\n",
116
+ " return self * other**-1\n",
117
+ "\n",
118
+ " def __neg__(self):\n",
119
+ " return self * -1\n",
120
+ "\n",
121
+ " def __sub__(self, other): #self - other\n",
122
+ " return self + (-other)\n",
123
+ "\n",
124
+ " def __pow__(self, other):\n",
125
+ " assert isinstance(other, (int, float)), \"only supporting int/float powers for now\"\n",
126
+ " out = Value(self.data ** other, (self, ), f\"**{other}\")\n",
127
+ "\n",
128
+ " def backward():\n",
129
+ " self.grad += (other * (self.data ** (other - 1))) * out.grad\n",
130
+ "\n",
131
+ " out._backward = backward\n",
132
+ " return out\n",
133
+ "\n",
134
+ " def tanh(self):\n",
135
+ " x = self.data\n",
136
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
137
+ " out = Value(t, (self, ), 'tanh')\n",
138
+ "\n",
139
+ " def backward():\n",
140
+ " self.grad += 1 - (t**2) * out.grad\n",
141
+ "\n",
142
+ " out._backward = backward\n",
143
+ " return out\n",
144
+ "\n",
145
+ " def exp(self):\n",
146
+ " x = self.data\n",
147
+ " out = Value(math.exp(x), (self, ), 'exp') #We merged t and out, into just out\n",
148
+ "\n",
149
+ " def backward():\n",
150
+ " self.grad += out.data * out.grad\n",
151
+ "\n",
152
+ " out._backward = backward\n",
153
+ " return out\n",
154
+ "\n",
155
+ " def backward(self):\n",
156
+ "\n",
157
+ " topo = []\n",
158
+ " visited = set()\n",
159
+ " def build_topo(v):\n",
160
+ " if v not in visited:\n",
161
+ " visited.add(v)\n",
162
+ " for child in v._prev:\n",
163
+ " build_topo(child)\n",
164
+ " topo.append(v)\n",
165
+ "\n",
166
+ " build_topo(self)\n",
167
+ "\n",
168
+ " self.grad = 1.0\n",
169
+ " for node in reversed(topo):\n",
170
+ " node._backward()"
171
+ ],
172
+ "metadata": {
173
+ "id": "tA0zbyEwFbD5"
174
+ },
175
+ "execution_count": 3,
176
+ "outputs": []
177
+ },
178
+ {
179
+ "cell_type": "markdown",
180
+ "source": [
181
+ "---------------"
182
+ ],
183
+ "metadata": {
184
+ "id": "m9hy05zbxhLP"
185
+ }
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "source": [
190
+ "import random"
191
+ ],
192
+ "metadata": {
193
+ "id": "gu3tnJu1Wti5"
194
+ },
195
+ "execution_count": 4,
196
+ "outputs": []
197
+ },
198
+ {
199
+ "cell_type": "code",
200
+ "source": [
201
+ "class Neuron:\n",
202
+ "\tdef __init__(self, nin):\n",
203
+ "\t\tself.w = [ Value(random.uniform(-1,1)) for _ in range(nin) ]\n",
204
+ "\t\tself.b = Value(random.uniform(-1,1))\n",
205
+ "\n",
206
+ "\tdef __call__(self, x):\n",
207
+ "\t\t# (w*x)+b\n",
208
+ "\t\tact = sum( (wi*xi for wi,xi in zip(self.w, x)), self.b )\n",
209
+ "\t\tout = act.tanh()\n",
210
+ "\t\treturn out\n",
211
+ "\n",
212
+ "class Layer:\n",
213
+ "\tdef __init__(self, nin, nout):\n",
214
+ "\t\tself.neurons = [Neuron(nin) for _ in range(nout)]\n",
215
+ "\n",
216
+ "\tdef __call__(self, x):\n",
217
+ "\t\touts = [n(x) for n in self.neurons]\n",
218
+ "\t\treturn outs[0] if len(outs)==1 else outs #The New added line for making the output better\n",
219
+ "\n",
220
+ "class MLP:\n",
221
+ "\tdef __init__(self, nin, nouts):\n",
222
+ "\t\tsz = [nin] + nouts\n",
223
+ "\t\tself.layers = [ Layer(sz[i], sz[i+1]) for i in range(len(nouts)) ]\n",
224
+ "\n",
225
+ "\tdef __call__(self, x):\n",
226
+ "\t\tfor layer in self.layers:\n",
227
+ "\t\t\tx = layer(x)\n",
228
+ "\t\treturn x"
229
+ ],
230
+ "metadata": {
231
+ "id": "aCXXYNg_W680"
232
+ },
233
+ "execution_count": null,
234
+ "outputs": []
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "source": [
239
+ "x = [2.0, 3.0, -1.0]\n",
240
+ "n = MLP(3, [4, 4, 1])\n",
241
+ "n(x)"
242
+ ],
243
+ "metadata": {
244
+ "colab": {
245
+ "base_uri": "https://localhost:8080/"
246
+ },
247
+ "id": "aG9pKV_RXsO8",
248
+ "outputId": "e6f183b9-896b-458f-9322-e91bc79e9da2",
249
+ "collapsed": true
250
+ },
251
+ "execution_count": null,
252
+ "outputs": [
253
+ {
254
+ "output_type": "execute_result",
255
+ "data": {
256
+ "text/plain": [
257
+ "Value(data=-0.33393070997191954)"
258
+ ]
259
+ },
260
+ "metadata": {},
261
+ "execution_count": 23
262
+ }
263
+ ]
264
+ },
265
+ {
266
+ "cell_type": "markdown",
267
+ "source": [
268
+ "-----------"
269
+ ],
270
+ "metadata": {
271
+ "id": "6DemdSsv_abu"
272
+ }
273
+ },
274
+ {
275
+ "cell_type": "markdown",
276
+ "source": [
277
+ "Now, we'll be returning the **parameters** from the MLP. So that will be from Neuron -> Layer -> MLP"
278
+ ],
279
+ "metadata": {
280
+ "id": "rhKQgN2LKBf9"
281
+ }
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "source": [
286
+ "class Neuron:\n",
287
+ " def __init__(self, nin):\n",
288
+ " self.w = [Value(random.uniform(-1, 1)) for _ in range(nin)]\n",
289
+ " self.b = Value(random.uniform(-1, 1))\n",
290
+ "\n",
291
+ " def __call__(self, x):\n",
292
+ " act = sum((wi * xi for wi, xi in zip(self.w, x)), self.b)\n",
293
+ " out = act.tanh()\n",
294
+ " return out\n",
295
+ "\n",
296
+ " def parameters(self):\n",
297
+ " return self.w + [self.b]\n",
298
+ "\n",
299
+ "class Layer:\n",
300
+ " def __init__(self, nin, nout):\n",
301
+ " self.neurons = [Neuron(nin) for _ in range(nout)]\n",
302
+ "\n",
303
+ " def __call__(self, x):\n",
304
+ " outs = [n(x) for n in self.neurons]\n",
305
+ " return outs[0] if len(outs) == 1 else outs\n",
306
+ "\n",
307
+ " def parameters(self):\n",
308
+ " return [p for n in self.neurons for p in n.parameters()]\n",
309
+ "\n",
310
+ " # Alternative way of writing the above return function:\n",
311
+ " # parameters = []\n",
312
+ " # for n in self.neurons:\n",
313
+ " # p = n.parameters()\n",
314
+ " # parameters.extend(p)\n",
315
+ "\n",
316
+ "class MLP:\n",
317
+ " def __init__(self, nin, nouts):\n",
318
+ " sz = [nin] + nouts\n",
319
+ " self.layers = [Layer(sz[i], sz[i + 1]) for i in range(len(nouts))]\n",
320
+ "\n",
321
+ " def __call__(self, x):\n",
322
+ " for layer in self.layers:\n",
323
+ " x = layer(x)\n",
324
+ " return x\n",
325
+ "\n",
326
+ " def parameters(self):\n",
327
+ " return [p for l in self.layers for p in l.parameters()]"
328
+ ],
329
+ "metadata": {
330
+ "id": "HmEO8Gi1KN_m"
331
+ },
332
+ "execution_count": 5,
333
+ "outputs": []
334
+ },
335
+ {
336
+ "cell_type": "code",
337
+ "source": [
338
+ "x = [2.0, 3.0, -1.0]\n",
339
+ "n = MLP(3, [4, 4, 1])\n",
340
+ "n(x)"
341
+ ],
342
+ "metadata": {
343
+ "colab": {
344
+ "base_uri": "https://localhost:8080/"
345
+ },
346
+ "id": "e2VaJPFdMVUs",
347
+ "outputId": "0a229e8c-2084-4037-e808-cc27cb3fd2ca"
348
+ },
349
+ "execution_count": 6,
350
+ "outputs": [
351
+ {
352
+ "output_type": "execute_result",
353
+ "data": {
354
+ "text/plain": [
355
+ "Value(data=0.7625252102576119)"
356
+ ]
357
+ },
358
+ "metadata": {},
359
+ "execution_count": 6
360
+ }
361
+ ]
362
+ },
363
+ {
364
+ "cell_type": "code",
365
+ "source": [
366
+ "n.parameters()"
367
+ ],
368
+ "metadata": {
369
+ "colab": {
370
+ "base_uri": "https://localhost:8080/"
371
+ },
372
+ "id": "cfOp08kYNmDX",
373
+ "outputId": "fe98dfd7-0e2b-4dd7-fb08-6f4cf60161ff"
374
+ },
375
+ "execution_count": 7,
376
+ "outputs": [
377
+ {
378
+ "output_type": "execute_result",
379
+ "data": {
380
+ "text/plain": [
381
+ "[Value(data=0.31785584973173164),\n",
382
+ " Value(data=0.2998372553774835),\n",
383
+ " Value(data=-0.8029008199517247),\n",
384
+ " Value(data=-0.39340060142531286),\n",
385
+ " Value(data=0.23322412084873956),\n",
386
+ " Value(data=0.29891813550514534),\n",
387
+ " Value(data=-0.5314862907700675),\n",
388
+ " Value(data=0.19661072911432642),\n",
389
+ " Value(data=0.9142418954398666),\n",
390
+ " Value(data=0.041208786424172805),\n",
391
+ " Value(data=-0.23983634992214187),\n",
392
+ " Value(data=-0.593538786941121),\n",
393
+ " Value(data=0.39482399486723296),\n",
394
+ " Value(data=-0.9880306400643504),\n",
395
+ " Value(data=-0.8097855189886964),\n",
396
+ " Value(data=0.4629484174790124),\n",
397
+ " Value(data=0.31168805444961634),\n",
398
+ " Value(data=-0.9828138115624934),\n",
399
+ " Value(data=0.5221437252554255),\n",
400
+ " Value(data=-0.19703997468926882),\n",
401
+ " Value(data=-0.5504279057638468),\n",
402
+ " Value(data=-0.8365261779265616),\n",
403
+ " Value(data=-0.22783861276612227),\n",
404
+ " Value(data=0.5666981389300718),\n",
405
+ " Value(data=-0.06415010714317604),\n",
406
+ " Value(data=0.845414529622897),\n",
407
+ " Value(data=0.4793425135418725),\n",
408
+ " Value(data=-0.38321354069020086),\n",
409
+ " Value(data=-0.10963021731006206),\n",
410
+ " Value(data=0.14485994942129898),\n",
411
+ " Value(data=-0.19028270981146433),\n",
412
+ " Value(data=0.5148204886483112),\n",
413
+ " Value(data=-0.8559156650791364),\n",
414
+ " Value(data=0.3778416962066449),\n",
415
+ " Value(data=0.09608787032156774),\n",
416
+ " Value(data=-0.8288362456839788),\n",
417
+ " Value(data=0.5641592956285757),\n",
418
+ " Value(data=0.13764114112689052),\n",
419
+ " Value(data=-0.19625087652731277),\n",
420
+ " Value(data=-0.6117936229921406),\n",
421
+ " Value(data=0.7546009612155813)]"
422
+ ]
423
+ },
424
+ "metadata": {},
425
+ "execution_count": 7
426
+ }
427
+ ]
428
+ },
429
+ {
430
+ "cell_type": "markdown",
431
+ "source": [
432
+ "So these are all our parameters provided as inputs. The weights, inputs and biases"
433
+ ],
434
+ "metadata": {
435
+ "id": "W0hGhhMaNozj"
436
+ }
437
+ },
438
+ {
439
+ "cell_type": "code",
440
+ "source": [
441
+ "len(n.parameters())"
442
+ ],
443
+ "metadata": {
444
+ "colab": {
445
+ "base_uri": "https://localhost:8080/"
446
+ },
447
+ "id": "itFmD8hFNnph",
448
+ "outputId": "f43eee99-5831-4708-f203-518ddf7011e5"
449
+ },
450
+ "execution_count": 8,
451
+ "outputs": [
452
+ {
453
+ "output_type": "execute_result",
454
+ "data": {
455
+ "text/plain": [
456
+ "41"
457
+ ]
458
+ },
459
+ "metadata": {},
460
+ "execution_count": 8
461
+ }
462
+ ]
463
+ }
464
+ ]
465
+ }
x14_manual_gradient_descent_optimization.ipynb ADDED
@@ -0,0 +1,652 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "from graphviz import Digraph\n",
21
+ "\n",
22
+ "def trace(root):\n",
23
+ " #Builds a set of all nodes and edges in a graph\n",
24
+ " nodes, edges = set(), set()\n",
25
+ " def build(v):\n",
26
+ " if v not in nodes:\n",
27
+ " nodes.add(v)\n",
28
+ " for child in v._prev:\n",
29
+ " edges.add((child, v))\n",
30
+ " build(child)\n",
31
+ " build(root)\n",
32
+ " return nodes, edges\n",
33
+ "\n",
34
+ "def draw_dot(root):\n",
35
+ " dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
36
+ "\n",
37
+ " nodes, edges = trace(root)\n",
38
+ " for n in nodes:\n",
39
+ " uid = str(id(n))\n",
40
+ " #For any value in the graph, create a rectangular ('record') node for it\n",
41
+ " dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
42
+ " if n._op:\n",
43
+ " #If this value is a result of some operation, then create an op node for it\n",
44
+ " dot.node(name = uid + n._op, label=n._op)\n",
45
+ " #and connect this node to it\n",
46
+ " dot.edge(uid + n._op, uid)\n",
47
+ "\n",
48
+ " for n1, n2 in edges:\n",
49
+ " #Connect n1 to the node of n2\n",
50
+ " dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
51
+ "\n",
52
+ " return dot"
53
+ ],
54
+ "metadata": {
55
+ "id": "T0rN8d146jvF"
56
+ },
57
+ "execution_count": 1,
58
+ "outputs": []
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "source": [
63
+ "import math"
64
+ ],
65
+ "metadata": {
66
+ "id": "JlYxBvFK0AjA"
67
+ },
68
+ "execution_count": 2,
69
+ "outputs": []
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "source": [
74
+ "class Value:\n",
75
+ "\n",
76
+ " def __init__(self, data, _children=(), _op='', label=''):\n",
77
+ " self.data = data\n",
78
+ " self.grad = 0.0\n",
79
+ " self._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
80
+ " self._prev = set(_children)\n",
81
+ " self._op = _op\n",
82
+ " self.label = label\n",
83
+ "\n",
84
+ "\n",
85
+ " def __repr__(self):\n",
86
+ " return f\"Value(data={self.data})\"\n",
87
+ "\n",
88
+ " def __add__(self, other):\n",
89
+ " other = other if isinstance(other, Value) else Value(other)\n",
90
+ " out = Value(self.data + other.data, (self, other), '+')\n",
91
+ "\n",
92
+ " def backward():\n",
93
+ " self.grad += 1.0 * out.grad\n",
94
+ " other.grad += 1.0 * out.grad\n",
95
+ "\n",
96
+ " out._backward = backward\n",
97
+ " return out\n",
98
+ "\n",
99
+ " def __radd__(self, other): #here\n",
100
+ " return self + other\n",
101
+ "\n",
102
+ " def __mul__(self, other):\n",
103
+ " other = other if isinstance(other, Value) else Value(other)\n",
104
+ " out = Value(self.data * other.data, (self, other), '*')\n",
105
+ "\n",
106
+ " def backward():\n",
107
+ " self.grad += other.data * out.grad\n",
108
+ " other.grad += self.data * out.grad\n",
109
+ " out._backward = backward\n",
110
+ " return out\n",
111
+ "\n",
112
+ " def __rmul__(self, other): #other * self\n",
113
+ " return self * other\n",
114
+ "\n",
115
+ " def __truediv__(self, other): #self/other\n",
116
+ " return self * other**-1\n",
117
+ "\n",
118
+ " def __neg__(self):\n",
119
+ " return self * -1\n",
120
+ "\n",
121
+ " def __sub__(self, other): #self - other\n",
122
+ " return self + (-other)\n",
123
+ "\n",
124
+ " def __pow__(self, other):\n",
125
+ " assert isinstance(other, (int, float)), \"only supporting int/float powers for now\"\n",
126
+ " out = Value(self.data ** other, (self, ), f\"**{other}\")\n",
127
+ "\n",
128
+ " def backward():\n",
129
+ " self.grad += (other * (self.data ** (other - 1))) * out.grad\n",
130
+ "\n",
131
+ " out._backward = backward\n",
132
+ " return out\n",
133
+ "\n",
134
+ " def tanh(self):\n",
135
+ " x = self.data\n",
136
+ " t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
137
+ " out = Value(t, (self, ), 'tanh')\n",
138
+ "\n",
139
+ " def backward():\n",
140
+ " self.grad += 1 - (t**2) * out.grad\n",
141
+ "\n",
142
+ " out._backward = backward\n",
143
+ " return out\n",
144
+ "\n",
145
+ " def exp(self):\n",
146
+ " x = self.data\n",
147
+ " out = Value(math.exp(x), (self, ), 'exp') #We merged t and out, into just out\n",
148
+ "\n",
149
+ " def backward():\n",
150
+ " self.grad += out.data * out.grad\n",
151
+ "\n",
152
+ " out._backward = backward\n",
153
+ " return out\n",
154
+ "\n",
155
+ " def backward(self):\n",
156
+ "\n",
157
+ " topo = []\n",
158
+ " visited = set()\n",
159
+ " def build_topo(v):\n",
160
+ " if v not in visited:\n",
161
+ " visited.add(v)\n",
162
+ " for child in v._prev:\n",
163
+ " build_topo(child)\n",
164
+ " topo.append(v)\n",
165
+ "\n",
166
+ " build_topo(self)\n",
167
+ "\n",
168
+ " self.grad = 1.0\n",
169
+ " for node in reversed(topo):\n",
170
+ " node._backward()"
171
+ ],
172
+ "metadata": {
173
+ "id": "tA0zbyEwFbD5"
174
+ },
175
+ "execution_count": 3,
176
+ "outputs": []
177
+ },
178
+ {
179
+ "cell_type": "markdown",
180
+ "source": [
181
+ "---------------"
182
+ ],
183
+ "metadata": {
184
+ "id": "m9hy05zbxhLP"
185
+ }
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "source": [
190
+ "import random"
191
+ ],
192
+ "metadata": {
193
+ "id": "gu3tnJu1Wti5"
194
+ },
195
+ "execution_count": 4,
196
+ "outputs": []
197
+ },
198
+ {
199
+ "cell_type": "code",
200
+ "source": [
201
+ "class Neuron:\n",
202
+ " def __init__(self, nin):\n",
203
+ " self.w = [Value(random.uniform(-1, 1)) for _ in range(nin)]\n",
204
+ " self.b = Value(random.uniform(-1, 1))\n",
205
+ "\n",
206
+ " def __call__(self, x):\n",
207
+ " act = sum((wi * xi for wi, xi in zip(self.w, x)), self.b)\n",
208
+ " out = act.tanh()\n",
209
+ " return out\n",
210
+ "\n",
211
+ " def parameters(self):\n",
212
+ " return self.w + [self.b]\n",
213
+ "\n",
214
+ "class Layer:\n",
215
+ " def __init__(self, nin, nout):\n",
216
+ " self.neurons = [Neuron(nin) for _ in range(nout)]\n",
217
+ "\n",
218
+ " def __call__(self, x):\n",
219
+ " outs = [n(x) for n in self.neurons]\n",
220
+ " return outs[0] if len(outs) == 1 else outs\n",
221
+ "\n",
222
+ " def parameters(self):\n",
223
+ " return [p for n in self.neurons for p in n.parameters()]\n",
224
+ "\n",
225
+ " # Alternative way of writing the above return function:\n",
226
+ " # parameters = []\n",
227
+ " # for n in self.neurons:\n",
228
+ " # p = n.parameters()\n",
229
+ " # parameters.extend(p)\n",
230
+ "\n",
231
+ "class MLP:\n",
232
+ " def __init__(self, nin, nouts):\n",
233
+ " sz = [nin] + nouts\n",
234
+ " self.layers = [Layer(sz[i], sz[i + 1]) for i in range(len(nouts))]\n",
235
+ "\n",
236
+ " def __call__(self, x):\n",
237
+ " for layer in self.layers:\n",
238
+ " x = layer(x)\n",
239
+ " return x\n",
240
+ "\n",
241
+ " def parameters(self):\n",
242
+ " return [p for l in self.layers for p in l.parameters()]"
243
+ ],
244
+ "metadata": {
245
+ "id": "HmEO8Gi1KN_m"
246
+ },
247
+ "execution_count": 5,
248
+ "outputs": []
249
+ },
250
+ {
251
+ "cell_type": "markdown",
252
+ "source": [
253
+ "-------------"
254
+ ],
255
+ "metadata": {
256
+ "id": "VHG86ZRipV_g"
257
+ }
258
+ },
259
+ {
260
+ "cell_type": "markdown",
261
+ "source": [
262
+ "Now we are trying to slighly nudge the value in order to reduce the loss"
263
+ ],
264
+ "metadata": {
265
+ "id": "4P4QTecRpfJy"
266
+ }
267
+ },
268
+ {
269
+ "cell_type": "markdown",
270
+ "source": [
271
+ "So this essentially adds as an **update function**"
272
+ ],
273
+ "metadata": {
274
+ "id": "VoV7xT_Ip60A"
275
+ }
276
+ },
277
+ {
278
+ "cell_type": "code",
279
+ "source": [
280
+ "for p in n.parameters():\n",
281
+ " p.data += -0.01 * p.grad #The negative sign is to convert any negative value to positive. Therefore increasing the value of the data, therefore decresing the loss"
282
+ ],
283
+ "metadata": {
284
+ "id": "9GQoQUYEpMRP"
285
+ },
286
+ "execution_count": 12,
287
+ "outputs": []
288
+ },
289
+ {
290
+ "cell_type": "markdown",
291
+ "source": [
292
+ "------------------"
293
+ ],
294
+ "metadata": {
295
+ "id": "TwdGr8Awqam4"
296
+ }
297
+ },
298
+ {
299
+ "cell_type": "markdown",
300
+ "source": [
301
+ "------------"
302
+ ],
303
+ "metadata": {
304
+ "id": "ux4ZrKc_riiA"
305
+ }
306
+ },
307
+ {
308
+ "cell_type": "markdown",
309
+ "source": [
310
+ "Now we follow three steps: Forward pass -> Backward pass -> Update"
311
+ ],
312
+ "metadata": {
313
+ "id": "dyoqzuslp-kP"
314
+ }
315
+ },
316
+ {
317
+ "cell_type": "code",
318
+ "source": [
319
+ "x = [2.0, 3.0, -1.0]\n",
320
+ "n = MLP(3, [4, 4, 1])\n",
321
+ "n(x)"
322
+ ],
323
+ "metadata": {
324
+ "colab": {
325
+ "base_uri": "https://localhost:8080/"
326
+ },
327
+ "id": "SYoQlzlMrelv",
328
+ "outputId": "3ce2cbfa-fec9-4618-cd27-8ed9d28bcf5b"
329
+ },
330
+ "execution_count": 35,
331
+ "outputs": [
332
+ {
333
+ "output_type": "execute_result",
334
+ "data": {
335
+ "text/plain": [
336
+ "Value(data=0.33215137965743546)"
337
+ ]
338
+ },
339
+ "metadata": {},
340
+ "execution_count": 35
341
+ }
342
+ ]
343
+ },
344
+ {
345
+ "cell_type": "code",
346
+ "source": [
347
+ "xs = [\n",
348
+ " [2.0, 3.0, -1.0],\n",
349
+ " [3.0, -1.0, 0.5],\n",
350
+ " [0.5, 1.0, 1.0],\n",
351
+ " [1.0, 1.0, -1.0]\n",
352
+ "]\n",
353
+ "\n",
354
+ "ys = [1.0, -1.0, -1.0, 1.0] #output we want"
355
+ ],
356
+ "metadata": {
357
+ "id": "wRwCzkhequ5C"
358
+ },
359
+ "execution_count": 36,
360
+ "outputs": []
361
+ },
362
+ {
363
+ "cell_type": "code",
364
+ "source": [
365
+ "#forward pass\n",
366
+ "ypred = [n(x) for x in xs]\n",
367
+ "loss = sum((yout - ygt)**2 for ygt, yout in zip(ys, ypred))\n",
368
+ "loss"
369
+ ],
370
+ "metadata": {
371
+ "colab": {
372
+ "base_uri": "https://localhost:8080/"
373
+ },
374
+ "id": "Bxbev90VqFnG",
375
+ "outputId": "52407404-8787-4e29-c07b-31063bea7111"
376
+ },
377
+ "execution_count": 54,
378
+ "outputs": [
379
+ {
380
+ "output_type": "execute_result",
381
+ "data": {
382
+ "text/plain": [
383
+ "Value(data=5.767047506521353)"
384
+ ]
385
+ },
386
+ "metadata": {},
387
+ "execution_count": 54
388
+ }
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "code",
393
+ "source": [
394
+ "#backward pass\n",
395
+ "loss.backward()"
396
+ ],
397
+ "metadata": {
398
+ "id": "swKzizdIqJQf"
399
+ },
400
+ "execution_count": 55,
401
+ "outputs": []
402
+ },
403
+ {
404
+ "cell_type": "code",
405
+ "source": [
406
+ "#update\n",
407
+ "for p in n.parameters():\n",
408
+ " p.data += -0.01 * p.grad"
409
+ ],
410
+ "metadata": {
411
+ "id": "BtOE6keaqLse"
412
+ },
413
+ "execution_count": 56,
414
+ "outputs": []
415
+ },
416
+ {
417
+ "cell_type": "code",
418
+ "source": [
419
+ "#check the prediction\n",
420
+ "ypred"
421
+ ],
422
+ "metadata": {
423
+ "colab": {
424
+ "base_uri": "https://localhost:8080/"
425
+ },
426
+ "id": "O5iGQsw3qY-S",
427
+ "outputId": "2b14907c-d35f-4c4d-e503-d954e6f74435"
428
+ },
429
+ "execution_count": 57,
430
+ "outputs": [
431
+ {
432
+ "output_type": "execute_result",
433
+ "data": {
434
+ "text/plain": [
435
+ "[Value(data=-0.25151630590655727),\n",
436
+ " Value(data=0.42164884655021817),\n",
437
+ " Value(data=-0.09631033350969018),\n",
438
+ " Value(data=-0.16748189979649136)]"
439
+ ]
440
+ },
441
+ "metadata": {},
442
+ "execution_count": 57
443
+ }
444
+ ]
445
+ },
446
+ {
447
+ "cell_type": "markdown",
448
+ "source": [
449
+ "-----------"
450
+ ],
451
+ "metadata": {
452
+ "id": "qvIdNB-LsBFt"
453
+ }
454
+ },
455
+ {
456
+ "cell_type": "markdown",
457
+ "source": [
458
+ "-------------"
459
+ ],
460
+ "metadata": {
461
+ "id": "XR8TNjCDsKfb"
462
+ }
463
+ },
464
+ {
465
+ "cell_type": "markdown",
466
+ "source": [
467
+ "Putting the entire process together in a single function"
468
+ ],
469
+ "metadata": {
470
+ "id": "jUvrBdh9sLPt"
471
+ }
472
+ },
473
+ {
474
+ "cell_type": "code",
475
+ "source": [
476
+ "#Initialize the neural net\n",
477
+ "x = [2.0, 3.0, -1.0]\n",
478
+ "n = MLP(3, [4, 4, 1])\n",
479
+ "n(x)"
480
+ ],
481
+ "metadata": {
482
+ "colab": {
483
+ "base_uri": "https://localhost:8080/"
484
+ },
485
+ "id": "8wJ2E5Vsshho",
486
+ "outputId": "416ad55f-351d-4e98-f1ff-7108a2a32f65"
487
+ },
488
+ "execution_count": 58,
489
+ "outputs": [
490
+ {
491
+ "output_type": "execute_result",
492
+ "data": {
493
+ "text/plain": [
494
+ "Value(data=0.9135198339971514)"
495
+ ]
496
+ },
497
+ "metadata": {},
498
+ "execution_count": 58
499
+ }
500
+ ]
501
+ },
502
+ {
503
+ "cell_type": "code",
504
+ "source": [
505
+ "#Data definition\n",
506
+ "xs = [\n",
507
+ " [2.0, 3.0, -1.0],\n",
508
+ " [3.0, -1.0, 0.5],\n",
509
+ " [0.5, 1.0, 1.0],\n",
510
+ " [1.0, 1.0, -1.0]\n",
511
+ "]\n",
512
+ "\n",
513
+ "ys = [1.0, -1.0, -1.0, 1.0] #output we want"
514
+ ],
515
+ "metadata": {
516
+ "id": "qqZYLdOVrQ2i"
517
+ },
518
+ "execution_count": 59,
519
+ "outputs": []
520
+ },
521
+ {
522
+ "cell_type": "code",
523
+ "source": [
524
+ "\n",
525
+ "for k in range(10):\n",
526
+ "\n",
527
+ " #forward pass\n",
528
+ " ypred = [n(x) for x in xs]\n",
529
+ " loss = sum((yout - ygt)**2 for ygt, yout in zip(ys, ypred))\n",
530
+ "\n",
531
+ " #backward pass\n",
532
+ " for p in n.parameters():\n",
533
+ " p.grad = 0.0 #This is because after one round of update, we need to reset the value of the grads so that it can calculate and store the grad value of the updated loss function (i.e. The loss value that was improved after gradient descent). If we don't do this, the previous value of grad gets increamented with the new value during each back propagation (each time backward is called)\n",
534
+ " loss.backward()\n",
535
+ "\n",
536
+ " #update\n",
537
+ " #THIS HERE, WHAT WE ARE DOING IS 'GRADIENT DESCENT'. WE ARE NUDGING THE INPUT VALUES BY A LITTLE BIT\n",
538
+ " for p in n.parameters():\n",
539
+ " p.data += -0.04 * p.grad\n",
540
+ "\n",
541
+ " print(k, loss.data) #Printing the current number/iteration number plus how much loss"
542
+ ],
543
+ "metadata": {
544
+ "colab": {
545
+ "base_uri": "https://localhost:8080/"
546
+ },
547
+ "id": "S3ffWvfDsS88",
548
+ "outputId": "d3b74c8c-2d0d-4b8b-e31e-f1081138a321"
549
+ },
550
+ "execution_count": 92,
551
+ "outputs": [
552
+ {
553
+ "output_type": "stream",
554
+ "name": "stdout",
555
+ "text": [
556
+ "0 7.6021312440956095\n",
557
+ "1 8.0\n",
558
+ "2 6.398187062451399\n",
559
+ "3 7.999999999997639\n",
560
+ "4 8.0\n",
561
+ "5 7.999964084143684\n",
562
+ "6 8.0\n",
563
+ "7 8.0\n",
564
+ "8 7.999999961266539\n",
565
+ "9 8.0\n"
566
+ ]
567
+ }
568
+ ]
569
+ },
570
+ {
571
+ "cell_type": "code",
572
+ "source": [
573
+ "ypred"
574
+ ],
575
+ "metadata": {
576
+ "colab": {
577
+ "base_uri": "https://localhost:8080/"
578
+ },
579
+ "id": "JoKOXxUWtB7K",
580
+ "outputId": "3420fc62-2352-47fc-d0e5-6547f3748ca5"
581
+ },
582
+ "execution_count": 93,
583
+ "outputs": [
584
+ {
585
+ "output_type": "execute_result",
586
+ "data": {
587
+ "text/plain": [
588
+ "[Value(data=-1.0), Value(data=-1.0), Value(data=-1.0), Value(data=-1.0)]"
589
+ ]
590
+ },
591
+ "metadata": {},
592
+ "execution_count": 93
593
+ }
594
+ ]
595
+ },
596
+ {
597
+ "cell_type": "code",
598
+ "source": [
599
+ "loss"
600
+ ],
601
+ "metadata": {
602
+ "colab": {
603
+ "base_uri": "https://localhost:8080/"
604
+ },
605
+ "id": "iyZ2e7FvwT5H",
606
+ "outputId": "5666e89c-6a29-486b-d3c8-290c29635124"
607
+ },
608
+ "execution_count": 94,
609
+ "outputs": [
610
+ {
611
+ "output_type": "execute_result",
612
+ "data": {
613
+ "text/plain": [
614
+ "Value(data=8.0)"
615
+ ]
616
+ },
617
+ "metadata": {},
618
+ "execution_count": 94
619
+ }
620
+ ]
621
+ },
622
+ {
623
+ "cell_type": "markdown",
624
+ "source": [
625
+ "If the loss was reduced, then you can `n.parameters` to see what were the values into the NN that caused to get the desired target outputs"
626
+ ],
627
+ "metadata": {
628
+ "id": "JLcsDJkhwsVS"
629
+ }
630
+ },
631
+ {
632
+ "cell_type": "markdown",
633
+ "source": [
634
+ "--------------------------"
635
+ ],
636
+ "metadata": {
637
+ "id": "ubTHHuwzvNNh"
638
+ }
639
+ },
640
+ {
641
+ "cell_type": "markdown",
642
+ "source": [
643
+ "Okay so the predicted output didn't exactly come as expected 🥲 (The first and last value weren't supposed to be negative lol) \\\n",
644
+ "\\\n",
645
+ "But that was the idea of how we **train a neural net**!"
646
+ ],
647
+ "metadata": {
648
+ "id": "xFwHw6IVvOKb"
649
+ }
650
+ }
651
+ ]
652
+ }