Hi everyone, in this TensorFlow tutorial we will look at the indexing and reshaping in tensorflow. If you have used the numpy library before, you will be able to easily understand the issue of indexing and reshaping in tensorflow. Let’s look at closer how we can do indexing and reshaping operations in TensorFlow.


Indexing


Create a 1 dimentional tensor for indexing operation. Like this x = tf.constant([0,1,2,3,4,5,6])

  • If we write like this x[:], it returns to us all values in the tensor.
  • If we write like this x[:3], it returns to us [0,1,2]
  • If we write like this x[3:], it returns to us [3,4,5,6]
  • If we write like this x[2:5], it returns to us [2,3,4]
  • If we write like this x[::2], it returns to us [0,2,4,6]. It progresses by skipping two by two.
  • If we write like this x[::-1], it returns to us [6,5,4,3,2,1,0]. It prints all values in reverse.
  • If we want to get spesific values, we can define like indices = tf.constant([2,4]) it return to us [2,4]
x = tf.constant([0,1,2,3,4,5,6])
print(x[:])
print("------------")
print(x[:3])
print("------------")
print(x[3:])
print("------------")
print(x[2:5])
print("------------")
print(x[::2])

indices = tf.constant([2,4])
x_ind = tf.gather(x, indices=indices)
print("------------")
print(x_ind)
tf.Tensor([0 1 2 3 4 5 6], shape=(7,), dtype=int32)
------------
tf.Tensor([0 1 2], shape=(3,), dtype=int32)
------------
tf.Tensor([3 4 5 6], shape=(4,), dtype=int32)
------------
tf.Tensor([2 3 4], shape=(3,), dtype=int32)
------------
tf.Tensor([0 2 4 6], shape=(4,), dtype=int32)
------------
tf.Tensor([2 4], shape=(2,), dtype=int32)

Create a 2 dimentional tensor for indexing operation. Like this x = tf.constant([[0,1,2,3],[4,5,6,7],[8,9,5,3]]).

  • x[:,:] It returns to us all values in the tensor.
  • x[:,:3] It returns to us all rows an 0,1,2 columns.
  • x[:3,:] It returns to us 0,1,2 rows and all columns
  • x[1:3,2:3] It returns to us 1,2 rows and 2 columns.
  • x[::-1] It prints all values in reverse.
x = tf.constant([[0,1,2,3],[4,5,6,7],[8,9,5,3]])
print(x[:,:])
print("-----------------")
print(x[:,:3])
print("-----------------")
print(x[:3,:])
print("-----------------")
print(x[1:3,2:3])
print("-----------------")
print(x[::-1])
tf.Tensor(
[[0 1 2 3]
 [4 5 6 7]
 [8 9 5 3]], shape=(3, 4), dtype=int32)
-----------------
tf.Tensor(
[[0 1 2]
 [4 5 6]
 [8 9 5]], shape=(3, 3), dtype=int32)
-----------------
tf.Tensor(
[[0 1 2 3]
 [4 5 6 7]
 [8 9 5 3]], shape=(3, 4), dtype=int32)
-----------------
tf.Tensor(
[[6]
 [5]], shape=(2, 1), dtype=int32)
-----------------
tf.Tensor(
[[8 9 5 3]
 [4 5 6 7]
 [0 1 2 3]], shape=(3, 4), dtype=int32)

Reshaping


Create a tensor for reshaping operations.

x = tf.range(1,10,1) # it will create the [1,2,3,4,5,6,7,8,9] values.
print("x shape : ", x.shape) # it returns (9,)
print(x)

print("---------------")
y = tf.reshape(x, (3,3)) # it returns (3,3)
print("y shape : ", y.shape)
print(y)

print("---------------")
transpose = tf.transpose(y, perm=[1,0]) # rows and columns are swapped.
print("transpose shape : ", transpose.shape)
print(transpose)
x = tf.range(1,10,1) # it will create the [1,2,3,4,5,6,7,8,9] values.
print("x shape : ", x.shape) # it returns (9,)
print(x)

print("---------------")
y = tf.reshape(x, (3,3)) # it returns (3,3)
print("y shape : ", y.shape)
print(y)

print("---------------")
transpose = tf.transpose(y, perm=[1,0]) # rows and columns are swapped.
print("transpose shape : ", transpose.shape)
print(transpose)
image.png