TensorFlow simple operations: tensors vs Python variables
<p>I'm unsure about the practical differences between the 4 variations below (they all evaluate to the same value). My understanding is that if I call <code>tf</code>, it <em>will</em> create an operation on the graph, and otherwise it <em>might</em>. If I don't create the <code>tf.constant()</code> at the beginning, I believe that the constants will be created implicitly when doing the addition; but for <code>tf.add(a,b)</code> vs <code>a + b</code> where <code>a</code> and <code>b</code> are both Tensors (#1 and #3), I can see no difference besides the default naming (former is <code>Add</code> and the latter one is <code>add</code>). Can anyone shed some light on the differences between those, and when should one use each?</p>

<pre><code>## 1
a = tf.constant(1)
b = tf.constant(1)
x = tf.add(a, b)
with tf.Session() as sess:
    x.eval()

## 2
a = 1
b = 1
x = tf.add(a, b)
with tf.Session() as sess:
    x.eval()

## 3
a = tf.constant(1)
b = tf.constant(1)
x = a + b
with tf.Session() as sess:
    x.eval()

## 4
a = 1
b = tf.constant(1)
x = a + b
with tf.Session() as sess:
    x.eval()
</code></pre>

<p>They are all the same. </p>

<p>The python-'+' in a + b is captured by tensorflow and actually does generate the same op as tf.add(a, b) does.</p>

<p>The tf.conctant allows you more specifics, such as defining the shape, type and name of the created tensor. But again tensorflow owns that "a" in your example a = 1 and it is equivalent to tf.constant(1) (treating the constant as an int-value in this case)</p>

python tensorflow