sampleuser
sampleuser

Reputation: 320

Can I combine Conv2D and LeakyReLU into a single layer?

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?

Upvotes: 6

Views: 4963

Answers (1)

Yannick Funk
Yannick Funk

Reputation: 1581

You can just pass it as an activation:

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

Upvotes: 11

Related Questions