Tensorflow Basics : Part 1

  • tf.constant
  • tf.ones
  • tf.zeros
  • tf.rank
  • tf.random.normal
  • tf.random.uniform
  • tf.Variable
    • tf.convert_to_tensor
    • tf.argmax
    • tf.reshape
    • tf.Variable.assign
    • tf.Variable.assign_add
    • tf.Variable.assign_sub
1
2
import tensorflow as tf
print("Version , ",tf.__version__)
Version ,  2.1.0

Constant : tf.constant

1
2
3
tf.constant(
value, dtype=None, shape=None, name='Const'
)
1
2
3
4
5
6
7
8
x1 = tf.constant([5, 2 ,1, 3])
print(x1)
print(x1.numpy)
print(x1.shape)
print(x1.dtype)

x2 = tf.constant([[5, 2], [1, 3]])
print(x2)
tf.Tensor([5 2 1 3], shape=(4,), dtype=int32)
<bound method _EagerTensorBase.numpy of <tf.Tensor: shape=(4,), dtype=int32, numpy=array([5, 2, 1, 3])>>
(4,)
<dtype: 'int32'>
tf.Tensor(
[[5 2]
 [1 3]], shape=(2, 2), dtype=int32)
1
2
x3 = tf.constant([5,2,1,3] , dtype=float)
print(x3)
tf.Tensor([5. 2. 1. 3.], shape=(4,), dtype=float32)

tf.ones

1
2
3
tf.ones(
shape, dtype=tf.dtypes.float32, name=None
)

tf.zeros

1
2
3
tf.zeros(
shape, dtype=tf.dtypes.float32, name=None
)
1
2
3
4
5
print(tf.ones(shape=(2, 2)))
print(tf.zeros(shape=(2, 2),dtype=tf.int16))

print(tf.ones((3, 4), tf.int32))
print(tf.zeros((3, 4), tf.int32))
tf.Tensor(
[[1. 1.]
 [1. 1.]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[0 0]
 [0 0]], shape=(2, 2), dtype=int16)
tf.Tensor(
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]], shape=(3, 4), dtype=int32)
tf.Tensor(
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]], shape=(3, 4), dtype=int32)

rank : tf.rank

1
2
3
4
5
6
7
8
9
10
rank_0 = tf.Variable(7)
rank_1 = tf.Variable([7])
rank_2 = tf.Variable([[1, 2], [3, 4]])
rank_3 = tf.Variable([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
rank_4 = tf.reshape(tf.range(start=0, limit=24, delta=1), shape=[2, 3, 2, 2])

ranks = [rank_0, rank_1, rank_2, rank_3, rank_4]

for r in ranks:
print("Rank is:", tf.rank(r).numpy())
Rank is: 0
Rank is: 1
Rank is: 2
Rank is: 3
Rank is: 4
Rank Math entity Example
0 Scalar (magnitude only) [1.5]
1 Vector (magnitude and direction) [2, 3, 5, 8 11]
2 Matrix (table of numbers) [[1, 2], [3, 4]]
3 3-Tensor (cube of numbers) [[[1], [2]], [[3], [4]]]
n n-Tensor (you get the idea) [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]

Note : The rank of a tensor is not the same as the rank of a matrix. The rank of a tensor is the number of indices required to uniquely select each element of the tensor. Rank is also known as “order”, “degree”, or “ndims.”

1
2
3
4
x = tf.ones((1,3,2))
print(x)
rank = tf.rank(x).numpy()
print(rank)
tf.Tensor(
[[[1. 1.]
  [1. 1.]
  [1. 1.]]], shape=(1, 3, 2), dtype=float32)
3

normal distribution : tf.random.normal

1
2
3
tf.random.normal(
shape, mean=0.0, stddev=1.0, dtype=tf.dtypes.float32, seed=None, name=None
)
1
2
3
4
5
6
7
8
9
from matplotlib import pyplot as plt

N = 100
tf.random.set_seed(12);
normal_dist = tf.random.normal(shape=(N,), mean=5.5, stddev=1.)
plt.plot(normal_dist)
plt.xlabel('Random number idx')
plt.ylabel('N random value')
plt.show()

png

uniform distribution : tf.random.uniform

1
2
3
tf.random.uniform(
shape, minval=0, maxval=None, dtype=tf.dtypes.float32, seed=None, name=None
)
1
2
3
4
5
6
7
N = 100
tf.random.set_seed(12);
uniform_dist = tf.random.uniform(shape=(N,), minval=0, maxval=10)
plt.plot(uniform_dist)
plt.xlabel('Random number idx')
plt.ylabel('N random value')
plt.show()

png

Variable : tf.Variable

1
2
3
4
my_tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]])
print(my_tensor)
my_variable = tf.Variable(my_tensor)
print(my_variable)
tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
1
2
3
4
# Variables can be all kinds of types, just like tensors

bool_variable = tf.Variable([False, False, False, True])
complex_variable = tf.Variable([5 + 4j, 6 + 1j])

Most tensor operations work on variables as expected,
although variables cannot be reshaped

tf.convert_to_tensor

tf.argmax

tf.reshape

1
2
3
4
5
6
7
print("variable:", my_variable)
print("\nViewed as a tensor:", tf.convert_to_tensor(my_variable))
print("variable:", my_variable)
print("\nIndex of highest value:", tf.argmax(my_variable))

# This creates a new tensor; it does not reshape the variable.
print("\nCopying and reshaping: ", tf.reshape(my_variable, ([1,4])))
variable: <tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>

Viewed as a tensor: tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)
variable: <tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>

Index of highest value: tf.Tensor([1 1], shape=(2,), dtype=int64)

Copying and reshaping:  tf.Tensor([[1. 2. 3. 4.]], shape=(1, 4), dtype=float32)

can reassign the tensor using tf.Variable.assign Calling assign does not (usually) allocate a new tensor; instead, the existing tensor’s memory is reused.

1
2
3
4
new_value = tf.ones(shape=(2, 2))
print(my_variable)
my_variable.assign(new_value)
print('value of my_variable after assign new value:', my_variable)
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
value of my_variable after assign new value: <tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>

assignment operations

tf.Variable.assign_add
tf.Variable.assign_sub

1
2
3
4
5
my_variable.assign_add(my_variable)
print(my_variable)

my_variable.assign_sub(my_variable*0.5)
print(my_variable)
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[2., 2.],
       [2., 2.]], dtype=float32)>
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>

some mathematical operations

1
2
3
4
5
6
7
8
9
10
11
12
a = tf.ones(shape=(2, 2))
b = tf.ones(shape=(2, 2))
print('a:', a.numpy().flatten())
print('b:', b.numpy().flatten())
c = a + b
print('c:', c.numpy().flatten())
d = tf.square(c)
print('square:', d.numpy().flatten())
e = tf.exp(d)
print('exp :', e.numpy().flatten())


a: [1. 1. 1. 1.]
b: [1. 1. 1. 1.]
c: [2. 2. 2. 2.]
square: [4. 4. 4. 4.]
exp   : [54.598152 54.598152 54.598152 54.598152]