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>The result is the same because every operator (<code>add</code> or <code>__add__</code> that's the overload of <code>+</code>) call <a href="https://www.tensorflow.org/versions/master/api_docs/python/framework.html#convert_to_tensor%60" rel="nofollow"><code>tf.convert_to_tensor</code></a> on its operands.</p>

<p>The difference between <code>tf.add(a + b)</code> and <code>a + b</code> is that the former gives you the ability to give a name to the operation with the <code>name</code> parameter. The latter, instead, does not give you this ability and also make it possibile that the computation is done by the Python interpreter and not outside it, in the Tensorflow environment.</p>

<p>This happen if (and only if) both <code>a</code> and <code>b</code> are not <code>Tensor</code> objects and thus Tensorflow will be not involved in the computation.</p>

python tensorflow