如何将Mish函数用到深度学习算法中

目录

摘要

如何在Pytorch使用Mish函数

如何在Keras中使用Mish激活函数。


摘要

Diganta Misra的一篇题为“Mish: A Self Regularized Non-Monotonic Neural Activation Function”的新论文介绍了一个新的深度学习激活函数,该函数在最终准确度上比Swish(+.494%)和ReLU(+ 1.671%)都有提高

公式如下:

                                                         https://i-blog.csdnimg.cn/blog_migrate/9c10856256038b3f5a769c091a23f719.png

https://i-blog.csdnimg.cn/blog_migrate/fd845ee2ebb58d3a2939bbf166548e08.png

  • 如何在Pytorch使用Mish函数

定义Mish函数。

class Mish(torch.nn.Module):

    def __init__(self):

        super().__init__()



    def forward(self, x):

        x = x * (torch.tanh(torch.nn.functional.softplus(x)))

        return x

调用函数:

class Path1_64(nn.Module):

    def __init__(self):

        super().__init__()

        self.conv1 = ConvBN(32, 64, 3)

        self.conv2 = ConvBN(64, 64, [1, 9])

        self.conv3 = ConvBN(64, 64, [9, 1])

        self.conv4 = ConvBN(64, 64, 1)

        self.resBlock = ResBlock(ch=64, nblocks=2)

        self.conv5 = ConvBN(64, 64, [1, 7])

        self.conv6 = ConvBN(64, 64, [7, 1])

        self.conv7 = ConvBN(64, 64, 1)

        self.relu = Mish()



    def forward(self, input):

        x1 = self.conv1(input)

        x2 = self.conv2(x1)

        x3 = self.conv3(x2)

        x4 = self.conv4(x3)

        r1 = self.resBlock(x4)

        x5 = self.conv5(r1)

        x6 = self.conv6(x5)

        x7 = self.conv7(x6)

        x7 = self.relu(x7 + x4)

        return x7

调用Mish激活函数和调用其他的激活函数一样,直接调用即可。

  • 如何在Keras中使用Mish激活函数。

定义Mish激活函数

import tensorflow as tf
from tensorflow.python.keras.layers import *
from tensorflow.keras.layers import Activation
from tensorflow.keras.utils import get_custom_objects



class Mish(Activation):

    def __init__(self, activation, **kwargs):

        super(Mish, self).__init__(activation, **kwargs)

        self.__name__ = 'Mish'

def mish(inputs):

    return inputs * tf.math.tanh(tf.math.softplus(inputs))

get_custom_objects().update({'Mish': Mish(mish)})



调用激活函数:

def bn_prelu(x):

    x = BatchNormalization(epsilon=1e-5)(x)

    x = Activation('Mish')(x)

    return x

def build_model(out_dims, input_shape=(100, 100, 3)):

    inputs_dim = Input(input_shape)

    x = Conv2D(32, (3, 3), strides=(2, 2), padding='same')(inputs_dim)

    x = bn_prelu(x)

    x = Conv2D(32, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x)

    x = bn_prelu(x)

    x = GlobalAveragePooling2D()(x)

    dp_1 = Dropout(0.5)(x)

    fc2 = Dense(out_dims)(dp_1)

    fc2 = Activation('softmax')(fc2)  # 此处注意,为sigmoid函数

    model = Model(inputs=inputs_dim, outputs=fc2)

    return model



model = build_model(2)  # 生成模型

optimizer = Adam(lr=1e-3)  # 加入优化器,设置优化器的学习率。

model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值