What is TensorFlow?
TensorFlow is a library developed by Google for performing deep learning tasks. The library provides users with an easy to use interface and modern functionalities good for deep learning tasks. TensorFlow is commonly used for image recognition and text classification amongst other uses. TensorFlow is a great framework for development of deep learning frameworks like neural networks.
TensorFlow is used by both researchers and developers for development of artificial intelligence models. TensorFlow was first announced publicly in 2015 and the first stable version of TensorFlow was released in 2017. TensorFlow is open sourced under the Apache open source license. You can modify its source code and distribute the modifications for a fee and Google will not require you to pay them anything.
Installing TensorFlow on Windows
TensorFlow comes in two versions, the CPU-supported and the GPU-supported versions. It is up to you to choose the version that you need to install. The CPU-supported version is good for simple machine learning tasks while the GPU-supported version is good for complex and heavy tasks.
TensorFlow Installation Methods
There are two ways to install TensorFlow on Windows:
Pip provides an easy and faster way of installing Python. Pip is a Python package manager that you can use to install various Python libraries, including TensorFlow. The pip tool comes built-in with Python, meaning that if you have installed Python on your computer, you already have pip. You can use the pip package manager to install pip and its dependencies.
Anaconda is another way of installing TensorFlow. However, this one is not shipped with Python like pip. You have to download and set it up separately. With Anaconda, you setup a different Python environment, different from the one that is already installed on your computer. This means that the libraries installed in one environment will not affect the libraries installed in the other environments.
Installing TensorFlow with pip
If your computer has Python installed, you already have pip. If not, install Python and get pip. You can download Python from its official website found at:
Once you have installed Python, you can check the version of pip running on your computer by running the following command on the terminal of your operating system:
We have installed Python 3.X, hence, we will be using the command pip3. For Python 2.7, only pip was used as the command.
Now that pip is ready, it is time for you to install TensorFlow. The installation can be done with a simple command from the terminal of the operating system. Just run the command prompt of your operating system and run the following command on it:
pip3 install --upgrade tensorflow
The GPU-supported version has many requirements for libraries and drivers, including NVIDIA GPU drivers, CUDA Toolkit: CUDA 9.0, cuDNN SDK and TensorRT.
To install TensorFlow with GPU support, run the following command:
pip3 install tensorflow-gpu
The installation takes some time, so be patient 🙂
Installing TensorFlow with Anaconda
Unlike pip, Python doesn’t come with Anaconda, meaning that you have to install it separately. You can download Anaconda from the following URL:
The installation of Anaconda is easy. You simply have to double click the downloaded package and the installation will begin immediately. More instructions about its installation can be found online.
After double-clicking the downloaded package, a screen will popup. Just click Next to continue with the installation.
You will be prompted to accept the license terms. Click I Agree.
Choose the installation directory and click the Install button to start the installation process.
In the next windows, click the Next and Finish buttons to complete the installation. You will have anaconda installed on your computer.
Anaconda provides us with the conda package that we can use for installation of libraries.
To start the Anaconda prompt, click Start, choose All Programs, choose Anaconda… then select Command Prompt.
We will then be running our commands on the Anaconda prompt. To get started we will create a Python environment. Remember what we said earlier, that Anaconda allows us to create a separate Python environment with its own libraries and packages.
We will create a virtual environment and give it the name pythontensorenviron. We will use the conda create command as shown below:
conda create -n pythontensorenviron
Type the above command then hit the return key. Type “y” and hit the return key to allow the process to continue. The environment will be created successfully.
For us to be able to use the environment that we have just created, we need to activate it. Run the following command:
Next, we need to install TensorFlow in our active environment. We will use the conda package for this. Just run the following command:
conda install tensorflow
You will be presented with a list of other packages that should be installed together with TensorFlow. Just type “y” and hit the return key to allow for installation of these packages. The installation should then run to the end.
Verifying the Installation
We now need to check whether TensorFlow was installed successfully or not. We will import TensorFlow and write the Hello World example.
To import TensorFlow in Python, we use the import statement.
Still on the same command prompt, type python and hit the return key:
This should take you to the Python terminal. Import the TensorFlow library into your workspace by running the following command:
import tensorflow as tf
The command should not return anything. However, if the installation was not successful, it will return an error.
Now, let us write a piece of code to print Hello World. Here is the code:
import tensorflow as tf hello_world = tf.constant('Hello World') with tf.Session() as ses: output = ses.run(hello_world) print(output)
In the above code, we began by importing the TensorFlow library. Anytime we need to refer to TensorFlow, we will use the object tf. Next, we have created a TensorFlow object and given it the name hello_world. This object has been assigned the message “Hello World”.
Next, we have started a new session by calling the Session() function of TensorFlow. The constant hello_world has then been run in the session and its result assigned to the constant named output. The values of the constant have then been printed out, which should return Hello World.
Installing TensorFlow on Mac
In case you are a Mac user, you can follow instructions in this section to get started with TensorFlow on Mac.
To start, ensure that you have Python already installed on your computer. You also need to have Virtualenv installed.
We begin by installing the Homebrew package. Just run the following command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Next, add the global path by modifying or adding the line given below to .bash_profile or .zshrc file:
Next, run the following commands one by one:
brew update brew install python sudo pip3 install -U virtualenv
Creating a Virtual Environment
We will create a new virtual environment and give it the name pythonenviron.
Choose a python interpreter then run the following command:
virtualenv --system-site-packages -p python3 ./pythonenviron
Change directory to pythonenviron:
Run the following command to activate the virtual environment:
Your shell should now be prefixed with pythonenviron.
Any packages that we install in the virtual environment will not affect the setup of the host system. Run the following command to upgrade pip:
pip install --upgrade pip
You can then run the following command to install TensorFlow:
pip install tensorflow
This will install TensorFlow on your machine.
Verifying the Installation
To verify whether TensorFlow was installed successfully or not, create a new folder and give it the name tflow.
Inside this folder, create a new file and give it the name tensortest.py.
Add the following code to the file:
import os import tensorflow as tf os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' hello_world = tf.constant('Hello World') ses = tf.Session() print(ses.run(hello_world))
Notice that we have imported two libraries, os and TensorFlow. If we don’t import the os library, we will get a warning for using the environ function. To avoid the warning, we have used the setting
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'.
Finally, you can run the above file on the terminal using the following command:
It should print the following line
TensorFlow is a machine learning library good for development of deep learning models. It is one of the most popular libraries for development of neural network models. TensorFlow is open source under Apache license, meaning that you can modify its source code and distribute it at a fee, without having to pay the repository owner. The simplest way to install TensorFlow is by use of the pip package manager. However, you can also use Anaconda, which will allow you to setup a separate Python environment.