Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconArtificial Intelligences USbreadcumb forward arrow iconWhat is TensorFlow? How it Works [With Examples]

What is TensorFlow? How it Works [With Examples]

Last updated:
22nd Sep, 2021
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
What is TensorFlow? How it Works [With Examples]

TensorFlow is an open-source library used to build machine learning models. It is an incredible platform for anyone passionate about working with machine learning and artificial intelligence. Furthermore, with the steady growth that the machine learning market is witnessing, tools like TensorFlow have come to the spotlight as tech companies explore the diverse capabilities of AI technology. No doubt, the global machine learning market is projected to reach a valuation of US$ 117.19 billion by 2027.

But on the outset, it is pertinent to know what is TensorFlow and what makes it a popular choice among developers worldwide. 

What is TensorFlow?

TensorFlow is an end-to-end open-source platform for machine learning with a particular focus on deep neural networks. Deep learning is a subset of machine learning that involves the analysis of large-scale unstructured data. Deep learning differs from traditional machine learning in that the latter typically deals with structured data. 

TensorFlow boasts of a flexible and comprehensive collection of libraries, tools, and community resources. It lets developers build and deploy state-of-the-art machine learning-powered applications. One of the best things about TensorFlow is that it uses Python to provide a convenient front-end API for building applications while executing them in high-performance, optimized C++. 

Ads of upGrad blog

The Google Brain team initially developed the TensorFlow Python deep-learning library for internal use. Since then, the open-source platform has seen tremendous growth in usage in R&D and production systems. 

Some TensorFlow Basics

Now that we have a fundamental idea of what is TensorFlow, it’s time to delve into some more details about the platform. 

Following is a brief overview of some basic concepts related to TensorFlow. We’ll begin with tensors – the core components of TensorFlow from which the platform derives its name.

Tensors

In the TensorFlow Python deep-learning library, a tensor is an array that represents the types of data. Unlike a one-dimensional vector or array or a two-dimensional matrix, a tensor can have n dimensions. In a tensor, the values hold identical data types with a known shape. The shape represents dimensionality. Thus, a vector will be a one-dimensional tensor, a matrix is a two-dimensional tensor, and a scalar would be a zero-dimensional tensor.

tensors

Source

Shape

In the TensorFlow Python library, shape refers to the dimensionality of the tensor. 

In the TensorFlow Python library, shape refers to the dimensionality of the tensor.

Source

In the above image, the shape of the tensor is (2,2,2).

Type

The type represents the kind of data that the values in a tensor hold. Typically, all values in a tensor hold an identical data type. The datatypes in TensorFlow are as follows:

  • integers
  • floating point
  • unsigned integers
  • booleans
  • strings
  • integer with quantized ops
  • complex numbers

Graph

A graph is a set of computations that take place successively on input tensors. It comprises an arrangement of nodes representing the mathematical operations in a model.

Session

A session in TensorFlow executes the operations in the graph. It is run to evaluate the nodes in a graph. 

Operators

Operators in TensorFlow are pre-defined mathematical operations. 

How Do Tensors Work?

In TensorFlow, data flow graphs describe how data moves through a series of processing nodes. TensorFlow uses data flow graphs to build models. The graph computations in TensorFlow are facilitated through the interconnections between tensors. 

The n-dimensional tensors are fed to the neural network as input, which goes through several operations to give the output. The graphs have a network of nodes, where each node represents a mathematical operation. But the edge between the nodes is a multidimensional data array or a tensor. A TensorFlow session allows the execution of graphs or parts of graphs. For that, the session allocates resources on one or more machines and holds the actual values of intermediate results and variables.

tensor's functions

Source

TensorFlow applications can be run on almost any convenient target, which could be CPUs, GPUs, a cluster in the cloud, a local machine, or Android and iOS devices.

TensorFlow Computation Graph 

A computation graph in TensorFlow is a network of nodes where each node operates multiplication, addition, or evaluates some multivariate equation. In TensorFlow, codes are written to create a graph, run a session, and execute the graph. Every variable we assign becomes a node where we can perform mathematical operations such as multiplication and addition. 

Here’s a simple example to show the creation of a computation graph:

Suppose we want to perform the calculation: F(x,y,z) = (x+y)*z. 

The three variables x, y, and z will translate into three nodes in the graph shown below:

TensorFlow Computation Graph

Source

Steps of building the graph:

Step 1: Assign the variables. In this example, the values are:

x = 1, y = 2, and z = 3

Step 2: Add x and y.

Step 3: Multiply z with the sum of x and y.

Finally, we get the result as ‘9.’

In addition to the nodes where we have assigned the variables, the graph has two more nodes – one for the addition operation and another for the multiplication operation. Hence, there are five nodes in all.

Fundamental Programming Elements in TensorFlow

In TensorFlow, we can assign data to three different types of data elements – constants, variables, and placeholders.

Let’s look at what each of these data elements represents.

1. Constants

As evident from the name, constants are parameters with unchanging values. In TensorFlow, a constant is defined using the command tf.constant(). During computation, the values of constants cannot be changed.

Here’s an example:

c = tf.constant(2.0,tf.float32)

d = tf.constant(3.0)

Print (c,d)

2. Variables

Variables allow the addition of new parameters to the graph. The tf.variable() command defines a variable that must be initialized before running the graph in a session.

Here’s an example:

Y = tf.Variable([.4],dtype=tf.float32)

a = tf.Variable([-.4],dtype=tf.float32)

b = tf.placeholder(tf.float32)

linear_model = Y*b+a

3. Placeholders

Using placeholders, one can feed data into a model from the outside. It allows later assignment of values. The command tf.placeholder() defines a placeholder.

Here’s an example:

c = tf.placeholder(tf.float32)

d = c*2

result = sess.run(d,feed_out={c:3.0})

The placeholder is primarily used to feed a model. Data from outside is fed to a graph using a variable name (the variable name in the above example is feed_out). Subsequently while running the session, we specify how we want to feed the data to the model.

Example of a session:

The execution of the graph is done by calling a session. A session is run to evaluate the graph’s nodes, called the TensorFlow runtime. The command sess = tf.Session() creates a session.

Example:

x = tf.constant(3.0)

y = tf.constant(4.0)

z = x+y

sess = tf.Session() #Launching Session

print(sess.run(z)) #Evaluating the Tensor z

In the above example, there are three nodes – x, y, and z. The node ‘z’ is where the mathematical operation is carried out, and subsequently, the result is obtained. Upon creating a session and running the node z, first, the nodes x and y will be created. Then, the addition operation will take place at node z. Hence, we will obtain the result ‘7’.

Advance Your Career in ML and Deep Learning with upGrad

Looking for the best place to know more about what is TensorFlow? Then upGrad is here to assist you in your learning journey.

With a learner base covering 85+ countries, upGrad is South Asia’s largest higher EdTech platform that has impacted more than 500,000 working professionals globally. With world-class faculty, collaborations with industry partners, the latest technology, and the most up-to-date pedagogic practices, upGrad ensures a wholesome and immersive learning experience for its 40,000+ paid learners globally.

The Advanced Certificate Program in Machine learning and Deep Learning is an academically rigorous and industry-relevant 6-months course covering the concepts of Deep Learning. 

Program Highlights:

  •  Prestigious recognition from IIIT Bangalore
  • 240+ hours of content with 5+ case studies and projects, 24+ live sessions, and 15+ expert coaching sessions
  • Comprehensive coverage of 12 tools, languages, and libraries (including TensorFlow)
  • 360-degree career assistance, mentorship sessions, and peer-to-peer networking opportunities

upGrad’s Master of Science in Machine Learning and Artificial Intelligence is an 18-months robust program for those who want to learn and upskill themselves with advanced Machine Learning and cloud technologies.

Program Highlights:

  • Prestigious recognition from Liverpool John Moores University and IIT Madras
  • 650+ hours of content with 25+ case studies and projects, 20+ live sessions, and 8+ coding assignments
  • Comprehensive coverage of 7 tools and programming languages (including TensorFlow)
  • 360-degree career assistance, mentorship sessions, and peer-to-peer networking opportunities

Conclusion

Ads of upGrad blog

Machine Learning and Artificial Intelligence continue to evolve. What was once the theme of sci-fi movies is now a reality. From Netflix movie recommendations and virtual assistants to self-driving cars and drug discovery, machine learning impacts all dimensions of our lives. Furthermore, with tools like TensorFlow, innovations in machine learning have reached new heights. The open-source library is undoubtedly a boon to developers and budding professionals innovating machine learning-driven technologies. 

So what are you waiting for? Start learning with upGrad today!

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Best Artificial Intelligence Course

Frequently Asked Questions (FAQs)

1What is TensorFlow used for?

TensorFlow Python is an open-source platform that lets developers create large-scale neural networks. Some of the primary use cases of TensorFlow include text-based applications (such as fraud detection), voice recognition, image recognition, video detection, and analysis of time-series data.

2Is TensorFlow written in Python or C++?

TensorFlow allows the front-end APIs to be implemented using various languages such as Python, R, C, and C++. However, the runtime in TensorFlow is written using the C++ language.

3Does TensorFlow need coding?

Since TensorFlow is an open-source library for machine learning, there are four core areas that one needs to master. While coding skills are a must, the other critical components of machine learning education are mathematics and statistics, machine learning theory, and hands-on experience in building machine learning projects from scratch.

Explore Free Courses

Suggested Blogs

Top 25 New & Trending Technologies in 2024 You Should Know About
63209
Introduction As someone deeply immersed in the ever-changing landscape of technology, I’ve witnessed firsthand the rapid evolution of trending
Read More

by Rohit Sharma

23 Jan 2024

Basic CNN Architecture: Explaining 5 Layers of Convolutional Neural Network [US]
6375
A CNN (Convolutional Neural Network) is a type of deep learning neural network that uses a combination of convolutional and subsampling layers to lear
Read More

by Pavan Vadapalli

15 Apr 2023

Top 10 Speech Recognition Softwares You Should Know About
5507
What is a Speech Recognition Software? Speech Recognition Software programs are computer programs that interpret human speech and convert it into tex
Read More

by Sriram

26 Feb 2023

Top 16 Artificial Intelligence Project Ideas & Topics for Beginners [2024]
6115
Artificial intelligence controls computers to resemble the decision-making and problem-solving competencies of a human brain. It works on tasks usuall
Read More

by Sriram

26 Feb 2023

15 Interesting Machine Learning Project Ideas For Beginners & Experienced [2024]
5614
Taking on machine learning projects as a beginner is an excellent way to gain hands-on experience and develop a better understanding of the fundamenta
Read More

by Sriram

26 Feb 2023

Explaining 5 Layers of Convolutional Neural Network
5205
A CNN (Convolutional Neural Network) is a type of deep learning neural network that uses a combination of convolutional and subsampling layers to lear
Read More

by Sriram

26 Feb 2023

20 Exciting IoT Project Ideas & Topics in 2024 [For Beginners & Experienced]
9718
IoT (Internet of Things) is a network that houses multiple smart devices connected to one Cloud source. This network can be regulated in several ways
Read More

by Sriram

25 Feb 2023

Why Is Time Complexity Important: Algorithms, Types & Comparison
7565
Time complexity is a measure of the amount of time needed to execute an algorithm. It is a function of the algorithm’s input size and the type o
Read More

by Sriram

25 Feb 2023

Curse of dimensionality in Machine Learning: How to Solve The Curse?
11228
Machine learning can effectively analyze data with several dimensions. However, it becomes complex to develop relevant models as the number of dimensi
Read More

by Sriram

25 Feb 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon