Skip to content
Advertisement

Element wise multiplication of two list that are tf.Tensor in tensorflow

What is the fastest way to do an element-wise multiplication between a tensor and an array in Tensorflow 2?

For example, if the tensor T (of type tf.Tensor) is:

JavaScript

and we have an array a (of type np.array):

JavaScript

I wand to have:

JavaScript

as output.

Advertisement

Answer

This is called the outer product of two tensors. It’s easy to compute by taking advantage of Tensorflow’s broadcasting rules:

JavaScript

results in

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement