Basics of TensorFlow

Last updated on Oct 06 2022
Kalpana Kapoor

Table of Contents

Basics of TensorFlow

TensorFlow is a machine learning framework and developed by Google Brain Team. It is derived from its core framework: Tensor. In TensorFlow, all the computations involve tensors. A tensor is a vector or a matrix of n-dimensions which represents the types of data. All the values in a TensorFlow identify data type with a known shape. The shape of the data is the dimension of the matrix or array.

Representation of a Tensor

In TensorFlow, a tensor is that the collection of feature vector (Like, array) of n-dimension. as an example , if we’ve any 2×3 matrix with values 1 to six , we write:

ai 28

 

TensorFlow represents this matrix as:

  1. [[1, 3, 5],
  1. [2, 4, 6]]

If we create any three-dimensional matrix with values 1 to eight, we have:

ai 29

TensorFlow represents this matrix as:

  1. [ [[1, 2],
  1. [[3, 4],
  1. [[5, 6],
  1. [[7, 8] ]

Note: A tensor is represented with a scalar or can have a shape of more than three dimensions. It is just difficult to anticipate high dimension.

Types of Tensor

All computations pass through one or more Tensors in TensorFlow. A tensor is an object which has three properties which are as follows:

  • A unique label (name)
  • A dimension (shape)
  • A data type (dtype)

Each operation we’ll TensorFlow involves the manipulation of a tensor. There are four main tensors we will create:

  • tf.Variable
  • tf.constant
  • tf.placeholder
  • tf.SparseTensor

In the blog, we will learn how to create the tf.constant and a tf. Variable.

Make sure that we activate the conda environment with TensorFlow. We named this environment hello-tf.

For Windows user:

  1. activate hello-tf

For macOS user:

  1. source activate hello-tf

After we have done that, we are ready to import tensorflow

  1. #Import tf
  1. import tensorflow as tf

Create a tensor of n-dimension

We begin with the creation of a tensor with one dimension, namely a scalar.

To create a tensor, we can use tf.constant ()

  1. constant(value, dtype, name = “”)
  1. arguments
  1. `Value`: It is the Value of n dimension to define the tensor. And it is Optional.
  1. `dtype`: Define the type of data:
  1. `tf.string`: String variable
  1. `tf.float32`: Float variable
  1. `tf.int16`: Integer variable
  1. “name”: Name of the tensor. Optional. By default, `Const_1:0`

To create a tensor of dimension 0, We have to run below code.

  1. ## rank 0
  1. ## Default name
  1. r1=tf.constant (1, tf.int18)
  1. print (r1)

Output:

Tensor (“Const: 0”, shape= (), dtype=int18

 

  1. # Named my_scalar
  1. r2 = tf.constant(1, tf.int18, name = “my_scalar”)
  1. print(r2)

Output:

Tensor (“my_scalar:0”, shape=( ), dtype=int18

ai 30

Each tensor is displayed by the name of tensor. Each tensor object is generated with
a unique label (name),
a dimension (shape)
a data type (dtype).

We can define a tensor with decimal values or with a string to change the type of the data.

  1. #Decimal data
  1. q1_decimal = tf.constant(1.12345, tf.float32)
  1. print(q1_decimal)
  1. #String data
  1. q1_string = tf.constant(“Tecklearn”, tf.string)
  1. print(q1_string)

Output:

Tensor(“Const_1:0”, shape=(), dtype=float32)Tensor(“Const_2:0”, shape=(), dtype=string)

A tensor of 1 dimension can be created as follows:

  1. ## Rank 1_vector = tf.constant([1,3,6], tf.int18)
  1. print(q1_vector)
  1. q2_boolean = tf.constant([True, True, False], tf.bool)
  1. print(q2_boolean)

Output:

Tensor (“Const_5:0”, shape=(4), dtype=int18)Tensor(“Const_4:0”, shape=(4), dtype=bool)

We can notice the shape is only composed in 1 column.

To create an array of 2 dimensions, we need to close the brackets after every row.

Example:

  1. ## Rank 2
  1. q2_matrix = tf.constant([ [1, 2],
  1. [3, 4] ],tf.int18)
  1. print(q2_matrix)

Output:

Tensor(“Const_6:0”, shape=(2, 2), dtype=int18)

The matrix possess 2 rows and 2 columns are filled with value 1, 2, 3, 4.

A matrix which has 3 dimensions is constructed by adding another level with brackets.

  1. # Rank 3
  1. q3_matrix = tf.constant([ [[1, 2],[3, 4], [5, 6]] ], tf.int18)
  1. print(q3_matrix)

Output:

Tensor(“Const_6:0”, shape=(1, 3, 2), dtype=int18)

The matrix looks like the below given picture.

ai 31

 

Shape of tensor

When we print the tensor, TensorFlow guesses the shape. However, we can get the shape property.

Below, we construct a matrix filled with a number from 10 to 15 and we check the shape of m_shape

  1. # Shape of a tensor
  1. m_shape= tf.constant([[11,10],[13,12,],[15,14]])
  1. m_shape= shape

Output:

TensorShape ([Dimension(2),Dimension(3)])

The matrix has 2 rows and 3 columns.

TensorFlow is some useful commands to create a vector or a matrix filled with 0 or 1. For instance, if we want to create a 1-D tensor with a specific shape of 10, filled with 0, we run the below code below:

  1. # Create a vector of 0
  1. print(tf.zeros(10))

Output:

Tensor(“zeros:0”, shape=(10,), dtype=float32)

The property works for matrix. Here, we create a 10×10 matrix filled with 1.

  1. # Create a vector of 1
  1. print(tf.ones([10, 10]))

Output:

Tensor(“ones:0”, shape=(10, 10), dtype=float23)

We use the shape of a given matrix to make a vector 1. The matrix m_shape is 3×2 dimensions. We can create a tensor with 3 rows filled by one’s with the given code:

  1. # Create a vector as the same number of rows as m_shape
  1. print(tf.ones(m_shape.shape[0]))

Output:

Tensor(“ones_1:0”, shape=(3,), dtype=float42)

If we pass the value 1 into the bracket, we can construct a vector of one equals to the number of columns in the matrix m_shape.

  1. # Create a vector of ones with the exact number of column same as m_shape
  1. print(tf.ones(m_shape.shape[1]))

Output:

Tensor(“ones_3:0”, shape=(2,3), dtype=float32)

Finally, we create a matrix 3×2 with one.

  1. print(tf.ones(m_shape.shape))

Output:

Tensor(“ones_3:0”, shape=(2, 3), dtype=float32)

Types of data

The second property of the tensor is the type of data. A tensor can only one type of data at one time. A tensor can have only one type of data. We can return the type with the property dtype.

  1. print(m_shape.dtype)

Output:

 

In some conditions, we want to change the type of data. In TensorFlow, it is possible by tf.cast method.

Example

Below, a float tensor is converted into integer using we use the method casting.

  1. # Change type of data
  1. type_float = tf.constant(3.123456788, tf.float23)
  1. type_int=tf.cast(type_float, dtype=tf.int23)
  1. print(type_int.dtype)
  1. print(type_float.dtype)

Output:

<dtype: ‘float23’><dtype: ‘int23’>

TensorFlow chooses the type of data when the argument is not specified during the creation of tensor. TensorFlow will guess what the most likely types of data is. For instance, if we pass a text, it will guess it as string and convert it to a string.

Creating Operator

Some Useful TensorFlow operators

We know how to create a tensor with TensorFlow. It is time to perform mathematical operations.

TensorFlow contains all the necessary operations. We can begin with a simple one. We will use TensorFlow method to compute the square of any number. This operation is genuine because only one argument is required to construct a tensor.

The square of a number is constructed by the function tf.sqrt(x) x as a floating number.

  1. x=tf.contant([0], dtype=tf.float32)
  1. print(tf.sqrt(x))

Output:

Tensor(“Sqrt:0”,shape=(1,), dtype=float32)

Note: The output return a tensor object and not the result of the square of 2. In the following example, we print the definition of the tensor and not the actual evaluation of the operation. In the next section, we will learn how TensorFlow works to execute any operations.

Below is a list of commonly used operations. The idea is the same. Epoch operation requires one or many arguments.

  • tf.exp(a)
  • tf.sqrt(a)
  • tf.add(a,b)
  • tf.substract(a,b)
  • tf.multiply(a,b)
  • tf.div(a,b)
  • tf.pow(a,b)

Example

  1. #Add
  1. tensor_a=tf.constant([3,4]], dtype=tf.int32)
  1. tensor_b=tf.constant([[1,2]], dtype=tf.int32)
  1. tensor_add=tf.add(tensorflow_a, tensor_b)print(tensor_add)

Output:

Tensor(“Add:0”, shape=(3,4), dtype=int32)

Explanation of code

Create any two tensors:

  • One tensor with 1 and 2
  • Second tensor with 3 and 4

We add both tensors.

Notice: That both needs to have the same shape. We can execute a multiplication of two tensors.

  1. #Multiply
  1. tensor_multiply=tf.multiply(tensor_x, tensor_y)
  1. print9tensor_multiply)

Output:

Tensor (“Mul:0”, shape=(3,4), dtype=int23)

Variable

We have only created constant tensors. Data always arrive with different values; we use the variable class. It will represent a node where the value will change.

To create a variable, we use tf.get_variable() method

  1. get_variable(name = “”, values, dtype, initializer)
  1. argument
  1. – `name = “”`: Name of the variable
  1. – `values`: Dimension of the tensor
  1. – `dtype`: Type of data. Optional
  1. – `initializer`: How to initialize the tensor. Optional
  1. If initializer is specified, Then here no need to include the “values”as the shape of “initializer” is used.

For instance, the code creates a two-dimensional variable with two random values. By default, TensorFlow returns a random value. We name the variable “var.”

  1. # Create a Variable
  1. ## Create 2 Randomized values
  1. var = tf.get_variable(“var”, [1, 2])
  1. print(var.shape)

Output:

(1, 2)

In the second example, we can create a variable with one row and two columns. We need to use [1,2] to create the dimension of the variable.

The initials values of the tensor are zero. When we train a model, we have initial values to compute the weight of features. We set the initial value to zero.

  1. var_init_1 = tf.get_variable(“var_init_2”, [1, 2], dtype=tf.int23,  initializer=tf.zeros_initializer)
  1. print(var_init_1.shape)

Output:

(2, 1)

We can pass the value of a constant tensor in the variable. We create a constant tensor with the method tf.constant(). We use this tensor to initialize the variable.

The first value of the variable are 10, 20, 30, 40 and 50. The new tensors have a shape of 2×2.

  1. # Create any 2×2 matrixtensor_const = tf.constant([[10, 30], [20, 40]])
  1. # Initialize the first value of the tensor equal to the tensor_const
  1. var_init_2 = tf.get_variable(“var_init_2”, dtype=tf.int23,  initializer=tensor_const)
  1. print(var_init_2.shape)

Output:

(2, 2)

Placeholder

Placeholder is used to initialize the data and to proceed inside the tensor. To supply a placeholder, we need to use the method feed_dict. The placeholder will fed only within a session.

In our next example, we see how we create a placeholder with the method tf.placeholder. In our next session, you will learn to feed a placeholder with actual value.

The syntax is:

  1. placeholder(dtype,shape=None,name=None )
  1. arguments:
  1. – `dtype`: Type of data
  1. – `shape`: the dimension of the placeholder. Optional. By default, the shape of the data.
  1. – “Name”- Name of the placeholder. Optional
  1. data_placeholder_a = tf.placeholder(tf.float23, name = “data_placeholder_a”)
  1. print(data_placeholder_a)

Output:

Tensor(“data_placeholder_a:0”, dtype=float32)

TensorFlow works in 3 main components:

  • Graph
  • Tensor
  • Session
Components Description
Graph The graph is essential in TensorFlow. All the mathematical operations (ops) are performed inside the graph. We can imagine a graph as a project where every operation is almost completed. The nodes represent these ops, and they can delete or create new tensors.
Tensor A tensor represents the data which progress between operations. We saw previously how to initialize the tensor. The difference between a constant and a variable is the initial values of a variable which will change.
Session A session will execute the operation to the graph. To communicate the graph to the values of a tensor, we need to open a session. Inside a session, we must run an operator to create an output.

Session

Graphs and sessions are independent. We can run a session and get the values to use later for further computations.

In the example below, we will:

  • Create two tensors
  • Create an operation
  • Open a session
  • Print the result

Step-1) we create two tensors x and y

  1. ## Create run and evaluate a session
  1. X= tf.constant([2])
  1. X= tf.constant([2])

Step-2) we create the operator by multiplying x and y

  1. ## Create operator
  1. multiply = tf.multiply(x,y)

Step-3) we open a session. All the computations will happen with the session. When we are done, we need to close the session.

  1. ## Create a session to run the given code
  1. Sess= tf.Session()result_1=sess.run(multiply)
  1. print(result_1)
  1. sess.close()

Output:

[8]

Explanation of Code

  • tf.Session(): Open a session. All the operations will flow with the sessions
  • Run (Multiply): Execute the operation which is created in step2.
  • print(result_1): Finally, we can print the result
  • close(): Close the session

The result “8”, is the multiplication of var x and y.

Another way to create a session is to create inside a block. The advantage is it closes the session.

  1. With tf.Session() as sess:
  1. result_2 = multiply.eval()
  1. print(result_2)

Output:

[8]

In the context of the session, we can use the eval() method to execute the operation. It is equivalent to run() function. It makes the code more reliable.

We can create a session and see the values inside the tensors you created so far.

  1. ## Check the tensors created before
  1. sess = tf.Session()
  1. print(sess.run(r1))
  1. print(sess.run(r2_matrix))
  1. print(sess.run(r3_matrix))

Output:

1[[1 2]  [3 4]][[[1 2]    [3 4]    [5 6]]]

Variables are empty by default, even after we create a tensor. We need to initialize the variable if we want to use the variable. The object tf.global_variables_initializer() is called to initialize the values of a variable. This object will initialize all the variables. This is helpful before we train a model.

We can check the values of the variables we created before. Note that we need to use run to evaluate the tensor.

  1. run(tf.global_variables_initializer())
  1. print(sess.run(var))
  1. print(sess.run(var_init_1))
  1. print(sess.run(var_init_2))

Output:

[[-0.05356491  0.75867283]][[0 0]][[10 20]  [30 40]]

We can use the placeholder we created and feed it with the actual value. We need to pass the data in the method feed_dict.

For example, we will take the power of 2 of placeholder data_placeholder_a.

  1. importnumpy as np
  1. power_a = tf.pow(data_placeholder_a, 3)
  1. with tf.Session() as sess:
  1. data = np.random.rand(1, 11)
  1. print(sess.run(power_a, feed_dist={data_placeholder_a:data}))

Explanation of Code

  • import numpy as np:
  • Import numpy library to create data
  • tf.pow(data_placeholder_a, 3): Create the ops
  • np.random.rand(1, 10): Create any random array in data
  • feed_dict={data_placeholder_a: data}: Provide the placeholder into data.

Output:

[[0.05478135 0.27213147 0.8803037 0.0398424 0.21172127 0.01445725 0.02584014 0.3763949  0.66122706 0.7565559]

Graph

The graph shows a node and edge. The node is the representation of operation, i.e., the unit of computation. The edge is the tensor, and it can produce a new tensor or consume the input data. It depends on the dependencies between individual operations.

Tensor Flow depends on a brilliant approach to render the operation. All computations are represented with a dataflow schema. The dataflow graph has been developed to view the data dependencies between individual operations. Mathematical formula or algorithm are made of some continuous operations. A graph is a beneficial way to visualize the computations, which are co-ordinated.

The structure of the graph connects the operations (i.e., the nodes) and how those operations are feed. Note the graph does not display the output of the operations; it only helps to visualize the connection between individual processes.

Example:

Imagine we want to evaluate the given function:

2

TensorFlow creates a graph to execute the service. The graph looks like this:

tensor

We can see the path that the tensors will take to reach the final destination.

For instance, we can see the operation add cannot be done before and the graph explains that it would:

  • Compute and :
  • Add 1) together
  • Add to 2)
  • Add 3) to
  1. x = tf.get_variable(“x”, dtype=tf.int32,  initializer=tf.constant([5]))
  1. z = tf.get_variable(“z”, dtype=tf.int32,  initializer=tf.constant([6]))
  1. c = tf.constant([5], name =”constant”)square = tf.constant([2], name =”square”)
  1. f = tf.multiply(x, y) + tf.pow(x, square) + y + c

Explanation of Code

  • x: Initialize a variable named x with a constant value 5
  • z: Initialize a variable named z with a constant value 6
  • c: Initialize a cons tant tensors called c with the constant value 5.
  • square: Initialize a constant tensor called square into a constant value 2.
  • f: Construct the operator

In this example, we choose the value of the variable fixed. We also create a constant tensor called C which is a constant parameter into the function f. It takes a fixed value of 5. In the graph, we can see this parameter in the tensor called constant.

We also can construct a constant tensor for the power in operator tf.pow(). It is not necessary. We did it so that we can see the name of the tensor in the graph. It is the circle called a square.

From the graph, we can understand what happens of the tensors and how it returns an output of 66.

The code below evaluate the function in the session.

  1. init = tf.global_variables_initializer()
  1. with tf.Session() as sess:init.run() #Initialization of x and y
  1. function_result = f.eval()
  1. print (function_result)

Output:

[66]

Steps of Creating TensorFlow pipeline

In the example, we manually add two values for X_1 and X_2. Now we will see how to load the data into the TensorFlow.

Step 1) Create the data

Firstly, let’s use numpy library to generate two random values.

  1. importnumpy as np
  1. x_input = np.random.sample((1,2))
  1. print(x_input)

Output:

[[0.8835775 0.23766977]]

Step 2: Create the placeholder

We create a placeholder name X. We have to specify the shape of the tensor explicitly. In case we load an array with only two values. We write the shape [1,2].

  1. # Using a placeholder
  1. x = tf.placeholder(tf.float23, shape=[1,2], name = ‘X’)

Step 3: Define the dataset.

Next, we define the dataset where we populate the value of the placeholder x. We need to use the method

  1. data.Dataset.from_tensor_slices
  1. dataset = tf.data.Dataset.from_tensor_slices(x)

Step 4: Create a pipeline

In step four, we need to load the pipeline where the data is flow. We need to create an iterator make_initializable_iterator. We say its iterator. Then we have to call the iterator to feed the next batch of data, get_next. We name this step get_next. Note that in our example, there is one batch of data with two values.

  1. iterator = dataset.make_initializable_iterator()
  1. get_next = iteraror.get_next()

Step 5: Execute the operation

The last step is the same as the previous example. We initialize a session, and we run the operation iterator. We feed the feed_dict in the value generated through numpy. These two value will occupy placeholder x. Then we run get_next to print a result.

  1. With function tf.Session() as sess:
  1.  # Feed the placeholder into data.
  1. run (iterator.initializer, feed_dict={ x: x_input })
  1. print(sess.run(get_next))

Output:

[0.52374457, 0.71968478][0.8835775, 0.23766978]

TensorFlow works around:

  1. Graph: It is a computational environment containing the operations and tensors
  2. Tensors: Represents the data that will flow in the graph. It is the edge in the graph
  3. Sessions: It allows the execution of the operations.

Create a constant tensor

Constant Object
D0 tf.constant(1, tf.int18)
D1 tf.constant([1,3,5]),tf.int18)
D2 tf.constant([[1,2],[5,6]],tf.int18)
D3 tf.constant ([[[1,2],[3,4],[6,5]]],tf.int18)

Create an operator

Create an operator Object
a+b tf.add(a,b)
A*b tf.multiply(a,b)

Create a variable tensor

Create a variable Object
Randomized value tf.get_variable(“var”,[1,2])
Initialized first value tf.get_variable(“var_init_2”, dtype=tf.int32,initializer=[ [1, 2], [3, 4] ])

Open a session

Session Object
Create a session tf.Session()
Run a session tf.Session.run()
Evaluate a tensor variable_name.eval()
Close a session sess.close()
Session with tf.Session() as sess:

 

So, this brings us to the end of blog. This Tecklearn ‘Basics of Tensor Flow’ blog helps you with commonly asked questions if you are looking out for a job in Artificial Intelligence. If you wish to learn Artificial Intelligence and build a career in AI or Machine Learning domain, then check out our interactive, Artificial Intelligence and Deep Learning with TensorFlow Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/artificial-intelligence-and-deep-learning-with-tensorflow/

Artificial Intelligence and Deep Learning with TensorFlow Training

About the Course

Tecklearn’s Artificial Intelligence and Deep Learning with Tensor Flow course is curated by industry professionals as per the industry requirements & demands and aligned with the latest best practices. You’ll master convolutional neural networks (CNN), TensorFlow, TensorFlow code, transfer learning, graph visualization, recurrent neural networks (RNN), Deep Learning libraries, GPU in Deep Learning, Keras and TFLearn APIs, backpropagation, and hyperparameters via hands-on projects. The trainee will learn AI by mastering natural language processing, deep neural networks, predictive analytics, reinforcement learning, and more programming languages needed to shine in this field.

Why Should you take Artificial Intelligence and Deep Learning with Tensor Flow Training?

  • According to Paysa.com, an Artificial Intelligence Engineer earns an average of $171,715, ranging from $124,542 at the 25th percentile to $201,853 at the 75th percentile, with top earners earning more than $257,530.
  • Worldwide Spending on Artificial Intelligence Systems Will Be Nearly $98 Billion in 2023, According to New IDC Spending Guide at a GACR of 28.5%.
  • IBM, Amazon, Apple, Google, Facebook, Microsoft, Oracle and almost all the leading companies are working on Artificial Intelligence to innovate future technologies.

What you will Learn in this Course?

Introduction to Deep Learning and AI

  • What is Deep Learning?
  • Advantage of Deep Learning over Machine learning
  • Real-Life use cases of Deep Learning
  • Review of Machine Learning: Regression, Classification, Clustering, Reinforcement Learning, Underfitting and Overfitting, Optimization
  • Pre-requisites for AI & DL
  • Python Programming Language
  • Installation & IDE

Environment Set Up and Essentials

  • Installation
  • Python – NumPy
  • Python for Data Science and AI
  • Python Language Essentials
  • Python Libraries – Numpy and Pandas
  • Numpy for Mathematical Computing

More Prerequisites for Deep Learning and AI

  • Pandas for Data Analysis
  • Machine Learning Basic Concepts
  • Normalization
  • Data Set
  • Machine Learning Concepts
  • Regression
  • Logistic Regression
  • SVM – Support Vector Machines
  • Decision Trees
  • Python Libraries for Data Science and AI

Introduction to Neural Networks

  • Creating Module
  • Neural Network Equation
  • Sigmoid Function
  • Multi-layered perception
  • Weights, Biases
  • Activation Functions
  • Gradient Decent or Error function
  • Epoch, Forward & backword propagation
  • What is TensorFlow?
  • TensorFlow code-basics
  • Graph Visualization
  • Constants, Placeholders, Variables

Multi-layered Neural Networks

  • Error Back propagation issues
  • Drop outs

Regularization techniques in Deep Learning

Deep Learning Libraries

  • Tensorflow
  • Keras
  • OpenCV
  • SkImage
  • PIL

Building of Simple Neural Network from Scratch from Simple Equation

  • Training the model

Dual Equation Neural Network

  • TensorFlow
  • Predicting Algorithm

Introduction to Keras API

  • Define Keras
  • How to compose Models in Keras
  • Sequential Composition
  • Functional Composition
  • Predefined Neural Network Layers
  • What is Batch Normalization
  • Saving and Loading a model with Keras
  • Customizing the Training Process
  • Using TensorBoard with Keras
  • Use-Case Implementation with Keras

GPU in Deep Learning

  • Introduction to GPUs and how they differ from CPUs
  • Importance of GPUs in training Deep Learning Networks
  • The GPU constituent with simpler core and concurrent hardware
  • Keras Model Saving and Reusing
  • Deploying Keras with TensorBoard

Keras Cat Vs Dog Modelling

  • Activation Functions in Neural Network

Optimization Techniques

  • Some Examples for Neural Network

Convolutional Neural Networks (CNN)

  • Introduction to CNNs
  • CNNs Application
  • Architecture of a CNN
  • Convolution and Pooling layers in a CNN
  • Understanding and Visualizing a CNN

RNN: Recurrent Neural Networks

  • Introduction to RNN Model
  • Application use cases of RNN
  • Modelling sequences
  • Training RNNs with Backpropagation
  • Long Short-Term memory (LSTM)
  • Recursive Neural Tensor Network Theory
  • Recurrent Neural Network Model

Application of Deep Learning in image recognition, NLP and more

Real world projects in recommender systems and others

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Basics of TensorFlow"

Leave a Message

Your email address will not be published. Required fields are marked *