TensorFlow is an open source computation framework for building machine learning models. Its design make use of lessons learnt from earlier machine learning frameworks — Torch, Theano, Caffe, and Keras. Torch is the earliest machine learning framework that made of the term Tensor. Theano makes use of Graph data structure to store operations and compile them to high-performance code. Caffe is a high performance framework written in C++ and makes feasible to execute applications on different devices. Keras provides an easy to use API to interface with various machine learning frameworks like Theano.
TensorFlow is composed of two parts:
- A library to define computational graphs
- A runtime to execute graphs on a variety of different hardware. It supports execution on both CPUs and GPUs.
Installing TensorFlow on your machine with PyCharm
I am going to use PyCharm IDE by JetBrains for working with TensorFlow. PyCharm is a Python IDE that provides code completion, refactoring , and in-built support for running Jupyter notebooks.
Start by downloading Python 3 from the official website.
After installing Python, download and install PyCharm from its official website. We are going to use Community edition of PyCharm.
Open the PyCharm application and you will see following screen.
Create New Project and specify location and select
New environment using Virtualenv. Virtualenv avoids polluting the global python installation. We can work with different versions of python for different projects.
Create after entering the details. It will create the project and open a new window as shown below.
Next, we will install TensorFlow library using PyCharm itself. On Mac, go to PyCharm preferences as shown below.
Once Preferences window is open, go to Project > Project Interpreter as shown below.
Click on the
+ icon in left bottom and you will get a window in which you can search TensorFlow module as shown below.
Install Package to install TensorFlow. It might take a couple of minutes depending on your internet speed. Once installed you will get
Packages installed successfully Installed packages: 'tensorflow'.
Working with TensorFlow API
Create a directory named
src inside the
01-tensorflow-intro directory. We will use it to store Python files.
Create a ne Python file
01.py inside the
src directory. We will write our first TensorFlow program inside it.
We will start by creating a simple TensorFlow program to add two numbers.
import tensorflow as tf # 1 a = tf.constant(1) # 2 b = tf.constant(2) # 3 c = tf.add(a, b) # 4 with tf.Session() as sess: # 5 print(sess.run(c)) # 6
The code shown above does the following:
- It imports the
tensorflowmodule and give it an alias
- We created two constant tensors named a and b. As mentioned before, tensor is a multidimensional array. What might confuse you is that
bdoes not refer to arrays. They are constant values represented as unit arrays. If you print
a, the output will be following
Tensor("Const:0", shape=(), dtype=int32).
Const:0is the autogenerated name of the Tensor.
()is the shape of the tensor. And
int32is the datatype.
- Next, we defined another Tensor
sumoperator to add both
- The line 5 creates as instance of
Sessionclass and names it
sess. Session provides an environment in which TensorFlow operations are executed.
- Using the session created in the line 5, we run the tensor
cthat performed addition of
To run the code example above, you can right click anywhere on the file and run the program as shown in the image below.
You will see the output
3 in the console.
Key points about TensorFlow API
The two key points that you should be aware of TensorFlow API are:
- TensorFlow API is by default lazy. It does not perform any computation till you call
sess.run(). If you print
cafter line 4 you will get following output
Tensor("Add:0", shape=(), dtype=int32). As you can see, we didn’t see the result
3in the print statement. Nothing will get executed. We have only defined our computation graph. This is the difference between declarative programming and imperative programming.
- TensorFlow will only execute necessary portion of the graph. In the example above, it will evaluate everything but it different example it might execute a sub-portion of the graph.
In the first part of this series, we have set up our TensorFlow environment and written our first TensorFlow program. In the next part, we will get deeper into TensorFlow API covering Tensors and Graphs in detail.