0
votes

I tried to apply ReLU and PReLU with a CNN layer to compare the results and I tried these code:

with ReLU:

model.add(Conv1D(filters, kernel_size, activation='relu'))

with PReLU:

model.add(Conv1D(filters, kernel_size))
model.add(PReLU())

Does the Conv1D layer use the PReLU as the activation function?

I doubt because I printed the model summary and it shows separate layers between CNN and PReLU with a different number of parameters, meanwhile the CNN layer with the ReLU function they are in the same layer.

enter image description here

If I used the wrong code, how can I correct it?

2

2 Answers

1
votes

Yes, the Conv1D layer will use the PReLu activation function. When you are defining a Conv2D layer like,

x = tf.keras.layers.Conv2D( 13 , kernel_size=( 3 , 3 ) , strides=1 , activation='relu' )( inputs )

The above statement is equivalent to,

x = tf.keras.layers.Conv2D( 13 , kernel_size=( 3 , 3 ) , strides=1 )( inputs )
x = tf.keras.layers.Activation( 'relu' )( x )

The reason for providing activation functions as separate layers is that sometimes we'll need to add our logic to the feature maps before passing the feature maps to the activation function.

For instance, a BatchNormalization layer is added before passing the feature maps to the activation function,

x = tf.keras.layers.Conv2D( 13 , kernel_size=( 3 , 3 ) , strides=1 )( inputs )
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation( 'relu' )( x )

Coming back to your question,

Some special activation functions like elu, LeakyReLU and PReLU are added as separate layers and we can't include them in the Conv1D layers using the activation= argument.

Regarding the trainable parameters, the conv1d_18 layer has 15050 parameters which form the kernel in 1D convolution. These parameters have nothing to do with the activation function.

The 4900 parameters of PReLU are the slope parameters which are optimized with backpropagation. These parameters, along with kernel weights, will update with every batch and hence are included in trainable parameters.

So, the outputs ( unactivated ) of the Conv1D layer will pass through the PReLU activation which indeed uses the slope parameter to calculate the activated outputs.

1
votes

According to keras docs

This will be equivalent

model.add(Dense(64))
model.add(Activation('tanh'))

to this one

model.add(Dense(64, activation='tanh'))

I do not know that why advanced activation functions must be used as a layer but PReLu can be used with CNNs, and there is no problem at all.