Basics of TensorFlow in Python

In this tutorial, we will learn about the basics of TensorFlow in Python.

TensorFlow and its History

TensorFlow is an end-to-end open-source platform for machine learning. It is made up of 2 words : tensor and flow. Tensor is a multidimensional array and flow is a graph of operations. Internally, TensorFlow implements machine learning algorithms as a graph of operations on the multi-dimensional array.

TensorFlow was developed by Google Brain and it was released under Apache 2.0 license in November 2015.

Why do we really need TensorFlow ? TensorFlow provides easy to build and deploy machine learning models for a newcomer in machine learning. Also, TensorFlow supports the production of machine learning models, anywhere from CPUs, GPUs to edge devices as well as web servers.

Uses of TensorFlow in industry-related work

TensorFlow is used by various big companies in different domains to carry out state-of-art machine learning models. For example, Google is using TensorFlow to better its various products like Gmail or doc, Airbnb is using TensorFlow to classify images and detect objects in their set of photographs, Airbus is using TensorFlow to detect objects from the satellite imagery and make it available to its customers, Paypal uses it to detect fraudulent transactions, Twitter uses it to run tweets, etc.

We can execute the TensorFlow API in any editor after installing it. The best editor to use is Google Colaboratory. The biggest advantage of Google Colaboratory is its easiness to use and deploy the model. We can see the output directly there and its work place is almost similar to Jupyter Notebook. Alsom there is no system requirement for running the Google Colab as it is a cloud based server for running and deploying models.

Start using TensorFlow

We can start using TensorFlow by installing it using pip and then running the following Python code:

# Install TensorFlow
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:

import tensorflow as tf

The code line “import tensorflow as tf”  imports the library installed into the workspace for usage. We can call its functions using the dot operator.

In addition to this, TensorFlow also contains deep learning libraries, for example Convolution Neural Networks, Max Pooling, Dropping Features, etc. which we will be learning in later posts. 

For more details on TensorFlow and the companies using it, visit :


To know more about Google Colaboratory and get hands-on experience of it, please visit :

Google Colaboratory

TensorFlow’s “Hello World” Program

Now let’s move on to write our first program using TensorFlow. Normally, when we learn C++, JAVA or any other programming language, we write our first program by displaying “hello world” on the display screen.

Here, as TensorFlow is a solution for executing Machine Learning Algorithms, so we take a naive dataset and start building a TensorFlow model to demonstrate it.

Here we use the MNIST dataset which has 60000 images of handwritten alphabets from 0 to 9. This dataset is self-inbuilt in the TensorFlow module so we need not download the dataset

So, let’s start:

Step 1 – We import the header file TensorFlow as :

import tensorflow as tf

Step 2 – Load and Prepare the MNIST dataset. Convert the samples from integers to floating point numbers :

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

Step 3 – Build the Keras model by stacking layers. Choose an optimizer and loss function for training –

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')


Step 4 – Train and Evaluate the model, y_train, epochs=5)

model.evaluate(x_test, y_test)

This completes our first TensorFlow program. Don’t worry, if you are unable to understand some of the function. That’s completely fine. Just Google about it and you will know about it better.

More details coming up on 2nd tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *