tfgraphviz

Examples


In [1]:
import tensorflow as tf
import tfgraphviz as tfg

In [2]:
calc_g = tf.Graph()

In [3]:
with calc_g.as_default():
    a = tf.constant(1, name="a")
    b = tf.constant(2, name="b")
    c = tf.add(a, b, name="add")

Visualize a graph with tfg.board(...)


In [4]:
tfg.board(calc_g)


Out[4]:
G a a add add a->add b b b->add

In [5]:
reg_g = tf.Graph()

In [6]:
with reg_g.as_default():
    import numpy as np
    x_data = np.random.rand(100).astype(np.float32)
    y_data = x_data * 0.1 + 0.3
    W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
    b = tf.Variable(tf.zeros([1]))
    y = W * x_data + b
    loss = tf.reduce_mean(tf.square(y - y_data))
    optimizer = tf.train.GradientDescentOptimizer(0.5)
    train = optimizer.minimize(loss)

In [7]:
tfg.board(reg_g)


Out[7]:
G Square Square Mean Mean Square->Mean 100 Const Const Const->Mean 1 sub sub sub->Square 100 gradients gradients sub->gradients 100 GradientDescent GradientDescent add add add->sub 100 zeros zeros Variable_1 Variable_1 zeros->Variable_1 1 Variable_1->GradientDescent 1 Variable_1->add 1 Variable Variable Variable->GradientDescent 1 mul mul Variable->mul 1 Variable->gradients 1 mul->add 100 mul->gradients 100 gradients->GradientDescent random_uniform random_uniform random_uniform->Variable 1

In [8]:
tfg.board(reg_g, depth=2)


Out[8]:
G cluster_6 random_uniform cluster_0 sub cluster_1 GradientDescent cluster_2 Variable_1 cluster_3 Variable cluster_4 mul cluster_5 gradients Square Square Mean Mean Square->Mean 100 Const Const Const->Mean 1 add add sub sub add->sub 100 zeros zeros Variable_1/Assign Assign zeros->Variable_1/Assign 1 sub/y y sub/y->sub 100 sub->Square 100 gradients/Square_grad Square_grad sub->gradients/Square_grad 100 GradientDescent/update_Variable_1 update_Variable_1 GradientDescent/learning_rate learning_rate GradientDescent/learning_rate->GradientDescent/update_Variable_1 GradientDescent/update_Variable update_Variable GradientDescent/learning_rate->GradientDescent/update_Variable GradientDescent GradientDescent Variable_1/read read Variable_1/read->add 1 Variable_1 Variable_1 Variable_1->GradientDescent/update_Variable_1 1 Variable_1->Variable_1/Assign 1 Variable_1->Variable_1/read 1 Variable/read read mul mul Variable/read->mul 1 gradients/mul_grad mul_grad Variable/read->gradients/mul_grad 1 Variable/Assign Assign Variable Variable Variable->GradientDescent/update_Variable 1 Variable->Variable/read 1 Variable->Variable/Assign 1 mul/y y mul/y->mul 100 mul/y->gradients/mul_grad 100 mul->add 100 gradients/Mean_grad Mean_grad gradients/Mean_grad->gradients/Square_grad gradients/Shape Shape gradients/Fill Fill gradients/Shape->gradients/Fill 0 gradients/Fill->gradients/Mean_grad gradients/add_grad add_grad gradients/add_grad->GradientDescent/update_Variable_1 gradients/add_grad->gradients/mul_grad gradients/sub_grad sub_grad gradients/sub_grad->gradients/add_grad gradients/Square_grad->gradients/sub_grad gradients/Const Const gradients/Const->gradients/Fill gradients/mul_grad->GradientDescent/update_Variable gradients gradients random_uniform/sub sub random_uniform/mul mul random_uniform/sub->random_uniform/mul random_uniform/RandomUniform RandomUniform random_uniform/RandomUniform->random_uniform/mul 1 random_uniform random_uniform random_uniform/mul->random_uniform 1 random_uniform/min min random_uniform/min->random_uniform/sub random_uniform/min->random_uniform random_uniform/max max random_uniform/max->random_uniform/sub random_uniform/shape shape random_uniform/shape->random_uniform/RandomUniform 1 random_uniform->Variable/Assign 1

In [9]:
mnist_g = tf.Graph()

In [10]:
with mnist_g.as_default():
    x = tf.placeholder(tf.float32, [None, 784])
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.nn.softmax(tf.matmul(x, W) + b)
    y_ = tf.placeholder(tf.float32, [None, 10])
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

In [11]:
tfg.board(mnist_g, depth=1)


Out[11]:
G GradientDescent GradientDescent Const Const Mean Mean Const->Mean 1 Log Log mul mul Log->mul ?×10 gradients gradients Log->gradients ?×10 Neg Neg Neg->gradients ? Neg->Mean ? Sum Sum Sum->Neg ? Sum->gradients ? MatMul MatMul add add MatMul->add ?×10 MatMul->gradients ?×10 Placeholder_1 Placeholder_1 Placeholder_1->mul ?×10 Placeholder_1->gradients ?×10 zeros_1 zeros_1 Variable_1 Variable_1 zeros_1->Variable_1 10 Softmax Softmax add->Softmax ?×10 zeros zeros Variable Variable zeros->Variable 784×10 Softmax->Log ?×10 Softmax->gradients ?×10 Variable_1->GradientDescent 10 Variable_1->add 10 Variable->GradientDescent 784×10 Variable->MatMul 784×10 Variable->gradients 784×10 mul->Sum ?×10 mul->gradients ?×10 gradients->GradientDescent Placeholder Placeholder Placeholder->MatMul ?×784 Placeholder->gradients ?×784