- sigmod函数
- tanh函数
- ReLU函数
- softplus函数
- 绘制swish函数图形,绘制当\beta = [0, 0.5, 1, 100]时的图形。
代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/11/24 15:42
# @Author : Cai
# @Site :
# @File : 1.py
# @Software: PyCharm
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist
def sigmoid():
nums = np.arange(-10, 10, step=1) # 生成一个numpy数组
# ax = plt.subplots(figsize=(12, 4)) # 绘制子图
ax = fig.add_subplot(1, 2, 1)
# ax.set(title="sigmoid", xlabel="x", ylabel="y=sigmoid(x)")
ax.plot(nums, 1.0 / (1 + np.exp(-nums)), 'r--') # 绘制sigmoid的函数图像,r代表红色,--,..,-.,marker可以选择o^v
def tanh():
# 采样
x = np.linspace(-10, 10, 10000)
y = (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
ax = fig.add_subplot(1, 2, 1)
# ax.set(title="tanh", xlabel="x", ylabel="y=tanh(x)")
# 设置线宽和颜色
ax.plot(x, y, linewidth=3.0, color="blue", linestyle='-')
def relu():
# 采样
x = np.linspace(-10, 10, 10000)
y = np.zeros(shape=(x.shape[0]), dtype=np.float)
for i, v in enumerate(x):
if v > 0:
y[i] = v
else:
y[i] = 0
# fig = plt.figure()
ax = fig.add_subplot(1, 2, 1)
# ax.set(title="Relu", xlabel="x", ylabel="y=Relu(x)")
# 设置线宽和颜色
ax.plot(x, y, linewidth=3.0, color="yellow")
def softplus():
# 采样
x = np.linspace(-10, 10, 100)
y = np.log(1 + np.exp(x))
ax = fig.add_subplot(2, 6, 1)
# ax.set(title="tanh", xlabel="x", ylabel="y=tanh(x)")
# 设置线宽和颜色
ax.plot(x, y, linewidth=3.0, color="purple", linestyle='-')
class ActivateFunc():
def __init__(self, x, b=None, lamb=None, alpha=None, a=None):
super(ActivateFunc, self).__init__()
self.x = x
self.b = b
self.lamb = lamb
self.alpha = alpha
self.a = a
def Swish(self): # b是一个常数,指定b
y = self.x * (np.exp(self.b * self.x) / (np.exp(self.b * self.x) + 1))
y_grad = np.exp(self.b * self.x) / (1 + np.exp(self.b * self.x)) + self.x * (
self.b * np.exp(self.b * self.x) / ((1 + np.exp(self.b * self.x)) * (1 + np.exp(self.b * self.x))))
return [y, y_grad]
def PlotActiFunc(x, y):
plt.grid(which='minor', alpha=0.2)
plt.grid(which='major', alpha=0.5)
plt.plot(x, y)
def PlotMultiFunc(x, y):
plt.grid(which='minor', alpha=0.2)
plt.grid(which='major', alpha=0.5)
plt.plot(x, y)
fig = plt.figure()
sigmoid()
tanh()
relu()
softplus()
plt.show()
x = np.arange(-10, 10, 0.01)
activateFunc = ActivateFunc(x)
activateFunc.b = 0
PlotActiFunc(x, activateFunc.Swish()[0])
activateFunc.b = 0.5
PlotActiFunc(x, activateFunc.Swish()[0])
activateFunc.b = 1
PlotActiFunc(x, activateFunc.Swish()[0])
activateFunc.b = 100
PlotActiFunc(x, activateFunc.Swish()[0])
plt.show()