File size: 16,212 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
{
  "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": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AIP2sPDm6Los",
        "outputId": "f467edac-8c3a-4695-a651-5325a4ecea8f"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Value(data=-8.0)"
            ]
          },
          "metadata": {},
          "execution_count": 2
        }
      ],
      "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": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "L.grad = 1.0\n",
        "f.grad = 4.0\n",
        "d.grad = -2.0\n",
        "c.grad = -2.0\n",
        "e.grad = -2.0\n",
        "a.grad = 6.0\n",
        "b.grad = -4.0"
      ],
      "metadata": {
        "id": "3TCgz-n6DbzI"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "draw_dot(L)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 212
        },
        "id": "k7wjwrfo6nUl",
        "outputId": "b915567c-ba7b-44ec-fd34-97f35d258fd4"
      },
      "execution_count": 5,
      "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=\"128pt\"\n viewBox=\"0.00 0.00 1157.00 128.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 124)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-124 1153,-124 1153,4 -4,4\"/>\n<!-- 132400623613456 -->\n<g id=\"node1\" class=\"node\">\n<title>132400623613456</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642,-27.5 642,-63.5 831,-63.5 831,-27.5 642,-27.5\"/>\n<text text-anchor=\"middle\" x=\"653.5\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">d</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"665,-27.5 665,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"705\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 4.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"745,-27.5 745,-63.5 \"/>\n<text text-anchor=\"middle\" x=\"788\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 132400623605104* -->\n<g id=\"node9\" class=\"node\">\n<title>132400623605104*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"894\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"894\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132400623613456&#45;&gt;132400623605104* -->\n<g id=\"edge5\" class=\"edge\">\n<title>132400623613456&#45;&gt;132400623605104*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M831.1,-61.75C840.4,-63.37 849.38,-64.93 857.49,-66.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"857.14,-69.83 867.59,-68.09 858.34,-62.93 857.14,-69.83\"/>\n</g>\n<!-- 132400623613456+ -->\n<g id=\"node2\" class=\"node\">\n<title>132400623613456+</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"579\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"579\" y=\"-41.8\" font-family=\"Times,serif\" font-size=\"14.00\">+</text>\n</g>\n<!-- 132400623613456+&#45;&gt;132400623613456 -->\n<g id=\"edge1\" class=\"edge\">\n<title>132400623613456+&#45;&gt;132400623613456</title>\n<path fill=\"none\" stroke=\"black\" d=\"M606.03,-45.5C613.66,-45.5 622.52,-45.5 631.94,-45.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"631.94,-49 641.94,-45.5 631.94,-42 631.94,-49\"/>\n</g>\n<!-- 132400623602320 -->\n<g id=\"node3\" class=\"node\">\n<title>132400623602320</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"321,-55.5 321,-91.5 515,-91.5 515,-55.5 321,-55.5\"/>\n<text text-anchor=\"middle\" x=\"332.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">e</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"344,-55.5 344,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"429,-55.5 429,-91.5 \"/>\n<text text-anchor=\"middle\" x=\"472\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 132400623602320&#45;&gt;132400623613456+ -->\n<g id=\"edge9\" class=\"edge\">\n<title>132400623602320&#45;&gt;132400623613456+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M515.16,-56.57C524.82,-54.87 534.13,-53.23 542.49,-51.75\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"543.3,-55.17 552.54,-49.98 542.08,-48.27 543.3,-55.17\"/>\n</g>\n<!-- 132400623602320* -->\n<g id=\"node4\" class=\"node\">\n<title>132400623602320*</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"257\" y=\"-69.8\" font-family=\"Times,serif\" font-size=\"14.00\">*</text>\n</g>\n<!-- 132400623602320*&#45;&gt;132400623602320 -->\n<g id=\"edge2\" class=\"edge\">\n<title>132400623602320*&#45;&gt;132400623602320</title>\n<path fill=\"none\" stroke=\"black\" d=\"M284.26,-73.5C292.05,-73.5 301.1,-73.5 310.75,-73.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"310.99,-77 320.99,-73.5 310.99,-70 310.99,-77\"/>\n</g>\n<!-- 132400623613216 -->\n<g id=\"node5\" class=\"node\">\n<title>132400623613216</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"642.5,-82.5 642.5,-118.5 830.5,-118.5 830.5,-82.5 642.5,-82.5\"/>\n<text text-anchor=\"middle\" x=\"653\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">f</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"663.5,-82.5 663.5,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"706\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"748.5,-82.5 748.5,-118.5 \"/>\n<text text-anchor=\"middle\" x=\"789.5\" y=\"-96.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 4.0000</text>\n</g>\n<!-- 132400623613216&#45;&gt;132400623605104* -->\n<g id=\"edge7\" class=\"edge\">\n<title>132400623613216&#45;&gt;132400623605104*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M830.65,-83.73C840.2,-82.01 849.43,-80.35 857.72,-78.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"858.47,-82.27 867.69,-77.06 857.23,-75.39 858.47,-82.27\"/>\n</g>\n<!-- 132400623615808 -->\n<g id=\"node6\" class=\"node\">\n<title>132400623615808</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"4.5,-83.5 4.5,-119.5 189.5,-119.5 189.5,-83.5 4.5,-83.5\"/>\n<text text-anchor=\"middle\" x=\"16\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">a</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"27.5,-83.5 27.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"67.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 2.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"107.5,-83.5 107.5,-119.5 \"/>\n<text text-anchor=\"middle\" x=\"148.5\" y=\"-97.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 6.0000</text>\n</g>\n<!-- 132400623615808&#45;&gt;132400623602320* -->\n<g id=\"edge6\" class=\"edge\">\n<title>132400623615808&#45;&gt;132400623602320*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M189.91,-85.21C200.76,-83.29 211.28,-81.43 220.61,-79.77\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"221.37,-83.19 230.6,-78 220.15,-76.3 221.37,-83.19\"/>\n</g>\n<!-- 132400623609664 -->\n<g id=\"node7\" class=\"node\">\n<title>132400623609664</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"320,-0.5 320,-36.5 516,-36.5 516,-0.5 320,-0.5\"/>\n<text text-anchor=\"middle\" x=\"331.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">c</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"343,-0.5 343,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"386.5\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">data 10.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"430,-0.5 430,-36.5 \"/>\n<text text-anchor=\"middle\" x=\"473\" y=\"-14.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\n</g>\n<!-- 132400623609664&#45;&gt;132400623613456+ -->\n<g id=\"edge4\" class=\"edge\">\n<title>132400623609664&#45;&gt;132400623613456+</title>\n<path fill=\"none\" stroke=\"black\" d=\"M516.07,-34.98C525.42,-36.57 534.43,-38.1 542.55,-39.48\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"542.21,-42.97 552.65,-41.2 543.38,-36.07 542.21,-42.97\"/>\n</g>\n<!-- 132400623605104 -->\n<g id=\"node8\" class=\"node\">\n<title>132400623605104</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"957,-54.5 957,-90.5 1149,-90.5 1149,-54.5 957,-54.5\"/>\n<text text-anchor=\"middle\" x=\"969.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">L</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"982,-54.5 982,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1024.5\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;8.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"1067,-54.5 1067,-90.5 \"/>\n<text text-anchor=\"middle\" x=\"1108\" y=\"-68.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad 1.0000</text>\n</g>\n<!-- 132400623605104*&#45;&gt;132400623605104 -->\n<g id=\"edge3\" class=\"edge\">\n<title>132400623605104*&#45;&gt;132400623605104</title>\n<path fill=\"none\" stroke=\"black\" d=\"M921.28,-72.5C928.78,-72.5 937.44,-72.5 946.67,-72.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"946.87,-76 956.87,-72.5 946.87,-69 946.87,-76\"/>\n</g>\n<!-- 132400623612880 -->\n<g id=\"node10\" class=\"node\">\n<title>132400623612880</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"0,-28.5 0,-64.5 194,-64.5 194,-28.5 0,-28.5\"/>\n<text text-anchor=\"middle\" x=\"11.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">b</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"23,-28.5 23,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"65.5\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n<polyline fill=\"none\" stroke=\"black\" points=\"108,-28.5 108,-64.5 \"/>\n<text text-anchor=\"middle\" x=\"151\" y=\"-42.8\" font-family=\"Times,serif\" font-size=\"14.00\">grad &#45;4.0000</text>\n</g>\n<!-- 132400623612880&#45;&gt;132400623602320* -->\n<g id=\"edge8\" class=\"edge\">\n<title>132400623612880&#45;&gt;132400623602320*</title>\n<path fill=\"none\" stroke=\"black\" d=\"M194.01,-62.91C203.35,-64.5 212.37,-66.04 220.49,-67.43\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"220.16,-70.93 230.6,-69.16 221.34,-64.03 220.16,-70.93\"/>\n</g>\n</g>\n</svg>\n",
            "text/plain": [
              "<graphviz.graphs.Digraph at 0x786aec236c20>"
            ]
          },
          "metadata": {},
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "--------------------"
      ],
      "metadata": {
        "id": "WqQ2p-U1eUnJ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now, we are going to try and nudge the leaf nodes (because those are usually what we have control over. In our example: a,b,c,f) slightly towards the gradient value, to nudge L towards a more positive direction."
      ],
      "metadata": {
        "id": "cpx9Me4LeVfx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a.data += 0.01 * a.grad\n",
        "b.data += 0.01 * b.grad\n",
        "c.data += 0.01 * c.grad\n",
        "f.data += 0.01 * f.grad\n",
        "\n",
        "e = a*b;\n",
        "d= e + c;\n",
        "L = d*f;\n",
        "L\n",
        "\n",
        "print(L.data)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_edHadeReTBn",
        "outputId": "3631e3e6-5bbf-4ceb-d1e1-45599a5a2736"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-6.723584000000001\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Therefore the value of L was pushed to a more positive direction from -8.0 to -6.0"
      ],
      "metadata": {
        "id": "aXwYpQKKgYGg"
      }
    }
  ]
}