O.rka
O.rka

Reputation: 30677

Get original value of Tensor in Tensorflow

I'm learning tensorflow right now and I have made a habit in programming of printing my variables to see what I'm working with as I'm coding. I can't figure out how to print the values of constant and Variable objects in tensorflow

How can I get back the values that I assigned to the instances?

import tensorflow as tf

C_int = tf.constant(134)
#Tensor("Const_11:0", shape=TensorShape([]), dtype=int32)
V_mat = tf.Variable(tf.zeros([2,3]))
#<tensorflow.python.ops.variables.Variable object at 0x10672fa10>
C_str = tf.constant("ABC")
#Tensor("Const_16:0", shape=TensorShape([]), dtype=string)


C_int
#134
V_mat
#array([[ 0.,  0.,  0.],
#       [ 0.,  0.,  0.]])
C_str
#ABC

Upvotes: 2

Views: 1838

Answers (1)

mrry
mrry

Reputation: 126154

The simplest way to see the values of your tensors is to create a tf.Session and use Session.run to evaluate the tensors. Thus, your code would look like:

import tensorflow as tf

C_int = tf.constant(134)
V_mat = tf.Variable(tf.zeros([2,3]))
C_str = tf.constant("ABC")

sess = tf.Session()
sess.run(C_int)
#134
sess.run(V_mat)
#array([[ 0.,  0.,  0.],
#       [ 0.,  0.,  0.]])
sess.run(C_str)
#ABC

sess.close()

Upvotes: 4

Related Questions