File size: 17,637 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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **INITIAL TEMPLATE**\n",
"**Will be useful for the remaining lectures or calculations within this section**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----------------"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jtRAdDVT6jf2"
},
"outputs": [],
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AIP2sPDm6Los",
"outputId": "11771f7b-a039-4311-b173-bf656135506b"
},
"outputs": [
{
"data": {
"text/plain": [
"Value(data=-8.0)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
"execution_count": null,
"metadata": {
"id": "T0rN8d146jvF"
},
"outputs": [],
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 247
},
"id": "k7wjwrfo6nUl",
"outputId": "d78c4618-6574-49f9-8e80-f2faa8dad69a"
},
"outputs": [
{
"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 -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->135449920632192+ -->\n",
"<g id=\"edge9\" class=\"edge\">\n",
"<title>135449920624224->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*->135449920624224 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>135449920624224*->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 -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*->135449920621248 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>135449920621248*->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 -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->135449920624224* -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>135449920632624->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->135449920624224* -->\n",
"<g id=\"edge8\" class=\"edge\">\n",
"<title>135449920619856->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->135449920621248* -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>135449920632192->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+->135449920632192 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>135449920632192+->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->135449920632192+ -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>135449920619424->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 -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->135449920621248* -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>135449920632288->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>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"draw_dot(L)"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|