10-problems.pdf
(
242 KB
)
Pobierz
Artificial Neural Networks
Prof. Dr. Sen Cheng
Dec 6, 2021
Problem Set 10: Convolutional Neural Networks
Tutors:
Nicolas Diekmann (nicolas.diekmann@rub.de), Mohammadreza Mohagheghi Nejad (m.mohagheghi@ini.rub.de)
1.
Fully connected (dense) networks
Build and train a fully connected network with 2 layers using
tensorflow.keras.
Make use of the documen-
tation (tensorflow.org).
(a) Import the
fashion_mnist
dataset from the
keras
library and load it. The dataset contains 60,000 images
with a dimension of 28x28 pixels. Visualize a few images to get a feel for the data.
(b) Vectorize the images for the training and test set. Normalize the images to the range
[0.,
1.].
(c) Build a Sequential network. Add two hidden
Dense
layers, with 5 units each and
relu
activation function.
Add an output
Dense
layer with 10 units and
softmax
activation function. Compile the network using the
Adam
as optimizer, the
sparse_categorical_crossentropy
as loss function and
accuracy
as metrics.
(d) Use pen and paper to calculate how many trainable parameters your network has (including the bias terms).
Also get this number directly from the keras model by calling the method
count_params()
and confirm
whether your manual calculation was correct.
(e) Train the network for 20 epochs using a mini batch size of 64. Use the test set for validation. Visualize
the model loss on both training and validation sets as a function of the training epoch. How many epochs
are necessary to get a roughly stable pattern for validation and training losses?
(f) Fix the number of epochs with this number and set the number of neurons in the hidden layers to 5, 10,
15 and 20. For each model, compute the mean loss for the validation and the training sets for the last 3
epochs and store it in a variable. Also store the number of trainable parameters for each model.
(g) Plot the losses as a function of the number of trainable parameters and label them properly. What do you
conclude about the relationship between the number of trainable parameters and the model performance?
2.
Convolutions
(a) Using pen and paper, compute the 2d convolutions
K
i
⇤
I
i
, where
i
=
x, y
and
j
=
A, B,
for the following
two ‘image’ matrices:
2
3
2
3
0 0 1 0 0
0 0 0 0 0
6
0 0 1 0 0
7
6
0 0 0 0 0
7
6
7
6
7
I
A
=
6
0 0 1 0 0
7
I
B
=
6
1 1 1 1 1
7
6
7
6
7
4
0 0 1 0 0
5
4
0 0 0 0 0
5
0 0 1 0 0
0 0 0 0 0
and two filter (kernel) matrices:
3
1 0 1
K
x
=
4
2 0 2
5
1 0 1
2
1
K
y
=
4
0
1
2
2
0
2
3
1
0
5
1
Pad the image matrices with the appropriate number of additional rows and colums by repeating their cur-
rent border. Use the CNN-convention for computing the convolution, which differs from the mathematical
convention. You can verify your result with the ‘nearest’ method in ‘scipy.ndimage.correlate’.
1
(b) Inspect the results of the convolutions above: What kind of features do the filter kernels extract?
3.
Convolutional Neural Networks (CNN)
Build and train a 6-layer CNN using the
keras
library.
(a) Proceed as in problem 1 but replace the fullly connected hidden layers by convolutional layers as follows.
Use two consecutive
Conv2D
layers each followed by a
MaxPooling2D
layer. The
Conv2D
layers contain
4 and 8 3x3 filters, respectively. For the
MaxPooling2D
layers use a kernel of size 2x2.
(b) Calculate the number of network parameters using pen and paper and confirm whether your calculations
were correct by directly getting it from the keras model. Where in the network do you find the most
parameters?
(c) Set the number of filters in the first convolutional layer to 4, 8, 12 and 16 in 4 different models. For the
second layer, use twice as many filters as in the first layer. Train the models on
fashion_mnist.
For each
model, compute the mean of the loss for the validation and the training sets for the last 3 epochs and store
it in a variable. Store also the number of trainable parameters for each model.
(d) Plot the losses as a function of the number of trainable parameters and label them properly. Compare it
against the corresponding outcome of the fully connected network in 1g and draw a conclusion.
(e) For the last network (16 filters in the first layer) load the CNN’s weights and visualize the filters of the
first convolutional layer. Can you identify any edge detection or a filter similar to it?
4.
(Bonus Problem) Convolutional Autoencoder (CAE)
Autoencoders are a special type of network which are trained to learn low-dimensional representations of their
input data. (Note: The topic of this bonus problem will not be tested in the final exam and is not required for
later assignments!)
(a) Build a convolutional autoencoder as depicted in Figure 1 by extending the model from problem 3. For
the decoder network use
Conv2DTranspose
and
UpSampling2D
layers. For the output layer use a
Conv2D
layer with one filter and a kernel of size 1x1.
(b) Train networks with different code sizes
{32,
64, 128, 256} for 20 epochs each. Let the networks recon-
struct the first 10 examples of the test set. Plot the reconstructions along with the ground truths. How do
reconstructions compare for different code sizes?
Figure 1: Illustration of a convolutional autoencoder. The autoencoder consists of an encoder network, an internal
representation layer (i.e. the code), and a decoder network. During training the input also serves as the learning target.
The internal representation layer’s dimensionality is (usually) much lower than that of the input.
2
Plik z chomika:
mxp-pl
Inne pliki z tego folderu:
10-CNN.pdf
(993 KB)
10-problems.pdf
(242 KB)
lec10-quiz.pdf
(80 KB)
Inne foldery tego chomika:
0 - Introduction to scientific computing
01-02 - Introduction-Optimization
03-Regression
04-Classification
05-Model Selection
Zgłoś jeśli
naruszono regulamin