Deep Learning Intro

This is the introduction to deep learning

Let us start with a simple examples

Loading Libraries

In [1]:
# Numerical Computations
import numpy as np

# For Deep Learning 
import keras

# Visualisation 
import matplotlib.pyplot as plt
%matplotlib inline
Using TensorFlow backend.

Problem: A Noisy Function

$$ Z = 2X^2 - 3Y^2 + 5 + \epsilon $$

In [2]:
x = np.arange(start = -1, stop = 1, step = 0.01)
y = np.arange(start = -1, stop = 1, step = 0.01)
In [3]:
len(x), len(y)
Out[3]:
(200, 200)
In [4]:
X,Y = np.meshgrid(x,y)
c = np.ones((200, 200))
e = np.random.rand(200,200)*0.1
In [5]:
Z = 2*X*X - 3*Y*Y + 5*c + e
In [6]:
#!wget http://bit.do/dl-vis
In [7]:
#!mv dl-vis vis.py
In [8]:
#!pip install altair
In [9]:
import vis
In [10]:
vis.plot3d(X,Y,Z)

Deep Learning 101

Step 1: Get the input and output data

In [11]:
input_xy = np.c_[X.reshape(-1), Y.reshape(-1)]
output_z = Z.reshape(-1)
In [12]:
input_xy.shape, output_z.shape
Out[12]:
((40000, 2), (40000,))

Step 2: Create the Transformation & Regression Model

In [15]:
from keras.models import Sequential
from keras.layers import Dense
In [48]:
model = Sequential()
model.add(Dense(10, input_dim =2, activation="relu"))
model.add(Dense(10, activation="relu"))
model.add(Dense(1))
In [49]:
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_16 (Dense)             (None, 10)                30        
_________________________________________________________________
dense_17 (Dense)             (None, 10)                110       
_________________________________________________________________
dense_18 (Dense)             (None, 1)                 11        
=================================================================
Total params: 151
Trainable params: 151
Non-trainable params: 0
_________________________________________________________________

Step 3: Comile the Model - Loss, Optimizer & Fit on the Data

In [41]:
model.compile(loss="mean_squared_error", optimizer="sgd")
In [42]:
%time
output = model.fit(input_xy, output_z, batch_size= 32, 
                   epochs=10, validation_split=0.2, verbose=0)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 9.3 µs

Step 4: Evaluate Model Perfomance

In [43]:
vis.metrics(output.history)
Out[43]:

Step 5: Make Prediction from the model

In [44]:
Z_pred = model.predict(input_xy).reshape(200,200)
In [45]:
vis.plot3d(X,Y,Z_pred)