TensorFlow is a flexible, open-source library for deep learning written by the Google brain team in 2015.
It was originally developed to carry out large numerical computations. It is mainly used to train machine learning/deep learning models. It is written in three languages - Python, C++, and CUDA.
TensorFlow offers several advantages for an application.
It provides both a Python and a C++ API, but the Python API is more complete and is generally easier to use.
You will be able to access many pre-built deep learning models.
It is a whole stack: which means you can preprocess data, model data, and deploy models in your application.
TensorFlow has great compilation times in comparison to the alternative deep learning libraries and it supports CPUs, GPUs, and even distributed processing in a cluster.
Tensor: A tensor is a mathematical object which is a standard way of representing data in deep learning. Tensors are defined as a multidimensional array or as a list. Tensors are identified by the following parameters.
Rank: The rank of a tensor is defined by the number of directions required to describe a tensor. Example: A zero rank tensor is a scalar, a first rank tensor is a vector, a second rank tensor is typically a square matrix, etc.
Shape: Number of rows and columns of a tensor.
Type: Data type assigned to the tensor’s elements. Example: integer, float, string, or Boolean.
Flow: Flow is the graphical representation of mathematical operations. TensorFlow performs computations with the help of dataflow graphs. It has nodes that represent the operations in your model. We then write code to build the graph, create a session, and execute that graph.
As the name suggests, these have constant values when called. Constants are initialized as soon as they are called.
To create a constant tensor from a tensor-like object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
tf.constant()
import tensorflow as tf
b = tf.constant(100)
b
<
tf.Tensor: shape = (), dtype = int32, numpy = 100 >
#creating string constant
a = tf.constant("India")
a
<
tf.Tensor: shape = (), dtype = string, numpy = b 'India' >
#creating boolean constant
bol = tf.constant(True)
bol
<
tf.Tensor: shape = (), dtype = bool, numpy = True >
#creating numpy array / list as constant
import numpy as np
np_array = tf.constant(np.array([1, 3, 5, 7, 9]))
np_array
<
tf.Tensor: shape = (5, ), dtype = int64, numpy = array([1, 3, 5, 7, 9]) >
#defining the type
td = tf.constant([1, 2, 4, 5, 6, 7], shape = (2, 3), dtype = "float32")
td
<
tf.Tensor: shape = (2, 3), dtype = float32, numpy =
array([[1., 2., 4.],
[5., 6., 7.]], dtype = float32) >
Having the same inputs to vary the output is an important thing in deep learning. Variables are used to add functionality to the graph such as training parameters. The initial value of the variable must be provided at the time of declaring a variable. Variables allow us to add new trainable parameters to the graph.
1
2
3
4
5
6
7
8
9
10
11
12
Tf.variable()
var1 = tf.Variable([1, 2, 3, 4, 5, 6])
var1
<
tf.Variable 'Variable:0'
shape = (6, ) dtype = int32, numpy = array([1, 2, 3, 4, 5, 6], dtype = int32) >
var2 = tf.Variable([5, 2, 6, 3, 1], dtype = np.float32)
var2
<
tf.Variable 'Variable:0'
shape = (5, ) dtype = float32, numpy = array([5., 2., 6., 3., 1.], dtype = float32) >
As the name suggests, they are entities that are not initialized and contain no data. They are put into place to later store a dynamic value when needed by the graph to execute.
Placeholders allow us to feed data to a TensorFlow model from outside a model.
1
2
3
4
5
6
7
tf.placeholder()
tf.compat.v1.disable_eager_execution()
tf.compat.v1.placeholder(dtype = tf.float32, shape = None, name = None)
<
tf.Tensor 'Placeholder:0'
shape = <unknown> dtype=float32>
The add() operation performs element-wise addition with two tensors.
We can perform addition for two matrices only if they are of the same dimension.
from tensorflow import constant, add
1
2
3
4
5
6
7
8
A = constant([1, 2])
B = constant([3, 4])
C = A + B
C
<
tf.Tensor: shape = (2, ), dtype = int32, numpy = array([4, 6], dtype = int32) >
Example:
1
2
3
Scalar addition: 2 + 5 = 7
Vector addition: [1, 2] + [3, 4] = [4, 6]
Matrix addition: [[1, 2], [4, 6]] + [[3, 4], [7, 3]] = [[4, 6], [11, 9]]
from tensorflow import constant, add
1
2
3
4
5
6
7
8
9
10
A = constant([[1, 2], [4, 6]])
B = constant([[3, 4], [7, 3]])
C = A - B
print(C.numpy)
<
bound method _EagerTensorBase.numpy of < tf.Tensor: shape = (2, 2), dtype = int32, numpy =
array([[-2, -2],
[-3, 3]], dtype = int32) >>
Element-wise multiplication is performed using the multiply() operation. However, the tensors multiplied must have the same shape.
The matrix multiplication is performed using the matmul() operation. For example, matmul(A,B) – Multiplies A by B. However, the number of columns of A must equal the number of rows of B.
Example:
from tensorflow import constant, matmul
1
2
3
4
5
6
7
8
9
10
A = constant([[1, 2], [4, 6]])
B = constant([[3, 4], [7, 3]])
C = matmul(A, B)
C
<
tf.Tensor: shape = (2, 2), dtype = int32, numpy =
array([[17, 10],
[54, 34]], dtype = int32) >
tensordot performs element-wise multiplication and its summation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import tensorflow as tf
from tensorflow
import constant, tensordot
A = constant([[1, 2], [4, 6]])
B = constant([[3, 4], [7, 3]])
c = tf.tensordot(A, B, axes = 1)
c
<
tf.Tensor: shape = (2, 2), dtype = int32, numpy =
array([[17, 10],
[54, 34]], dtype = int32) >
Indexing:
import tensorflow as tf
a = tf.constant([2, 6, 4, 8, 9, 1, 0])
#print all
print(a[: ])
tf.Tensor([2 6 4 8 9 1 0], shape = (7, ), dtype = int32)
#print from 1 to last
print(a[1: ])
tf.Tensor([6 4 8 9 1 0], shape = (6, ), dtype = int32)
#print from 1 to 4
print(a[1: 4])
tf.Tensor([6 4 8], shape = (3, ), dtype = int32)
#print alternate indices
print(a[::2])
tf.Tensor([2 4 9 0], shape = (4, ), dtype = int32)
#print in reverse order
print(a[::-1])
tf.Tensor([0 1 9 8 4 6 2], shape = (7, ), dtype = int32)
#printing specific indices
ind = tf.constant([0, 2])
a_ind = tf.gather(a, ind)
a_ind
<
tf.Tensor: shape = (2, ), dtype = int32, numpy = array([2, 4], dtype = int32) >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x = tf.constant([0, 1, 2, 3, 4, 5, 6, 7, 8])
x = tf.reshape(x, (3, 3))
x
<
tf.Tensor: shape = (3, 3), dtype = int32, numpy =
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]], dtype = int32) >
x = tf.transpose(x, perm = [1, 0])
x
<
tf.Tensor: shape = (3, 3), dtype = int32, numpy =
array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]], dtype = int32) >
The datatype of a tensor can be changed using the function cast as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
tf.cast()
.
import tensorflow as tf
#changing the datatype of a tensor
b = tf.constant([1, 4, 6, 8, 9])
b.dtype
tf.int32
#change from int32 to flaot32
b = tf.cast(b, dtype = tf.float32)
b.dtype
tf.float32
c = tf.constant([1.4, 6.8, 2.3, 5.7])
c.dtype
tf.float32
#change from float32 to float16
c = tf.cast(c, dtype = tf.float16)
c.dtype
tf.float16
This article covers the basics of TensorFlow. It starts with the introduction, its applications, and finally the basic operations of tensors.
TensorFlow is a great library that is mainly used in carrying out the numerical and graphical computation of data and also in creating deep learning networks. It has gained huge popularity in the field of machine learning.