简体   繁体   中英

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?

You can just pass it as an activation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM