Ale
Ale

Reputation: 1

Tensorflow: '+' operator

I'd like to ask: is the symbol '+' used in Tensorflow? I mean. in Pytorch I can sum/subtract tensors using + and - operators, while in Tensorflow is good use to exploit tf.add for example. Could you please help me figure it out?

Thanks

Upvotes: 0

Views: 1401

Answers (2)

moctarjallo
moctarjallo

Reputation: 1625

if your version of tensorflow is 2.0.0 or above, you can perfectly do tensor addition and substraction something like:

import tensorflow as tf

a = tf.constant(2)
print(a)

b = tf.constant(3)
print(b)

c = a + b
print(c)

would print

tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
tf.Tensor(5, shape=(), dtype=int32)

Upvotes: 0

javidcf
javidcf

Reputation: 59731

You can use either the + operator or the add function. In most cases it is the same. The only case where a + b and tf.add(a, b) is different is if neither a nor b are TensorFlow values, e.g. if they are Python scalars or NumPy arrays. In that case, a + b will perform a "normal" addition (add the scalars, or the arrays, or whatever) and give you (in principle) a result of the same kind as a and b, whereas tf.add(a, b) will automatically convert a and b into tensors and compute the TensorFlow addition operation (assuming eager mode, in graph mode it would just create the graph operation), and so the result would be a TensorFlow tensor object.

In some cases this makes a technical difference. For example, consider this tf.function:

import tensorflow as tf
@tf.function
def f(a, b):
    return tf.math.sqrt(a + b)

If I call f(2.0, 3.0), then a and b will be considered "constant" values (as opposed to "parameters" to the tf.function), and a + b would be computed to be 5.0 in the first call and made into a constant in the function graphs. Subsequent calls to f(2.0, 3.0) would not need to recompute this value, while a different call like f(1.0, 2.0) would generate a new similar function graph. If the function used tf.math.add(a, b) instead of that, it would be more or less the same, but and Add operation would be added to the graph with the constant inputs 2.0 and 3.0, so in principle the addition would be recomputed on every call to f(2.0, 3.0). This is usually a very minor and unimportant technical difference, specially since these function graphs are later optimized by TensorFlow too, so probably there will be no real difference in the actual execution.

The only other significant difference is that add, like about any other TensorFlow operation, offers a name parameter. This is only relevant in graph mode, and even then not in most cases, but it allows you to give a specific name to the addition operation, which the + operator does not.

In general, I prefer to use the + operator, because it looks cleaner and it automatically makes the computation in the most convenient way, e.g. if the operators are NumPy arrays using the NumPy library. It also makes it easier for the same code to work with TensorFlow, NumPy or other kind of data. Only when I explicitly want to use TensorFlow operations for some reason I may use the function, and even then I will probably just pass the operators through tf.convert_to_tensor before.

Upvotes: 0

Related Questions