WTF is Tensor Flow? — Part 1

Venkatesh Gupta
4 min readJun 6, 2018
Tensor Flow is easy!

Let’s not get started by the phrase :- Tensor Flow is an open source library created by Google to design, develop and train deep learning models.

Pause for a moment and and re-read :- Tensor and Flow! You got it right. There are two words that are clubbed together, and that makes Tensor Flow.

1. What is a Tensor?

In TensorFlow, data is represented in the form of tensors. A tensor is an n dimensional array.

You know how a computer stores a gray-scale image. Right? It is something like this:-

A gray scale image is stored in the form of two dimensional array.

Similarly, in tensorflow, we call this a 2 dimensional tensor instead of a two dimensional array. Pretty, cool! Isn’t it?

Dimensionality determines the rank of tensor. If a tensor has zero(0) dimension, it’s rank is zero. If a tensor has one(1) dimension, it’s rank is one.

Dimensionality determines the rank of tensor. If a tensor has zero(0) dimension, it’s rank is zero. If a tensor has one(1) dimension, it’s rank is one.

Just see this image for a while and keep scrolling down.

Image result for rank in tensorflow

0- dimensional tensor :-

A tensor of rank zero or a tensor with zero dimensions can be thought of as a point.

import tensorflow as tf
t = tf.constant(1)
sess = tf.Session()
print sess.run(tf.shape(t))
print sess.run(tf.size(t))
print sess.run(tf.rank(t))
Output :-
[]
1
0

Are you clear about the term shape and size that was introduced just now? If not, just keep scrolling down.

1- dimensional tensor :-

A tensor of rank one or a tensor with one dimension can be thought of as a line.

import tensorflow as tf
t = tf.constant([1,3,4,5,6,1])
sess = tf.Session()
print sess.run(tf.shape(t))
print sess.run(tf.size(t))
print sess.run(tf.rank(t))
Output :-
[6]
6
1

Well, shape is the array length along each dimension. Here, you got only one dimension and the array length in this dimension is 1,2,3,4,5,6. Yes, you counted it right. Six!

2- dimensional tensor :-

A tensor of rank two or a tensor with two dimension can be thought of as a matrix. (An entity of rows and columns.)

import tensorflow as tf
t = tf.constant([[1,3,4],[1,6,8],[1,2,3],[1,1,2]])
sess = tf.Session()
print sess.run(tf.shape(t))
print sess.run(tf.size(t))
print sess.run(tf.rank(t))
Output :-
[4,3]
12
2

Well, shape is the array length along each dimension. Here, you got two dimensions. The array length along horizontal direction is four and along the vertical direction, it’s 3.

2. What is Flow?

Flow is used to describe interaction between tensors (and functions) by using computational graph.

B, W and X are tensors. So basically B, W and X must be having some values and they stored in a data structure called as tensors. On these tensors, operations like multiplication, addition are done. The entire model is represented as a computational graph or flow graph.

By seeing the above graph , you can see that we are multiplying W and X and adding B to it and then we are applying an activation function Relu on the equation. What are we doing here?

We have defined a model, Z = Relu(W * X + B) in our computational graph. It doesn’t hold any value. It’s just an abstract of what and how will our model will perform.

To make this model actually run, we build and run a session.

So, whenever you are reminded of Tensor Flow, remember these two phrases :- Build and Run!

  1. Build an abstract model (computational graph)
  2. Run that model (computational graph)

Building a computational graph (abstract model) is easy. Try it!

import tensorflow as tf
a = tf.constant(3.0, tf.float32)
b = tf.constant(4.0)
print(a,b)
Output :-
(<tf.Tensor 'Const_8:0' shape=() dtype=float32>, <tf.Tensor 'Const_9:0' shape=() dtype=float32>)

Hey! Have you just noticed, you haven’t got the output as a = 3.0 and b=4.0 rather you have got a long line. Wonder, why?

You are just getting the abstract i.e how the tensor is. What the structure of tensor is!

Let’s make this model a reality. Let’s run the session :-

import tensorflow as tf
a = tf.constant(3.0, tf.float32)
b = tf.constant(4.0)
sess = tf.Session()
output= sess.run([a,b])
print(output)
sess.close()
Output :-
[3.0, 4.0]

Session puts the graph in the CPU/GPU and provides methods to execute them. We are closing the session at the end, to free the resources that the computational graph is using.

À la carte:-

You can avoid writing sess.close() everytime. You can rather put the statements within a with statement.

with tf.Session() as sess:
output= sess.run(add)
print(output)

I hope, you have enjoyed it!

To read Part-2 (about constants, variables, placeholders and tensorboard), click on the link to read more.

--

--

Venkatesh Gupta

Product @ Internshala | Career-tech (Ed-tech + Recruitment-tech) & creator economy enthusiast | Writes on Xplainerr