More Keras - CNNs and RNNs




CS256

Chris Pollett

Nov 8, 2021

Outline

Introduction

Creating Custom Layers in Keras

Example Custom Layer

The code below implements a Parametric ReLU layer and activation layer we discussed in the Oct 14 Lecture:

import tensorflow as tf
class ParametricRelu(tf.keras.layers.Layer):
    #constructor here is just calling the parent
    def __init__(self, **kwargs):
        super(ParametricRelu, self).__init__(**kwargs)
    # in build below we specify one new weight to train
    # with initial value 0
    # we also say this layer's units only take 1 input
    def build(self, input_shape):
        self.alpha = self.add_weight(
            name = 'alpha', shape=(input_shape[1]),
            initializer = 'zeros',
            trainable = True
        )
        super(ParametricRelu, self).build(input_shape)
    # for the inputs to a unit (in this case just 1),
    # how we compute the output of that unit
    def call(self, x):
        return tf.maximum(0.,x)+ self.alpha * tf.minimum(0.,x)

Given a Sequential model, we could then add this layer using:

model.add(ParametricRelu());

Creating Custom Layer Connections in Keras

Custom Connection Example 2 - Mini ResNET

inputs = keras.Input(shape=(32, 32, 3), name="img")
x = layers.Conv2D(32, 3, activation="relu")(inputs)
x = layers.Conv2D(64, 3, activation="relu")(x)
block_1_output = layers.MaxPooling2D(3)(x)

x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_1_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_2_output = layers.add([x, block_1_output]) #this implements skip connection

x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_2_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_3_output = layers.add([x, block_2_output]) #this implements skip connection

x = layers.Conv2D(64, 3, activation="relu")(block_3_output)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(256, activation="relu")(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(10)(x)

model = keras.Model(inputs, outputs, name="toy_resnet")
model.summary()

Quiz

Which of the following is true?

  1. `L1`-regularization always leads to a sparse parameterization of weights.
  2. Optimization is the process by which we try to find inputs which minimize or maximize some objective function.
  3. We used the Cramer Rao theorem to prove Nesterov Momentum is the correct approach to SGD.

Convolutional Neural Nets

Convolutional Neural Net Layers

Example Feature Map

CNN Motivation Biology

Eye Anatomy with Fovea Eye Movement Example V Areas of the Brain

Why are CNNs good for image processing?

Stages of a CNN Layer and Pooling

Variants on a Basic CNN Layer

What is the effect of having multiple CNN layers?

CNN Architecture

CNN Architecture - LeNet-5 Example

Due to Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. 1998

  1. Inputs: 32 x 32 pixel digits images
  2. Hidden Layer 1: Uses 5x5 kernel (with bias this means 26 weights), outputs 6, 28 x 28 feature maps.
  3. Hidden Layer 2: Uses 2x2 pixel maxpool layer, outputs 6, 14 x 14 feature maps.
  4. Hidden Layer 3: Uses 5x5 pixel kernel, outputs 16, 10 x 10 feature maps.
  5. Hidden Layer 4: Uses 2x2 pixel maxpool layer, outputs 16, 5 x 5 feature maps.
  6. Hidden Layer 6: Uses 5x5 pixel kernel, outputs 120, 1 x 1 feature maps.
  7. Hidden Layer 7: Fully connected to 120 input, 84 outputs.
  8. Output: 10 radial basis functions each getting 84 inputs, and whose outputs correspond to the different digits.

Intro to Recurrent Neural Nets