Beginning ML – basics : KnowDev

Let’s get our hands dirty !

First. Some basics

First things first, Import tensorflow library

import tensorflow as tf

Let’s first understand how tensorflow works by taking 2 tensorflow constants.

x1 = tf.constant(5)

x2 = tf.constant(6)

Multiply x1 and x2.

result = tf.mul(x1, x2)

Now, if you try to print the result and run the program you wouldn’t get any output because till now if have constructed the computational graph. To actually multiply the constants and get the result of the multiplication, you must launch the graph in the session.

with tf.Session() as sess:

out = sess.run(result)

print out

The actual computaion takes place when sess.run(result) is called ! The output can be seen in the terminal as :

Tensor(“Mul:0”, shape=(), dtype=’int32′)

30

As you can see the everything in tensorflow is represented as tensor. A tensor can be thought of as an multi-dimensional matrix. Each node in tensorflow computational graph is called ‘ops’. An op can contain zero or more tensors and tensors can only be passed for operations in the graph.

I hope this post gives you a brief introduction of tensorflow and its core working in a nutshell. In coming posts we shall be looking into more complicated and yet interesting concepts.

 

Advertisements