Getting Started with Tensorflow v1

Why TensorFlow?


TensorFlow was created to ease the development of (deep) neural networks.  One of the advantages of the TensorFlow library is the ability to develop networks that can train on the GPU.  Deep neural networks are computationally expensive and can take several days to weeks (or even months!) to train on a CPU.  Training on a GPU can speed up training by 10-100 times.


Currently, there are a few high level libraries available for building neural networks.  TensorFlow generally has a good balance of easy-of-use versus customizability compared to other such libraries.  In our opinion, this makes TensorFlow a good choice for both serious beginners and experienced data scientists.

What types of problems are suitable for TensorFlow?


Most people use TensorFlow to create neural networks (NNs).  Neural networks are a class of sophisticated and powerful machine learning techniques that can find novel correlations in data to make accurate predictions.  Although the types of problems that can be solved continue to grow, some common applications are found in object recognition and classification of photos and videos, noise reduction, medical diagnoses, decision making in games, speech translation, detecting credit card fraud and so on.


What are neural networks?


Early attempts of neuroscientists modeling the brain originally developed NNs and their architecture is determined by the (artificial) neurons and the arrangement of their connections. The arrangements are often organized into layers.   Several types of NNs can be created can be created with TensorFlow and are typically referred in name by architecture. Some examples of these are:

-Multi-Layer Perception (MLP), which has a single input layer, one (or more) hidden layer(s) and one output layer


-Autoencoder, which replaces the hidden layer of a MLP with a compression layer


- Convolutional Neural Network – a complicated neural network that has several hidden layers of convolutional filters and convolutional maps.


Deep neural networks, or deep learning, refer to neural networks with many layers.  The advantage here is that more layers tend to result in finding correlations in the data at higher levels of abstraction.  For example, the transition from learning to form edges from the numerical information in a digital image, to using those edges to represent eyes and ears and, finally, to using representations of eye and ears to build a face would represent three levels of abstraction for, say, a facial recognition algroithm.


Getting started with Tensorflow


Installation


Depending on your operating system (OS), TensorFlow installation will be different.  Currently, TensorFlow is supported on Ubuntu, MacOS and Windows.  It may be possible to install on other OS, and so this guide may serve helpful in those cases.


TensorFlow may be installed using CPU support or GPU support.  Although TensorFlow can run considerably faster with GPU support, we recommend beginning with CPU support as the installation (and use) of the GPU version adds an extra layer of complication.  So for now we are providing a guide to install the CPU version.  We plan to include instructions for the GPU version at a later date.


MacOS


The first step is to make sure python is installed on your machine. First we can check that python is installed by opening a terminal window (shell) and typing this command:


python --version


If python is installed, the version number should appear.  The very first number will tell you if you are using python 2 or python 3.  TensorFlow is currently compatible with either  python version 2.7 or higher on MacOS.

If python is not installed, please follow the guidelines on

https://wiki.python.org/moin/BeginnersGuide/Download



A second prerequisite for installing TensorFlow is pip.  Pip is an installation and software management system for python.  Most likely pip was installed during the installation of python.  To confirm, open a terminal window (shell) and type one of the following commands:

$ pip -V  # for Python 2.7

$ pip3 -V # for Python 3.x

For the installation of TensorFlow, it is recommened that pip version 8.1 or higher is installed.  If this is not the case, try the following commands:

$ sudo easy_install --upgrade pip

$ sudo easy_install --upgrade six


Installing TensorFlow


There are several options to install TensorFlow on MacOS.  These include pip, virtualenv,  docker, and directly from the source.  


The recommended installation is with virtualenv.  Using this method isolates your development with Tensorflow from other python development environments and ensures that different dependencies do not interfere with each other.  Furthermore, this circumvents possible permission requirements that you may not have access to if you are using a shared machine.


To install using virtualenv:


  1. Open a terminal window (shell).

  2. Check if virtualenv is already installed on your machine by typing

virtualenv --version


If it has been already installed, the terminal will output the current version    such as:


13.1.2


If  a  command not found error occurs  then you can install virtualenv using:

$pip install --upgrade virtualenv #for Python 2.7

    $pip3 install --upgrade virtualenv #for Python 3.x


The pip command was most likely

  1. Now that virtualenv is installed, we create a virtualenv environment using one of the following commands (depending on which version of python is installed)

$ virtualenv --system-site-packages mydir # for Python 2.7  

$ virtualenv --system-site-packages -p python3 mydir # for Python 3.x

here mydir can be any name you choose to call the new directory that will store the created virtualenv environment


  1. Next, we activate the environment using one of the following:

$ source ~/mydir/bin/activate      # If using bash, sh, ksh, or zsh

$ source ~/mydir/bin/activate.csh  # If using csh or tcsh

Upon activation the command prompt will change to   


(mydir)$


  1. Next ensure that pip is up to date using:

(mydir)$  sudo -H pip install -U pip

or:

(mydir)$ easy_install -U pip

  1. Finally we install TensorFlow in our virtualenv using one of the following:

(mydir)$ pip install --upgrade tensorflow     # for Python 2.7  (mydir)$ pip3 install --upgrade tensorflow    # for Python 3.x

  1. If the above failed try the command found in Alternative installation Commands


Finally, you are done using TensorFlow, you may deactivate the environment by invoking the deactivate function as follows:

(mydir)$ deactivate



To install using native pip:


  1. We install TensorFlow using one of the following:

(mydir)$ pip install --upgrade tensorflow     # for Python 2.7  (mydir)$ pip3 install --upgrade tensorflow    # for Python 3.x

  1. If the above failed try the command found in Alternative installation Commands


Alterative Installation Commands

If the installation of TensorFlow failed using the above procedure, try one of the following:

$ pip install --upgrade tfBinaryURL   # Python 2.7  

$ pip3 install --upgrade tfBinaryURL  # Python 3.x

where tfBinaryURL  refers to  one of the following urls:


For Python 2.7

https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.4.1-py2-none-any.whl

For Python 3.4, 3.5, or 3.6

https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.4.1-py3-none-any.whl


To uninstall TensorFlow, issue one of following commands:

$ pip uninstall tensorflow

$ pip3 uninstall tensorflow




Ubuntu


Python is automatically installed on Ubuntu. First we can check which python version is installed by opening a terminal window (shell) and typing this command:


python --version


If python is installed, the version number should appear.  The very first number will tell you if you are using python 2 or python 3.  TensorFlow is currently compatible with python version 2.7 or higher on Ubuntu.


If python is not installed, please follow the guidelines on

https://wiki.python.org/moin/BeginnersGuide/Download



A second prerequisite for installing TensorFlow is pip.  Pip is an installation and software management system for python.  Most likely pip was installed during the installation of python.  To confirm, open a terminal window (shell) and type one of the following commands:

$ pip -V  # for Python 2.7

$ pip3 -V # for Python 3.x

For the installation of TensorFlow, it is recommened that pip version 8.1 or higher is installed.  If this is not the case, try the following commands:

$ sudo apt-get install python-pip python-dev   # for Python 2.7     $ sudo apt-get install python3-pip python3-dev # for Python 3.x


Installing TensorFlow


There are several options to install TensorFlow on MacOS.  These include pip, virtualenv,  docker, and directly from the source.  


The recommended installation is with virtualenv.  Using this method isolates your development with Tensorflow from other python development environments and ensures that different dependencies do not interfere with each other.  Furthermore, this circumvents possible permission requirements that you may not have access to if you are using a shared machine.


To install using virtualenv:


  1. Open a terminal window (shell).

  2. Check if virtualenv is already installed on your machine by typing

virtualenv --version


If it has been already installed, the terminal will output the current version    such as:


13.1.2


If a command not found error occurs  then you can install virtualenv using:

$ sudo apt-get install python-virtualenv # for Python 2.7     $ sudo apt-get install python-virtualenv # for Python 3.n


  1. Now that virtualenv is installed, we create a virtualenv environment using one of the following commands (depending on which version of python is installed)

$ virtualenv --system-site-packages mydir # for Python 2.7  

$ virtualenv --system-site-packages -p python3 mydir # for Python 3.x

here mydir can be any name you choose to call the new directory that will store the created virtualenv environment


  1. Next, we activate the environment using one of the following:

$ source ~/mydir/bin/activate      # If using bash, sh, ksh, or zsh

$ source ~/mydir/bin/activate.csh  # If using csh or tcsh

Upon activation the command prompt will change to   


(mydir)$


  1. Next ensure that pip is up to date using:

(mydir)$ easy_install -U pip

  1. Finally we install TensorFlow in our virtualenv using one of the following:

(mydir)$ pip install --upgrade tensorflow     # for Python 2.7  (mydir)$ pip3 install --upgrade tensorflow    # for Python 3.x

  1. If the above failed try the command found in Alternative installation Commands below


Finally, you are done using TensorFlow, you may deactivate the environment by invoking the deactivate function as follows:

(mydir)$ deactivate


To install using native pip:


  1. We install TensorFlow using one of the following:


$ sudo apt-get install python-pip python-dev   # for Python 2.7

$ sudo apt-get install python3-pip python3-dev # for Python 3.x


  1. If the above failed try the command found in Alternative installation Commands


Alterative Installation Commands

If the installation of TensorFlow failed using the above procedure, try one of the following:

$ sudo pip install --upgrade tfBinaryURL   # Python 2.7  

$ sudo pip3 install --upgrade tfBinaryURL  # Python 3.x

where tfBinaryURL  refers to  one of the following urls:


For Python 2.7

https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.1-cp27-none-linux_x86_64.whl


For Python 3.4, 3.5, or 3.6

https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.1-cp34-cp34m-linux_x86_64.whl


To uninstall TensorFlow, issue one of following commands:

$ rm -r mydir


Windows


Installing TensorFlow


Currently TensorFlow is only supported on windows using Python 3.  To check which version is installed on your machine, open a terminal and type

C:\> python --version



If python 3 is not installed, please install   one of the following:



Next to install TensorFlow using native pip, enter the following:

C:\> pip3 install --upgrade tensorflow


It is also possible to install using Anaconda


Validating Tensorflow Installation

First start the terminal. If you installed using virtualenv, then activate the environment.  Next we will run a quick TensorFlow program

Invoke python from your shell as follows:

$ python

Enter the following short program inside the python interactive shell:

# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing TensorFlow programs:

Hello, TensorFlow!



The above contents were modified from https://www.tensorflow.org/install/


In accordance with the Creative Commons Attribution 3.0 License


Comments

There are no comments yet.