{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Note: Start from 3-value-object.ipynb and then come back to this notebook**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "a3vQTv60x2DL" }, "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": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mMs20Lgfx5T-", "outputId": "75f6bb3c-b38b-43cd-8790-f4d533a9d786" }, "outputs": [ { "data": { "text/plain": [ "Value(data=4.0)" ] }, "execution_count": 2, "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": "markdown", "metadata": { "id": "nw3EmXumzbrm" }, "source": [ "------------------------------" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "uFstZf41x7Y-" }, "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 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": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 191 }, "id": "ctAf9yvAx-Gd", "outputId": "d2ec2ab2-6f50-495d-825f-13eb8fe9e11b" }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "132024466696192\n", "\n", "data -6.0000\n", "\n", "\n", "\n", "132024466692304+\n", "\n", "+\n", "\n", "\n", "\n", "132024466696192->132024466692304+\n", "\n", "\n", "\n", "\n", "\n", "132024466696192*\n", "\n", "*\n", "\n", "\n", "\n", "132024466696192*->132024466696192\n", "\n", "\n", "\n", "\n", "\n", "132024466696240\n", "\n", "data 10.0000\n", "\n", "\n", "\n", "132024466696240->132024466692304+\n", "\n", "\n", "\n", "\n", "\n", "132024466692304\n", "\n", "data 4.0000\n", "\n", "\n", "\n", "132024466692304+->132024466692304\n", "\n", "\n", "\n", "\n", "\n", "132024466695424\n", "\n", "data 2.0000\n", "\n", "\n", "\n", "132024466695424->132024466696192*\n", "\n", "\n", "\n", "\n", "\n", "132024466702288\n", "\n", "data -3.0000\n", "\n", "\n", "\n", "132024466702288->132024466696192*\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draw_dot(d)" ] }, { "cell_type": "markdown", "metadata": { "id": "0jbMh4zuzdPe" }, "source": [ "----------------------------" ] }, { "cell_type": "markdown", "metadata": { "id": "5hRXnSyp0AVJ" }, "source": [ "Same graph, but we are just adding labels" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "W4YOVlr3yDk1" }, "outputs": [], "source": [ "class Value:\n", "\n", " def __init__(self, data, _children=(), _op='', label=''):\n", " self.data = data\n", " self._prev = set(_children)\n", " self._op = _op\n", " self.label = label\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": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "N_Hfq2Ew0HDD", "outputId": "4bac705a-5a44-41a0-958f-54529ac4ee28" }, "outputs": [ { "data": { "text/plain": [ "Value(data=4.0)" ] }, "execution_count": 6, "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", "d" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "hLg_Kkmi0I-T" }, "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 }\" % ( n.label, n.data), 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": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 192 }, "id": "-sUwmERm0Khb", "outputId": "ccbf71a3-95cd-4449-b35c-40534ad17f2f" }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "132023956475904\n", "\n", "b\n", "\n", "data -3.0000\n", "\n", "\n", "\n", "132023956476336*\n", "\n", "*\n", "\n", "\n", "\n", "132023956475904->132023956476336*\n", "\n", "\n", "\n", "\n", "\n", "132023956474896\n", "\n", "c\n", "\n", "data 10.0000\n", "\n", "\n", "\n", "132023956471968+\n", "\n", "+\n", "\n", "\n", "\n", "132023956474896->132023956471968+\n", "\n", "\n", "\n", "\n", "\n", "132023956471968\n", "\n", "d\n", "\n", "data 4.0000\n", "\n", "\n", "\n", "132023956471968+->132023956471968\n", "\n", "\n", "\n", "\n", "\n", "132023956476144\n", "\n", "a\n", "\n", "data 2.0000\n", "\n", "\n", "\n", "132023956476144->132023956476336*\n", "\n", "\n", "\n", "\n", "\n", "132023956476336\n", "\n", "e\n", "\n", "data -6.0000\n", "\n", "\n", "\n", "132023956476336->132023956471968+\n", "\n", "\n", "\n", "\n", "\n", "132023956476336*->132023956476336\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draw_dot(d)" ] }, { "cell_type": "markdown", "metadata": { "id": "vKd4M_c10-ww" }, "source": [ "------------------------" ] }, { "cell_type": "markdown", "metadata": { "id": "-NTWJR0Z0_kg" }, "source": [ "Adding another layer to this NN" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5C6tTU9A0o4d", "outputId": "a3992bc0-43ea-45fe-fca4-676a26b18e44" }, "outputs": [ { "data": { "text/plain": [ "Value(data=-8.0)" ] }, "execution_count": 9, "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": 10, "metadata": { "id": "q968N1DP1Kgp" }, "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 }\" % ( n.label, n.data), 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": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 230 }, "id": "jirvOGVt1LHZ", "outputId": "a3dcfab6-0663-4631-8465-02acfd219e19" }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "132023956477440\n", "\n", "a\n", "\n", "data 2.0000\n", "\n", "\n", "\n", "132023956475136*\n", "\n", "*\n", "\n", "\n", "\n", "132023956477440->132023956475136*\n", "\n", "\n", "\n", "\n", "\n", "132023956475136\n", "\n", "e\n", "\n", "data -6.0000\n", "\n", "\n", "\n", "132023956479264+\n", "\n", "+\n", "\n", "\n", "\n", "132023956475136->132023956479264+\n", "\n", "\n", "\n", "\n", "\n", "132023956475136*->132023956475136\n", "\n", "\n", "\n", "\n", "\n", "132023956479264\n", "\n", "d\n", "\n", "data 4.0000\n", "\n", "\n", "\n", "132023956481472*\n", "\n", "*\n", "\n", "\n", "\n", "132023956479264->132023956481472*\n", "\n", "\n", "\n", "\n", "\n", "132023956479264+->132023956479264\n", "\n", "\n", "\n", "\n", "\n", "132023956484928\n", "\n", "f\n", "\n", "data -2.0000\n", "\n", "\n", "\n", "132023956484928->132023956481472*\n", "\n", "\n", "\n", "\n", "\n", "132023956483920\n", "\n", "c\n", "\n", "data 10.0000\n", "\n", "\n", "\n", "132023956483920->132023956479264+\n", "\n", "\n", "\n", "\n", "\n", "132023956482432\n", "\n", "b\n", "\n", "data -3.0000\n", "\n", "\n", "\n", "132023956482432->132023956475136*\n", "\n", "\n", "\n", "\n", "\n", "132023956481472\n", "\n", "L\n", "\n", "data -8.0000\n", "\n", "\n", "\n", "132023956481472*->132023956481472\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draw_dot(L)" ] }, { "cell_type": "markdown", "metadata": { "id": "7ZGzut4D1UXS" }, "source": [ "----------------------" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }