In [1]:
import tensorflow as tf

Create a Graph


In [4]:
# this code does nothing but creating a computation graph
x = tf.Variable(3, name="x")
y = tf.Variable(4, name="y")
f = x*x*y + y + 2

Evaluate Graph


In [5]:
# we open a TF session and use it to init variables and evaluate f
sess = tf.Session()

sess.run(x.initializer)
sess.run(y.initializer)

result = sess.run(f)

print result


42

In [10]:
# another better way to evaluate is
with tf.Session() as sess:
    x.initializer.run()
    y.initializer.run()
    result = f.eval()
print result


42

In [12]:
# or by init all variables at once
init = tf.global_variables_initializer()
with tf.Session() as sess:
    init.run()
    result = f.eval()
print result


42

In [15]:
# if in notebooks it might be better to use interactive session
sess = tf.InteractiveSession()
init.run()
result = f.eval()
print result
sess.close() # but in this case we have to close manually the session


42

In [ ]: