O.rka
O.rka

Reputation: 30677

How to use custom/non-default tf.Graph in Tensorflow the correct way?

I'm very new to Tensorflow and I am reading https://www.amazon.com/TensorFlow-Machine-Learning-Cookbook-McClure/dp/1786462168. One argument that I have noticed in tf.Session is graph. I like having full control of the flow and I was wondering how to properly use the tf.Graph with tf.Session and how to add computations to a specific graph? Also, what is the canonical syntax, if any, in which people add operations to a specific graph in Tensorflow?

Analogous to the below:

t = np.linspace(0,2*np.pi)
fig, ax = plt.subplots()
ax.scatter(x=t, y=np.sin(t))

compared to:

plt.scatter(x=t, y=np.sin(t))

How can I have this same flexibility with tf.Graph()?

G = tf.Graph()

t_query = np.linspace(0,2*np.pi,50)
pH_t = tf.placeholder(tf.float32, shape=t_query.shape)

def simple_sinewave(t, name=None):
    return tf.sin(t, name=name)

with tf.Session() as sess:
    r = sess.run(simple_sinewave(pH_t), feed_dict={pH_t:t_query})
# array([  0.00000000e+00,   1.27877161e-01,   2.53654599e-01,
# ...
#         -1.27877384e-01,   1.74845553e-07], dtype=float32)

Now trying it with specifying a graph argument:

with tf.Session(graph=G) as sess:
    r = sess.run(simple_sinewave(pH_t), feed_dict={pH_t:t_query})
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-51-d73a1f0963e3> in <module>()
     26 #         -1.27877384e-01,   1.74845553e-07], dtype=float32)
     27 with tf.Session(graph=G) as sess:
---> 28     r = sess.run(simple_sinewave(pH_t), feed_dict={pH_t:t_query})

... RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

Update using David Parks answer with this problem:

# Custom Function
def simple_sinewave(t, name=None):
    return tf.sin(t, name=name)

# Synth graph
G = tf.Graph()

# Build Graph
with G.as_default():
    t_query = np.linspace(0,2*np.pi,50)
    pH_t = tf.placeholder(tf.float32, shape=t_query.shape)

# Run session using Graph
with tf.Session(graph=G) as sess:
    r = sess.run(simple_sinewave(pH_t), feed_dict={pH_t:t_query})
r
# array([  0.00000000e+00,   1.27877161e-01,   2.53654599e-01,
#          3.75266999e-01,   4.90717560e-01,   5.98110557e-01,
# ...
#         -4.90717530e-01,  -3.75267059e-01,  -2.53654718e-01,
#         -1.27877384e-01,   1.74845553e-07], dtype=float32)

Bonus: Is there a specific nomenclature for naming placeholder variables in Tensorflow? Like pd.DataFrame as df_data.

Upvotes: 3

Views: 1215

Answers (1)

David Parks
David Parks

Reputation: 32061

You normally do it this way:

with tf.Graph().as_default():
  # build your model
    with tf.Session() as sess:
      sess.run(...)

I use multiple graphs sometimes for running a test set separately from the training set, which is similar to your example above, you would do:

g = tf.Graph()
with g.as_default():
  # build your model
  with tf.Session() as sess:
    sess.run(...)

As you also pointed out, you can avoid using with and just do sess = tf.Session(graph=g), and you'd have to close your session yourself. Most use cases will be simplified by using python's with

When you have two graphs, you're setting each as_default() as the default graph, whenever you're working with that graph.

Example:

g1 = tf.Graph()
g2 = tf.Graph()

with g1.as_default():
  # do stuff like normal, g1 is the graph that will be used
  with tf.Session() as session_on_g1:
    session_on_g1.run(...)

with g2.as_default():
  # do stuff like normal, g2 is the graph that will be used
  with tf.Session() as session_on_g2:
    session_on_g2.run(...)

Upvotes: 4

Related Questions