Mish激活函数

前言

人们对激活函数都在不断探究,而现在广泛应用的激活函数通常是relu,tanh这两种
但是relu在负值的时候直接截断
梯度下降的不够平滑
因而有团队提出一种新的激活函数来进行优化

Mish激活函数

Mish激活函数的表达式为

						Mish = x*tanh(ln(1+e^x))

使用matplotlib画图可得

从图中可以看出他在负值的时候并不是完全截断

而是允许比较小的负梯度流入
从而保证信息流动

并且激活函数无边界这个特点,让他避免了饱和这一问题
比如sigmoid,tanh激活函数通常存在梯度饱和问题,在两边极限情况下,梯度趋近于1
而Mish激活函数则巧妙的避开了这一点

另外Mish函数也保证了每一点的平滑,从而使得梯度下降效果比Relu要好

# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as plt

class Mish(nn.Module):
    def __init__(self):
        super().__init__()
        print("Mish activation loaded...")
    def forward(self,x):
        x = x * (torch.tanh(F.softplus(x)))
        return x

mish = Mish()
x = torch.linspace(-10,10,1000)
y = mish(x)

plt.plot(x,y)
plt.grid()
plt.show()

 

posted @ 2023-07-06 16:43  海_纳百川  阅读(398)  评论(0编辑  收藏  举报
本站总访问量