3

I am basically trying to add two tensors in tensorflow, the crux is that they are of different lengths a = [1, 2, 3, 4, 5] and b = [1, 2, 3] and am looking for a function that I am calling tf.myadd in the following

tf.myadd(a, b) = [2, 4, 6, 4, 5]

I have been looking into broadcasting, yet this has not the expected behavior.

mrk
  • 8,059
  • 3
  • 56
  • 78
  • since `tf` has broadcasting as default, you will need to do post padding with zeros and then use tf.add to get what you need. check my answer for more details. – Akshay Sehgal Aug 19 '21 at 12:12
  • 1
    @mrk You can do it like this: first `paddings = tf.zeros([tf.shape(a)[0]-tf.shape(b)[0]],dtype=tf.int32)` and then `tf.add(a,tf.concat([b, paddings],0))`. – Kaveh Aug 19 '21 at 12:15
  • thanks that nicely complements the numpy solution below. – mrk Aug 19 '21 at 12:37

1 Answers1

2

Broadcasting is the default for all tensor operations in tf. In this case, you are trying to avoid broadcasting since the 2 tensors ((5,) and (3,)) are NOT broadcastable along the axis=0 by the standard broadcasting rules. So what you need is an element-wise addition without broadcasting.

What you can do as in this case is use post-padding on the smaller array such that the two 1D tensors have the same shape and then add them elementwise over axis=0.

Like this -

import numpy as np
import tensorflow as tf

a = [1, 2, 3, 4, 5]
b = [1, 2, 3]

b_pad = np.pad(b, (0,len(a)-len(b)))

tf.add(a,b_pad).numpy()
array([2, 4, 6, 4, 5], dtype=int32)
Innat
  • 16,113
  • 6
  • 53
  • 101
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51