Introduction
TensorFlow is a strong, opensource software program library for constructing deep studying functions. This tutorial will information you thru utilizing TensorFlow to construct, prepare, and consider a deep studying algorithm.
Here is what you may be taught:
 The fundamentals of TensorFlow
 Tips on how to use its options when growing deep studying functions
 Tips on how to implement a predictive mannequin with TensorFlow’s Keras API
TensorFlow has change into extremely popular lately as a result of it offers a simpleyetefficient option to construct and deploy fashions, permitting builders to resolve complicated issues shortly and precisely.
About This Tutorial
This tutorial is predicated on a part of our interactive course on TensorFlow, accessible right here.
The tutorial is predicated on the most recent model of the TensorFlow library, TensorFlow 2, an opensource platform developed by Google.
Let’s get began!
What Is TensorFlow?
(We are able to see deep studying in motion on this ultimate plot from the Deep Studying Course Guided Mission, “TimeSequence Forecasting on the S&P 500.” Do not you want you could possibly predict the S&P 500 this precisely?)
TensorFlow is an opensource machine studying platform developed by Google that gives a strong suite of instruments for information scientists and builders to construct, deploy, and prepare machine studying fashions. It was initially launched in 2015 as an early model of the software program, nevertheless it has continued to evolve over time with extra highly effective capabilities.
The TensorFlow library permits builders to create complicated neural networks utilizing a wide range of programming languages, corresponding to Python and JavaScript. Moreover, TensorFlow makes it straightforward to deploy fashions on cellular units or cloud platforms like Google Cloud Platform (GCP) and Amazon Net Providers (AWS).
TensorFlow is mostly used for deep studying functions, corresponding to pure language processing (NLP), picture recognition, textual content classification, object detection, advice programs, and rather more.
What Are Tensors?
A tensor is an array of information that may be processed by TensorFlow. A tensor will be represented as a matrix or a vector. In easy phrases, we are able to consider it as a set of numbers organized into a selected form. Mathematically, a tensor can be utilized to create ndimensional datasets.
A zerodimensional tensor is a scalar, which accommodates a single worth and has no axes. A onedimensional tensor is a vector, which accommodates a listing of values and has one axis. A twodimensional tensor is a matrix that accommodates values saved throughout two axes.
What Is TensorFlow Used for?
TensorFlow is utilized in a wide range of functions, starting from pure language processing (NLP) and picture recognition to predictive analytics and autonomous automobile management. It may be used to coach deep neural networks for object detection and classification, generate suggestions, classify photographs, and construct voicepowered functions.
As well as, TensorFlow is used for forecasting, textbased functions, algorithmic buying and selling, and optimization. It is usually utilized in numerous healthcare functions corresponding to medical prognosis and drug discovery.
All of those use circumstances exhibit the flexibility and energy that TensorFlow gives to information scientists and builders. With its flexibility and scalability, TensorFlow permits the implementation of complicated machine studying duties in a fraction of the time in comparison with conventional strategies.
How Does TensorFlow Work?
On the core of TensorFlow is a dataflow graph, which describes how information strikes via a sequence of operations or transformations. The essential thought behind the dataflow graph is that operations are expressed as nodes, with every node performing a single operation on its inputs. The inputs and outputs of the operations are handed via edges (tensors). This makes it doable to interrupt down complicated computations into smaller, extra manageable chunks.
TensorFlow additionally offers quite a few instruments for establishing and coaching neural networks. One of the crucial standard instruments is the utility tf.keras
, which permits customers to shortly construct and prepare deep studying fashions with out having to jot down code from scratch. It additionally contains highly effective visualization instruments to assist customers perceive the info and mannequin parameters.
TensorFlow can also be extensible, and it may be used with a wide range of programming languages, together with Python, C++, JavaScript, and Go. It additionally has assist for working on GPUs (graphics processing items) for optimum efficiency. With its sturdy set of options, TensorFlow is likely one of the hottest frameworks for deep studying, and it continues to evolve as extra highly effective algorithms are developed.
Utilizing TensorFlow for Constructing Fashions
Now that we perceive what TensorFlow is, let’s check out methods to use it in observe. We’ll stroll via an instance of constructing a regression mannequin with the Keras API in TensorFlow. It will embody defining our mannequin structure, coaching it, and evaluating its efficiency. We’ll observe the next steps. Be aware that we’ll define the important thing parts of those steps, nevertheless, if you wish to go into in depth depth, would suggest try the course on TensorFlow.
Step 1: Loading the required libraries and modules
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
tf.random.set_seed(100)
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
Step 2: Loading the info and performing fundamental information checks
We’ll use the inbuilt mpg
dataset accessible underneath the seaborn
library. We do that with the next code.
df = sns.load_dataset('mpg')
print(df.form)
df.describe(embody='all')
(398, 9)
mpg  cylinders  displacement  horsepower  weight  acceleration  model_year  origin  title  

rely  398.000000  398.000000  398.000000  392.000000  398.000000  398.000000  398.000000  398  398 
distinctive  NaN  NaN  NaN  NaN  NaN  NaN  NaN  3  305 
prime  NaN  NaN  NaN  NaN  NaN  NaN  NaN  usa  ford pinto 
freq  NaN  NaN  NaN  NaN  NaN  NaN  NaN  249  6 
imply  23.514573  5.454774  193.425879  104.469388  2970.424623  15.568090  76.010050  NaN  NaN 
std  7.815984  1.701004  104.269838  38.491160  846.841774  2.757689  3.697627  NaN  NaN 
min  9.000000  3.000000  68.000000  46.000000  1613.000000  8.000000  70.000000  NaN  NaN 
25%  17.500000  4.000000  104.250000  75.000000  2223.750000  13.825000  73.000000  NaN  NaN 
50%  23.000000  4.000000  148.500000  93.500000  2803.500000  15.500000  76.000000  NaN  NaN 
75%  29.000000  8.000000  262.000000  126.000000  3608.000000  17.175000  79.000000  NaN  NaN 
max  46.600000  8.000000  455.000000  230.000000  5140.000000  24.800000  82.000000  NaN  NaN 
If you happen to study the output, it exhibits there are 398 rows (or observations) and 9 columns (or variables). The horsepower
variable has rely of 392, which signifies there are six lacking values within the horsepower
variable.
You may also see three variables, cylinders
, origin
, and model_year
are categorical variables, and we’ll must convert them into numeric options for modeling within the subsequent step.
Additionally, the title
variable has many distinctive values, 305 to be exact; therefore, it is not helpful for modeling function.
Step 3: Knowledge Preprocessing
We’ll carry out the next three preprocessing steps.

Fill the lacking values in
horsepower
variable with its median worth. You’ll be able to impute lacking values with imply additionally, however on this tutorial, we now have used median, as it’s much less susceptible to be influenced by outliers. 
Drop the
title
variable because it would not add any predictive energy to the info. 
Carry out dummy encoding to the variables,
cylinders
,origin
, andmodel_year
for changing them into numeric options.
df['horsepower'].fillna(df['horsepower'].median(), inplace=True)
df = df.drop(['name'], axis=1)
df = pd.get_dummies(df, columns=['cylinders'], drop_first=True, prefix='Cylinder')
df = pd.get_dummies(df, columns=['model_year'], drop_first=True, prefix='12 months')
df = pd.get_dummies(df, columns=['origin'], drop_first=True, prefix='Origin')
df.head()
mpg  displacement  horsepower  weight  acceleration  Cylinder_4  Cylinder_5  Cylinder_6  Cylinder_8  Year_71  …  Year_75  Year_76  Year_77  Year_78  Year_79  Year_80  Year_81  Year_82  Origin_japan  Origin_usa  

0  18.0  307.0  130.0  3504  12.0  0  0  0  1  0  …  0  0  0  0  0  0  0  0  0  1 
1  15.0  350.0  165.0  3693  11.5  0  0  0  1  0  …  0  0  0  0  0  0  0  0  0  1 
2  18.0  318.0  150.0  3436  11.0  0  0  0  1  0  …  0  0  0  0  0  0  0  0  0  1 
3  16.0  304.0  150.0  3433  12.0  0  0  0  1  0  …  0  0  0  0  0  0  0  0  0  1 
4  17.0  302.0  140.0  3449  10.5  0  0  0  1  0  …  0  0  0  0  0  0  0  0  0  1 
5 rows × 23 columns
Step 4: Create Options and Response Variables
The code under performs the next duties:

Creates an object for the goal variable and checklist containing all of the unbiased variables.

Performs normalization of the predictor variables because the items of the variables differ considerably which will affect the modeling course of. To stop this, we normalize the predictors by scaling the values between 0 and 1.
target_variable = ['mpg']
predictors = checklist(set(checklist(df.columns))set(target_variable))
df[predictors] = df[predictors]/df[predictors].max()
df.describe()
mpg  displacement  horsepower  weight  acceleration  Cylinder_4  Cylinder_5  Cylinder_6  Cylinder_8  Year_71  …  Year_75  Year_76  Year_77  Year_78  Year_79  Year_80  Year_81  Year_82  Origin_japan  Origin_usa  

rely  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  …  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000  398.000000 
imply  23.514573  0.425112  0.453496  0.577904  0.627746  0.512563  0.007538  0.211055  0.258794  0.070352  …  0.075377  0.085427  0.070352  0.090452  0.072864  0.072864  0.072864  0.077889  0.198492  0.625628 
std  7.815984  0.229164  0.166185  0.164755  0.111197  0.500471  0.086601  0.408571  0.438523  0.256061  …  0.264331  0.279868  0.256061  0.287190  0.260241  0.260241  0.260241  0.268335  0.399367  0.484569 
min  9.000000  0.149451  0.200000  0.313813  0.322581  0.000000  0.000000  0.000000  0.000000  0.000000  …  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 
25%  17.500000  0.229121  0.330435  0.432636  0.557460  0.000000  0.000000  0.000000  0.000000  0.000000  …  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 
50%  23.000000  0.326374  0.406522  0.545428  0.625000  1.000000  0.000000  0.000000  0.000000  0.000000  …  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  1.000000 
75%  29.000000  0.575824  0.543478  0.701946  0.692540  1.000000  0.000000  0.000000  1.000000  0.000000  …  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  1.000000 
max  46.600000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  …  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000 
8 rows × 23 columns
Step 5: Create the Coaching and Take a look at Datasets
The code under performs the next duties:

Creates an array of unbiased (X) and dependent (y) variables, respectively

Splits the info into coaching and check set
X = df[predictors].values
y = df[target_variable].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=100)
print(X_train.form); print(X_test.form)
(278, 22)
(120, 22)
Step 6: Constructing, Coaching, and Evaluating the Mannequin
Step one right here is to outline the mannequin structure utilizing the Sequential class from Keras API. This performs the next duties:

Instantiates the Sequential mannequin class underneath the Tensorflow’s Keras API

Makes use of
relu
as an activation operateReLU (Rectified Linear Unit) is a extensively used nonlinear activation operate in deep studying fashions. It’s used so as to add nonlinearity to the neural networks by introducing a nonzero gradient when the inputs are better than 0.
Be aware that there are various doable configurations to construct the community. This structure is only one of them, and never a advice. Please be at liberty to alter the mannequin with completely different configurations.
# outline mannequin
mannequin = tf.keras.Sequential()
mannequin.add(tf.keras.layers.Dense(32, input_shape = (X_train.form[1],), activation = 'relu'))
mannequin.add(tf.keras.layers.Dense(16, activation= "relu"))
mannequin.add(tf.keras.layers.Dense(8, activation= "relu"))
mannequin.add(tf.keras.layers.Dense(4, activation= "relu"))
mannequin.add(tf.keras.layers.Dense(1))
Subsequent, we have to compile our mannequin with the suitable loss and optimizer capabilities for our job. The imply squared error is our loss measure, and the “adam” optimizer is our minimization algorithm used to reduce a given loss operate.
The loss operate is Imply Absolute Error (MAE), which is a metric utilized in regression fashions to measure the distinction between predicted values and true values. It’s the common of absolutely the errors throughout all samples, the place every error is outlined as absolutely the distinction between true worth and predicted worth.
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
mannequin.compile(loss = 'mae', metrics = ['mae'], optimizer = optimizer)
Subsequent, we’ll prepare our mannequin by passing in coaching information and setting the variety of epochs to 50, which represents the variety of coaching iterations.
mannequin.match(X_train, y_train, epochs=50)
Epoch 1/50
9/9 [==============================]  1s 2ms/step  loss: 22.8766  mae: 22.8766
Epoch 2/50
9/9 [==============================]  0s 2ms/step  loss: 18.9705  mae: 18.9705
Epoch 3/50
9/9 [==============================]  0s 2ms/step  loss: 10.0245  mae: 10.0245
Epoch 4/50
9/9 [==============================]  0s 2ms/step  loss: 7.5122  mae: 7.5122
Epoch 5/50
9/9 [==============================]  0s 2ms/step  loss: 4.7029  mae: 4.7029
Epoch 6/50
9/9 [==============================]  0s 2ms/step  loss: 3.4516  mae: 3.4516
Epoch 7/50
9/9 [==============================]  0s 2ms/step  loss: 3.2318  mae: 3.2318
Epoch 8/50
9/9 [==============================]  0s 3ms/step  loss: 2.9560  mae: 2.9560
Epoch 9/50
9/9 [==============================]  0s 3ms/step  loss: 2.9059  mae: 2.9059
Epoch 10/50
9/9 [==============================]  0s 2ms/step  loss: 2.7966  mae: 2.7966
Epoch 11/50
9/9 [==============================]  0s 2ms/step  loss: 2.7716  mae: 2.7716
Epoch 12/50
9/9 [==============================]  0s 2ms/step  loss: 2.7951  mae: 2.7951
Epoch 13/50
9/9 [==============================]  0s 2ms/step  loss: 2.9418  mae: 2.9418
Epoch 14/50
9/9 [==============================]  0s 2ms/step  loss: 2.7087  mae: 2.7087
Epoch 15/50
9/9 [==============================]  0s 2ms/step  loss: 2.6385  mae: 2.6385
Epoch 16/50
9/9 [==============================]  0s 2ms/step  loss: 2.7181  mae: 2.7181
Epoch 17/50
9/9 [==============================]  0s 2ms/step  loss: 2.5412  mae: 2.5412
Epoch 18/50
9/9 [==============================]  0s 2ms/step  loss: 2.6168  mae: 2.6168
Epoch 19/50
9/9 [==============================]  0s 3ms/step  loss: 2.5298  mae: 2.5298
Epoch 20/50
9/9 [==============================]  0s 2ms/step  loss: 2.4457  mae: 2.4457
Epoch 21/50
9/9 [==============================]  0s 2ms/step  loss: 2.3907  mae: 2.3907
Epoch 22/50
9/9 [==============================]  0s 2ms/step  loss: 2.4315  mae: 2.4315
Epoch 23/50
9/9 [==============================]  0s 3ms/step  loss: 2.4447  mae: 2.4447
Epoch 24/50
9/9 [==============================]  0s 2ms/step  loss: 2.5072  mae: 2.5072
Epoch 25/50
9/9 [==============================]  0s 2ms/step  loss: 2.5910  mae: 2.5910
Epoch 26/50
9/9 [==============================]  0s 2ms/step  loss: 2.4720  mae: 2.4720
Epoch 27/50
9/9 [==============================]  0s 2ms/step  loss: 2.3334  mae: 2.3334
Epoch 28/50
9/9 [==============================]  0s 2ms/step  loss: 2.3399  mae: 2.3399
Epoch 29/50
9/9 [==============================]  0s 2ms/step  loss: 2.2091  mae: 2.2091
Epoch 30/50
9/9 [==============================]  0s 2ms/step  loss: 2.2084  mae: 2.2084
Epoch 31/50
9/9 [==============================]  0s 2ms/step  loss: 2.2190  mae: 2.2190
Epoch 32/50
9/9 [==============================]  0s 2ms/step  loss: 2.2318  mae: 2.2318
Epoch 33/50
9/9 [==============================]  0s 2ms/step  loss: 2.2458  mae: 2.2458
Epoch 34/50
9/9 [==============================]  0s 2ms/step  loss: 2.2424  mae: 2.2424
Epoch 35/50
9/9 [==============================]  0s 2ms/step  loss: 2.1630  mae: 2.1630
Epoch 36/50
9/9 [==============================]  0s 2ms/step  loss: 2.1858  mae: 2.1858
Epoch 37/50
9/9 [==============================]  0s 3ms/step  loss: 2.2238  mae: 2.2238
Epoch 38/50
9/9 [==============================]  0s 3ms/step  loss: 2.1893  mae: 2.1893
Epoch 39/50
9/9 [==============================]  0s 3ms/step  loss: 2.1103  mae: 2.1103
Epoch 40/50
9/9 [==============================]  0s 2ms/step  loss: 2.0991  mae: 2.0991
Epoch 41/50
9/9 [==============================]  0s 2ms/step  loss: 2.0380  mae: 2.0380
Epoch 42/50
9/9 [==============================]  0s 2ms/step  loss: 2.0410  mae: 2.0410
Epoch 43/50
9/9 [==============================]  0s 2ms/step  loss: 2.1349  mae: 2.1349
Epoch 44/50
9/9 [==============================]  0s 2ms/step  loss: 2.0254  mae: 2.0254
Epoch 45/50
9/9 [==============================]  0s 2ms/step  loss: 2.1620  mae: 2.1620
Epoch 46/50
9/9 [==============================]  0s 2ms/step  loss: 2.0948  mae: 2.0948
Epoch 47/50
9/9 [==============================]  0s 2ms/step  loss: 2.0497  mae: 2.0497
Epoch 48/50
9/9 [==============================]  0s 2ms/step  loss: 1.9978  mae: 1.9978
Epoch 49/50
9/9 [==============================]  0s 2ms/step  loss: 1.9770  mae: 1.9770
Epoch 50/50
9/9 [==============================]  0s 2ms/step  loss: 1.9518  mae: 1.9518
<keras.callbacks.Historical past at 0x7f72fbeea040>
As soon as the mannequin has been educated, we are able to consider its efficiency on the prepare and testing datasets.
print(mannequin.consider(X_train, y_train))
9/9 [==============================]  0s 2ms/step  loss: 1.8617  mae: 1.8617
[1.8616846799850464, 1.8616846799850464]
print(mannequin.consider(X_test, y_test))
4/4 [==============================]  0s 3ms/step  loss: 2.3199  mae: 2.3199
[2.319859266281128, 2.319859266281128]
The imply absoulte error for the prepare and check dataset comes out to be 1.86 and a couple of.32, respectively. The decrease the worth, the higher the mannequin efficiency. Generally it is extra intuitive to judge the regression mannequin efficiency utilizing the metric referred to as as Rsquared.
The Rsquared worth is a measure of how properly a regression mannequin matches the info. It is often known as the coefficient of willpower and is a measure of the energy of the linear relationship between dependent and unbiased variables.
The code chunks under computes the Rsquared worth on the prepare and check datasets.
from sklearn.metrics import r2_score
pred_train = mannequin.predict(X_train)
r2_score(y_train, pred_train)
9/9 [==============================]  0s 2ms/step
0.8904764861187693
pred_test = mannequin.predict(X_test)
r2_score(y_test, pred_test)
4/4 [==============================]  0s 2ms/step
0.8281138794449109
The Rsquared worth for prepare and check datasets comes out to be 0.89 and 0.83, respectively. The Rsquared worth of 1 is ideal, whereas a decrease worth signifies that the mannequin would not clarify the variability of the info. For the reason that Rsquared worth forthe prepare and check datasets is excessive, we are able to conclude that the mannequin we educated is performing fairly properly.
By following this tutorial, you need to now have a greater understanding of methods to use TensorFlow and its numerous parts to construct deep studying fashions.
Buying this information on TensorFlow is a vital element for information scientists and machine studying engineers. By studying the fundamentals of methods to use TensorFlow, you can shortly create subtle deep studying fashions that may resolve realworld issues. With extra observe and studying via interactive programs, you can construct efficient fashions utilizing TensorFlow that may assist to enhance decisionmaking, course of information quicker, and create extra correct predictions.
So, are you in search of indepth understanding of the world of deep studying and develop highly effective fashions with TensorFlow? If the reply is sure, than you need to think about taking our Introduction to TensorFlow course. This course offers a complete introduction to deep studying, ranging from fundamental ideas corresponding to shallow neural networks and progressing as much as extra complicated architectures. By means of interactive workouts culminating in a guided venture, you may get handson expertise working with realworld datasets and making use of fashions developed with TensorFlow.
To be taught extra about associated ideas, please discuss with the next assets: