Introduction to Tensorflow
Tensorflow is a library for performing machine learning / deep learning tasks with graphs and data like objects in a distributed manner. A lot is described about it on its home page, you can visit that here. The focus of this post will be to get started with tensorflow and understand the basics.
We will be using tensorflow’s python API. So lets get started.
In order to proceed you will need to install tensorflow library on your computer with a python distribution. You can find the details here.
Import tensorflow in your python code/shell.
#!/bin/pythonimport tensorflow as tf
The above line of code will import the tensorflow library and will refer to it as tf.
Tensorflow separates definition of computation from their execution, it is done via. Graphs.
Graphs
A tensorflow graph is a definition of a computation prior to its execution.
Once the graphs are assembled they are executed under a session.
Sessions
Tensorflow Session is an environment under which a graph or graphs are executed.
What is a tensor?
A tensor is a n dimensional matrix.
0-d : A Number (e.g. 2)
1-d: A Vector (e.g. V = [2, 4, 5, 2.3, 8])
2-d: A Matrix (e.g. M = [[1, 2, 3],[4,5,6,],[7,8,9]])
And so on…
Since most of the time our data will be in the form of a tensor and it will be executed as per the computational graph, what ends up happening is the data (tensor) moving from each graph node to another after the required computations are performed. Hence the name Tensor — Flow!
>>>> import tensorflow as tf# Define a graph to perform some operation
>>>> ops = tf.add(2,6)# Create a session
>>>> sess = tf.Session()# Print the result of the operation
>>>> print (sess.run())
8# Close the session
>>>> sess.close()
Common way to use a session is with the with clause.
with tf.Session() as sess:
sess.run(ops)
The use of with clause eliminates the use of sess.close() statement. It takes care of the close call in case any exception occurs.
Some more graphs,
import tensorflow as tfa = 3
b = 4ops1 = tf.add(a, b)
ops2 = tf.mul(a, b)
ops3 = tf.sub(ops2, ops1)with tf.Session() as sess:
sess.run(ops3)# Output
5
One interesting question arises that is what happens with we execute ops2 or ops1?
Well since the ops1 or ops2 are independent statements the other operations ops3 are not evaluated.
So for e.g. if we execute ops2 then ops1 and ops3 will not be evaluated thus saving computation and time. This is true for any operations in complex graphs.
Lets say if you want to execute the remaining statement also then one can do it in this way:
with tf.Session() as sess:
ops1, ops2 = sess.run([ops1, ops2])
In the above code we created nodes (operations) in a default graph. If needed you can create more than one graph. Although it is a good practice to build independent subgraphs inside a single graph than creating multiple graphs.
# Create a Graph
g = tf.Graph()
Simple operation inside a user created graph
# Create graph g
g = tf.Graph()# Set graph g as default
with g.as_default():
x = tf.add(3, 5)# Initialize a session with graph g
sess = tf.Session(graph=g) # Evaluate g inside session sess
with tf.Session() as sess:
sess.run(x)# Output ERROR
ValueError: Fetch argument <tf.Tensor 'Add:0' shape=() dtype=int32> cannot be interpreted as a Tensor.
(Tensor Tensor("Add:0", shape=(), dtype=int32) is not an element of this graph.)
One thing to note while creating graphs in tensorflow is to make sure all the variables that are used inside the graph are defined inside the same graph. Since this gets quite complex to understand and code becomes messy its advised to use a single graph (the default one) and create independent subgraphs inside the same graph.
To fix the above code we have to define the variables inside the graph
# Create graph g
g = tf.Graph()# Set graph g as default and define variables and operations (nodes)
with g.as_default():
a = 3
b = 5
x = tf.add(a, b)# Initialize a session with graph g
sess = tf.Session(graph=g)# Execute the session to evaluate the value of x
sess.run(x)# Close the session
session sess.close()# Output
8
Now since we have defined the variables inside the same graph it doesn’t throw the error as it did previously.
That concludes our introduction to tensorflow.
To get in touch say hello on twitter @kaustubhn.