Error in tensor evaluation - python

The below code runs fine in a tutorial but there is an error when I run it locally. Are there any installation errors or something else? Please help. This is link to that tutorial:
https://colab.research.google.com/notebooks/mlcc/tensorflow_programming_concepts.ipynb?utm_source=mlcc&utm_campaign=colab-external&utm_medium=referral&utm_content=tfprogconcepts-colab&hl=en#scrollTo=Md8ze8e9geMi
And the code:
import tensorflow as tf
#create a graph
g = tf.Graph()
#establish the graph as the default graph
with g.as_default():
x = tf.constant(8, name = "x_const")
y = tf.constant(5, name = "y_const")
my_sum = tf.add(x,y, name = "x_y_sum")
#create the session
#this runs the default graph
with tf.Session() as sess:
print(my_sum.eval())
Below is the error that occurs:
gunjan#gunjan-Inspiron-3558:~/Desktop$ python tf1.py
/home/gunjan/anaconda3/lib/python3.5/site-
packages/h5py/__init__.py:34: FutureWarning: Conversion of the second
argument of issubdtype from `float` to `np.floating` is deprecated. In
future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
2018-08-20 22:10:41.619062: I
tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports
instructions that this TensorFlow binary was not compiled to use: AVX2
FMA
Traceback (most recent call last):
File "tf1.py", line 15, in <module>
print(my_sum.eval())
File "/home/gunjan/anaconda3/lib/python3.5/site-
packages/tensorflow/python/framework/ops.py", line 680, in eval
return _eval_using_default_session(self, feed_dict, self.graph,
session)
File "/home/gunjan/anaconda3/lib/python3.5/site-
packages/tensorflow/python/framework/ops.py", line 4942, in
_eval_using_default_session
raise ValueError("Cannot use the default session to evaluate tensor: "
ValueError: Cannot use the default session to evaluate tensor: the
tensor's graph is different from the session's graph. Pass an explicit
session to `eval(session=sess)`.

The problem is that you've created one graph (g) and you're executing code in a separate graph (sess). If you don't need two graphs, you can just use sess:
x = tf.constant(8, name = "x_const")
y = tf.constant(5, name = "y_const")
my_sum = tf.add(x,y, name = "x_y_sum")
#create the session
#this runs the default graph
with tf.Session() as sess:
print(my_sum.eval())

To simply get it working you can pass the session explicitly, as suggested by the error message:
print(my_sum.eval(session=sess))
To understand why it doesn't work exactly as the tutorial specifies it, you could start by comparing the versions of Python and TensorFlow to those used in the tutorial.
import tensorflow as tf
import platform
print("Python version: ", platform.python_version())
print("TensorFlow version", tf.__version__)
For the colab environment you linked, this prints:
Python version: 2.7.14
TensorFlow version 1.10.0
EDIT
Taking another look at your code sample, it's not an issue of version compatibility. The issue is that your copy of the tutorial did not properly preserve the indentation. The second with block needs to be enclosed in the first.
# Establish the graph as the "default" graph.
with g.as_default():
# ...
# Now create a session.
# The session will run the default graph.
with tf.Session() as sess:
print(my_sum.eval())
This ensures that g is used as the default graph for the session, instead of creating a new one like MatthewScarpino points out your incorrectly-indented version does.

If you create/use a Graph object explicitly rather than using the default graph, you need to either (a) pass the graph object to your Session constructor, or (b) create the session in the graph context.
graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session(graph=graph) as sess:
do_stuff_with(sess)
or
graph = tf.Graph()
with graph.as_default():
build_graph()
with tf.Session() as sess:
do_stuff_with(sess)

Related

Can we change tensorflow version on the fly in a python script?

I have an old python script(tf-1.15.2) that needs to be run in TensorFlow-2.2.0 (can not use tf <2.2), I have migrated most of the code to tf-2.2.0, but there are some tensorflow.contrib related methods that are used in the code. So, I would like to use the old version tf-1.15 for running those lines of code that use tensorflow.contrib related APIs.
So, now the question is I have installed tf-1.15.2 globally, I have installed tf-2.2.0 locally. But how to access the specific version of the TensorFlow at a specific point in time while the python process is running?
Example code is below
import tensorflow as tf # version: tf-2.2.0 (local package is imported)
isess = tf.compat.v1.Session()
tf.compat.v1.disable_eager_execution()
​
# Creatoin of the required placeholders
p = []
for shape in input_shapes:
p.append(tf.compat.v1.placeholder(shape=shape, dtype=input_dtype))
out = tf.einsum(equation, *p)
graph_def = isess.graph_def
# TODO
# To feed this (graph_def, feed_dict, output_tensors) to a session object of tf-1.15.2 and find the output
Now to test the unit test given in tf_einsum_op_test in tf_1.15.2 after replacing the einsum with appropriate function (trace/dot_product/...), I would like to revert back to tf-1.15.2 and check the execution.
The underlying need is to find if the tf versions can be interchanged during the execution flow of a python process. Einsum op is considered since it is not directly supported in tf-1.15.2
Up on Experimenting with subprocess API, I found that it is possible to switch between the tf versions during the python process execution through subprocess call.
# main.py
import tensorflow as tf # version: tf-2.2.0 (local package is imported)
import subprocess
import os
isess = tf.compat.v1.Session()
tf.compat.v1.disable_eager_execution()
​
# Creatoin of the required placeholders
p = []
for shape in input_shapes:
p.append(tf.compat.v1.placeholder(shape=shape, dtype=input_dtype))
out = tf.einsum(equation, *p)
graph_def = isess.graph_def
#TODO: Save the graph_def in graph.pb
#TODO: Save the feed_dict in input.npz
#TODO: Save the output_tensors
#Change the python path to the global package
os.environ['PYTHONPATH'] = '/usr/local/lib/python3.6/dist-packages'
cmd = ['python3.6','run.py']
out = subprocess.check_output(cmd) #Subprocess call
#run.py
import tensorflow as tf # version: tf-1.15.2 (global package is imported)
import numpy as np
#TODO: Load the graphdef from graph.pb
#TODO: Load the feed_dict from input.npz
#TODO: Load the output tensors
g = tf.import_graph_def(graph_def,name='')
with tf.Session(graph=g) as sess:
output = sess.run(output_tensors,feed_dict)
This works for me.

Tensorflow: The Session graph is empty. Python

Hello guys I am using Tensorflow 2.0
and in these lines of code:
import tensorflow as tf
hello = tf.constant('Hello World')
sess = tf.compat.v1.Session()
sess.run(hello) <-- Error in this line
RuntimeError: The Session graph is empty. Add operations to the graph
before calling run().
Any idea on how to solve it?
ok guys I found the way:
g = tf.Graph()
with g.as_default():
# Define operations and tensors in `g`.
hello = tf.constant('hello')
assert hello.graph is g
sess = tf.compat.v1.Session(graph=g)
sess.run(hello)
b'hello'
thank you for your time!
Tensorflow core r2.0 have enabled eager execution by default. so, without changing it we just have to change our code as like as below by Launch the graph in a session.
> with tf.compat.v1.Session() as sess:
> # Building a graph
> hello = tf.constant("hello")
> print(sess.run(hello))
According to the Tensorflow doc..
A default Graph is always registered, and accessible by calling
tf.compat.v1.get_default_graph
For this basic operations not required to declare a tf.Graph() , you can define a graph which has more computations and dataset you can define a graph and invoke into the session.
Please Refer: For More informations
https://www.tensorflow.org/api_docs/python/tf/Graph
https://github.com/OlafenwaMoses/ImageAI/issues/400

tf.print has no result

I want to use tf.print to show tensor value, but it has no result?
This is my code and is there something wrong for that:
from __future__ import print_function
import tensorflow as tf
sess = tf.InteractiveSession()
a = tf.constant([1.0, 3.0])
tf.print(a)
From the documentation of tf.Print - that's deprecated and suggests to use tf.print:
Note that tf.print returns a no-output operator that directly prints the output. Outside of defuns or eager mode, this operator will not be executed unless it is directly specified in session.run or used as a control dependency for other operators.
This is only a concern in graph mode. Below is an example of how to ensure tf.print executes in graph mode:
sess = tf.Session()
with sess.as_default():
tensor = tf.range(10)
print_op = tf.print(tensor)
with tf.control_dependencies([print_op]):
out = tf.add(tensor, tensor)
sess.run(out)
Hence, if you enable the eager mode your code will work as you expected, if you want to continue using the static-graph mode you have to use sess.run
import tensorflow as tf
a = tf.constant([1.0, 3.0])
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(a))
is what I'd do. Import tensorflow, set up your variables, set up and run the initializer for them, and then print the session evaluating the constant

Tensorflow With Context Manager vs. With Session

I'm trying to understand how a with block behaves in the above scenarios. I assume to always have one graph and one session only. I understand that I have (at least?) 2 ways to work using a session in a with block:
Example 1 : using as_default() that creates a context manager:
Same documentation says:
Use with the with keyword to specify that calls to
tf.Operation.run or tf.Tensor.eval should be executed in this
session.
# Create session
sess = tf.Session()
# Enter with block on a new context manager
with sess.as_default():
# Train: Following line should result calling tf.Operation.run
sess.run([optimizer, loss], feed_dict={x: x_train, y: y_train)
# Eval: Following line should result calling tf.Tensor.eval
sess.run([loss], feed_dict={x: x_eval, y: y_eval)
Example 2 : with block on session as stated in same documentation in a lower section:
Alternatively, you can use with tf.Session(): to create a session that
is automatically closed on exiting the context, including when an
uncaught exception is raised.
# Enter with block on session instead of Context Manager
with tf.Session() as sess:
# Train: Following line seems calling tf.Operation.run as per my test
sess.run([optimizer, loss], feed_dict={x: x_train, y: y_train)
# Eval: Following is unclear
sess.run([loss], feed_dict={x: x_eval, y: y_eval)
I would like to understand what is the correct usage as I see GitHub examples of both cases but of course without the results. In my tests both Example 1 and 2 works for training. For evaluation it seems there is a difference that I can't understand. It exceeds my knowledge to browse the Tensorflow source. Can someone please explain?
They do slightly different things, so each may or may not be correct depending on the usage. tf.Session.as_default() will just ensure that the session is set as the default ones, so calls to eval and run will use that session by default:
import tensorflow as tf
sess = tf.Session()
with sess.as_default():
print(sess is tf.get_default_session()) # True
However, as stated in the documentation, tf.Session.as_default() will not automatically close the session after the with block. If you want that, you can use the session itself as the context manager:
import tensorflow as tf
with tf.Session() as sess:
# Do something with the session
sess.run([]) # RuntimeError: Attempted to use a closed Session.
However, although (from my point of view) not clearly documented, using a session as context manager also makes it the default session.
import tensorflow as tf
with tf.Session() as sess:
print(sess is tf.get_default_session()) # True
What is the point of tf.Session.as_default(), then? Well, simply when you want to temporarily make a session the default one but you do not one to close it after that (in which case you should manually close it later, or use it as an outer context manager). This is probably most relevant when you have more than one open session. Consider the following case:
import tensorflow as tf
with tf.Session() as sess1, tf.Session() as sess2:
print(sess2 is tf.get_default_session()) # True
Here sess2 is the default because its context was added later (it can be considered to be "inner" to the context created by sess1). But now maybe you want to make sess1 the default for a while. However you can not use sess1 itself as context manager again:
import tensorflow as tf
with tf.Session() as sess1, tf.Session() as sess2:
# Do something with sess2
with sess1:
# RuntimeError: Session context managers are not re-entrant.
# Use `Session.as_default()` if you want to enter
# a session multiple times.
So you can switch between one and other default sessions with tf.Session.as_default():
import tensorflow as tf
with tf.Session() as sess1, tf.Session() as sess2:
with sess1.as_default():
# Do something with sess1
with sess2.as_default():
# Do something with sess2
# This is not really needed because sess2 was the default
# in the outer context but you can add it to be explicit
# Both sessions are closed at the end of the outer context
Of course, you can be extra explicit even with one session, if you want:
import tensorflow as tf
with tf.Session() as sess, sess.as_default():
# ...
Personally, I have never used tf.Session.as_default() in my actual code, but then again I have rarely needed to use multiple sessions, and I prefer to use tf.Session.run() instead of relying on the default session anyway, but that is mostly a matter of personal taste I suppose.

Building a graph with custom ops in tensorflow

I'm trying to explore use of tensorflow with custom ops. I build a simple switch op and verified it as suggested in tensorflow document. Now I'm trying to build the graph and then call run() method in a tensorflow
Session. Below is my code. I get the following error. Can someone help what should I do to fix it. Do I need to re-install tensorflow everytime I add a new custom op to /user_ops/?
import tensorflow as tf
# Create a Constant op that produce integer value
input1 = tf.constant(10)
# Create another op that produce an integer value
input2 = tf.constant(5)
# Create op that produce 0 or 1 as the control input in a switch
input3 = tf.constant(1)
# Create a switch op that takes input1 and input2 as inputs and input3 as
# the control input to produce an output
out = tf.user_ops.simple_switch(input1, input2, input3)
# Launch a default graph
sess = tf.Session()
# Call the 'run()' method and get the result
result = sess.run(out)
print(result)
# Close the Session when we're done!
sess.close()
When executed in python interpreter I get the following error:
Traceback (most recent call last):
File "tensorflow-switch.py", line 14, in
out = tf.simple_switch(input1, input2, input3)
AttributeError: 'module' object has no attribute 'simple_switch'
After adding a user-defined op (in TensorFlow 0.6.0 or earlier), to use it in the Python interpreter you must reinstall from the source repository. The easiest way to do this is to build and install a PIP package using Bazel. (The unit test would pass because running bazel test would cause TensorFlow to be rebuilt, and the rebuilt version to be used when running the tests.)
NOTE: This feature is experimental, and an improved workflow for adding user-defined ops is in development.

Categories