Deep Learning W6
Deep Learning W6
Machine Learning
Lecture: Recap
Ted Scully
RECAP
Mini-Batch Processing
If your batch size is the same as your training set size then you are using batch GD.
Most of time you don’t just adopt batch GD. We have already outlined the
problem with this option. Too slow for large training sets.
It has been observed in practice that when using a larger batch there is a
degradation in the quality of the model (Keshar et al 2016).
If batch size > 1 and < m then we are using what is called mini-batch gradient
descent.
Typically sizes for mini-batch is 32, 64, 128, 256, 512, 1024.
The advantage of this technique is that we can achieve a relatively high
frequency of weight updates while still retaining the benefit of vectorised
speed up advantages.
RECAP Gradient Descent with Momentum
gradient
gradient
RECAP
Gradient Descent with Momentum
Notice in the code we maintain an EMA for the gradients for both trainable
parameters. It is this value that now becomes core to the update of these
parameters.
Notice if there is a high level of oscillation for one parameter such as bias then
it will smoothen out this value and slow down the rate of update.
In turn this can allow us to use a large learning rate and obtain convergence
faster.
repeat
import tensorflow as tf
t1 = [Link]((4, 5))
t2 = [Link](3, 15, 2)
t3 = [Link]((2,2))
t4 = [Link](shape=[2,2],dtype=tf.float32)
import tensorflow as tf
v = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) A [Link] is a special type of tensor
meant to hold a mutable state such as the
print (v) weights of a neural network.
print ([Link])
[Link](v-2)
print (v)
RECAP
• To determine the gradients of the loss function with respect to some variables, we
call the method gradient.
• The first argument is the “target” for the calculation, i.e. typically the output of
the loss function (or whatever function you are trying to optimize)
• The second argument is the “source”, these are the variables values you can alter
in order to optimize the target loss function.
• The gradient function will return the partial derivatives of the (loss) function with
respect to each of the listed trainable variables.
• We can either update the variables ourselves (using the gradient descent update rule)
or more commonly we can use an existing optimizer to update the gradients for us.
RECAP def predict(x):
# x2 – 20x + 100
firstTerm = x**2
secondTerm = -20.0* x
quadratic = firstTerm + secondTerm +100.0
return quadratic
learning_rate = 0.1
iterations = 50
x = [Link](20.0, tf.float32)
for i in range(iterations):
gradient = [Link](prediction, x)
x.assign_sub(gradient * learning_rate)
print (x)
Deep Learning
Deep Learning
Lecture: Keras – Sequential API
Ted Scully
Keras – Layers
1. The fundamental data structure in Keras is a layer.
2. A layer is a data-processing module that takes as
input one or more tensors and outputs one or
more tensors.
3. Typically, each layer has a current state that is
defined by the layer’s weights, one or several
tensors learned with gradient descent (some layers
are stateless).
4. Different types of layers are defined for different
types of networks and problem types.
a. For example, 2D tensors of shape (samples,
features), are used with densely connected
layers.
b. Sequence data, stored in 3D tensors of shape
(samples, time, features), is typically
processed by recurrent layers, such as an
LSTM layer, or 1D convolution layers
(Conv1D).
c. Image data, stored in 4D tensors, is usually
processed by 2D convolution layers (Conv2D).
Keras – Sequential Model and Dense Layer
1. In Keras, a model, is a way to organize layers.
2. The standard type of model is the Sequential model, a linear stack of layers
3. The most common type of layer in Keras is a Dense layer ([Link]),
which is a fully connected neural network layer.
a) In the code below we first add a fully connected layer of neurons containing 512
neurons each with a ReLu activation function (note the dense layer in this
example assumes a flattened input shape).
b) We then add a fully connected Softmax layer.
c) You can add as many layers as you want to your network.
model = [Link]( [
[Link](shape=(784,)),
[Link](512, activation=[Link]),
[Link](10, activation=[Link])
])
Model Compilation
1. Before training a model, you need to set up the details around the loss function, the
optimizer and the metrics you want to use. In Keras this is done via the compile
method. It receives three arguments:
○ A loss function. This is the cost function that the model will try to minimize. It
can be the string identifier of an existing loss function (such as
CategoricalCrossentropy, SparseCategoricalCrossentropy, BinaryCrossentropy or
MeanSquaredError) or it can be an instance of an objective function. Full list of
loss functions available at [Link]
○ A list of metrics. For any classification problem you will want to set this to
metrics=['accuracy']. A metric could be the string identifier of an existing metric
or a custom metric function. Again a full list of metrics is available at
[Link].
Multi-class Classification on MINST Dataset
The MNIST database (Modified National Institute of
Standards and Technology database) is a large database
of handwritten digits that is commonly used for
training various image processing systems.
mnist = [Link]
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = [Link]( [
[Link](512, activation=[Link], input_shape= (784,)),
[Link](10, activation=[Link])
]) In this example, we have specified a
validation split of 0.1. We will see this
[Link](optimizer='adam', reflected in the output from the
loss='sparse_categorical_crossentropy', training process. Finally once the
model has been trained we can then
metrics=['accuracy']) use the evaluate function to determine
the loss value & metrics values for the
[Link](x_train, y_train, epochs=5, validation_split=0.1) model on the test data.
results = [Link](x_test, y_test) Notice the last line prints out the loss
print (results) on the test data and the accuracy on
the test data.
import tensorflow as tf
Keras
from [Link] import layers
mnist = [Link]
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = [Link]( [
[Link](shape=(784,)),
[Link](512, activation=[Link]),
[Link](10, activation=[Link])
])
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']) The summary() provides a useful
overview of the current state of
[Link](x_train, y_train, epochs=5, validation_split=0.1) your model broken down by
layers and the overall number of
print ([Link]()) parameters.
Predict Function import tensorflow as tf
from [Link] import layers
1. Rather than using the Import numpy as np
[Link] function we
can use the [Link] mnist = [Link]
function. (x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Reshape so that each individual row is an image
x_train = x_train.reshape(x_train.shape[0], 784)
x_test = x_test.reshape(x_test.shape[0], 784)
model = [Link]( [
[Link](512, activation=[Link], input_shape= (784,)),
[Link](10, activation=[Link])
])
The [Link] function will
output the probability scores for
[Link](optimizer='adam',
each class for each test instance. In
loss='sparse_categorical_crossentropy',
this example the predict function
metrics=['accuracy'])
would have produced an array
10000 * 10
[Link](x_train, y_train, epochs=5, validation_split=0.1)
2. The following are the most common parameters for the Dense Layer:
○ units: Specify the number of units(neurons) in this layer.
○ activation: Activation function to use. You can use a range of standard activation
functions here such as ReLu, Sigmoid, Tanh, etc (see [Link])
○ use_bias: Boolean, whether the layer uses a bias vector.
○ kernel_initializer: Initializer for the kernel weights matrix . The Initializations
define the way to set the initial random weights of Keras layers (see
[Link]).
○ bias_initializer: Initializer for the bias vector.
○ kernel_regularizer: Regularizer function applied to the kernel weights matrix
bias_regularizer: Regularizer function applied to the bias vector (see
[Link]).
Dropout and Flatten
• As we have seen Dropout consists of randomly setting a
fraction of output activations to 0 at each update during
training time, which helps prevent overfitting.
• Dropout is as an additional layer added to our model
[Link]
• The dropout layer will apply dropout to the values that
are inputted to it
• The main parameters are
• rate: float between 0 and 1. Fraction of the input
units to drop.
4. You can easily see the data available to you by printing out
print([Link]())
Label Class
0 T-shirt/top
1 Trouser
2 Pullover
3 Dress
4 Coat
5 Sandal
6 Shirt
7 Sneaker
8 Bag
9 Ankle boot
import tensorflow as tf
import [Link] as keras The shape of the training
from [Link] import layers data is (60000, 28, 28)
import [Link] as plt
import numpy as np
The first layer in the
fashion_mnist = [Link].fashion_mnist network we will build will
be [Link],
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0 which transforms the
test_images = test_images / 255.0 format of the images from a
2d-array (of 28 by 28 pixels),
model = [Link]([ to a 1d-array of 28 * 28 =
[Link](shape=(28,28)), 784 pixels.
[Link](),
[Link](128, activation=[Link]), [Link](10, activation=[Link]) ])
num_epochs = 20
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = [Link](train_images, train_labels, epochs=num_epochs, validation_split=0.1)
[Link]("ggplot")
[Link]()
[Link]([0, num_epochs, 0, 1])
[Link]([Link](0, num_epochs), [Link]["loss"], label="train_loss")
[Link]([Link](0, num_epochs), [Link]["val_loss"], label="val_loss")
[Link]([Link](0, num_epochs), [Link]["accuracy"], label="train_acc")
[Link]([Link](0, num_epochs), [Link]["val_accuracy"], label="val_acc")
[Link]('Model accuracy')
Cork Institute of Technology 24
[Link]('Accuracy')
import tensorflow as tf
import [Link] as keras The shape of the training
from [Link] import layers data is (60000, 28, 28)
import [Link] as plt
import numpy as np
The first layer in the
fashion_mnist = [Link].fashion_mnist network we will build will
be [Link],
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0 which transforms the
test_images = test_images / 255.0 format of the images from a
2d-array (of 28 by 28 pixels),
model = [Link]([ to a 1d-array of 28 * 28 =
[Link](shape=(28,28)), 784 pixels.
[Link](),
[Link](128, activation=[Link]), [Link](10, activation=[Link]) ])
num_epochs = 20
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = [Link](train_images, train_labels, epochs=num_epochs, validation_split=0.1)
[Link]("ggplot")
[Link]()
[Link]([0, num_epochs, 0, 1])
[Link]([Link](0, num_epochs), [Link]["loss"], label="train_loss")
[Link]([Link](0, num_epochs), [Link]["val_loss"], label="val_loss")
[Link]([Link](0, num_epochs), [Link]["accuracy"], label="train_acc")
[Link]([Link](0, num_epochs), [Link]["val_accuracy"], label="val_acc")
[Link]('Model accuracy')
Cork Institute of Technology 25
[Link]('Accuracy')
CIFAR 10 Dataset
▪ When it comes to computer vision and machine learning, the
MNIST dataset is the classic definition of a “benchmark” dataset,
one that is too easy to obtain high accuracy results on, and not
representative of the images we’ll see in the real world.
27
CIFAR 10 Dataset
1. CIFAR-10 is substantially harder than the MNIST dataset.
2. The challenge comes from the dramatic variance in how objects appear. For
example, we can no longer assume that an image containing a green pixel at a given
(x, y)-coordinate is a frog. This pixel could be a background of a forest that contains a
deer. Or it could be the colour of a green car or truck.
3. These assumptions are a stark contrast to the MNIST dataset, where the network
can learn assumptions regarding the spatial distribution of pixel intensities. For
example, the spatial distribution of foreground pixels of a 1 is substantially different
than a 0 or a 5.
5. As you’ll see in the following code, standard fully-connected layer networks are not
suited for this type of image classification.
28
import tensorflow as tf
import [Link] as keras
from [Link] import layers
import [Link] as plt
import numpy as np
1. This code is similiar to
what we= have
num_epochs 50 looked
cifar =[Link].cifar10
previously.
(x_train,
2. However, y_train),(x_test, y_test) = cifar.load_data()
in this case
x_train, x_test = x_train / 255.0, x_test / 255
we iterate for a larger
number of epochs.
model = [Link]([
[Link](shape=(32,32,3)),
[Link](), [Link](1024, activation=[Link]),
[Link](512, activation=[Link]),
[Link](10, activation=[Link])])
[Link]("ggplot")
[Link]()
[Link]([Link](0, num_epochs), [Link]["loss"], label="train_loss")
[Link]([Link](0, num_epochs), [Link]["val_loss"], label="val_loss")
[Link]([Link](0, num_epochs), [Link]["acc"], label="train_acc")
[Link]([Link](0, num_epochs), [Link]["val_acc"], label="val_acc")
[Link]("Training Loss and Accuracy")
[Link]("Epoch #")
[Link]("Loss/Accuracy")
[Link]
Clearly the network we have trained is not performing well. Validation accuracy flattens out
at approximately 50% accuracy. Also we can clearly see that the network is overfitting on the
training data. Overfitting is beginning to occur as early as epoch 8 or 9.
2. The Keras API saves all of these pieces together in a single format, marked by
the .keras extension. This is a zip archive consisting of the following:
cifar = [Link].cifar10
(x_train, y_train),(x_test, y_test) = cifar.load_data()
x_train, x_test = x_train / 255.0, x_test / 255
model = [Link]([
[Link](shape=(32,32,3)),
[Link](),
[Link](64, activation='relu'),
[Link](32, activation='relu'),
[Link](10, activation='softmax') # Output layer for 3 classes
])
[Link]()
num_epochs = 2
history = [Link](x_train, y_train, epochs=num_epochs, validation_data=(x_test, y_test))
Saving and Loading Keras Models to Disk
1. [Link]() The saved .keras file contains:
[Link]('[Link]')
print("Model saved to '[Link]’”)
reloaded_model = [Link].load_model('[Link]')
• For example, one callback that is automatically called is the history callback. After
each epoch the history callback is called and it updates the history object with the
loss and accuracy for the current epoch.
• Another alternative is an early stopping callback that will terminate the training
process if a monitored metric such as validation loss has stopped improving.
• The learning rate scheduler callback allows to update the learning rate used after a
fixed epoch or a certain number of epochs.
• You can pass a list of callbacks (using the keyword argument callbacks) to the .fit()
method of the Sequential or Model classes. A full list of callbacks is available here.
Checkpointing Models – What are Callbacks
There is also a ModelCheckpoint callback that allows you to checkpoint your model during the
training process.
• filepath: A string that can contain formatting options such as the epoch number. For example, the
following is a common filepath( weights.{epoch:02d}-{val_loss:.2f}. weights.h5)
• For large models this can consume a lot of space!!!
• mode: Should be minimizing or maximizing the monitor value (typically either ‘min’ or ‘max’)
• save_weights_only: if True, then only the model's weights will be saved. The extension of the
name file should be end in .weights.h5 (if False then it should be .keras)
• save_best_only: If this is set to true then it will only save the model weights for the current epoch
if its metric value is better than what has gone before. However, if you set save_best_only to false
it will save every model after each epoch (regardless of whether that model was better then
previous models or not).
model = [Link]([
Notice here we create an
[Link](shape=(32, 32, 3)),
instance of a
[Link](),
ModelCheckpoint, which
[Link](1024, activation=[Link]),
will monitor the validation
[Link](512, activation=[Link]),
loss and only save the state
[Link](10, activation=[Link])])
of the current model if the
current validation is lower
fname = "weights.{epoch:02d}-{val_loss:.2f}.weights.h5”
than an preceding loss
value.
checkpoint = [Link](fname, monitor="val_loss",
mode="min", save_best_only=True, save_weights_only=True, verbose=1)
[Link](optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy’])
Associated best_model.load_weights("[Link].h5")
Colab
Notebook [Link](optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
NUM_EPOCHS = 50
final_num_epochs = len([Link]['val_loss'])
[Link]("ggplot")
[Link]()
[Link]([Link](0, final_num_epochs), [Link]["loss"], label="train_loss")
[Link]([Link](0, final_num_epochs), [Link]["val_loss"], label="val_loss")
[Link]([Link](0, final_num_epochs), [Link]["accuracy"], label="train_acc")
[Link]([Link](0, final_num_epochs), [Link]["val_accuracy"], label="val_acc")
Learning Rate Scheduler
After each epoch the Learning Rate Scheduler will call a function named schedule which
will modify the current learning rate.
• schedule: a function that takes an epoch index (integer, indexed from 0) and current
learning rate (float) as inputs and returns a new learning rate as output (float).
import tensorflow as tf
import [Link] as plt
import [Link] as keras
from [Link] import layers
import numpy as np
NUM_EPOCHS = 10
cifar = [Link].cifar10
(x_train, y_train),(x_test, y_test) = cifar.load_data()
x_train, x_test = x_train / 255.0, x_test / 255
model = [Link]([
[Link](shape=(32, 32, 3)),
[Link](),
[Link](1024, activation=[Link]),
[Link](512, activation=[Link]),
[Link](10, activation=[Link])])
learning_rate_callback = [Link](schedule = scheduler)
opt= [Link](learning_rate=0.1)
[Link](optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
final_num_epochs = len([Link]['val_loss'])
print ("Final Learning Rate ", round([Link](), 5))
[Link]("ggplot")
[Link]()
[Link]([Link](0, NUM_EPOCHS), [Link]["loss"], label="train_loss")
[Link]([Link](0, NUM_EPOCHS), [Link]["val_loss"], label="val_loss")
[Link]([Link](0, NUM_EPOCHS), [Link]["accuracy"], label="train_acc")
[Link]([Link](0, NUM_EPOCHS), [Link]["val_accuracy"], label="val_acc")
[Link]("Training Loss and Accuracy")
[Link]("Epoch #")
[Link]("Loss/Accuracy")
[Link]
Original Learning Rate 0.1
Epoch 1/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.2653 - loss: 2.0295 - val_accuracy:
0.3824 - val_loss: 1.7026 - learning_rate: 0.1000
Epoch 2/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.3874 - loss: 1.7043 - val_accuracy:
0.3876 - val_loss: 1.7230 - learning_rate: 0.1000
Epoch 3/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 10s 3ms/step - accuracy: 0.4544 - loss: 1.5220 - val_accuracy:
0.4158 - val_loss: 1.6477 - learning_rate: 0.0500
Epoch 4/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.4774 - loss: 1.4605 - val_accuracy:
0.4681 - val_loss: 1.4880 - learning_rate: 0.0500
Epoch 5/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.5110 - loss: 1.3686 - val_accuracy:
0.5022 - val_loss: 1.4035 - learning_rate: 0.0250
Epoch 6/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.5306 - loss: 1.3244 - val_accuracy:
0.4770 - val_loss: 1.4414 - learning_rate: 0.0250
Epoch 7/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 4s 3ms/step - accuracy: 0.5470 - loss: 1.2669 - val_accuracy:
0.5265 - val_loss: 1.3483 - learning_rate: 0.0125
Epoch 8/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.5593 - loss: 1.2331 - val_accuracy:
0.5232 - val_loss: 1.3542 - learning_rate: 0.0125
Epoch 9/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.5701 - loss: 1.2105 - val_accuracy:
0.5338 - val_loss: 1.3237 - learning_rate: 0.0063
Epoch 10/10
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 5s 3ms/step - accuracy: 0.5804 - loss: 1.1836 - val_accuracy:
0.5291 - val_loss: 1.3252 - learning_rate: 0.0063
Final Learning Rate 0.00625
Deep Learning
Deep Learning
Lecture: Keras – Functional API
Ted Scully
Non Standard Models Types
1. So far, we have been using the Sequential API in Keras, which is suited for creating
standard models in a linear fashion. We create an instance of our Sequential model
and sequentially add individual layers. However, many model architectures don’t
follow this basic structure.
2. For example, some models can be designed to take a diversity of different input
types.
3. Let’s assume we wanted to predict the selling price of a secondhand car we might
have an image of the car as well as numerical data such as standard feature data
such as age, make, etc. We might push the feature data through a dense network
and the picture through a convolution network, the outputs of these networks are
then fused and pushed through a final network in order to make some prediction.
Non Standard Models Types
1. Some models can be designed so that they have multiple outputs.
2. For example, if we do object localization we want our model to
predict both the bounding box of an object and the class of object
within the bounding box.
3. More specifically we might want out model to output the x,y pixel
location of an object, the height and width of the bounding box
and also the class of object contain therein.
Inception Network
The model below is taken from the original paper on inception
networks (Going Deeper with Convolutions 2014).
This is the original inception network (also called GoogLeNet)
3. There are three basic steps to creating a model using the functional API:
• Connect layers by passing the output of one layer to the next layer.
• Create an instance of the model specifying the input and output layers
that will populate the model.
Layers are callable
1. Before we talk about the functional API in more detail let’s revisit the idea of a Keras layer.
2. As we have seen before a layer is a fundamental component of a neural networks in
Keras.
3. An instance of a layer is callable. Notice below we create a basic dense layer. We
subsequently call the layer and pass it a tensor. We can then collect the resulting tensor in
outputs.
import tensorflow as tf
from [Link] import layers
#create a tensor
inputs = [Link](shape=(4, 5))
# call the dense layer and pass it a tensor, results in stored in outputs
outputs = basicLayer(inputs)
The Input object is not a layer. Instead, it returns a symbolic tensor (think of it as a
placeholder).
In the code example we will look at the Functional API implementation of the same
model we implemented previously (with the Sequential API) for tackling the Fashion
MNIST problem.
# First step is creating a input layer that specifies the incoming data instance shape
In the example, we create a flatten layer (because this is just a densely connected network we
are going to flatten the incoming instance). As we have seen previously layers are callable,
which means I can call this layer and pass it an incoming tensor, which will return an output
tensor. This is exactly what we do below.
We take the flat layer and call it by pass it the input tensor. This returns a resulting tensor called
layer1 in this case. (In essence we have added the first node to our network graph)
# First step is creating a input tensor that specifies the incoming data instance shape
input = [Link](shape=(28,28))
# call the flatten layer (just created) and pass it incoming tensor
layer1_output = layer1(input)
(None, 784)
print (layer1_output.shape)
Functional API Keras – Step 2. Calling Layers
We can then continue in this same way. Notice
below we create a densely connected layer and
call it by passing it the tensor output of the
previous flat layer. We then create a Softmax layer
and call it by passing it the tensor outputted from
the densely connected layer.
input = [Link](shape=(28,28))
layer1 = [Link]()
layer1_output = layer1(input)
NUM_EPOCHS= 10
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Shortened example
input = [Link](shape=(28,28))
layer1_output = [Link]()(input)
In the code we saw on the previous slide we used concatenate to merge two linear
tensors together. However, there are many cases where we want to take two multi-
dimensional structures and concatenate them together.
flat1_output = [Link]()(pool3_output)
dense1_output = [Link](32, activation='relu')(flat1_output)
final_output = [Link](10, activation=[Link])(dense1_output)
#FIRST MODEL
input1 = [Link](shape=(28,28,1))
#MERGED MODEL
input = [Link](shape=(28,28,1))
covd_output = covd_model(input)
final_output = dense_model(covd_output)