6 min read

[box type=”note” align=”” class=”” width=””]This article is taken from the book Machine Learning with Tensorflow 1.x, written by Quan Hua, Shams Ul Azeem and Saif Ahmed. This book will help tackle common commercial machine learning problems with Google’s TensorFlow 1.x library.[/box]

Today, we shall explore the basics of getting started with TensorFlow, its installation and configuration process.

The proliferation of large public datasets, inexpensive GPUs, and open-minded developer culture has revolutionized machine learning efforts in recent years. Training data, the lifeblood of machine learning, has become widely available and easily consumable in recent years. Computing power has made the required horsepower available to small businesses and even individuals. The current decade is incredibly exciting for data scientists. Some of the top platforms used in the industry include Caffe, Theano, and Torch. While the underlying platforms are actively developed and openly shared, usage is limited largely to machine learning practitioners due to difficult installations, non-obvious configurations, and difficulty with productionizing solutions.

TensorFlow has one of the easiest installations of any platform, bringing machine learning capabilities squarely into the realm of casual tinkerers and novice programmers. Meanwhile, high-performance features, such as—multiGPU support, make the platform exciting for experienced data scientists and industrial use as well. TensorFlow also provides a reimagined process and multiple user-friendly utilities, such as TensorBoard, to manage machine learning efforts. Finally, the platform has significant backing and community support from the world’s largest machine learning powerhouse–Google. All this is before even considering the compelling underlying technical advantages, which we’ll dive into later.

Installing TensorFlow

TensorFlow conveniently offers several types of installation and operates on multiple operating systems. The basic installation is CPU-only, while more advanced installations unleash serious horsepower by pushing calculations onto the graphics card, or even to multiple graphics cards. We recommend starting with a basic CPU installation at first. More complex GPU and CUDA installations will be discussed in Appendix, Advanced Installation. Even with just a basic CPU installation, TensorFlow offers multiple options, which are as follows:

  1. A basic Python pip installation
  2. A segregated Python installation via Virtualenv
  3. A fully segregated container-based installation via Docker

Ubuntu installation

Ubuntu is one of the best Linux distributions for working with Tensorflow. We highly recommend that you use an Ubuntu machine, especially if you want to work with GPU. We will do most of our work on the Ubuntu terminal. We will begin with installing pythonpip and python-dev via the following command:

sudo apt-get install python-pip python-dev

A successful installation will appear as follows:

Ubuntu install

If you find missing packages, you can correct them via the following command:

sudo apt-get update --fix-missing

Then, you can continue the python and pip installation. We are now ready to install TensorFlow. The CPU installation is initiated via the following command:

sudo pip install tensorflow

A successful installation will appear as follows:

Ubuntu installation

macOS installation

If you use Python, you will probably already have the Python package installer, pip. However, if not, you can easily install it using the easy_install pip command. You’ll note that we actually executed sudo easy_install pip—the sudo prefix was required because the installation requires administrative rights.

We will make the fair assumption that you already have the basic package installer, easy_install, available; if not, you can install it from https://pypi.python.org/pypi/setuptools. A successful installation will appear as shown in the following screenshot:

Mac OS installation

Next, we will install the six package:

sudo easy_install --upgrade six

A successful installation will appear as shown in the following screenshot:

Mac OS installation

Surprisingly, those are the only two prerequisites for TensorFlow, and we can now install the core platform. We will use the pip package installer mentioned earlier and install TensorFlow directly from Google’s site. The most recent version at the time of writing this book is v1.3, but you should change this to the latest version you wish to use:

sudo pip install tensorflow

The pip installer will automatically gather all the other required dependencies. You will see each individual download and installation until the software is fully installed. A successful installation will appear as shown in the following screenshot:

Mac OS installation

That’s it! If you were able to get to this point, you can start to train and run your first model. Skip to Chapter 2, Your First Classifier, to train your first model. macOS X users wishing to completely segregate their installation can use a VM instead, as described in the Windows installation.

Windows installation

As we mentioned earlier, TensorFlow with Python 2.7 does not function natively on Windows. In this section, we will guide you through installing TensorFlow with Python 3.5 and set up a VM with Linux if you want to use TensorFlow with Python 2.7.

First, we need to install Python 3.5.x or 3.6.x 64-bit from the following links: https://www.python.org/downloads/release/python-352/ https://www.python.org/downloads/release/python-362/ Make sure that you download the 64-bit version of Python where the name of the installation has amd64, such as python-3.6.2-amd64.exe. The Python 3.6.2 installation looks like this:

Windows Installation

We will select Add Python 3.6 to PATH and click Install Now. The installation process will complete with the following screen:

Set up successful

We will click the Disable path length limit and then click Close to finish the Python installation. Now, let’s open the Windows PowerShell application under the Windows menu. We will install the CPU-only version of Tensorflow with the following command: pip3 install tensorflow.

The result of the installation will look like this:

Windows PowerShell

Congratulations, you can now use TensorFlow on Windows with Python 3.5.x or 3.6.x support. In the next section, we will show you how to set up a VM to use TensorFlow with Python 2.7. However, you can skip to the Test installation section of Chapter 2, Your First Classifier, if you don’t need Python 2.7. Now, we will show you how to set up a VM with Linux to use TensorFlow with Python 2.7. We recommend the free VirtualBox system available at https://www.virtualbox.org/wiki/Downloads. The latest stable version at the time of writing is v5.0.14, available at the following URL: http:/ / download. virtualbox. org/ virtualbox/ 5. 1. 28/ VirtualBox- 5. 1. 28- 117968- Win. exe A successful installation will allow you to run the Oracle VM VirtualBox Manager dashboard, which looks like this:

Virtual board

Testing the installation

In this section, we will use TensorFlow to compute a simple math operation. First, open your terminal on Linux/macOS or Windows PowerShell in Windows. Now, we need to run python to use TensorFlow with the following command:

python

Enter the following program in the Python shell:

import tensorflow as tf

a = tf.constant(1.0)

b = tf.constant(2.0)

c = a + b

sess = tf.Session()

print(sess.run(c))

The result will look like the following screen where 3.0 is printed at the end:

Windows PowerShell

We covered TensorFlow installation on the three major operating systems, so that you are up and running with the platform. Windows users faced an extra challenge, as TensorFlow on Windows only supports Python 3.5.x or Python 3.6.x 64-bit version. However, even Windows users should now be up and running. Further get a detailed understanding of implementing Tensorflow with contextual examples in this post.

If you liked this article, be sure to check out Machine Learning with Tensorflow 1.x which will help you take up any challenge you may face while implementing TensorFlow 1.x in your machine learning environment.

Machine Learning with TensorFlow 1.x

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here