Beruflich Dokumente
Kultur Dokumente
Introduction to
deep learning
Deep Learning in Python
Retirement Status
…
Deep Learning in Python
Predicted Predicted
Transactions Not Retired Transactions Not Retired
Retired Retired
Interactions
● Neural networks account for interactions really well
● Deep learning uses especially powerful neural networks
● Text
● Images
● Videos
● Audio
● Source code
Deep Learning in Python
Course structure
● First two chapters focus on conceptual knowledge
● Debug and tune deep learning models on conventional
prediction problems
● Lay the foundation for progressing towards modern
applications
● This will pay off in the third and fourth chapters
Deep Learning in Python
In [9]: model.add(Dense(1))
Deep Learning in Python
Number of
Bank Balance Transactions
Retirement Status
…
Deep Learning in Python
Let’s practice!
DEEP LEARNING IN PYTHON
Forward
propagation
Course Title
Forward propagation
Input Hidden Layer
1
# Children 2 5
1 2
Output
9 # Transactions
-1
-1
3 1 1
# Accounts
Deep Learning in Python
Forward propagation
Input Hidden Layer
1
# Children 2 5
2
1 Output
9 # Transactions
-1
-1
3 1 1
# Accounts
Deep Learning in Python
Forward propagation
Input Hidden Layer
1
# Children 2 5
2
1 Output
9 # Transactions
-1
-1
3 1 1
# Accounts
Deep Learning in Python
Forward propagation
Input Hidden Layer
1
# Children 2 5
2
1 Output
9 # Transactions
-1
-1
3 1 1
# Accounts
Course Title
Forward propagation
● Multiply - add process
● Dot product
● Forward propagation for one data point at a time
● Output is the prediction for that data point
Deep Learning in Python
3 -1
1 -1
1
Deep Learning in Python
In [7]: print(hidden_layer_values)
[5, 1]
In [9]: print(output)
9
Let’s practice!
DEEP LEARNING IN PYTHON
Activation
functions
Deep Learning in Python
Activation functions
● Applied to node inputs to produce node output
Deep Learning in Python
3 1 1
Deep Learning in Python
Activation functions
Input Hidden Layer
1 tanh(2+3)
2
2
1 Output
9
-1
-1
3 1
tanh(-2+3)
Deep Learning in Python
Activation functions
In [1]: import numpy as np
In [10]: print(output)
1.2382242525694254
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Deeper networks
Deep Learning in Python
4
2 7
-5 2
55
4
2 7
-5 2
55
4
2 7
-5 2
55
4
2 7
-5 2
55
4
2 7
-5 2
55
55
55
55
55
-5
55
-5
55
-5
55
4
2 7
-5 2
55 0
364
4
2 7
-5 2
55 0 52
Representation learning
● Deep networks internally build representations of pa"erns in
the data
● Partially replace the need for feature engineering
● Subsequent layers build increasingly sophisticated
representations of raw data
Deep Learning in Python
Representation learning
Deep Learning in Python
Deep learning
● Modeler doesn’t need to specify the interactions
● When you train the model, the neural network gets weights
that find the relevant pa"erns to make be"er predictions
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
The need
for optimization
Deep Learning in Python
3 1 1
3 1 1
Loss function
● Aggregates errors in predictions from many data points into
single number
● Measure of model’s predictive performance
Deep Learning in Python
10 20 -10 100
8 3 5 25
6 1 5 25
Loss function
Loss function
We
ight1 h t 2
ei g
W
Deep Learning in Python
Loss function
● Lower loss function value means a be!er model
● Goal: Find the weights that give the lowest value for the loss
function
● Gradient descent
Deep Learning in Python
Gradient descent
● Imagine you are in a pitch dark field
● Want to find the lowest point
● Feel the ground to see how it slopes
● Take a small step downhill
● Repeat until it is uphill in every direction
Deep Learning in Python
Loss(w)
Minimum value
w
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Gradient descent
Deep Learning in Python
Gradient descent
Loss(w)
w
Deep Learning in Python
Gradient descent
● If the slope is positive:
● Going opposite the slope means moving to lower
numbers
● Subtract the slope from the current value
● Too big a step might lead us astray
● Solution: learning rate
● Update each weight by subtracting
learning rate * slope
Deep Learning in Python
● 2 * -4 * 3
● -24
● If learning rate is 0.01, the new weight would be
● 2 - 0.01(-24) = 2.24
Deep Learning in Python
3
1
4
Deep Learning in Python
In [4]: target = 6
In [8]: print(error)
5
Deep Learning in Python
In [10]: gradient
Out[10]: array([30, 40])
In [14]: print(error_updated)
-2.5
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Backpropagation
Deep Learning in Python
Backpropagation
● Allows gradient descent to update all weights in neural network (by
ge!ing gradients for all weights)
● Comes from chain rule of calculus
● Important to understand the process, but you will generally use a
library that implements this
Prediction
Error
Deep Learning in Python
Backpropagation process
● Trying to estimate the slope of the loss function w.r.t each weight
● Do forward propagation to calculate predictions and errors
Deep Learning in Python
4 2 2
-1 3
1
Deep Learning in Python
-1 3
1 1 3
Deep Learning in Python
Backpropagation process
● Go back one layer at a time
● Gradients for weight is product of:
1. Node value feeding into that weight
2. Slope of loss function w.r.t node it feeds into
3. Slope of activation function at the node it feeds into
1
Deep Learning in Python
1
Deep Learning in Python
Backpropagation process
● Need to also keep track of the slopes of the loss function
w.r.t node values
● Slope of node values are the sum of the slopes for all
weights that come out of them
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Backpropagation in
practice
Deep Learning in Python
Backpropagation
ReLU Activation Function
1 Actual Target Value = 10
1 Error = 3
7
2
Backpropagation
0
0 6
1
1 3 18
Deep Learning in Python
Backpropagation
0 Current
Gradient
0 6 Weight Value
1 0 0
1 6
2
2 0
1 3 18
3 18
Deep Learning in Python
Backpropagation: Recap
● Start at some random set of weights
● Use forward propagation to make a prediction
● Use backward propagation to calculate the slope of
the loss function w.r.t each weight
● Multiply that slope by the learning rate, and subtract
from the current weights
● Keep going with that cycle until we get to a flat part
Deep Learning in Python
Let’s practice
DEEP LEARNING IN PYTHON
Creating a
keras model
Deep Learning in Python
Model specification
In [1]: import numpy as np
In [9]: model.add(Dense(1))
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Compiling and
fi!ing a model
Deep Learning in Python
Compiling a model
In [1]: n_cols = predictors.shape[1]
In [5]: model.add(Dense(1))
Fi!ing a model
In [1]: n_cols = predictors.shape[1]
In [5]: model.add(Dense(1))
Let’s practice!
DEEP LEARNING IN PYTHON
Classification
models
Deep Learning in Python
Classification
● ‘categorical_crossentropy’ loss function
● Similar to log loss: Lower is be!er
● Add metrics = [‘accuracy’] to compile step for easy-to-
understand diagnostics
● Output layer has separate node for each possible outcome,
and uses ‘so"max’ activation
Deep Learning in Python
Transforming to categorical
shot_result Outcome 0 Outcome 1
1 0 1
0 1 0
0 1 0
0 1 0
Deep Learning in Python
Classification
In[1]: from keras.utils import to_categorical
Classification
Out[11]:
Epoch 1/10
128069/128069 [==============================] - 4s - loss: 0.7706 - acc: 0.5759
Epoch 2/10
128069/128069 [==============================] - 5s - loss: 0.6656 - acc: 0.6003
Epoch 3/10
128069/128069 [==============================] - 6s - loss: 0.6611 - acc: 0.6094
Epoch 4/10
128069/128069 [==============================] - 7s - loss: 0.6584 - acc: 0.6106
Epoch 5/10
128069/128069 [==============================] - 7s - loss: 0.6561 - acc: 0.6150
Epoch 6/10
128069/128069 [==============================] - 9s - loss: 0.6553 - acc: 0.6158
Epoch 7/10
128069/128069 [==============================] - 9s - loss: 0.6543 - acc: 0.6162
Epoch 8/10
128069/128069 [==============================] - 9s - loss: 0.6538 - acc: 0.6158
Epoch 9/10
128069/128069 [==============================] - 10s - loss: 0.6535 - acc: 0.6157
Epoch 10/10
128069/128069 [==============================] - 10s - loss: 0.6531 - acc: 0.6166
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Using models
Deep Learning in Python
Using models
● Save
● Reload
● Make predictions
Deep Learning in Python
In [2]: model.save('model_file.h5')
Let’s practice!
DEEP LEARNING IN PYTHON
Understanding
model
optimization
Deep Learning in Python
Vanishing gradients
tanh function
Deep Learning in Python
Vanishing gradients
● Occurs when many layers have very small slopes (e.g. due to
being on flat part of tanh curve)
● In deep networks, updates to backprop were close to 0
DEEP LEARNING IN PYTHON
Let’s practice!
DEEP LEARNING IN PYTHON
Model validation
Deep Learning in Python
Model validation
In [1]: model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics=['accuracy'])
Epoch 1/10
89648/89648 [==============================] - 3s - loss: 0.7552 - acc: 0.5775 - val_loss: 0.6969 -
val_acc: 0.5561
Epoch 2/10
89648/89648 [==============================] - 4s - loss: 0.6670 - acc: 0.6004 - val_loss: 0.6580 -
val_acc: 0.6102
...
Epoch 8/10
89648/89648 [==============================] - 5s - loss: 0.6578 - acc: 0.6125 - val_loss: 0.6594 -
val_acc: 0.6037
Epoch 9/10
89648/89648 [==============================] - 5s - loss: 0.6564 - acc: 0.6147 - val_loss: 0.6568 -
val_acc: 0.6110
Epoch 10/10
89648/89648 [==============================] - 5s - loss: 0.6555 - acc: 0.6158 - val_loss: 0.6557 -
val_acc: 0.6126
Deep Learning in Python
Early Stopping
In [3]: from keras.callbacks import EarlyStopping
Let’s practice!
DEEP LEARNING IN PYTHON
Thinking
about model
capacity
Deep Learning in Python
Overfi!ing
Model Capacity
Deep Learning in Python
Sequential experiments
Mean Squared
Hidden Layers Nodes Per Layer Next Step
Error
Increase
1 100 5.4
Capacity
Increase
1 250 4.8
Capacity
Increase
2 250 4.4
Capacity
Decrease
3 250 4.5
Capacity
Let’s practice!
DEEP LEARNING IN PYTHON
Stepping up
to images
Deep Learning in Python
Let’s practice!
DEEP LEARNING IN PYTHON
Final thoughts
Deep Learning in Python
Next steps
● Start with standard prediction problems on tables of
numbers
● Images (with convolutional neural networks) are common
next steps
● keras.io for excellent documentation
● Graphical processing unit (GPU) provides dramatic
speedups in model training times
● Need a CUDA compatible GPU
● For training on using GPUs in the cloud look here:
h!p://bit.ly/2mYQXQb
DEEP LEARNING IN PYTHON
Congratulations!