I want to print a tensor in my program to see its internal values once it gets evaluated. The problem, however, is that the tensor being declared inside a function. To understand my problem better, here is some example code to better explain what it is I want to do:
a = tf.Variable([[2,3,4], [5,6,7]])
b = tf.Variable([[1,2,2], [3,3,3]])
def divide(a,b):
with tf.variable_scope('tfdiv', reuse=True):
c = tf.divide(a,b, name='c')
# Cannot print(c) here, as this will only yield tf info on c
return c
d = divide(a,b)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(d)
sess.run(tf.get_variable('tfdiv/c:0').eval(session=sess))
Previously, I have been able to do a print(c.eval(session=sess)), but as c is a local variable inside a function now, that does not work. As can be seen in the code above, I have tried to use tensorflow's variable scope in order to access the variable and then evaluate it. Unfortunately, this results in the error message:
ValueError: Shape of a new variable (tfdiv/c:0) must be fully defined, but
instead was <unknown>.
I tried to use the reuse=True flag, but I still get the same error. Any thoughts on how I can solve this problem? Best would be if there is a print(c) equivalent that can be put into the divide function, as written in the code above.
This will achieve what you want to do:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(d))
Alternatively, you could replace the last line with:
print(sess.run(tf.get_default_graph().get_tensor_by_name('tfdiv/c:0')))