import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus'] = False
def sigmoid(x):
result = 1 / (1 + math.e ** (-x))
return result
def tanh(x):
# result = np.exp(x)-np.exp(-x)/np.exp(x)+np.exp(-x)
result = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x))
return result
def relu(x):
result = np.maximum(0, x)
return result
def elu(x, alpha=1):
a = x[x > 0]
b = alpha * (math.e ** (x[x < 0]) - 1)
result = np.concatenate((b, a), axis=0)
return result
def leaky(x):
a = x[x > 0]
b = 0.1 * x[x < 0]
result = np.concatenate((b, a), axis=0)
return result
yanse = 'teal'
# ===========================================
fig = plt.figure(figsize=(10, 8))
plt.rcParams['font.size'] = 11
plt.rc('font',family='Times New Roman')
ax = fig.add_subplot(221)
x = np.linspace(-10, 10)
y = sigmoid(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])
plt.plot(x, y, label="Sigmoid", linestyle='-', color=str(yanse))
plt.legend(loc='upper left',frameon=False,ncol=1)
# ============================================
ax = fig.add_subplot(222)
x = np.linspace(-10, 10)
y = tanh(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])
plt.plot(x, y, label="Tanh", linestyle='-', color=str(yanse))
plt.legend(loc='upper left',frameon=False,ncol=1)
# ============================================
ax = fig.add_subplot(223)
x = np.linspace(-10, 10)
y = relu(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([5, 10])
plt.plot(x, y, label="ReLU", linestyle='-', color=str(yanse))
plt.legend(loc='upper left',frameon=False,ncol=1)
# ============================================
# ax = fig.add_subplot(234)
# x = np.linspace(-10, 10)
# y = elu(x)
#
# ax.spines['top'].set_color('none')
# ax.spines['right'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')
# ax.spines['bottom'].set_position(('data', 0))
# ax.set_xticks([-10, -5, 0, 5, 10])
# ax.yaxis.set_ticks_position('left')
# ax.spines['left'].set_position(('data', 0))
# ax.set_yticks([5, 10])
#
# plt.plot(x, y, label="ELU alpha=1", linestyle='-', color='red')
# plt.legend()
# ============================================
ax = fig.add_subplot(224)
x = np.linspace(-10, 10)
y = leaky(x)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([5, 10])
plt.plot(x, y, label="Leaky Relu", linestyle='-', color=str(yanse))
plt.legend(loc='upper left', frameon=False, ncol=1)
plt.savefig('../激活函数/' + str("jihuohanshu"), bbox_inches='tight', dpi=500)
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
def sigmoid(x): result = 1 / (1 + math.e ** (-x)) return result def tanh(x): # result = np.exp(x)-np.exp(-x)/np.exp(x)+np.exp(-x) result = (math.e ** (x) - math.e ** (-x)) / (math.e ** (x) + math.e ** (-x)) return result def relu(x): result = np.maximum(0, x) return result def elu(x, alpha=1): a = x[x > 0] b = alpha * (math.e ** (x[x < 0]) - 1) result = np.concatenate((b, a), axis=0) return result def leaky(x): a = x[x > 0] b = 0.1 * x[
资源详情
资源评论
资源推荐
收起资源包目录
激活函数.rar (2个子文件)
激活函数
画激活函数.py 4KB
jihuohanshu.png 239KB
共 2 条
- 1
*猪耳朵*
- 粉丝: 5w+
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10