Introduction to Deep Learning

We will take a very simple tabular example to get started

Take a non-linear function

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

In [7]:
# DL & Numerical Library
import numpy as np
import keras

# Visualisation 
import matplotlib.pyplot as plt
%matplotlib inline
Using TensorFlow backend.
In [10]:
#!wget http://bit.do/vispy -O vis.py
In [12]:
x = np.arange(-1,1,0.01)
y = np.arange(-1,1,0.01)
In [20]:
x.shape, y.shape
Out[20]:
((200,), (200,))
In [21]:
x.min(), x.max()
Out[21]:
(-1.0, 0.9900000000000018)
In [13]:
X, Y = np.meshgrid(x, y)
c = np.ones((200,200))
e = np.random.rand(200,200)*0.1
In [22]:
X.shape
Out[22]:
(200, 200)
In [14]:
Z = 2*X*X - 3*Y*Y + c + e
In [16]:
import vis
In [17]:
vis.plot3d(X,Y,Z)

Lets do Deep Learning

Step #1: Create our Input and Output

In [26]:
X.shape
Out[26]:
(200, 200)
In [27]:
X.reshape(-1).shape
Out[27]:
(40000,)
In [29]:
input_xy = np.c_[X.reshape(-1), Y.reshape(-1)]
output_z = Z.reshape(-1)
In [30]:
input_xy.shape, output_z.shape
Out[30]:
((40000, 2), (40000,))

Step 2: Create the Learning Model

In [49]:
from keras.models import Sequential
from keras.layers import Dense
In [112]:
model = Sequential()
#model.add(Dense(units=10, input_dim=2, activation="linear"))
#model.add(Dense(units=10, input_dim=5, activation="linear"))
model.add(Dense(units=1, input_dim=2))
In [113]:
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_32 (Dense)             (None, 1)                 3         
=================================================================
Total params: 3
Trainable params: 3
Non-trainable params: 0
_________________________________________________________________
In [107]:
for layer in model.layers:
    print(layer.input_shape, layer.output_shape)
(None, 2) (None, 10)
(None, 10) (None, 10)
(None, 10) (None, 1)

Step 3: Compile the Model - Loss, Optimizer and Fit the Model

In [104]:
model.compile(loss="mean_squared_error", optimizer="sgd", metrics=["mse"])
In [109]:
%%time
output = model.fit(input_xy, output_z, epochs=10, shuffle=True, 
                   validation_split=0.2, verbose=0)
CPU times: user 17.5 s, sys: 5.29 s, total: 22.8 s
Wall time: 9.95 s

Step 4: Evaluate the Model

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

Step 5: Make Predictions from the Model

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