Showing posts with label CNN. Show all posts
Showing posts with label CNN. Show all posts

Monday, January 11, 2016

Installing Tensorflow using Virtualenv

Introduction

The TensorFlow Python API currently supports Python 2.7 and Python 3.3+ from source.

This tutorial supports a virtualenv of Tensorflow on Mac OS X (10.10.5) and Ubuntu 15.04.


Pip Installs Packages

pip is a package management system used to install and manage software packages written in Python.

Python comes with easy_install (2004). pip is a later alternative (2008), and introduced the idea of requirements files (pip freeze > requirements.txt) which gives users the ability to easily replicate environments (a de-facto POM file for Maven developers). Reference #4 gives a good breakdown on easy_install vs pip.

We can use easy_install to install pip:
sudo easy_install pip



Virtual Environments

virtualenv allows multiple Python projects that have different (and often conflicting) requirements, to coexist on the same computer.

We can use pip to install virtualenv:
$ sudo pip install --upgrade virtualenv

then create the virtual environment:
virtualenv --system-site-packages ~/tensorflow

and finally, activate it:
~/workspaces/public/skflow$ source ~/tensorflow/bin/activate
(tensorflow)~/workspaces/public/skflow$ 
Notice how the session prompt changes to reflect the virtualenv.

To exit the virtualenv at any time, type:
$ (tensorflow)~/workspaces/public/skflow$ deactivate
$ ~/workspaces/public/skflow$ 



Installing Tensorflow into the Virtual Environment

TensorFlow is a C++ library with a Python layer for configuring its internal graph.

Execute either of the following commands within the virtualenv:
Installing on Mac OS X, CPU only:
$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py2-none-any.whl

for Ubuntu (GPU enabled):
$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl
The official documentation will have the latest URLs and versions for installing Tensorflow.


Installing Skflow (Optional)

Skflow is a simplified interface for TensorFlow.

Within the virtualenv, type:
$ pip install -U scikit-learn
$ pip install git+git://github.com/google/skflow.git
$ pip install -U pandas
The github repo for Skflow has some helpful examples. These can be customized to meet common needs and I have found that they provide a quicker on-ramp to productive use of Tensorflow.


Testing the Installation

Run a simple application to ensure the installation was successful.

Sample Python Program:
import tensorflow as tf

a = tf.constant(6)
b = tf.constant(7)

sess = tf.Session()
print(sess.run(a * b))


Desired Output:
(tensorflow)~/workspaces/public/skflow$ python test_tf.py 
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 8
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 8
42



References

  1. [Sitepoint] Virtual Environments Made Easy
  2. [Official Docs] Python Virtualenvs
  3. [Blog] Introduction to Pip and Virtual Environments
  4. [Official Docs] Pip and easy_install
  5. [Official Docs] Tensorflow Docs
  6. [Github] Skflow