Fansly
Fansly

Reputation: 29

Tensorflow: What is difference of tf.constant(2) and 2

I just started learning Tensorflow by myself. I got a question, what is the difference between following code?

x = tf.add(3,5)

and

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

The print(sess.run(x)) both return 8. So what difference does it make of using tf.constant?

Upvotes: 1

Views: 247

Answers (1)

P-Gn
P-Gn

Reputation: 24641

It does not make any difference.

Each time a tensorflow operation expects a Tensor input but receives something else, it tries to convert it with tf.convert_to_tensor, and if successful, proceeds normally with that output.

In case of a constant like 2, but also np.arrays, lists, tuples, or virtually any container or (well-formed) hierarchy of containers, tf.convert_to_tensor will create a Const operation, just like the one you would create by hand by calling tf.constant.

tf.constant is nonetheless useful because it allows you, among other things, to name your constant in the graph and to control its data type (with name and dtype arguments respectively).

Upvotes: 3

Related Questions