import tensorflow as tf

print(tf.__version__)
2.6.0
scalar = tf.constant(7)
scalar
<tf.Tensor: shape=(), dtype=int32, numpy=7>
scalar.ndim
0
vector = tf.constant([10, 10])
vector
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([10, 10])>
vector.ndim
1
matrix = tf.constant([[10, 7],
                            [7, 10]])
matrix
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[10,  7],
       [ 7, 10]])>
matrix.ndim
2
another_matrix = tf.constant([[10., 7.],
                                    [3., 2.],
                                    [8., 9.]], dtype=tf.float16)
another_matrix
<tf.Tensor: shape=(3, 2), dtype=float16, numpy=
array([[10.,  7.],
       [ 3.,  2.],
       [ 8.,  9.]], dtype=float16)>
another_matrix.ndim
2
tensor = tf.constant(
    [
        [
            [1, 2, 3],
            [4, 5, 6],
        ],
        [
            [7, 8, 9],
            [10, 11, 12]
        ],
        [
            [13, 14, 15],
            [16, 17, 18]
        ]
    ]
)

tensor
<tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]],

       [[13, 14, 15],
        [16, 17, 18]]])>
tensor.ndim
3
tf.Variable
tensorflow.python.ops.variables.Variable
changable_tensor = tf.Variable([10, 7])
unchangable_tensor = tf.constant([10, 7])

changable_tensor, unchangable_tensor
(<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([10,  7])>,
 <tf.Tensor: shape=(2,), dtype=int32, numpy=array([10,  7])>)
%%script echo "TypeError: 'ResourceVariable' object does not support item assignment"

# Lets try change one of the elements in our changable tensor
changable_tensor[0] = 7
changable_tensor
TypeError: 'ResourceVariable' object does not support item assignment
changable_tensor[0].assign(7)
changable_tensor
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([7, 7])>
%%script echo "AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'"

# Let's try change our un-changable tensor

unchangable_tensor[0].assign(7)
unchangable_tensor
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'
random_1 = tf.random.Generator.from_seed(42) # Set seed for reproducibility
random_1 = random_1.normal(shape=(3, 2)) # probability is constant

random_2 = tf.random.Generator.from_seed(42)
random_2 = random_2.normal(shape=(3,2))

# Are they equal?

random_1, random_2, random_1 == random_2
(<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
 array([[-0.7565803 , -0.06854702],
        [ 0.07595026, -1.2573844 ],
        [-0.23193765, -1.8107855 ]], dtype=float32)>,
 <tf.Tensor: shape=(3, 2), dtype=float32, numpy=
 array([[-0.7565803 , -0.06854702],
        [ 0.07595026, -1.2573844 ],
        [-0.23193765, -1.8107855 ]], dtype=float32)>,
 <tf.Tensor: shape=(3, 2), dtype=bool, numpy=
 array([[ True,  True],
        [ True,  True],
        [ True,  True]])>)

Shuffle the order of elements i a tensors

not_shuffled = tf.constant(
    [
        [10, 7],
        [3, 4],
        [2, 5]
    ]
)

# Shuffle our non-shuffled tensor

shuffled=tf.random.shuffle(not_shuffled, seed=42, name="Shuffle")

not_shuffled.ndim, shuffled
(2,
 <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
 array([[ 2,  5],
        [ 3,  4],
        [10,  7]])>)
tf.random.set_seed(42)
tf.random.shuffle(not_shuffled, seed=42)
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[10,  7],
       [ 3,  4],
       [ 2,  5]])>
tf.random.set_seed(54)
tf.random.shuffle(not_shuffled)
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 2,  5],
       [ 3,  4],
       [10,  7]])>
tf.random.set_seed(54) # global level random seed
tf.random.shuffle(not_shuffled, seed=32) # operation level random seed
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[ 2,  5],
       [ 3,  4],
       [10,  7]])>

Other ways to make tensors

tf.ones([10, 7])
<tf.Tensor: shape=(10, 7), dtype=float32, numpy=
array([[1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1.]], dtype=float32)>
tf.zeros(shape=(3, 4))
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]], dtype=float32)>

Turn NumPy array into tensors

The main difference between Numpy array and Tensorflow tensors is that tensors can be run on a GPU computing

import numpy as np

numpy_A= np.arange(1, 25, dtype=np.int32) # create a NumPy array between 1 and 25.

numpy_A
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24])
A = tf.constant(numpy_A, shape=(2, 3, 4))
A
<tf.Tensor: shape=(2, 3, 4), dtype=int32, numpy=
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],

       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]]])>
A.ndim
3

Getting information from tensors

  • Shape
  • Rank
  • Axis or dimension
  • Size

Tensor Attributes

Attribute Meaning Code
Shape The length(number of elements) of each of the dimensions of a tensor tensor.shape
Rank The number of tensor dimension. A scalar has rank 0, a vector has rank 1, a matrix is rank 2, a tensor has rank n. tensor.ndim
Axis or dimension A particular dimension of a tensor tensor[0], tensor[:,1]
Size The total number of items in the tensor tf.size(tensor)
rank_4_tensor = tf.zeros(shape=[2, 3, 4, 5])
rank_4_tensor, rank_4_tensor.ndim
(<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)>,
 4)
rank_4_tensor.shape, rank_4_tensor.ndim, tf.size(rank_4_tensor)
(TensorShape([2, 3, 4, 5]), 4, <tf.Tensor: shape=(), dtype=int32, numpy=120>)
def TensorAttributes(tensor):
    tf.print("Datatypes of every element: ", tensor.dtype)
    tf.print("Number of dimensions (rank): ", tensor.ndim)
    tf.print("Shape of tensor: ", tensor.shape)
    tf.print("Elements along the 0 axis: ", tensor.shape[0])
    tf.print("Elements along the last axis: ", tensor.shape[-1])
    tf.print("Total number of elements in our tensor: ", tf.size(tensor))

TensorAttributes(rank_4_tensor)
Datatypes of every element:  tf.float32
Number of dimensions (rank):  4
Shape of tensor:  TensorShape([2, 3, 4, 5])
Elements along the 0 axis:  2
Elements along the last axis:  5
Total number of elements in our tensor:  120
rank_4_tensor[:1, :1, :1]
<tf.Tensor: shape=(1, 1, 1, 5), dtype=float32, numpy=array([[[[0., 0., 0., 0., 0.]]]], dtype=float32)>