Beginner’s Guide to TensorFlow

0
451

Introduction To Beginner’s Guide to TensorFlow

In This Article, We Are Providing an In-Depth Beginner’s Guide to TensorFlow and Its Applications Deep Learning has taken over the AI and automation industry. From self-driving cars to the famous Alexa, tech companies are developing technologies to make our lives easier. Data scientists prefer TensorFlow as it has built-in support for deep learning and neural networks. So it’s easy to design a net, assign parameters, and run the training model (you’ll learn about this in the latter part of the article). For making neural networks, TensorFlow also has a collection of simple, trainable mathematical functions.

In this tutorial, you will learn about some important concepts and features in TensorFlow 2.0. Beginner’s Guide to TensorFlow will also provide steps on how to use TensorFlow on a dataset.

Applications of TensorFlow

What started off as simple handwriting recognition project has now grown into a rich platform for deep learning. Google’s TensorFlow implements classification, perception and prediction concepts using neural networks in the following domains –

  1. Speech Recognition

    “OK Google”, “Hey Siri”, “Alexa…” has truly made using technology more simplified and useful. Voice recognition is one of the most well-known uses of TensorFlow. It uses neural networks for language understanding and speech-to-text conversions.
  2. Time Series Data Analysis

    Analyzing and making predictions on real-time data can be a challenge. TensorFlow uses Time Series algorithms for analyzing time series data in order to extract meaningful statistics. For example, it can be used to predict the stock market.
  3. Text-based Applications

    This includes applications like sentiment analysis on social media and fraud news detection. Convolutional neural networks have been set up for these purposes.
  4. Image Recognition

    Deep learning in Image recognition finds tremendous uses. Handwriting recognition, Vehicle license plate recognition, Tumour detection in healthcare are some use cases just on the tip of the iceberg. TensorFlow helps identify and classify arbitrary objects within an image, thus improving its performance and success rate. We will be implementing an image recognition project in this tutorial.

Installing TensorFlow

This is the First Stage of Beginner’s Guide to TensorFlow. In order to implement the project we have in store, you will need to download TensorFlow in Python. Some of the most common ways and latest instructions to install TensorFlow using virtualenv, pip, Docker. You will find this in the TensorFlow Installation Page. You can also install TensorFlow using Conda if you use Windows.

Once you’ve installed TensorFlow in your Python directory, you can import it using

import tensorflow as tf

Note – tf is a naming convention used widely in the data science domain. It ensures consistency in the implementation of deep learning projects within the vast TensorFlow community.


Widget not in any sidebars

Tensor

A tensor is a mathematical object represented as arrays of one or mostly multi-dimensions. For example, in image detection, pictures are only made two dimensions. However, to add information about colour to the model we make use of a 3D tensor. The layers of each colour act as the third dimension.

Source – Slide Share

Tensor Rank

Tensor rank is nothing but the dimension of an array or vector that makes up a tensor. A tensor can also be of zero rank, making it a scalar (only weight, no direction). The following image will make ranks in tensors easy to understand.

Tensor Data Type

Along with rank, tensors also have their own data types. The following is a list of the data type: 

Any type of data for your model can be stored using Tensors. They represent all the data in any TensorFlow program.

Graphs in TensorFlow

This is the Second Stage of Beginner’s Guide to TensorFlow. TensorFlow’s mechanism is strictly based on the execution of something called a data flow graph. This graph is nothing but a network of nodes. Each node is represented via a mathematical operation or a convoluted multi-variable equation. The tensors that we discussed above are an edge in this graph.

Thus graphs are made up of the following two components:

  • Nodes: Blocks or units of mathematical computation.
  • Edges: Multi-dimensional arrays, known as tensors. They represent the data consumed or produced by an operation.

Widget not in any sidebars

Computational graphs as a concept was undoubtedly a big breakthrough to simplify the creation of machine learning models. Tensors and nodes are programmable in Python, making them easy to implement. The algorithms behind each mathematical computation are written using C++ binaries which enhances performance multi-fold.

Like a typical workflow diagram, these graphs show how data moves through a machine learning training model. The output of each operation is a tensor. These resulting tensors accumulate the next one until the end to achieve the desired product.

Graphs are then executed using Sessions. From TensorFlow 2.0, we no longer use exclusive sessions. Everything is executed in global runtime using a feature called Eager Execution.

TensorBoard

Now that you have implemented your deep learning program, how do you view and evaluate its performance? TensorBoard allows you to track the progress of networks and compute metrics as they change over time. Charts are automatically produced that show accuracy or loss. TensorBoard acts like a performance dashboard that helps optimize model execution and improve its performance. It can also be used to view the data flow graph. This is a tool integrated with TensorFlow 2.0.

Implementation of TensorFlow in Image Classification

  1. We first import the necessary libraries. We import Keras too here along with TensorFlow since Keras makes training and testing models in Python very simple.
  2. We also load the dataset for Fashion apparel (Fashion MNIST) available freely in Keras datasets.


3. When we load this dataset, we get four NumPy arrays: train_images, train_labels, test_images, test_labels. It’s good if you have a little knowledge of how classification works before implementing this project. We train our model on some data and then test the model on a sample. The images in the dataset are nothing but NumPy arrays of 28×28 dimensions with values of a pixel range (0,255).

There are 60,000 images in the training dataset and 10,000 available for testing.

Labels range from 0-9 and follow this chart.

4. Let’s view the first few images in the training dataset.

4. We need to scale the images between 0 and 1 in order to feed them to the neural network. Perform division by 255 for this.

5. We then verify if our training data is in the correct format for model making.

6. Now we need to use an artificial neural network to make the deep learning model. For that, first, we configure the layers. Layers in a deep learning model are nothing but the extractions of the represented data. We shall stack up the layers here into a Sequential model.

tf.keras.layers.Flatten, transforms the format of images from 28 x 28 to a 1-D array of 784 pixels.
tf.keras.layers.Dense is used to build a network of fully connected neural layers with 128 neurons. They return a score for whether the image belongs to one of the ten labels.

7. Compiling the model to perform efficiently and to minimize errors can be accomplished with the following compile() function.

We fit our model using the training dataset. We have compiled the modelling function in such a way that it returns accuracy for each training run (epoch which we have taken as 10, so 10 runs). At the final epoch, the model returns an accuracy of 99%!

8. In the final block we see that we are evaluating our model using the test data. Here, we see that it returns a score of 100% which means that the model perfectly fits our test data. Don’t worry if you don’t see the same results. Since it is a preexisting dataset, overfitting and underfitting are bound to happen.

TensorFlow 2.0 Features

Now we discuss the features and enhancements TensorFlow 2.0 has to offer –

  1. Eager Execution
    Eager Execution allows users to analyze and modify each graph operation node individually. This allows for debugging the code line-by-line in Google Colab or Jupyter notebook. This feature enables the user-friendly and convenient analysis and evaluation of dataflow graphs.
  2. Anywhere Deployment

    TensorFlow 2.0 features model deployment in production using any platform. The distributed strategy feature allows you to build your model once, then decide how you want to use it over multiple GPUs or even TPUs (Tensor Processing Unit).
  3. Keras API

    Keras is an open-source neural network library built for deep learning. Keras has been combined with TensorFlow so you can access TensorFlow’s features whenever you want. The syntax of Keras is very much like Python as it is intuitive and easy to implement.

Summary

Through this guide on TensorFlow we have covered –

  • When and where TensorFlow is applied
  • Some important components of TensorFlow – Tensors, Datagraphs etc.
  • Installation of TensorFlow
  • Implemented an Image recognition project using TensorFlow
  • Features of the very new TensorFlow 2.0

So, go ahead and dip your feet in the waters of Deep Learning! Fireblaze AI School hopes this guide has inspired you to learn the mechanisms behind famous deep learning models and how neural networks are implemented using TensorFlow.

LEAVE A REPLY

Please enter your comment!
Please enter your name here