Reputation: 1036
I'm new to Tensorflow.
tf.constant()
to replace int32
, float32
or else?tf.mul()
instead of normal Python multiplication *
?tf.Print()
instead of print()
?Upvotes: 0
Views: 502
Reputation: 2190
As noted here,
A Tensor is a symbolic handle to one of the outputs of an Operation. It does not hold the values of that operation's output, but instead provides a means of computing those values in a TensorFlow Session
So tensor variables aren't like python variables. Rather they specify the relationship between operations in the computational graph. The python variables that you use to describe the graph are for the programmer's convenience, but it might be easier to think about the python variables and the tensor variables as being in parallel namespaces. This example may help:
with tf.Session() as sess:
a = tf.constant([1, 2, 3])
b = tf.Variable([])
b = 2 * a
sess.run(tf.initialize_all_variables())
print(b) # Tensor("mul:0", shape=(3,), dtype=int32)
print(b.eval()) # [2, 4, 6]
b = tf.Print(b, [b]) # [2, 4, 6] (at the command line)
From this you can see:
print(b)
returns information about the operation that 'b' refers to as well as the variable shape and data type, but not the value.b.eval()
(or sess.run(b)
) returns the value of b
as a numpy array which can be printed by a python print()
tf.Print()
allows you to see the value of b
during graph execution. Note that the syntax of tf.Print()
might seem a little strange to a newbie. As described in the documentation here, tf.Print()
is an identity operation that only has the side effect of printing to the command line. The first parameter is just passed through. The second parameter is the list of tensors that are printed and can be different than the first parameter. Also note that in order for tf.Print()
to do something, a variable used in a subsequent call to sess.run()
needs to be dependent on the tensor output by tf.Print()
, otherwise this portion of the graph will not be executed.
Finally with respect to math ops such as tf.mul()
vs *
many of the normal python ops are overloaded with the equivalent tensorflow ops, as described here.
Upvotes: 1
Reputation: 2455
Because tensorflow is built upon a computation graph. When you construct the graph in python, you are just building is a description of computations (not actually doing the computation). To compute anything, a graph must be launched in a Session. So it's best to do the computation with the tensorflow ops.
https://www.tensorflow.org/versions/r0.11/get_started/basic_usage.html
Upvotes: 1