{ "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": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "135449920624224\n", "\n", "e\n", "\n", "data -6.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920632192+\n", "\n", "+\n", "\n", "\n", "\n", "135449920624224->135449920632192+\n", "\n", "\n", "\n", "\n", "\n", "135449920624224*\n", "\n", "*\n", "\n", "\n", "\n", "135449920624224*->135449920624224\n", "\n", "\n", "\n", "\n", "\n", "135449920621248\n", "\n", "L\n", "\n", "data -8.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920621248*\n", "\n", "*\n", "\n", "\n", "\n", "135449920621248*->135449920621248\n", "\n", "\n", "\n", "\n", "\n", "135449920632624\n", "\n", "b\n", "\n", "data -3.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920632624->135449920624224*\n", "\n", "\n", "\n", "\n", "\n", "135449920619856\n", "\n", "a\n", "\n", "data 2.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920619856->135449920624224*\n", "\n", "\n", "\n", "\n", "\n", "135449920632192\n", "\n", "d\n", "\n", "data 4.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920632192->135449920621248*\n", "\n", "\n", "\n", "\n", "\n", "135449920632192+->135449920632192\n", "\n", "\n", "\n", "\n", "\n", "135449920619424\n", "\n", "c\n", "\n", "data 10.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920619424->135449920632192+\n", "\n", "\n", "\n", "\n", "\n", "135449920632288\n", "\n", "f\n", "\n", "data -2.0000\n", "\n", "grad 0.0000\n", "\n", "\n", "\n", "135449920632288->135449920621248*\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "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 }