Reputation: 826
I have a python program in which I have defined a network and as usual, I'm training it inside a function where I have
with tf.Session() as sess:
...
for epoch in xrange(num_epochs):
...
for n in xrange(num_batches):
_, c = sess.run([optimizer, loss], feed_dict={....
In the loss function, I have to work a lot to get the loss and in particular, I must take the maximum inside a tensor and use it to do stuff. Here an example
values = tf.constant([0, 1, 2, 0, 2], dtype=tf.float32)
max_values = tf.reduce_max(values) # Tensor...
...
in the max_values
line if I use the debug it says that it is a tensor and not a value, so if I change my code in this way passing to the function the session created in the previous piece of code
values = tf.constant([0, 1, 2, 0, 2], dtype=tf.float32)
max_values = sess.run(tf.reduce_max(values)) # 2.0
...
it works. But this loss function is already in a scope of a session so my question is why the result is a tensor and not a number? Is there a way to get the value without passing the session to the loss function?
Upvotes: 2
Views: 638
Reputation: 378
Use Tensor.eval() function to convert a Tensor to its value. In the following example, you can get the value of max_values Tensor.
def loss():
values = tf.constant([0, 1, 2, 0, 2], dtype=tf.float32)
max_values = tf.reduce_max(values)
print (max_values.eval())
with tf.Session() as sess:
loss()
If you called loss() outside Session scope, you got an error.
Also you can use Eager execution mode. https://www.tensorflow.org/tutorials/eager/eager_basics
Upvotes: 1
Reputation: 236
According to the documentation:
TensorFlow uses the tf.Session class to represent a connection between the client program---typically a Python program, although a similar interface is available in other languages---and the C++ runtime.
It means that when you do values = tf.constant([0, 1, 2, 0, 2], dtype=tf.float32)
you are just inserting a node to your tensorflow Graph! Since Python is the high level API for a low level C++ runtime, you actually need a Session to evaluate your Python code in this low level runtime.
That is why every time you need to calculate or evaluate a Tensorflow variable/method/constant/etc you need to run it in a Session with tf.Session().run(yournode)
I hope it helped
Upvotes: 2