{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### Expression: d = a * b + c" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "class Value:\n", "\n", " def __init__(self, data):\n", " self.data = data\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)\n", " return out\n", " \n", " def __mul__(self, other):\n", " out = Value(self.data * other.data)\n", " return out" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = Value(2.0)\n", "b = Value(-3.0)\n", "c = Value(10.0)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Value(data=4.0)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*b + c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SAME THING!! (Up and Down cells)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Value(data=4.0)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(a.__mul__(b)).__add__(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualization of the expression" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class Value:\n", "\n", " def __init__(self, data, _children=(), _op=''):\n", " self.data = data\n", " self._prev = set(_children)\n", " self._op = _op\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": 13, "metadata": {}, "outputs": [], "source": [ "d = a*b + c" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{Value(data=-6.0), Value(data=10.0)}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d._prev" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'+'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d._op" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------------------" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Value(data=4.0)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = Value(2.0)\n", "b = Value(-3.0)\n", "c = Value(10.0)\n", "\n", "d= a*b + c\n", "d" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting graphviz\n", " Downloading graphviz-0.20.3-py3-none-any.whl (47 kB)\n", "Installing collected packages: graphviz\n", "Successfully installed graphviz-0.20.3\n", "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: You are using pip version 21.2.3; however, version 24.2 is available.\n", "You should consider upgrading via the 'c:\\Users\\imdiv\\OneDrive\\Desktop\\GitHub Portal\\Micrograd\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n" ] } ], "source": [ "%pip install graphviz" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "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 = \"{ data %.4f }\" % ( n.data, ), shape='record')\n", " if n._op:\n", " #If this value is a result of some operation, then create an op node (in a circle/oval shape to distinguish) 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": {}, "outputs": [], "source": [ "draw_dot(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NOTE: The graph will only get generated if you have graphviz installed locally on your machine and the env variable path needs to be set. \n", "\n", "If you want to avoid downloading it locally, you can directly run this on Google Colab to view the output, as graphviz is already preinstalled for you" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Check 3_1-graph-visualisation.ipynb for viewing the outputs**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-------------------" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }