In [18]:
import tensorflow as tf

t = tf.zeros(shape=(2,3,4,5))
t
Out [18]:
<tf.Tensor: shape=(2, 3, 4, 5), dtype=float32, numpy=
array([[[[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]]],


       [[[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.]]]], dtype=float32)>
In [19]:
t.shape
Out [19]:
TensorShape([2, 3, 4, 5])
In [20]:
t.ndim
Out [20]:
4
In [21]:
tf.size(t)
Out [21]:
<tf.Tensor: shape=(), dtype=int32, numpy=120>
In [22]:
t.dtype
Out [22]:
tf.float32
In [23]:
# get element at index
t[0]
Out [23]:
<tf.Tensor: shape=(3, 4, 5), dtype=float32, numpy=
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]], dtype=float32)>
In [24]:
t[-1]
Out [24]:
<tf.Tensor: shape=(3, 4, 5), dtype=float32, numpy=
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]], dtype=float32)>
In [25]:
# get first 2 elements of EACH DIMENSION
t[:2, :2, :2, :2]
Out [25]:
<tf.Tensor: shape=(2, 2, 2, 2), dtype=float32, numpy=
array([[[[0., 0.],
         [0., 0.]],

        [[0., 0.],
         [0., 0.]]],


       [[[0., 0.],
         [0., 0.]],

        [[0., 0.],
         [0., 0.]]]], dtype=float32)>
In [26]:
# get 1st element from EACH dimension EXCEPT the last dimension
t[:1, :1, :1]
Out [26]:
<tf.Tensor: shape=(1, 1, 1, 5), dtype=float32, numpy=array([[[[0., 0., 0., 0., 0.]]]], dtype=float32)>

Add A dimension to a tensor

using tf.newaxis

In [27]:
t2 = tf.constant([
    [1,2], 
    [3,4]
])
t2
Out [27]:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4]], dtype=int32)>
In [35]:
t2.shape, t2.ndim
Out [35]:
(TensorShape([2, 2]), 2)
In [29]:
t2[:, -1]
Out [29]:
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 4], dtype=int32)>
In [32]:
t3 = t2[...,tf.newaxis]
t3
Out [32]:
<tf.Tensor: shape=(2, 2, 1), dtype=int32, numpy=
array([[[1],
        [2]],

       [[3],
        [4]]], dtype=int32)>
In [34]:
t3.shape, t3.ndim
Out [34]:
(TensorShape([2, 2, 1]), 3)
In [38]:
t4 = tf.expand_dims(t2, axis=-1)
In [40]:
t4, t4.shape, t4.ndim
Out [40]:
(<tf.Tensor: shape=(2, 2, 1), dtype=int32, numpy=
 array([[[1],
         [2]],
 
        [[3],
         [4]]], dtype=int32)>,
 TensorShape([2, 2, 1]),
 3)