TensorFlow Adds Different Dimensions (Shapes) Tensors with Examples: A Beginner Guide – TensorFlow Tutorial

By | November 25, 2019

Tensorflow add operation is common used in tensorflow application. However, it is hard to understand when it adds different dimensinal tensors. In this tutorial, we will discuss this topic with some examples to help you understand it.

A 2 * 3 tensor adds a scalar

We will create a tensor with (2, 3) shape, then add a scalar.

import tensorflow as tf
import numpy as np

x = tf.Variable(np.array([[1, 2, 3],[4, 5, 6]]), dtype = tf.float32)
z = 2
_sum = x + z

where x is a 2*3 tensor, z is a scalar 2. How about the result?

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run([_sum]))

The result is:

[array([[ 3.,  4.,  5.],
       [ 6.,  7.,  8.]], dtype=float32)]

From the result, we will find: if a tensor adds a scalar, each element in this tensor will add this scalar.

A 2 * 3 tensor adds a (1,) tensor

Here is an example:

x = tf.Variable(np.array([[1, 2, 3],[4, 5, 6]]), dtype = tf.float32)
y = tf.Variable(np.array([1, 1, 1]), dtype = tf.float32)
_sum_2 = x + y

where x is 2* 3 shape, y is (1, ) shape, the result is:

[array([[ 2.,  3.,  4.],
       [ 5.,  6.,  7.]], dtype=float32)]

A tensor x will add a (1, ) tensor y, which means the elements of x on last axis add y

A 2 * 3 * 2 tensor add 3 * 2 tensor

x = tf.Variable(np.array([[[1, 2], [2, 3], [3, 4]],[[5, 6], [6, 7], [7, 8]]]), dtype = tf.float32)
y = tf.Variable(np.array([[1, 2],[2, 1],[1, 1]]), dtype = tf.float32)
_sum = x + y

where x is 2*3*2 shape, y is 3*2 shape, the result is:

[array([[[ 2.,  4.],
        [ 4.,  4.],
        [ 4.,  5.]],

       [[ 6.,  8.],
        [ 8.,  8.],
        [ 8.,  9.]]], dtype=float32)]

The last two shape of x is the same to y, so we will add y on axis = 0.

A 2 * 3 * 2 tensor add 2* 1 * 2 tensor

x = tf.Variable(np.array([[[1, 2], [2, 3], [3, 4]],[[5, 6], [6, 7], [7, 8]]]), dtype = tf.float32)
y = tf.Variable(np.array([[[1, 1]],[[2, 2]]]), dtype = tf.float32)
_sum = x + y

where y is 2*1*2, x is 2*3*2, the result is:

[array([[[  2.,   3.],
        [  3.,   4.],
        [  4.,   5.]],

       [[  7.,   8.],
        [  8.,   9.],
        [  9.,  10.]]], dtype=float32)]

As the first axis of x is same to y, which is 2. So x + y means we will add 3*2 tensor and 1*2 tensor on axis = 0.

Leave a Reply