Skip to content

Instantly share code, notes, and snippets.

@1WorldCapture
Last active March 3, 2024 01:53
Show Gist options
  • Save 1WorldCapture/b7983f0753bcf5497554d6fa072d5c77 to your computer and use it in GitHub Desktop.
Save 1WorldCapture/b7983f0753bcf5497554d6fa072d5c77 to your computer and use it in GitHub Desktop.
[AI DIY] general python import statements in jupyter #python #jupyter
from graphviz import Digraph
def trace(root):
# builds a set of all nodes and edges in a graph
nodes,edges = set(), set()
def build(v):
if v not in nodes:
nodes.add(v)
for child in v._prev:
edges.add((child, v))
build(child)
build(root)
return nodes, edges
def draw_dot(root):
dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'}) # LR = left to right
nodes, edges = trace(root)
for n in nodes:
uid = str(id(n))
# for any value in the graph, create a rectangular ('record') node for it
dot.node(name = uid, label = "{ %s | data %.4f }" % (n.label, n.data), shape = 'record')
if n._op:
# if this value is a result of some operation, create an op node for it
dot.node(name = uid + n._op, label = n._op)
# and connect this node to it
dot.edge(uid + n._op, uid)
for n1, n2 in edges:
# connect n1 to the op node of n2
dot.edge(str(id(n1)), str(id(n2)) + n2._op)
return dot
import math
import random
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment