mrk
mrk

Reputation: 10396

Add two tensors with different dimensions in tensorflow

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.

Upvotes: 3

Views: 1358

Answers (1)

Akshay Sehgal
Akshay Sehgal

Reputation: 19307

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)

Upvotes: 2

Related Questions