How to Install and Use TensorFlow on Ubuntu 16.04

Introduction

TensorFlow is an open source machine learning software built by Google for training neural networks. TensorFlow neural networks are expressed as stateful data flow graphs. Each node in the graph represents operations performed by neural networks on multidimensional arrays. These multidimensional arrays are commonly known as “tensors”, hence the name TensorFlow.

TensorFlow is an in-depth study of system software. TensorFlow does a good job for finding information, as shown by Google in how search rankings are built in their machine learning artificial intelligence system, RankBrain. TensorFlow can perform pattern recognition as shown by Google, Inception, as well as audio recognition of human language. It is also useful for solving other non-machine learning problems such as partial differential equations.

TensorFlow architecture allows deployment on multiple CPUs or GPUs within a desktop, server, or mobile device. There are also extensions to integrate with CUDA, Nvidia’s parallel computing platform. This gives users who deploy direct GPU access to the many virtual instructions and other GPU elements that are required for parallel computing tasks.

In this article, we will install TensorFlow “CPU support only” version. This setup is ideal for those who want to install and use TensorFlow but don’t have an Nvidia graphics card or need to run mission-critical applications.

You can install TensorFlow in several ways. Each method has different use cases and development environment:

  • Python and Virtualenv: With this approach, you will install TensorFlow and any packages required to use TensorFlow in a Python virtual environment. This isolates TensorFlow environemnt from other Python programs on the same machine.
  • Native pip: In this method, you install TensorFlow on your system globally. This is recommended for people who want to make TensorFlow available to everyone on a user’s multiprocessor system. This installation method does not isolate TensorFlow in a closed development environment and may interfere with another Python or library installation.
  • Docker: Docker is a container runtime and completely isolates its contents from pre-existing packages on your system. This method uses a Docker container that contains TensorFlow and all of its dependencies. This method is ideal for incorporating TensorFlow into a larger application architecture that uses Docker. However, the Docker image size will be quite large.

In this tutorial, you will be installing TensorFlow in a Python virtual environment with virtualenv. This approach isolates the TensorFlow installation and is fast. After the installation is complete, you will test the installation by running the short program TensorFlow and then using TensorFlow to recognize the images.

Prerequisites

Before starting this tutorial, you need the following:

  • One Ubuntu 16.04 server with at least 1GB of RAM, following the Ubuntu 16.04 initial server setup guide including non-root sudo user and firewall. You will need at least 1GB of RAM to successfully complete the last example in this tutorial.
  • Python 3.3 or higher and virtualenv installed. Follow the guide how to install Python 3 on Ubuntu 16.04 to set up Python and virtualenv.
  • Installed Git, followed by the tutorial: How to Install Git on Ubuntu 16.04. You will use it to load the example repository.

Step 1 – Installing TensorFlow

In this step, we will create a virtual environment and install TensorFlow.

First, create a project directory called tf-demo:

mkdir ~/tf-demomkdir ~/tf-demo

Navigate to the newly created tf-demo directory:

cd ~/tf-demo

Then create a new virtual environment called tensorflow-dev. Run the following command to create the environment:

python3 -m venv tensorflow-dev

This creates a new tensorflow-dev directory that will contain all the packages you install while this environment is activated. It also includes pip and a standalone Python version.

Now activate the virtual environment:

source tensorflow-dev/bin/activate

Upon activation, you will see something similar to this in the terminal:

(tensorflow-dev)[email protected]:~/tf-demo $

You can now install TensorFlow in a virtual environment.

Run the following command to install and update to the latest version of TensorFlow available in PyPi:

pip3 install --upgrade tensorflow

TensorFlow will output:

Output

Collecting tensorflow
  Downloading tensorflow-1.4.0-cp36-cp36m-macosx_10_11_x86_64.whl (39.3MB)
    100% |████████████████████████████████| 39.3MB 35kB/s

...

Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0

If you want to disable the virtual environment at any time, the command:

deactivate

To activate the environment later, go to the project directory and run .source tensorflow-dev / bin / activate

Now that you have installed TensorFlow, let’s make sure the installation is running TensorFlow.

Step 2 – Verifying the Installation

To test the installation of TensorFlow, we are going to run a simple program in TensorFlow as a non-superuser. We’ll use a typical newbie example from Hello World! as a form of verification. Instead of creating a Python file, we will create this program using the Python interactive console.

To write a program, start the Python interpreter:

python

You will see the following message in your terminal

>>>

This is a prompt for the Python interpreter and indicates that it is ready for you to start typing some Python statements.

First, enter the following line to import the TensorFlow package and make it available as a local tf variable. Press ENTER after typing in the line of code:

import tensorflow as tf

Then add the following line of code to set the Hello World! Message:

hello = tf.constant("Привет, мир")

Then create a new TensorFlow session and assign it to the sess variable:

sess = tf.Session()

Note: Depending on the environment, you may see this output: Output

2017-12-08 2343:36.346946: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-12-08 2343:36.347157: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-12-08 2343:36.347281: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-12-08 2343:36.347403: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-12-08 2343:36.347526: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.

This tells you that you have a set of instructions that has the potential to optimize for the best TensorFlow performance. If you see this, you can safely ignore it and continue.

Finally, enter the following line of code to print the result of starting the hello session in TensorFlow that you created in the previous lines of code:

print(sess.run(hello))

You will see this output in the console:

Output

Привет, мир!

This indicates that everything is working and that you can start using TensorFlow to do something more fun.

Exit the Python interactive console by pressing CTRL + D.

Now, let’s use the TensorFlow API Image Recognition to get more familiar with TensorFlow.

Step 3 – Using TensorFlow for Image Recognition

Now that TensorFlow is installed and you have tested it, run a simple program, look at the pattern recognition capabilities of TensorFlow.

To classify the image, you need to train the model. Then you need to write some code to use the model.

TensorFlow provides a repository of models and examples, including code and model training for image classification.

Use Git to clone the TensorFlow model repository from GitHub in your project directory:

git clone https://github.com/tensorflow/models.git

You will see the following output as Git checks out the repository in a new folder called models:

Output

Cloning into 'models'...
remote: Counting objects: 8785, done.
remote: Total 8785 (delta 0), reused 0 (delta 0), pack-reused 8785
Receiving objects: 100% (8785/8785), 203.16 MiB | 24.16 MiB/s, done.
Resolving deltas: 100% (4942/4942), done.
Checking connectivity... done.

Go to the models / tutorials / image / imagenet directory:

cd models/tutorials/image/imagenet

This directory contains a classify_image.py file that TensorFlow uses to recognize the image. This program downloads a trained model from tensorflow.org on its first run. Downloading this model requires you to have 200 MB of free disk space.

In this example, we will classify the supplied Panda image. Run this command to start the classifier rendering program:

python classify_image.py

You will see output similar to this:

Output

giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89107)
indri, indris, Indri indri, Indri brevicaudatus (score = 0.00779)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00296)
custard apple (score = 0.00147)
earthstar (score = 0.00117)

You have classified your first pattern using the pattern recognition capabilities of TensorFlow.

If you want to use a different image, you can do so by adding the – image_file argument to your python3 classify_image.py command. You can specify the absolute path to the image file as an argument.

Output

You have installed TensorFlow in a Python virtual environment and confirmed that TensorFlow works by running a couple of examples. You now have the tools that make it possible for you to explore additional topics, including Convolutional Neural Networks and Word Embeddings.

TensorFlow’s Programmer’s Guide is a great resource and reference for developing TensorFlow. You can also explore Kaggle, a competitive landscape for practical application of machine learning concepts that power you against the other juj machine learning, data science and statistics enthusiasts. They have an excellent wiki where you can see and share solutions, some of which are at the forefront of statistical and machine learning.

Sidebar