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++. 

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

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!

What 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.

Is 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.

Does 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.

Plan Your Artificial Intelligence Career Today

0 replies on “What is TensorFlow? How it Works [With Examples]”

Accelerate Your Career with upGrad

Our Best Artificial Intelligence Course

×