Paul Yi
Paul Yi

Reputation: 1

How to get a tensor's value in TensorFlow (without making another session)

I'm finding a way to getting a tensor's value. In most case, the problem would be solved by calling "sess.run(target_op)". However, I want to know another way. I am editing the code downloaded from GitHub so there's already a session running code there. Without touching the session running part, is there any way to get some specific tensor value? In my case, the code is built for getting accuracy for image recognition. While session runs and doing the accuracy evaluation I also want to get "prediction" tensor value in the same session without creating another session. For example, an operation like tf.Print shows tensor value througha terminal window without running session directly(in the first figure we just have to do sess.run(e) to print out tensor from c) example of tf.Print

a = tf.constant(5)
b = tf.constant(3)
c = tf.add(a,b)

#print tensor c (which is 8)
d = tf.Print(c,[c])
f = tf.constant(2)
e = tf.multiply(f,d)
sess = tf.Session()

#print operation can be executed without running the session directly
g = sess.run(e)`

Like the tf.Print is there any operation that gets tensor value without running session directly? (like the second figure) example of operation I am looking for

More specifically, what I want is to get the value of tensor(with actual numbers and arrays, not just 'tensor' data structure)and pass it to the global variable to access the value freely even after the session closes. The session only executes the operator which is located at end of the graph while the tensor I want the value is located in the middle of the graph. With restriction that I cannot create more session than the original code has, is there any way to get the specific tensor value?( I can't use .eval() or .run() because either needs to access 'session'. the code I am editing runs the code by using slim.evaluate_once function and as session() is binded to the function, I cannot approach to session())

Upvotes: 0

Views: 2047

Answers (1)

ted
ted

Reputation: 14714

There is no reason why you can't just call any tensor from the graph, provided you feed in the appropriate feed_dict. For instance say you want a tensor called biasAdd:0 and your so called-end tensor is called prediction

Then you can just get this tensor and evaluate it:

tensor = graph.get_tensor_by_name("biasAdd:0")
tensor_value, prediction_value = ses.run([tensor, prediction],... )

In tensorflow you have to use run or eval to get a numerical value from the graph

Upvotes: 1

Related Questions