File size: 85,252 Bytes
525fbd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "class Value:\n",
        "\n",
        "    def __init__(self, data, _children=(), _op='', label=''):\n",
        "        self.data = data\n",
        "        self.grad = 0.0\n",
        "        self._prev = set(_children)\n",
        "        self._op = _op\n",
        "        self.label = label\n",
        "\n",
        "\n",
        "    def __repr__(self):   # This basically allows us to print nicer looking expressions for the final output\n",
        "        return f\"Value(data={self.data})\"\n",
        "\n",
        "    def __add__(self, other):\n",
        "        out = Value(self.data + other.data, (self, other), '+')\n",
        "        return out\n",
        "\n",
        "    def __mul__(self, other):\n",
        "        out = Value(self.data * other.data, (self, other), '*')\n",
        "        return out"
      ],
      "metadata": {
        "id": "jtRAdDVT6jf2"
      },
      "execution_count": 20,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AIP2sPDm6Los",
        "outputId": "8e1d5665-fc27-4ddb-95ac-a9cf53f25d51"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Value(data=-8.0)"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ],
      "source": [
        "a = Value(2.0, label='a')\n",
        "b = Value(-3.0, label='b')\n",
        "c = Value(10.0, label='c')\n",
        "e = a*b; e.label='e'\n",
        "d= e + c; d.label='d'\n",
        "f = Value(-2.0, label='f')\n",
        "L = d*f; L.label='L'\n",
        "L"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from graphviz import Digraph\n",
        "\n",
        "def trace(root):\n",
        "    #Builds a set of all nodes and edges in a graph\n",
        "    nodes, edges = set(), set()\n",
        "    def build(v):\n",
        "        if v not in nodes:\n",
        "            nodes.add(v)\n",
        "            for child in v._prev:\n",
        "                edges.add((child, v))\n",
        "                build(child)\n",
        "    build(root)\n",
        "    return nodes, edges\n",
        "\n",
        "def draw_dot(root):\n",
        "    dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) #LR == Left to Right\n",
        "\n",
        "    nodes, edges = trace(root)\n",
        "    for n in nodes:\n",
        "        uid = str(id(n))\n",
        "        #For any value in the graph, create a rectangular ('record') node for it\n",
        "        dot.node(name = uid, label = \"{ %s | data %.4f | grad %.4f }\" % ( n.label, n.data, n.grad), shape='record')\n",
        "        if n._op:\n",
        "            #If this value is a result of some operation, then create an op node for it\n",
        "            dot.node(name = uid + n._op, label=n._op)\n",
        "            #and connect this node to it\n",
        "            dot.edge(uid + n._op, uid)\n",
        "\n",
        "    for n1, n2 in edges:\n",
        "        #Connect n1 to the node of n2\n",
        "        dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
        "\n",
        "    return dot"
      ],
      "metadata": {
        "id": "T0rN8d146jvF"
      },
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 247
        },
        "id": "k7wjwrfo6nUl",
        "outputId": "d78c4618-6574-49f9-8e80-f2faa8dad69a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "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",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x7b30e4675ae0>"
            ]
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "----------------------"
      ],
      "metadata": {
        "id": "UO6I8Z-_CaNv"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### **Now, let's start to fill those grad values**"
      ],
      "metadata": {
        "id": "wB-SONL3CltR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "--------------"
      ],
      "metadata": {
        "id": "EhvqPDYqF50Z"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Let's first find the derivative of L w.r.t L**"
      ],
      "metadata": {
        "id": "dF0QlSFJCbsI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.001\n",
        "\n",
        "  #Here we are basically making them as local variables, to not affect the global variables on top\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data + h\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AWQsdevqCUks",
        "outputId": "e08b83fd-b101-4fdc-a554-41561c00a08b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1.000000000000334\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "This was theoritically obvious as well. The derivitive of L wrt L will be one.\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "So, lets add that value manually. (Remember to run the global variables for this)"
      ],
      "metadata": {
        "id": "HbfBzVQ4EEHM"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "L.grad = 1.0"
      ],
      "metadata": {
        "id": "3TCgz-n6DbzI"
      },
      "execution_count": 23,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 212
        },
        "id": "RS6YodRTEX43",
        "outputId": "3b58ed12-b486-4452-b700-8048b1e03e3b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "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",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x7f12efa36800>"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "-----------"
      ],
      "metadata": {
        "id": "4x1UEdrOHalT"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Now, we find the derivative of L wrt to f and d**"
      ],
      "metadata": {
        "id": "Hhj8DrcUF7fI"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "So, mathematically:\n",
        "\n",
        "dL/dd = ?\n",
        "\n",
        "**L = d * f**\n",
        "\n",
        "Therefore, dL/dd = f\n",
        "\n",
        "If we do manual calculation to verify,           \\\n",
        "\n",
        "=> f(x+h) - f(x) / h                                \\\n",
        "\n",
        "(Remember the f(x) is basically L here)          \\\n",
        "=> (d+h)*f - d*f / h                                \\\n",
        "=> df + hf - df / h                                 \\\n",
        "=> hf/h                                             \\\n",
        "= f\n"
      ],
      "metadata": {
        "id": "SUi_wdLTGCsq"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "So here if you see,\n",
        "\n",
        "The derivative of L wrt f is the value in d \\\n",
        "& \\\n",
        "The derivative of L wrt d is the value in f\n",
        "\n",
        "So, grad f is 4.0 \\\n",
        "and grad d is -2.0\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "Lets check this in code!"
      ],
      "metadata": {
        "id": "8ApC2l-HHfHi"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# STARTING WITH d\n",
        "\n",
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.001\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  d.data = d.data + h\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2wx02cE6EYOR",
        "outputId": "f284e1ac-4c6f-490b-c8f3-e94dfbd9923e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-2.000000000000668\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# NOW WITH f\n",
        "\n",
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.00001\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0 + h, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5haMwvymIRxx",
        "outputId": "7b372e31-8fa4-42d3-c591-371d3c49c78d"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "4.000000000026205\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "So, now that we have verified that mathematically and on our code. Lets manually add those variables to the graph"
      ],
      "metadata": {
        "id": "EB8w0lF0IofD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "f.grad = 4.0\n",
        "d.grad = -2.0"
      ],
      "metadata": {
        "id": "pS4NnAZVIML9"
      },
      "execution_count": 24,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 212
        },
        "id": "ko5oltNPJDtc",
        "outputId": "d78137fe-0afa-449f-db42-0a079ebfffa4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "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",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x7f12ef9a3eb0>"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "----------------------------"
      ],
      "metadata": {
        "id": "39nOWCuEskoQ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### **VERY IMPORTANT PART**"
      ],
      "metadata": {
        "id": "UKflj5yEsl6i"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we'll be calculating the derivatives of the middle nodes"
      ],
      "metadata": {
        "id": "AM3bFT0Eswmz"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "##### **Starting with c & e**\n",
        "\n",
        "dL/dd had already been calculated (Check end of 4_1-manual-backpropagation notebook)\n",
        "\n",
        "d = c + e\n",
        "\n",
        "now,\n",
        "Derivative of d wrt c, will be 1 \\\n",
        "Derivative of d wrt e, will be 1 \\\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "Because the derivative of '+' operation variables will lead to 1 (Calculus basics, it leads to constant, so 1)\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "If we try to prove this mathematically:\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "\td = c + e\n",
        "\tf(x+h) - f(x) / h\n",
        "\tNow, we'll calculate wrt c\n",
        "\t=> ( ((c+h)+e) - (c+e) ) / h\n",
        "\t=> c + h + e - c - e / h\n",
        "\t=> h / h\n",
        "\t=> 1\n",
        "\tTherefore, dd/dc = 1\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "Therefore, we can just substitute the value respectively.\n",
        "\n",
        "For node c:\n",
        "\tdL/dc = dL/dd . dd/dc \\\n",
        "  So here, the values should be -> dL/dc = -2.0 * 1 = -2.0\n",
        "\n",
        "For node e:\n",
        "\tdL/de = dL/dd . dd/de \\\n",
        "  So here, the values should be -> dL/de = -2.0 * 1 = -2.0"
      ],
      "metadata": {
        "id": "Fu0K97wfs4m9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# NOW WITH c\n",
        "\n",
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.00001\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0 + h, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "m_zBXwc6sgGA",
        "outputId": "70b0d9cd-59a3-48eb-8a24-c69aeaf776de"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-1.999999987845058\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# NOW WITH e\n",
        "\n",
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.00001\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  e.data += h\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "R-EE419ftb_x",
        "outputId": "26c102c8-5214-4f4c-f566-c0df6d13fb15"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-1.9999999999242843\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Therefore, we now add those values manually\n",
        "c.grad = -2.0\n",
        "e.grad = -2.0"
      ],
      "metadata": {
        "id": "v6VKbkQAtw7H"
      },
      "execution_count": 25,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 247
        },
        "id": "QubwLaK6t6fb",
        "outputId": "dbe3f5cf-1576-495a-9e24-9e16c9454d72"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "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",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x7b89fc291600>"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "--------------"
      ],
      "metadata": {
        "id": "m8wvNP7tuH5_"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "##### **Continuing with a & b**\n",
        "\n",
        "Same principle as above, but a different kind of equation here.\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "Also remember here, derivative of L wrt e was just calculated above^ (dL/de)\n",
        "\n",
        "e = a * b\n",
        "\n",
        "Therefore,\n",
        "Derivative of e wrt a, will be b\n",
        "Derivative of e wrt b, will be a\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "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",
        "\td/da(a * b) = b\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "If we try to prove this mathematically,\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "\te = a * b\n",
        "\tf(x+h) - f(x) / h\n",
        "\tRemember, f(x) is equation here. So, finding wrt a, substituting the values\n",
        "\t=> ( ((a + h) * b) - (a * b) ) / h\n",
        "\t=> ab + hb - ab / h\n",
        "\t=> hb / h\n",
        "\t=> b\n",
        "\tTherefore, de/da = b\n",
        "\n",
        "&nbsp;\n",
        "\n",
        "Therefore, we can just substitute the value respectively.\n",
        "\n",
        "For node a:\n",
        "\tdL/da = dL/de . dd/da \\\n",
        "  So here, the values should be -> dL/da = -2.0 * -3.0 = 6.0\n",
        "\n",
        "For node b:\n",
        "\tdL/db = dL/de . dd/db \\\n",
        "  So here, the values should be -> dL/db = -2.0 * 2.0 = -4.0"
      ],
      "metadata": {
        "id": "O8n9U7ZJuIyd"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# NOW WITH a\n",
        "\n",
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.00001\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0 + h, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YVDtrQyxuBet",
        "outputId": "b41d3c16-53f3-4c65-9a8e-c8bd44d5084f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "6.000000000128124\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# NOW WITH b\n",
        "\n",
        "#This is just a staging function to show how the calculation of each of the derivative is taking place\n",
        "def lol():\n",
        "\n",
        "  h = 0.00001\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L1 = L.data #L is basically a node, so we need its data\n",
        "\n",
        "  a = Value(2.0, label='a')\n",
        "  b = Value(-3.0 + h, label='b')\n",
        "  c = Value(10.0, label='c')\n",
        "  e = a*b; e.label='e'\n",
        "  d= e + c; d.label='d'\n",
        "  f = Value(-2.0, label='f')\n",
        "  L = d*f; L.label='L'\n",
        "  L2 = L.data\n",
        "\n",
        "  print((L2-L1)/h)\n",
        "\n",
        "lol()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sHZl0wacuaTI",
        "outputId": "48107eca-924f-4546-8088-cdc78f93f0bf"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-4.000000000026205\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#Now, we add those values manually\n",
        "a.grad = 6.0\n",
        "b.grad = -4.0"
      ],
      "metadata": {
        "id": "9chK6a53ugZ1"
      },
      "execution_count": 26,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 247
        },
        "id": "UTIlEpQSundk",
        "outputId": "434f0ad9-3ae0-4e38-b88f-0a9a5f87e8b1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "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",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x7b89ba3d7f70>"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "----------------"
      ],
      "metadata": {
        "id": "gzgHxzqZwEzZ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### **Hence the FINAL GENERATED GRAPH AFTER MANUAL BACKPROPAGATION!!**"
      ],
      "metadata": {
        "id": "1ucLHBf8uso3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 247
        },
        "id": "Witeo84_wF-p",
        "outputId": "ad44a9d6-d533-475d-f80b-46c681a1e6de"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "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",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x7b89ba3d7520>"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    }
  ]
}