Operators - Tensorflow vs Python/Numpy

Hi

My question is should we use Python/Numpy operators or Tensorflow operator

For example:
Y = WX + b

In python/numpy it is very simple and straight forward
Y = W@X + b

But in tensorflow
Y = tf.add(tf.matmul(W,X), b)

Hope can get some suggestion from experienced users
Best

1 Like

That all looks correct. As always with programming there can be multiple correct ways to write a given piece of code.

You can also write the numpy version as:

Y = np.dot(W, X) + b

or

Y = np.add(np.dot(W,X),b)

It also works if you write the TF version this way:

Y = tf.matmul(W,X) + b

What happens there is called “overloading” of operators. In the last expression, python knows that the type of both operands to the “+” operation are TF tensors, so it calls tf.add for you.

That is what happened in the numpy case as well (the “+” operator is overloaded):

Y = np.dot(W, X) + b

You can also say:

Y = W@X + b

and if all the operands there are TF tensors, that will also work and give you a tensor as the result.

Normally python will try to “do the right thing” based on the types of the operands. The place you can run into trouble is when you mix numpy arrays and TF tensors in the same computation.

Here’s some demo code:

W = tf.constant([[1, 2], [3, 4]])                 
X = tf.add(W, 1)
b = tf.constant([[1], [3]])  
print(f"type(X) = {type(X)}")
print(f"X =\n{X}")

Y = tf.add(tf.matmul(W,X), b)
print(f"type(Y) = {type(Y)}")
print(f"Y =\n{Y}")

Y = tf.matmul(W,X) + b
print(f"type(Y) = {type(Y)}")
print(f"Y =\n{Y}")

Y = W@X + b
print(f"type(Y) = {type(Y)}")
print(f"Y =\n{Y}")

Here’s what I see when I run that:

type(X) = <class 'tensorflow.python.framework.ops.EagerTensor'>
X =
[[2 3]
 [4 5]]
type(Y) = <class 'tensorflow.python.framework.ops.EagerTensor'>
Y =
[[11 14]
 [25 32]]
type(Y) = <class 'tensorflow.python.framework.ops.EagerTensor'>
Y =
[[11 14]
 [25 32]]
type(Y) = <class 'tensorflow.python.framework.ops.EagerTensor'>
Y =
[[11 14]
 [25 32]]
2 Likes

thank you @paulinpaloalto