In [2]:
from __future__ import print_function
import tensorflow as tf

In [3]:
# apt-get install graphviz
# pip install graphviz
from tfdot import tfdot

Basic


In [4]:
matrix1 = tf.constant([[3., 3.]])

matrix2 = tf.constant([[2.],[2.]])

product = tf.matmul(matrix1, matrix2)

In [5]:
tfdot()


Out[5]:
root Const Const MatMul MatMul Const->MatMul Const_1 Const_1 Const_1->MatMul

Session


In [6]:
sess = tf.Session()
print(sess.run(product))
sess.close()


[[ 12.]]

Context manager


In [8]:
with tf.Session() as sess:
    print(sess.run(product))


[[ 12.]]

Device context


In [10]:
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    with tf.device("/cpu:0"):
        m1 = tf.constant([[3., 3.]])
        m2 = tf.constant([[2.],[2.]])
        pd = tf.matmul(m1, m2)
        print(sess.run(pd))


[[ 12.]]

Interactive session


In [11]:
sess = tf.InteractiveSession()

In [12]:
product.eval()


Out[12]:
array([[ 12.]], dtype=float32)

In [13]:
pd.eval()


Out[13]:
array([[ 12.]], dtype=float32)

Variable


In [14]:
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init_op = tf.initialize_all_variables()

In [15]:
tfdot()


Out[15]:
root cluster_counter counter counter/initial_value initial_value counter/Assign Assign counter/initial_value->counter/Assign counter counter counter/Assign->counter counter/read read Add Add counter/read->Add Const Const MatMul MatMul Const->MatMul Const_1 Const_1 Const_1->MatMul Const_2 Const_2 MatMul_1 MatMul_1 Const_2->MatMul_1 Const_3 Const_3 Const_3->MatMul_1 counter->counter/read Const_4 Const_4 Const_4->Add Assign Assign Add->Assign Assign->counter init init

In [17]:
init_op.run()
# state.run() # variables do not run
print(state.eval())


0

In [18]:
for _ in range(300):
    update.eval()

In [19]:
state.eval()


Out[19]:
300

In [20]:
sess.run([update]*10)


Out[20]:
[301, 301, 301, 301, 301, 301, 301, 301, 301, 301]

Initialize from another variable


In [21]:
# will back to here later
# tf.ops.reset_default_graph()
# sess.close()
# sess = tf.InteractiveSession()

In [22]:
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name='weights')
w2 = tf.Variable(weights.initialized_value(), name ='w2')
w_twice = tf.Variable(weights.initialized_value()*0.2, name="w_twice")

In [23]:
tfdot()


Out[23]:
root cluster_w2 w2 cluster_counter counter cluster_weights weights cluster_mul mul cluster_random_normal random_normal cluster_w_twice w_twice w2/Assign Assign w2 w2 w2/Assign->w2 w2/read read counter/initial_value initial_value counter/Assign Assign counter/initial_value->counter/Assign counter counter counter/Assign->counter counter/read read Add Add counter/read->Add weights/Assign Assign weights weights weights/Assign->weights weights/read read mul/y y mul mul mul/y->mul random_normal/shape shape random_normal/RandomStandardNormal RandomStandardNormal random_normal/shape->random_normal/RandomStandardNormal random_normal/mean mean random_normal random_normal random_normal/mean->random_normal random_normal/stddev stddev random_normal/mul mul random_normal/stddev->random_normal/mul random_normal/RandomStandardNormal->random_normal/mul random_normal/mul->random_normal w_twice/Assign Assign w_twice w_twice w_twice/Assign->w_twice w_twice/read read Const Const MatMul MatMul Const->MatMul Const_1 Const_1 Const_1->MatMul Const_2 Const_2 MatMul_1 MatMul_1 Const_2->MatMul_1 Const_3 Const_3 Const_3->MatMul_1 counter->counter/read Const_4 Const_4 Const_4->Add Assign Assign Add->Assign Assign->counter init init random_normal->weights/Assign weights->weights/read Identity Identity weights->Identity Identity_1 Identity_1 weights->Identity_1 Identity->w2/Assign w2->w2/read Identity_1->mul mul->w_twice/Assign w_twice->w_twice/read

In [24]:
init_op = tf.initialize_all_variables()
init_op.run()

In [25]:
weights.eval()


Out[25]:
array([[ 0.04481283, -0.62056214, -0.69336498, ..., -0.30848733,
        -0.34599236, -0.42386416],
       [-0.78550404, -0.10613412,  0.09037922, ...,  0.66008407,
         0.57086742, -0.1224874 ],
       [-0.61029941,  0.35488346, -0.10510538, ..., -0.10046848,
        -0.00810294,  0.00366683],
       ..., 
       [ 0.33449978,  0.02138252, -0.14221951, ..., -0.49634036,
        -0.47021103,  0.12775964],
       [-0.09816289, -0.34331754,  0.03284245, ..., -0.37847662,
         0.16994159,  0.44379511],
       [ 0.36513436,  0.40277815, -0.48408747, ..., -0.46500829,
         0.12536223,  0.04070676]], dtype=float32)

In [27]:
for v in tf.all_variables():
    print(v.name, v)


counter:0 <tensorflow.python.ops.variables.Variable object at 0x7f4f603ba208>
weights:0 <tensorflow.python.ops.variables.Variable object at 0x7f4f603ca278>
w2:0 <tensorflow.python.ops.variables.Variable object at 0x7f4f60369f60>
w_twice:0 <tensorflow.python.ops.variables.Variable object at 0x7f4f60369e10>

Feeds


In [28]:
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
output.eval(feed_dict={input1: [9.], input2: [4.]})


Out[28]:
array([ 36.], dtype=float32)

In [29]:
tfdot()


Out[29]:
root cluster_w2 w2 cluster_counter counter cluster_weights weights cluster_mul mul cluster_random_normal random_normal cluster_w_twice w_twice w2/Assign Assign w2 w2 w2/Assign->w2 w2/read read counter/initial_value initial_value counter/Assign Assign counter/initial_value->counter/Assign counter counter counter/Assign->counter counter/read read Add Add counter/read->Add weights/Assign Assign weights weights weights/Assign->weights weights/read read mul/y y mul mul mul/y->mul random_normal/shape shape random_normal/RandomStandardNormal RandomStandardNormal random_normal/shape->random_normal/RandomStandardNormal random_normal/mean mean random_normal random_normal random_normal/mean->random_normal random_normal/stddev stddev random_normal/mul mul random_normal/stddev->random_normal/mul random_normal/RandomStandardNormal->random_normal/mul random_normal/mul->random_normal w_twice/Assign Assign w_twice w_twice w_twice/Assign->w_twice w_twice/read read Const Const MatMul MatMul Const->MatMul Const_1 Const_1 Const_1->MatMul Const_2 Const_2 MatMul_1 MatMul_1 Const_2->MatMul_1 Const_3 Const_3 Const_3->MatMul_1 counter->counter/read Const_4 Const_4 Const_4->Add Assign Assign Add->Assign Assign->counter init init random_normal->weights/Assign weights->weights/read Identity Identity weights->Identity Identity_1 Identity_1 weights->Identity_1 Identity->w2/Assign w2->w2/read Identity_1->mul mul->w_twice/Assign w_twice->w_twice/read init_1 init_1 Placeholder Placeholder Mul Mul Placeholder->Mul Placeholder_1 Placeholder_1 Placeholder_1->Mul

In [ ]: