File size: 73,654 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 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"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": 1,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import math"
],
"metadata": {
"id": "JlYxBvFK0AjA"
},
"execution_count": 2,
"outputs": []
},
{
"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._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\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",
"\n",
" def backward():\n",
" self.grad = 1.0 * out.grad #Remember we are doing chain rule here, hence the product with out.grad\n",
" other.grad = 1.0 * out.grad\n",
"\n",
" out._backward = backward\n",
" return out\n",
"\n",
" def __mul__(self, other):\n",
" out = Value(self.data * other.data, (self, other), '*')\n",
"\n",
" def backward():\n",
" self.grad = other.data * out.grad #Remember we are doing chain rule here, hence the product with out.grad\n",
" other.grad = self.data * out.grad\n",
"\n",
" out._backward = backward\n",
" return out\n",
"\n",
" def tanh(self):\n",
" x = self.data\n",
" t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
" out = Value(t, (self, ), 'tanh')\n",
"\n",
" def backward():\n",
" self.grad = 1 - (t**2) * out.grad #Remember we are doing chain rule here, hence the product with out.grad\n",
"\n",
" out._backward = backward\n",
" return out"
],
"metadata": {
"id": "4XPxg_t3wl35"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#Inputs x1, x2 of the neuron\n",
"x1 = Value(2.0, label='x1')\n",
"x2 = Value(0.0, label='x2')\n",
"\n",
"#Weights w1, w2 of the neuron - The synaptic values\n",
"w1 = Value(-3.0, label='w1')\n",
"w2 = Value(1.0, label='w2')\n",
"\n",
"#The bias of the neuron\n",
"b = Value(6.8813735870195432, label='b')\n",
"\n",
"x1w1 = x1*w1; x1w1.label = 'x1*w1'\n",
"x2w2 = x2*w2; x2w2.label = 'x2*w2'\n",
"\n",
"#The summation\n",
"x1w1x2w2 = x1w1 + x2w2; x1w1x2w2.label = 'x1*w1 + x2*w2'\n",
"\n",
"#n is basically the cell body, but without the activation function\n",
"n = x1w1x2w2 + b; n.label = 'n'\n",
"\n",
"#Now we pass n to the activation function\n",
"o = n.tanh(); o.label = 'o'"
],
"metadata": {
"id": "S3HaLbW_zvne"
},
"execution_count": 14,
"outputs": []
},
{
"cell_type": "code",
"source": [
"o.grad = 1.0 #This is the base case, we set this"
],
"metadata": {
"id": "WJyfRZOvCBlp"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"draw_dot(o)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 322
},
"id": "QhJO9xrb-Ius",
"outputId": "c923ed21-13d0-4a4d-d470-89b0d767e822"
},
"execution_count": 6,
"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=\"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 -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->132397003754704* -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397001389632->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->132397004827872tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397004820576->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+->132397004820576 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397004820576+->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->132397003754752* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397001386128->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->132397004820576+ -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003754176->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 -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->132397003766752+ -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003754704->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*->132397003754704 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397003754704*->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->132397004827872 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397004827872tanh->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->132397003766752+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003754752->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*->132397003754752 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397003754752*->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->132397003754752* -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397001390400->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->132397003754704* -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397001385312->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 -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->132397004820576+ -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003766752->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+->132397003766752 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397003766752+->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",
"text/plain": [
"<graphviz.graphs.Digraph at 0x786a1460c040>"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"source": [
"-----------"
],
"metadata": {
"id": "lbX2sHA9AN67"
}
},
{
"cell_type": "markdown",
"source": [
"**Instead of calling the '_ backward' function each time, we are creating it as a function in the Value object itself.**"
],
"metadata": {
"id": "UapnDEisAcZF"
}
},
{
"cell_type": "markdown",
"source": [
"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",
"\n",
"\n",
"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."
],
"metadata": {
"id": "33aDIzRvAOm0"
}
},
{
"cell_type": "code",
"source": [
"topo = []\n",
"visited = set() #We are maintaining a set of visited nodes\n",
"\n",
"def build_topo(v):\n",
"\n",
" if v not in visited:\n",
" visited.add(v)\n",
"\n",
" for child in v._prev:\n",
" build_topo(child)\n",
"\n",
" topo.append(v) #Adds itself, only after all its children have been added\n",
"\n",
"build_topo(o) #We are starting from the root node, for us 'o'\n",
"topo"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GyqAKGDrATCU",
"outputId": "e1502c86-e5d8-4173-bb60-f1cd1f008cec"
},
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[Value(data=6.881373587019543),\n",
" Value(data=1.0),\n",
" Value(data=0.0),\n",
" Value(data=0.0),\n",
" Value(data=-3.0),\n",
" Value(data=2.0),\n",
" Value(data=-6.0),\n",
" Value(data=-6.0),\n",
" Value(data=0.8813735870195432),\n",
" Value(data=0.7071067811865476)]"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"source": [
"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."
],
"metadata": {
"id": "YAxUnrVQAX6c"
}
},
{
"cell_type": "code",
"source": [
"for node in reversed(topo):\n",
" node._backward()"
],
"metadata": {
"id": "drPd-zukAYtV"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"And now, we will add all the calculated gradients to the graph!"
],
"metadata": {
"id": "66r_NGglCW-j"
}
},
{
"cell_type": "code",
"source": [
"draw_dot(o)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 322
},
"id": "G5IE00IVCVyJ",
"outputId": "fa67f40b-9803-4036-ede2-49094d23afa8"
},
"execution_count": 9,
"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=\"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 -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->132397003754704* -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397001389632->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->132397004827872tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397004820576->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+->132397004820576 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397004820576+->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->132397003754752* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397001386128->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->132397004820576+ -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003754176->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 -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->132397003766752+ -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003754704->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*->132397003754704 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397003754704*->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->132397004827872 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397004827872tanh->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->132397003766752+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003754752->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*->132397003754752 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397003754752*->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->132397003754752* -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397001390400->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 -1.5000</text>\n</g>\n<!-- 132397001385312->132397003754704* -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397001385312->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 -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->132397004820576+ -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003766752->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+->132397003766752 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397003766752+->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",
"text/plain": [
"<graphviz.graphs.Digraph at 0x786a4a853580>"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "markdown",
"source": [
"---------------------------------"
],
"metadata": {
"id": "ZO28CHT6CgBt"
}
},
{
"cell_type": "markdown",
"source": [
"## **Now, we add this functionality to our value object**"
],
"metadata": {
"id": "Ti2oC9OQCg_d"
}
},
{
"cell_type": "code",
"source": [
"import math"
],
"metadata": {
"id": "pWY4aqA8CocW"
},
"execution_count": null,
"outputs": []
},
{
"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._backward = lambda: None #Its an empty function by default. This is what will do that gradient calculation at each of the operations.\n",
" self._prev = set(_children)\n",
" self._op = _op\n",
" self.label = label\n",
"\n",
"\n",
" def __repr__(self):\n",
" return f\"Value(data={self.data})\"\n",
"\n",
" def __add__(self, other):\n",
" out = Value(self.data + other.data, (self, other), '+')\n",
"\n",
" def backward():\n",
" self.grad = 1.0 * out.grad\n",
" other.grad = 1.0 * out.grad\n",
"\n",
" out._backward = backward\n",
" return out\n",
"\n",
" def __mul__(self, other):\n",
" out = Value(self.data * other.data, (self, other), '*')\n",
"\n",
" def backward():\n",
" self.grad = other.data * out.grad\n",
" other.grad = self.data * out.grad\n",
"\n",
" out._backward = backward\n",
" return out\n",
"\n",
" def tanh(self):\n",
" x = self.data\n",
" t = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)\n",
" out = Value(t, (self, ), 'tanh')\n",
"\n",
" def backward():\n",
" self.grad = 1 - (t**2) * out.grad\n",
"\n",
" out._backward = backward\n",
" return out\n",
"\n",
" def backward(self):\n",
"\n",
" topo = []\n",
" visited = set()\n",
" def build_topo(v):\n",
" if v not in visited:\n",
" visited.add(v)\n",
" for child in v._prev:\n",
" build_topo(child)\n",
" topo.append(v)\n",
"\n",
" build_topo(self)\n",
"\n",
" self.grad = 1.0\n",
" for node in reversed(topo):\n",
" node._backward()"
],
"metadata": {
"id": "5g8aT0yjCeY1"
},
"execution_count": 16,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**This is the updated value object^**"
],
"metadata": {
"id": "aj0FopmoDf4M"
}
},
{
"cell_type": "markdown",
"source": [
"---------"
],
"metadata": {
"id": "iUJHQaBXDlz8"
}
},
{
"cell_type": "markdown",
"source": [
"Cross-verifying it once"
],
"metadata": {
"id": "k1LaYr_oDjic"
}
},
{
"cell_type": "code",
"source": [
"#I re-run the cells containing the node values as well\n",
"draw_dot(o)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 322
},
"id": "SPvXbZ-zDmsE",
"outputId": "285075bc-a1ce-4ad7-bf2f-00e524611495"
},
"execution_count": 17,
"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=\"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->132397006959184 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397006959184tanh->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->132397006954336* -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003209840->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->132397006954336* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397003210368->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->132397006954480+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003199232->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->132397004831184* -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003199280->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->132397006953856+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397006954336->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*->132397006954336 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397006954336*->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 -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->132397006954480+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397006953856->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+->132397006953856 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397006953856+->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 -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->132397004831184* -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003200912->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 -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->132397006953856+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397004831184->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*->132397004831184 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397004831184*->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->132397006959184tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397006954480->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+->132397006954480 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397006954480+->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",
"text/plain": [
"<graphviz.graphs.Digraph at 0x786a142ad240>"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"o.backward()"
],
"metadata": {
"id": "vcJmDVMxEPFJ"
},
"execution_count": 18,
"outputs": []
},
{
"cell_type": "code",
"source": [
"draw_dot(o)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 322
},
"id": "weL9YNfTD09O",
"outputId": "3d648d4e-c890-4347-8f23-9cd193d960d9"
},
"execution_count": 19,
"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=\"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->132397006959184 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132397006959184tanh->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->132397006954336* -->\n<g id=\"edge13\" class=\"edge\">\n<title>132397003209840->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->132397006954336* -->\n<g id=\"edge12\" class=\"edge\">\n<title>132397003210368->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->132397006954480+ -->\n<g id=\"edge14\" class=\"edge\">\n<title>132397003199232->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 -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->132397004831184* -->\n<g id=\"edge10\" class=\"edge\">\n<title>132397003199280->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->132397006953856+ -->\n<g id=\"edge6\" class=\"edge\">\n<title>132397006954336->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*->132397006954336 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132397006954336*->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 -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->132397006954480+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>132397006953856->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+->132397006953856 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132397006953856+->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 -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->132397004831184* -->\n<g id=\"edge8\" class=\"edge\">\n<title>132397003200912->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 -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->132397006953856+ -->\n<g id=\"edge7\" class=\"edge\">\n<title>132397004831184->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*->132397004831184 -->\n<g id=\"edge4\" class=\"edge\">\n<title>132397004831184*->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->132397006959184tanh -->\n<g id=\"edge11\" class=\"edge\">\n<title>132397006954480->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+->132397006954480 -->\n<g id=\"edge5\" class=\"edge\">\n<title>132397006954480+->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",
"text/plain": [
"<graphviz.graphs.Digraph at 0x786a142ad630>"
]
},
"metadata": {},
"execution_count": 19
}
]
}
]
} |