iterating over/broadcast_to tensorflow tensors

Issue

Consider the following two tensors

t1=tf.convert_to_tensor([1, 2])
and

t2=tf.convert_to_tensor([3,7.2,4,3,8])

the result of t1 t2 by tf.broadcast or any other method (other than conversion to numpy) shall be

result = ( [3, 7.2, 4, 3, 8, 6, 14.4, 8, 6, 16 ], dtype = float32)

Thanks in advance.

Solution

Maybe something like this:

import tensorflow as tf

t1=tf.convert_to_tensor([1.0, 2.0])

t2=tf.convert_to_tensor([3,7.2,4,3,8])

tf.reshape(t2*t1[:, None], [-1])
<tf.Tensor: shape=(10,), dtype=float32, numpy=
array([ 3. ,  7.2,  4. ,  3. ,  8. ,  6. , 14.4,  8. ,  6. , 16. ],
      dtype=float32)>

Answered By – AloneTogether

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published