Getting Started with a CPU-Enabled TensorFlow Instance on AWS

by Evheny ShaliovFebruary 23, 2016
The tutorial provides instructions on how to create an AWS instance, configure TensorFlow environment, and start using a set of visualization tools—TensorBoard.

TensorFlow supports deployment to one or more CPUs or GPUs in a single machine. In this tutorial, we focus on installing the CPU version of TensorFlow on Amazon Elastic Compute Cloud.

 

Software requirements

To get started with TensorFlow, your environment requires a certain amount of pre-installed software.

The chosen for the tutorial ami-d93622b8 contains the majority of the necessary software, including Python, pip, and glibc. On your part, you need to install the gcc compiler when following the instructions below.

 

Creating an instance on AWS

  1. Create the AWS instance from ami-d93622b8.

  2. Choose the instance type. For the tutorial, c3.2xlarge with 16 GB of SSD is used.

  3. Configure a security group and other instance details.
  4. Generate (or reuse) a key pair for access to the instance.

Note: When creating the tutorial, three other AMIs—Amazon ECS-Optimized Amazon Linux AMI, CentOS 7 (x86_64) with Updates HVM, and amzn-ami-hvm-2013.09.2.x86_64-s3—were tested. However, TensorFlow installation using these AMIs was unsuccessful.

 

Configuring an environment

  1. Log in to the remote instance via SSH with the default username—ec2-user.
  2. Check Python and pip availability:
    python --version
    pip --version
  3. Install the gcc compiler:
    sudo yum install gcc
  4. Install TensorFlow:
    sudo pip install
  5. Check whether the configured environment is correct in a Python terminal:
    $ python
    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello, TensorFlow!')
    >>> sess = tf.Session()
    >>> print sess.run(hello)
    Hello, TensorFlow!
    >>> a = tf.constant(10)
    >>> b = tf.constant(32)
    >>> print sess.run(a+b)
    42

 

Running a sample

  1. Install Git:
    yum install git -y
  2. Clone the project:
    git clone --recurse-submodules https://github.com/tensorflow/tensorflow
  3. Run the TensorFlow neural net model:
    python tensorflow/tensorflow/models/image/mnist/convolutional.py
    Initialized!
    Epoch 0.00
    Minibatch loss: 12.054, learning rate: 0.010000
    Minibatch error: 90.6%
    Validation error: 84.6%
    Epoch 0.12
    Minibatch loss: 3.285, learning rate: 0.010000
    Minibatch error: 6.2%
    Validation error: 7.0%
    Epoch 0.23
    Minibatch loss: 3.473, learning rate: 0.010000
    Minibatch error: 10.9%
    Validation error: 3.7%
    Epoch 0.35
    Minibatch loss: 3.221, learning rate: 0.010000
    Minibatch error: 4.7%
    Validation error: 3.2%
    Epoch 0.47
    Minibatch loss: 3.193, learning rate: 0.010000
    Minibatch error: 4.7%
    Validation error: 2.7%

 

Using TensorBoard

You may also employ a visualization tool called TensorBoard.

  1. Add /usr/local/bin to PATH via .bashrc:
    PATH=$PATH:$HOME/bin:/usr/local/bin
  2. Copy the TAG file:
    sudo cp -R /home/ec2-user/tensorflow/tensorflow/tensorboard/TAG /usr/local/lib/python2.7/site-packages/tensorflow/tensorboard/TAG
  3. Copy the favicon.ico file into the TensorBoard folder:
    sudo cp favicon.ico /usr/local/lib/python2.7/site-packages/tensorflow/tensorboard/
  4. Update the AWS security group. The inbound TCP port 6006 must be opened.

  5. Run TensorBoard on the server:
    tensorboard --logdir /var/log
  6. Open TensorBoard in a browser:

As a result, you have your CPU version of TensorFlow and TensorBoard available and ready to use on AWS.

For many types of tasks, the GPU version of TensorFlow demonstrates higher performance. At the same time, it requires additional hardware and software.

 

Further reading

 

About the author

Evheny Shaliov is a software developer at Altoros with 5+ years of experience in architecture design and database development. He has strong skills in building microservices-based architectures that use NoSQL data stores. Evheny is a teaching assistant in Belarusian State University of Informatics and Radioelectronics; he’s also studying for a PhD in information security.