TensorFlow

Understand the basics of TensorFlow

Sai Sasank
4 min readApr 4, 2018

TensorFlow is an open source library for machine intelligence developed by Google. It is a computational library with a wide range of functionality. However, its main purpose is to implement Machine Learning algorithms.

TensorFlow is extremely popular relative to other Deep Learning libraries due to many reasons. One of the most important reasons being its ability to facilitate both research and production. This was not possible with the other libraries. Researchers and developers had to use one language for research and prototyping and some other language to deploy their model into production. Needless to say, TensorFlow took good care of this.

Research AND Production?!

TensorFlow got it covered.

There are many other reasons for someone to choose TensorFlow over other libraries. Some of them include Python API, TensorBoard (kick-ass visualizations!) and Auto Differentiation.

Tensorflow Basics

Any TensorFlow program essentially contains 2 components : Computational Graph and Session.

Computational Graphs are a means to specify computations that need to be carried out in our model. They are directed graphs, where nodes represent operations and edges represent tensors and the direction in which they move (hence, the name TensorFlow!).

Once we define our graph, we can run it by creating a session. We can execute the whole graph or just a part of it. Session allocates memory for the graph and the variables.

(Note : TensorFlow has a new feature called Eager Execution which is more Python way of doing things. It is imperative in nature contrast to the graph-and-session approach, which is declarative.)

A Computational Graph. Nodes represent operations and edges represent data. Source: TensorFlow for Machine Intelligence

In the above graph, you can see nodes corresponding to multiplication and addition operations. You can also see how data flows in the graph from one node to another. After defining such graph, we can run it (or parts of it) in a session.

Tensors, Constants, Variables and Placeholders

import tensorflow as tf

Tensors are best understood as a means of generalizing scalars, vectors and matrices. Scalars are 0-dimensional, vectors are 1-dimensional and matrices are 2-dimensional.

Tensors are n-dimensional arrays.

Extending this idea, tensors are nothing but n-dimensional. If n is 0 they are scalars, if n is 1 they are vectors and so on. The rank of a tensor is nothing but the number of dimensions of the tensor.

Creating constants in TensorFlow is simple.

Constants in TensorFlow.

You can create constants using various other operations like tf.zeros(...) , tf.ones(...) and tf.random_normal(...) .

Constants’ values cannot be changed and are stored in the graph’s definition. They are loaded every time you load the graph.

Variables on the other hand, are mutable and are stored separately from the graph definition.

Defining variables in TensorFlow

Constants are immutable and are stored in the graph definition. Variables are mutable and are stored separately from the graph.

Before using the variables you have to initialize them in a session. For this, we usually create a initializer operation and run this operation in a session. One operation is enough to initialize all variables, although you can initialize them individually.

Defining variables in TensorFlow.

When we are defining our graph, we will not always know what values we need for certain computations. In order to define a computation without specifying the values, we use placeholders. It is similar to defining F(x,y) without specifying the values of x and y .

Placeholders allow us to define computations without providing values.

Let us define f(x,y) = x*y where x and y are placeholders.

Placeholders in TensorFlow.

As you can see in the code above, we need to supply values for placeholders if we want to evaluate f(x,y) .

TensorFlow has many operations ranging from matrix inversion to advanced optimizations. We will cover basic operations by implementing a linear regression model.

Linear Regression in TensorFlow

Linear Regression is basically finding a best fit line through a set of points.

Have you ever fit a line through data?
It’s liberating.

We will create two placeholders x and y whose values are later supplied in a session. We have weights w and bias b , whose optimal values need to be found by an optimizer. So, these will be variables. We will define our model f as f(x) = w * x + b .

Defining placeholders, variables and initializer op for our model.

Now, all that is left is to define a loss function and an optimizer. We will then run the optimizer operation in a session a few times and voila! We will have the model trained.

Defining loss, optimizer and finally training our linear regression model.

I hope you have gained a good sense of how TensorFlow works! You pretty much know the basic constructs and can further build up to develop awesome applications. I wish to write a post on TensorBoard in the near future, covering the visualization aspects of TensorFlow. Until then!

--

--