The introduction

TensorFlow is open source machine learning software built by Google to train neural networks. The neural network of TensorFlow is represented as a stateful data flow graph. Each node in the figure represents an operation performed by the neural network on a multidimensional array. These multidimensional arrays are often called “tensors”, hence the name “TensorFlow”.

TensorFlow is a deep learning software system. TensorFlow works well for information retrieval, according to RankBrain, Google’s machine learning ai system. TensorFlow can perform image recognition, such as Google Inception, as well as audio recognition in human languages. It also helps solve other problems that are not specifically machine learning, such as partial differential equations.

The TensorFlow architecture allows deployment on multiple cpus or Gpus on desktops, servers, or mobile devices. There are also extensions for integration with CUDA, Nvidia’s parallel computing platform. It gives users deployed on a GPU direct access to the virtual instruction set and other elements of the GPU, which is necessary for parallel computing tasks.

In this tutorial, you will install the “CPU only” version of TensorFlow. This is ideal for those who want to install and use TensorFlow without relying on Nvidia graphics cards or running critical performance applications.

You can install TensorFlow in the following ways. Each approach has a different use case and development environment:

  • Python and Virtualenv: In this method, you will install TensorFlow and use all the packages needed for TensorFlow in the Python virtual environment. This will isolate your TensorFlow environment from other Python programs on the same machine.

  • Native PIP: In this approach, you install TensorFlow globally on your system. This is recommended for those using TensorFlow on multi-user systems. This installation method does not isolate TensorFlow from the package environment and may interfere with other Python installations or libraries.

  • Docker: Docker is a container runtime environment that completely isolates content in preexisting packages on the system. In this method, you use a Docker container that contains TensorFlow and all of its dependencies. This approach is ideal for incorporating TensorFlow into larger application architectures that already use Docker. But doing so would make the Docker image rather large.

In this tutorial, you will install TensorFlow in the Python virtual environment VirtualEnv. This method isolates the TensorFlow installation and gets it up and running quickly. Once the installation is complete, you will verify the success of the installation by running a simple TensorFlow program, and then use TensorFlow to perform image recognition.

Installation preparation

To begin this tutorial, you need to prepare the following:

  • An Ubuntu 16.04 server with at least 1GB of memory, set up according to the Ubuntu 16.04 Initialization Server Setup Guide, including sudo non-root users and firewalls. You will need a system with at least 1GB of memory to successfully complete the last example in this tutorial.

  • Install Python 3.3 or later and virtualenv. Configure Python and VirtualEnv by following the How to Install Python 3 on Ubuntu 16.04 tutorial.

  • To install Git, you can do this by installing Git on Ubuntu 16.04. You will use it to download a sample repository.

Step 1 – Install TensorFlow

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

First, create a project directory called tF-demo:

$ mkdir ~/tf-demoCopy the code

Navigate to the newly created tF-demo directory:

$ cd ~/tf-demoCopy the code

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

$ python3 -m venv tensorflow-devCopy the code

This will create a new tensorflow-dev directory that will contain all the packages that need to be installed when the environment is activated. It also includes PIP and a separate version of Python.

Activate your virtual environment now:

$ source tensorflow-dev/bin/activateCopy the code

Once activated, you should see the following on your terminal:

(tensorflow-dev)username@hostname:~/tf-demo $Copy the code

TensorFlow can now be installed in a virtual environment.

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

(tensorflow-dev) $ pip3 install --upgrade tensorflowCopy the code

TensorFlow will start installing:

Output Collecting tensorflow Downloading tensorflow- 1.4.0-CP36-CP36m-MACOSX_10_11_x86_64. WHL (39.3MB) 100% | █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ | 35 KB 39.3 MB/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.0 RC3 werkZeug -0.12.2 wheel-0.30.0Copy the code

If you want to shut down the virtual environment at any time, use the following command:

$ deactivateCopy the code

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

Now that you have TensorFlow installed, let’s verify that TensorFlow has been successfully installed.

Step 2 – Verify that the installation is successful

To verify that TensorFlow is successfully installed, we will run a simple program in TensorFlow with non-root permissions. We’ll use the usual beginner’s example “Hello, world!” As a form of validation. Instead of creating a Python file, we will use Python’s Interactive Console to create this program.

To write a program, start the Python interpreter:

(tensorflow-dev) $ pythonCopy the code

You will see the following prompt on the terminal

>>>Copy the code

This is the Prompt for the Python interpreter, which indicates that it is ready for you to start typing some Python statements.

First, enter this line of code to import the TensorFlow package as a local variable tf. Enter the code and press Enter:

>>> import tensorflow as tfCopy the code

Next, add this line of code to set the message “Hello, world! :

>>> hello = tf.constant("Hello, world!" )Copy the code

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

>>> sess = tf.Session()Copy the code

Note: Depending on your environment, you might see the following output:

The Output 2017-06-18 16:22:45. 956946: W tensorflow/core/platform/cpu_feature_guard. Cc: 45] The tensorflow library wasn 't compiled to use SSE4.1 instructions, The largest CPU segments consists of the largest segments. The largest one goes to the largest segment. W tensorflow/core/platform/cpu_feature_guard. Cc: 45] The tensorflow library wasn 't compiled to use SSE4.2 instructions, The largest CPU segments consists of the largest segments. The largest one goes to the largest segment. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, The largest CPU segments consists of the largest segments. The largest ONE goes to the largest segment. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, The largest CPU segments consists of the largest one. The largest one goes to the largest one. 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.Copy the code

This tells you that you have a Instruction set that can be further optimized for better TensorFlow performance. If you see this, you can safely ignore it and proceed with the following steps.

Finally, enter this line of code to print out the result of the Hello TensorFlow session built in the previous line:

>>> print(sess.run(hello))Copy the code

You should see the following output in the console:

Output
Hello, world!Copy the code

This shows that everything is fine, and you can start doing some interesting things with TensorFlow.

Exit the Python interactive console by pressing CTRL+D.

Now let’s familiarize ourselves with TensorFlow using TensorFlow’s image recognition API.

Step 3 — Use TensorFlow for image recognition

Now that you have TensorFlow installed, verify this by running a simple program to see what TensorFlow can do for image recognition.

To classify images, you need to train a model. Then you need to write some code to use the model. To learn more about these concepts, take a look at An Introduction to Machine Learning.

TensorFlow provides a repository of models and examples, including code and a training model for classifying images.

Clone the TensorFlow model repository from GitHub to your project directory using Git:

(tensorflow-dev) $ git clone https://github.com/tensorflow/models.gitCopy the code

When Git checks the repository into a new folder, Models, you’ll see the following output:

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) and 203.16 MiB | 24.16 MiB/s, done. Resolving deltas: 100% (4942/4942), done. Checking connectivity... done.Copy the code

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

(tensorflow-dev) $ cd models/tutorials/image/imagenetCopy the code

This directory contains the Classify_image.py file, which uses TensorFlow to identify images. The program downloads a trained model from tensorflow.org the first time it runs. Downloading the model requires 200MB of free space on your disk.

In this example, we will classify the pre-processed panda images. Execute this command to run the image classifier program:

(tensorflow-dev) $ python classify_image.pyCopy the code

You should see the following output:

Output giant panda, panda, Panda bear, Coon Bear, Ailuropoda melanoleuca (score = 0.89107) Indri, indris, 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)Copy the code

You have classified your first image using TensorFlow’s image recognition capabilities.

If you want to use another image, you can do so by adding the — image_file parameter to python3 Classify_image.py. For this parameter, you will need to pass in the absolute path to the image file.

The last

You’ve installed TensorFlow in your Python virtual environment and verified TensorFlow by running several examples. You now have tools that allow you to explore other projects, including convolutional neural networks and Word Embeddings.

The Developer guide for TensorFlow is a great resource and manual. You can also explore Kaggle, a competitive platform to test out the practical application of machine learning concepts, where you compete with other machine learning, data science, and statistics enthusiasts. They have an excellent Wiki home page where you can see and share some of the solutions, some of which are cutting edge ideas in statistics and machine learning technologies.