CS256
Chris Pollett
Nov 8, 2021
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());
input_layer = Input(shape=input_shape) #take all inputs and feeds to 32 neurons dense_layer_1 = Dense(32, activation='relu')(input_layer) #take all inputs and feeds to 16 neurons dense_layer_2 = Dense(16, activation='relu')(input_layer) #makes a single layer with above two layers happening in parallel merged_layer = Concatenate()([dense_layer_1, dense_layer_2]) #feeds results of layer into a final layer final_layer = Dense(10, activation='softmax')(merged_layer) #create model specifying input and output layers. model = Model(inputs=input_layer, outputs=final_layer, name="My Model")
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()
Which of the following is true?
Due to Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. 1998