Home » Uncategorized

Tensorflow Tutorial : Part 2 – Getting Started

In this multi-part series, we will explore how to get started with tensorflow. This tensorflow tutorial will lay a solid foundation to this popular tool that everyone seems to be talking about. The second part is a tensorflow tutorial on getting started, installing and building a small use case.

This post is the second part of the multi-part series on a complete tensorflow tutorial –

  • Part 1: Introduction
  • Part 2: Getting Started
  • Part 3: Building the first model

Tensorflow Tutorial : Goals

tensorflow tutorial

  • Installing Tensorflow
  • Verifying Tensorflow Installion
  • Before Building your first tensorflow model

Installing Tensorflow

If you have tensorflow already installed, you can just skip to the next section.

1. Operating System

Different operating systems have different means to install tensorflow. You can go through the documentation for more details. I will just be discussing what is essential to get started.

  • Click the Install tab

tensorflow getting started

Try to follow the most basic structures with the best practices already mentioned in the documentation.

2. GPU

Installing Tensorflow with GPU requires you to have NVIDIA GPU. AMD video cards are not supported with tensorflow. NVIDIA uses low level GPU computing system called CUDA. It is an NVIDIA proprietary software.

One can go the OpenCL way with AMD but as of now it won’t work with tensorflow.

Also, all NVIDIA devices are not supported. Here is a list from the NVIDIA documentation listing the supported GPUs.

tensorflow tutorial - cuda

3. Environment

There are three environments you can leverage to setup tensorflow –

  1. Direct – This is just a typical way of installing any software library. It directly interacts with the installed operating system and installs tensorflow like any other library. pip is the preferred way of direct installation
  2. Virtual environment – To all who are unaware of the advantages of a python virtual environment, a virtualenv is like a parallel python installation apart from the default python that is already installed in our system. Installing libraries in a virtual environment keeps the libraries separate and you will never have a clash of compatibility with the other libs that are installed directly. If anything goes wrong, you can just fire up a new virtual environment and start afresh
  3. Docker container – This is another way of installing tensorflow. Think of a typical VMware image of a guest operating system to be a docker container on steroids. Docker can be used to start containers that have different environments at the operating system, thereby allowing you to have an environment completely separate to what you have on your host machine. All details are clearly labelled out in the tensorflow documentation
4. Python Version

The 2.7 version from Python 2 and 3.3 or later from Python 3 are supported. This holds true for all operating systems.

As of now, Windows only supports version 3.5. Python 2 with windows is a combination that is not supported

Verifying the Installation

Once tensorflow is installed, irrespective of the operating system, environment or the the python version, you should run the following script to verify tensorflow is up and running.

# import TensorFlow 
import tensorflow as tf
sess = tf.Session()
# Verify we can print a string
hello = tf.constant("Hello UNP from TensorFlow")
# Perform some simple math
a = tf.constant(20)
b = tf.constant(22)
print('a + b = {0}'.format(sess.run(a + b)))

Once this code runs and prints the output successfully, congratulations! You have successfully installed tensorflow. Let’s move to the next section to build our first application

Before Building your first tensorflow model


The following are the three types of tensors we need to learn before getting started.

Type Description
Constant Constant values
Variable Values adjusted in the graph
Placeholder Used to pass data in the graph

Before diving into the hands on, I just want to introduce a few more terms in the tensorflow terminologies along with their meanings.

tensorflow tutorial - tensor types

  1. Rank – Dimensionality of a tensor
  2. Shape – Shape of a tensor. Related to rank

The above diagram should help in understanding. Below we have the different data types in supported by Tensorflow.

tensorflow tutorial - getting started

Note: Quantitized values [qint8, qint16 and quint8] are special values for tensorflow that help reduce the size of the data. In fact, Google has gone to the extent of introducing Tensorflow Processing Units (TPUs) to speed up computation by leveraging quantitized values

Prepare the Data

We will quickly generate some data to get started. In this case, we will generate house size to predict house prices. The goal here isn’t to build a sophisticated house price predictor, but to get things moving in tensorflow in the simplest possible way.

We will generate some data using the below python code –

import tensorflow as tf import numpy as np import math import matplotlib.pyplot as plt  #  generation some house sizes between 1000 and 3500 (typical sq ft of house) num_house = 160 np.random.seed(42) house_size = np.random.randint(low=1000, high=3500, size=num_house )  # Generate house prices from house size with a random noise added. np.random.seed(42) house_price = house_size * 100.0 + np.random.randint(low=20000, high=70000, size=num_house)    # Plot generated house and size  plt.plot(house_size, house_price, "bx")  # bx = blue x plt.ylabel("Price") plt.xlabel("Size") plt.show()

This generates the below output [This is a plot of the generated data]

tensorflow tutorial- house price plot

Next, we are going to normalize the data. This helps in bringing the data in the same scale which in turn can lead to faster convergence.

We also split it into train and test data as part of the data science best practices. We will train our model on the training data and test our model on the test data to see how accurate our predictions are.

# you need to normalize values to prevent under/overflows. 
def normalize(array):
return (array - array.mean()) / array.std()
# define number of training samples, 0.7 = 70%.
# We can take the first 70% since the values are randomized
num_train_samples = math.floor(num_house * 0.7)

# define training data
train_house_size = np.asarray(house_size[:num_train_samples])
train_price = np.asanyarray(house_price[:num_train_samples:])
train_house_size_norm = normalize(train_house_size)
train_price_norm = normalize(train_price)

# define test data
test_house_size = np.array(house_size[num_train_samples:])
test_house_price = np.array(house_price[num_train_samples:])
test_house_size_norm = normalize(test_house_size)
test_house_price_norm = normalize(test_house_price)


I hope this sets the expectations for what is about to come. In the next post we will build our first TensorFlow model.

In the next part, we will finally be ready to train our first tensorflow model on house prices. It will give us our first hands on experience with tensorflow!

For original blog, click here