TensorFlow Graphs


In [1]:
import tensorflow as tf

Simple Example


In [2]:
n1 = tf.constant(1)
n2 = tf.constant(2)

In [3]:
n3 = n1 + n2

In [4]:
# Using with auto-closes the session
with tf.Session() as sess:
    result = sess.run(n3)
print(result)


3

When you start TF, a default Graph is created, you can create additional graphs easily:


In [5]:
print(tf.get_default_graph())


<tensorflow.python.framework.ops.Graph object at 0x0000011BEFB3ADA0>

In [6]:
g = tf.Graph()

In [7]:
print(g)


<tensorflow.python.framework.ops.Graph object at 0x0000011C6F652438>

Setting a graph as the default:


In [8]:
graph_one = tf.get_default_graph()
graph_two = tf.Graph()

In [9]:
graph_one is tf.get_default_graph()


Out[9]:
True

In [10]:
graph_two is tf.get_default_graph()


Out[10]:
False

In [11]:
with graph_two.as_default():
    print(graph_two is tf.get_default_graph())


True

In [12]:
graph_two is tf.get_default_graph()


Out[12]:
False