• Vipul Vaibhaw

Introduction to TensorFlow

This article aims to those readers who have no experience or very little understanding of tensorflow.


Tensorflow is a free and open-source software library for dataflow and differentiable programming across a range of tasks. Tensorflow has got use cases in many areas and not just deep learning. Tensorflow (or even pyTorch) can be considered an efficient library for scientific computing. However, Tensorflow is widely used in the field of deep learning.


Tensorflow is developed by Google Brain. Written in Python, c++ and CUDA.


It has got APIs in the following languages -

1. Python

2. Java

3. Go

4. Haskell and Rust etc


No matter whichever language you choose your code will be compiled in a highly optimized C++ backend.


Tensorflow framework is composed of two things -

1. A library for defining computational graphs

2. A runtime for executing the graphs on different hardwares


What is a computation graph?

Computational graphs are an abstract way to represent or describe computation as directed graph:

1. Edges are tensors or multi-dimensional arrays

2. Nodes are rules or Ops which manipulates the tensors.


Let us understand the computational graphs a bit more -

Suppose we have to execute following operations -


a = 10 b = 20 temp1 = a + b temp2 = a*b result = temp2/temp1 print result


The above flow can be represented like follows -




This representation helps tensorflow to compute dependencies in the graph and allows the tensorflow backend to parallelize the processes on multiple cores on your system. It gives very high performance with no inputs from user about the code execution.


Now let us see how can we define the graph shown above in tensorflow -


# defining a and b

a = tf.constant(10)

b = tf.constant(20)


# defining some more ops

temp1 = tf.add(a,b)

temp2 = tf.multiply(a,b)


result = tf.divide(temp2,temp1)

print(result)



If you notice the two nodes "a*b" and "a+b" are independent of each other. Hence they can be easily executed on different simultaneously and then merged together to calculate "t2/t1".



A tensorflow program consists of two parts -

1. Definition

2. Execution

tf.Graph is used to specify computations. tf.Session is responsible for executing sessions.


Tensors -


Tensorflow gives us a phenomenal way to deal with a trillion X trillion dimensional tensor.

#1000000000000x1000000000000

a = tf.zeros((int(1e12), int(1e12)))


If we define the same thing in numpy, we will get out of memory error.


import numpy as np np.zeros((int(1e12), int(1e12)))


Session -


A session in tensorflow is responsible for executing the graph.


a = tf.constant(1.0) b = tf.constant(1.0) c = tf.constant(4.0) d = tf.divide(c,tf.add(a,b))

with tf.Session() as sess: print(sess.run(d))


sess.run() calculates the shortest graph needed to fetch the result for a particular tensor.


Note - Read about shape inferencing in tensorflow.


Writing you first neural network in Tensorflow -


check it out here -https://github.com/vaibhawvipul/Reinforcement-Learning-Lectures/blob/master/Introduction_to_Tensorflow.ipynb


Keep coding! :)

31 views

©2019 by Deeplearned education pvt ltd