fbpx

Get Started with TensorFlow in 15 minutes

tensorflow

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
  • anaconda

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:

https://www.python.org/downloads/

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:

pip3 --version

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:

https://www.anaconda.com/distribution/#windows

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.

C

You will be prompted to accept the license terms. Click I Agree.

Agree

Choose the installation directory and click the Install button to start the installation process.

Install

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:

activate pythontensorenviron

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:

python

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:

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

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:

cd pythonenviron

Run the following command to activate the virtual environment:

source bin/activate

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

Install TensorFlow

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:

python tensortest.py

It should print the following line

Hello World

Conclusion

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.

ICC Cricket World Cup 2019 Win Predictions Using AI & ML

At BlobCity we used our AI & ML skills to predict the ICC Cricket World Cup 2019 winners. Only time will tell if we are correct. We announced our predictions on 27 Jun, while India & West Indies Match #34 was in progress and we correctly predicted for India to be the winning team.

So who will win the trophy? Well here are our predictions for Match 34 to 45.

We did use multiple prediction models and in some cases where we got conflicting results, we have both teams equal points assuming a draw match.

We did Tweet the above predictions to give proof of time of prediction. Here is the Tweet: https://twitter.com/BlobCity/status/1144273488045232129

The final predicted points table, leaving out any extra points the team may get due to winning margins, is as below.

So, according to this, India will play England in the first semi-finals and the second semi-finals will happen between Australia and New Zealand. If we do have our predictions right, then Australia and India will win in their respective semi’s and play the final for the ICC Cricket World Cup.

How did we do it?

Watch the video from our Meetup

We used a neural network for predicting the winners. There is no human bias in the equation, just a computer program saying who will win. A simple feedforward NN is used. We tried others but this one specifically happened to give better results than other models we tired.

The above diagram shows the NN used. It is important to note that both the left and right side are input sides. The output is in the centre of the network. The network has 11 + 6 = 17 input nodes and 1 output node.

The output node simply predicts whether Team 1 will be win against Team 2. It is important to note how the input is captured.

Batting Input

The average strike rate of each batsman is computed across sets of 5 overs. The match is split into 10 sets of overs, comprising overs 1-5, 6-10, 11-15 and so on till 46-50. This is done so as the performance of a team towards the end of the match is considered more critical than the performance towards the beginning of the match.

Most matches in World Cup are close as the teams are really good. It is the last few overs that make all the difference and that is why it is important to capture team and player behaviour in these last overs. It is also a good indication of how the players perform under pressure.

We computed a weighted strike rate. This means that if a batsman hit a four or a six towards the end of the inning, we would give them a few extra runs against hitting a four or a six at the beginning of the match.

We took 11 batsman of each team, arranged them in descending order of their strike rate, and then subtracted Team 1 Batsman 1 strike rate from Team 2 Batsman 1 strike rate and then fed the difference into the first node of the batting neural network. We did this so on and so forth for all the 11 batsman in the team.

Bowling Input

Similar to batting we split bowlers into buckets of 5 overs each. We computed the bowling economy of each bowler. A wicket and maiden over towards the end of the match improved their economy, while an extra such as no ball or wide given significantly decreases their bowling economy.

We computed the bowling economy of all bowlers, and took six best bowlers. These six are arranged in increasing order of their economy. The best bowler is on the one with the lowest value of bowling economy. We did Team 1 Bowler 1 economy minus the Team 2 Bowler 1 economy and fed the difference to the first input node, and so on for 6 bowlers in the team. Most teams have only 5 bowlers, but some do have a 6th bowler who is good. This is why we considered 6 bowlers as input. If some team has 7 bowlers, we are ignoring performance of the worst bowler in the team and taking only the top 6 bowlers as input into the NN.

Training

We used performance of these players across Match #1 to Match #33 of the ICC World Cup 2019. The output of the neural network is 1 if Team 1 will win and 0 if Team 2 will win. The NN was trained using the outcome of the first 33 matches and then used to predict Match #34 to Match #45 and the semi-finals and the finals.

The final prediction says that India will win ICC Cricket World Cup 2019.

Updates

Twitter post as proof of time: https://twitter.com/BlobCity/status/1148507778287165445

We were perfect in our predictions of the Top 4 teams: India, New Zealand, Australia and England.

Since the competing teams are slightly different than our predicted semi finals results, our updated predictions are as shown below: