Play with Tensorflow!

What is Tensorflow? How it is used ? Relation of graphs and tensorflow?

According to the documentation tensorflow is an open source library for performing machine learning operations.
But the more specific definition of tensorflow is that its an open source library for numeric computation which understand the program in form of data flow diagrams. Even if you make a simple addition program there will be a graph ready for it and that graph can be accessed through the tensorboard api which comes along with tensorflow. Each graph node represents a computations and each edge represent relation between these nodes.
Tensorflow was developed by Google’s brain team for machine learning research but now it is being widely used for general purpose machine learning tasks.
One of the major reason behind its success is  its flexible nature that it could run even on our smartphones.
And right know its community is very much active, so if you are a machine learning enthusiast it’s the perfect time to put tensorflow in your bag as it’s very productive and has great demand in the technology market ranging from production to research.

There are two versions of tensorflow available right now :

1. CPU Version – runs on CPU
2. GPU Version – runs on GPU (faster in most of the cases)

Installing tensorflow is very simple like any other pyhton library. For installation you can visit “install tensorflow“.

Simple tensorflow code :

import tensorflow as tf
a = tf.constant(5, name=”input_a”)
b = tf.constant(3, name=”input_b”)
c = tf.multiply(a,b,name=”mul_c”)
d = tf.add(a,b,name=”add_d”)
e = tf.add(c,d,name=”add_e”)
sess = tf.Session()
output = sess.run(e)
print(output)
writer = tf.summary.FileWriter(‘./my_graph’, sess.graph)
writer.close()
sess.close()

>> 23

Code Breakdown:

1.  import tensorflow as tf
>  importing tensorflow with alias ‘tf’.

2. a = tf.constant(5, name=”input_a”)
3. b = tf.constant(3, name=”input_b”)
>  Here a and b are nodes with constant 5 and 3 respectively.

4. c = tf.multiply(a,b,name=”mul_c”)
5. d = tf.add(a,b,name=”add_d”)
6. e = tf.add(c,d,name=”add_e”)
>  Here c, d and e are computation nodes with add and multiplication functions.

7.  sess = tf.Session()
>  Initialization of a session. Nothing works without a session in tensorflow. With session   a graph also gets initializes.

8.  output = sess.run(e)
>   executing tensor e in the session sess.

9.   writer = tf.summary.FileWriter(‘./my_graph’, sess.graph)
10. writer.close()
>    Opening a writer object for saving the current graph under the session ‘sess’.
>    closing the writer object.

11. sess.close()
>    closing the current session.

Now , how will you access the graph?
As simple as opening a bottle!

Just write this in you terminal and tensorboard is ready to serve!
>>  tensorboard – – log <write the path where the graph is saved>

>>  Open the localhost address in you browser and enjoy analysing your graph.

So here is the resultant graph of the given code !

Screenshot from 2017-08-22 02:03:27

Follow for more such updates ! Happy reading !

One thought on “Play with Tensorflow!

Add yours

Leave a comment

Website Powered by WordPress.com.

Up ↑