Can I combine Conv2D and LeakyReLU into a single layer?

Viewed 2591

The keras Conv2D layer does not come with an activation function itself. I am currently rebuilding the YOLOv1 model for practicing. In the YOLOv1 model, there are several Conv2D layers followed by activations using the leaky relu function. Is there a way to combine

from keras.layers import Conv2D, LeakyReLU

...

def model(input):
    ...

    X = Conv2D(filters, kernel_size)(X)
    X = LeakyReLU(X)

    ...

into a single line of code, like X = conv_with_leaky_relu(X)? I think it should be similar to

def conv_with_leaky_relu(*args, **kwargs):
    X = Conv2D(*args, **kwargs)(X)
    X = LeakyReLU(X)
    return X

but this of course doesn't work because it is undefined what X ist. Any ideas?

1 Answers

You can just pass it as an activation:

X = Conv2D(filters, kernel_size, activation=LeakyReLU())(X)
Related