Reputation: 21
I read a paper and I failed to understand the model.
In the paper,
input shape = (7,9,1), kernel size = (3,3,1)
"The filter convolves the input by shifting one unit at a time, and no padding is used, the input is padded with zeros around the border and maintained the same size after convolution. By applying multiple convolution layers, we can get multiple response maps. We have four convolution layers therefore the output after the convolution has a size of (7,9,4). ReLU was included in the convolution layer."
and I'm confused with the Bold parts.
I tried to make a model and this is my code,
model.add(Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu', input_shape=(7, 9, 1)))
model.add(Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu'))
Help me, please.
Upvotes: 1
Views: 449
Reputation:
Considering the model you have shared without adding padding to the layer:
model=tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(4, kernel_size=(3, 3), activation='relu', input_shape=(7, 9, 1)))
model.add(tf.keras.layers.Conv2D(4, kernel_size=(3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(2, kernel_size=(3, 3), activation='relu'))
The summary of the model is as below:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_30 (Conv2D) (None, 5, 7, 4) 40
conv2d_31 (Conv2D) (None, 3, 5, 4) 148
conv2d_32 (Conv2D) (None, 1, 3, 2) 74
=================================================================
Total params: 262
Trainable params: 262
Non-trainable params: 0
_________________________________________________________________
In a convolutional neural network, a kernel/filter which moves across the data scans each pixel and converts the data into a smaller data. Here the output size of the last layer is (1, 3, 2) whereas the input size is (7, 9, 1).
Considering the model with padding:
model=tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu', input_shape=(7, 9, 1)))
model.add(tf.keras.layers.Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(tf.keras.layers.Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(tf.keras.layers.Conv2D(4, kernel_size=(3, 3), padding='same', activation='relu'))
The model summary is as below:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 7, 9, 4) 40
conv2d_1 (Conv2D) (None, 7, 9, 4) 148
conv2d_2 (Conv2D) (None, 7, 9, 4) 148
conv2d_3 (Conv2D) (None, 7, 9, 4) 148
=================================================================
Total params: 484
Trainable params: 484
Non-trainable params: 0
_________________________________________________________________
As you can see in the above example, when padding is added , padding=same, the padding layers append zero values in the outer frame of the data so the filter we are using can cover the edge of the matrix and doesn't reduce the size of the image. 4 in the conv2D layer determines the number of output filters in the convolution. Hence the output shape of the all the layers is(7, 9, 4).
Upvotes: 2