Tensors

The concept of tensors has been introduced in spaces, and here we continue to work with tensors. I used python and TensorFlow library in the code, and the complete code file can be found in this repository. The imports and utility functions like info are found at the end of this page, here.

Throughout the examples, the tensors values correspond to the index to simplify the reading of the tensors. For example, the value at index [3] in a vector should be 3, the value at index [2,5] in a matrix should be 25, and so on. Also, I choose to show the result of the last index at each dimension since the beginning is obviously indexed with zeros. For example, I choose to print the value of the last index [7] in a vector of 8 elements, to print the value of the last index [2,4] in a matrix of 3 rows and 5 columns, as well as its last row [2], and so on.

Examples#

Points#

Points are 0d tensors, or rank 0, and they have no shape.

t = tf.Variable(2)
info(t,[[]])

Output:
Tensor:
2

    rank =  0   
    shape = ()
    
    t[] = 
    2 

Vectors#

Vectors are 1d tensors, or rank 1.

A vector with 2 elements

Shape is (2)

t = tf.Variable([0, 1])
info(t, indices = [[1]])

Output:
Tensor:
[0 1]

    rank =  1   
    shape = (2,)
    
    t[1] =
    1 

A vector with 8 elements

Shape is (8).

t = tf.Variable([0, 1, 2, 3, 4, 5, 6 ,7])
info(t, indices = [[7]])

Output:
Tensor:
[0 1 2 3 4 5 6 7]

    rank =  1   
    shape = (8,)

    t[7] =
    7

Matrices#

Matrices are 2d tensors, or rank 2.

A matrix with 2 rows and 1 column

Shape is (2,1).

t = tf.Variable([[0], [10]])
info(t, indices = [[1], [1,0]])


Output:
Tensor:
[[ 0]
[10]]

    rank =  2   
    shape = (2, 1)
    
    t[1] =
    [10] 

    t[1, 0] =
    10 

A matrix with 2 rows and 2 columns

Shape is (2,2).

t = tf.Variable([[1, 2], [10, 11]])
info(t, indices = [[1], [1,1]])

Output:
Tensor:
[[ 1  2]
[10 11]]

    rank =  2   
    shape = (2, 2)

    t[1] =
    [10 11] 

    t[1, 1] =
    11 

A matrix with 2 rows and 4 columns

Shape is (2,4).

t = tf.Variable([[1, 2, 3, 4, 5], [10, 11, 12, 13 , 14], [20, 21, 22, 23 , 24],])
info(t, indices = [[2],[2,4]])

Output:
Tensor:
[[ 1  2  3  4  5]
[10 11 12 13 14]
[20 21 22 23 24]]

    rank =  2   
    shape = (3, 5)

    t[2] =
    [20 21 22 23 24] 

    t[2, 4] =
    24

General Tensors#

Tensors can come in any rank like 0 ,1, or 2 as in the examples above, or more dimensions. Each dimension could be of a different size.

A tensor with shape (2,3,1)

t = tf.Variable([[[0], [10], [20]], [[100], [110], [120]]])
info(t, indices = [[1],[1,2],[1,2,0]])

Output:
Tensor:
[[[  0]
  [ 10]
  [ 20]]

 [[100]
  [110]
  [120]]]

    rank =  3   
    shape = (2, 3, 1)
    t[1] =
        [[100]
        [110]
        [120]] 

    t[1, 2] =
        [120] 

    t[1, 2, 0]=
        120 

A tensor with shape (2,2,3)

t = tf.Variable([[[0,1,2], [10, 11, 12]], [[100, 101, 102], [110, 111, 112]]])
info(t, indices = [[0],[0,1], [1,0,1]])

Output:
Tensor:
[[[  0   1   2]
  [ 10  11  12]]

 [[100 101 102]
  [110 111 112]]]

    rank =  3   
    shape = (2, 2, 3)
    t[0] =
        [[ 0  1  2]
        [10 11 12]] 

    t[0, 1] =
        [10 11 12] 

    t[1, 0, 1] =
        101 

A tensor with shape (2, 3, 2, 4)

t = tf.Variable([
    [[[0, 1, 2, 3], [10, 11, 12 , 13]], 
     [[100, 101, 102, 103], [110, 111, 112 , 113]],
     [[200, 201, 202, 203], [210, 211, 212 , 213]],
     ], 
     [[[1000, 1001, 1002, 1003], [1010, 1011, 1012 , 1013]], 
     [[1100, 1101, 1102, 1103], [1110, 1111, 1112 , 1113]],
     [[1200, 1201, 1202, 1203], [1210, 1211, 1212 , 1213]],]
                                                 ])
info(t, indices = [[1], [1,2], [1,2,1], [1,2,1,3]])

Output:
Tensor:
[[[[   0    1    2    3]
   [  10   11   12   13]]

  [[ 100  101  102  103]
   [ 110  111  112  113]]

  [[ 200  201  202  203]
   [ 210  211  212  213]]]


 [[[1000 1001 1002 1003]
   [1010 1011 1012 1013]]

  [[1100 1101 1102 1103]
   [1110 1111 1112 1113]]

  [[1200 1201 1202 1203]
   [1210 1211 1212 1213]]]]


    rank =  4   
    shape = (2, 3, 2, 4)
    
    t[1] =
        [[[1000 1001 1002 1003]
        [1010 1011 1012 1013]]

        [[1100 1101 1102 1103]
        [1110 1111 1112 1113]]

        [[1200 1201 1202 1203]
        [1210 1211 1212 1213]]] 

    t[1, 2] =
        [[1200 1201 1202 1203]
        [1210 1211 1212 1213]] 

    t[1, 2, 1] =
        [1210 1211 1212 1213] 

    t[1, 2, 1, 3] =
        1213 

Imports and Helper Functions#

import tensorflow as tf

class col:
    header = '\033[95m' ; blue = '\033[94m' ; green = '\033[92m' ; reset = '\033[0m' ; bold = '\033[1m'

def header(string):
  print(f'{col.header}{string}{col.reset}\n')

def print_index_value(t, index):
  value =t[tuple(index)]
  print(f'{col.blue}t{index}=\n{col.green}{value} {col.reset}\n')


def info(t, indices):
  print(f'{col.blue}Tensor:{col.reset}\n{col.bold}{t.numpy()}{col.reset}\n\n{col.blue}rank =\t{col.green}{tf.rank(t)}{col.reset}\t{col.blue}\nshape =\t{col.green}{t.shape}{col.reset}')
  
  for index in indices:
    print_index_value(t, index)
  print()

© 2022

Tensors