Pytorch专题实战——激活函数(Activation Functions)
文章目录
- 1.激活函数的两种用法
- 1.1.softmax激活函数
- 1.2.sigmoid激活函数
- 1.3.tanh激活函数
- 1.4.relu激活函数
- 1.5.leaky_relu激活函数
- 2.用激活函数的不同方法构造函数
- 2.1.nn.ReLU()法
- 2.2.torch.relu()法
- 3.可视化不同激活函数
- 3.1.sigmoid
- 3.2.tanh
- 3.3.relu
- 3.4.leaky_relu
- 3.5.阶梯函数
1.激活函数的两种用法
1.1.softmax激活函数
import torch
import torch.nn as nn
import torch.nn.functional as F x = torch.tensor([-1.0, 1.0, 2.0, 3.0])
output = torch.softmax(x, dim=0) #用torch调用softmax
print(output)sm = nn.Softmax(dim=0) #用nn.Softmax
output = sm(x)
print(output)
1.2.sigmoid激活函数
output = torch.sigmoid(x) #用torch调用sigmoid
print(output)s = nn.Sigmoid() #用nn.Sigmoid
output = s(x)
print(output)
1.3.tanh激活函数
output = torch.tanh(x) #用torch调用tanh
print(output)t = nn.Tanh() #用nn.Tanh
output = t(x)
print(output)
1.4.relu激活函数
output = torch.relu(x) #用torch调用relu
print(output)relu = nn.ReLU() #用nn.RuLU
output = relu(x)
print(output)
1.5.leaky_relu激活函数
output = F.leaky_relu(x) #用F调用leaky_relu
print(output)lrelu = nn.LeakyReLU() #用nn.LeakyReLU
output = lrelu(x)
print(output)
2.用激活函数的不同方法构造函数
2.1.nn.ReLU()法
class NeuralNet(nn.Module):def __init__(self, input_size, hidden_size):super(NeuralNet, self).__init__()self.Linear1 = nn.Linear(input_size, hidden_size)self.relu = nn.ReLU() #在__init__用 nn.ReLUself.linear2 = nn.Linear(hidden_size, 1)self.sigmoid = nn.Sigmoid()def forward(self, x):out = self.linear1(x)out = self.relu(out)out = slef.linear2(out)out = self.sigmoid(out)return out
2.2.torch.relu()法
class NeuralNet(nn.Module):def __init__(self, input_size, hidden_size):super(NerualNet, self).__init__()self.Linear1 = nn.Linear(input_size, hidden_size)self.Linear2 = nn.Linear(hidden_size, 1)def forward(self, x):out = torch.relu(self.Linear1(x)) #在forward中用torch.reluout = torch.sigmoid(self.Linear2(out))return out
3.可视化不同激活函数
3.1.sigmoid
import numpy as np
import matplotlib.pyplot as pltsigmoid = lambda x: 1 / (1 + np.exp(-x))
x = np.linspace(-10, 10, 10)
y = np.linspace(-10, 10, 10)
fig = plt.figure()
plt.plot(y, sigmoid(y))
plt.grid(linestyle='--')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sogmoid Function')
plt.xticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.yticks([-2, -1, 0, 1, 2])
plt.xlim(-4,4)
plt.ylim(-2,2)
plt.show()
3.2.tanh
tanh = lambda x: 2*sigmoid(2*x)-1
x = np.linspace(-10, 10, 10)
y = np.linspace(-10, 10, 10)
plt.plot(y, tanh(y), 'b', label='linspace(-10, 10, 100)')
plt.grid(linestyle='--')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Tanh Function')
plt.xticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.yticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.xlim(-4,4)
plt.ylim(-4,4)
plt.show()
3.3.relu
relu = lambda x: np.where(x>=0, x, 0)
x = np.linspace(-10, 10, 10)
y = np.linspace(-10, 10, 1000)
plt.plot(y, relu(y), 'b', label='linspace(-10, 10, 100)')
plt.grid(linestyle='--')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('RuLU')
plt.xticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.yticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.xlim(-4,4)
plt.ylim(-4,4)
plt.show()
3.4.leaky_relu
leakyrelu = lambda x: np.where(x>=0, x, 0.1*x)
x = np.linspace(-10, 10, 10)
y = np.linspace(-10, 10, 1000)
plt.plot(y, leakyrelu(y), 'b', label='linspace(-10, 10, 100)')
plt.grid(linestyle='--')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Leaky ReLU')
plt.xticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.yticks([-4, -3, -2, -1, 0, 1, 2, 3, 4])
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.show()
3.5.阶梯函数
bstep = lambda x: np.where(x>=0, 1, 0)
x = np.linspace(-10, 10, 10)
y = np.linspace(-10, 10, 1000)
plt.plot(y, bstep(y), 'b', label='linspace(-10, 10, 100)')
plt.grid(linestyle='--')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Step Function')
plt.xticks([-4, -3, -3, -1, 0, 1, 2, 3, 4])
plt.yticks([-2, -1, 0, 1, 2])
plt.xlim(-4, 4)
plt.ylim(-2, 2)
plt.show()
Pytorch专题实战——激活函数(Activation Functions)相关推荐
- Pytorch专题实战——交叉熵损失函数(CrossEntropyLoss )
文章目录 1.用CrossEntropyLoss预测单个目标 2.用CrossEntropyLoss预测多个目标 3.二分类使用BCELoss损失函数 4.多分类使用CrossEntropyLoss损 ...
- Pytorch专题实战——批训练数据(DataLoader)
文章目录 1.计算流程 2.Pytorch构造批处理数据 2.1.导入必要模块 2.2.定义数据类 2.3.定义DataLoader 2.4.打印效果 1.计算流程 # Implement a cus ...
- Pytorch专题实战——逻辑回归(Logistic Regression)
文章目录 1.计算流程 2.Pytorch搭建线性逻辑模型 2.1.导入必要模块 2.2.数据准备 2.3.构建模型 2.4.训练+计算准确率 1.计算流程 1)设计模型: Design model ...
- Pytorch专题实战——线性回归(Linear Regression)
文章目录 1.计算流程 2.Pytorch搭建线性回归模型 2.1.导入必要模块 2.2.构造训练数据 2.3.测试数据及输入输出神经元个数 2.4.搭建模型并实例化 2.5.训练 1.计算流程 1) ...
- Pytorch专题实战——前馈神经网络(Feed-Forward Neural Network)
文章目录 1.导入必要模块 2.超参数设置 3.数据准备 4.打印部分加载的数据 5.模型建立 6.训练 1.导入必要模块 import torch import torch.nn as nn imp ...
- Pytorch专题实战——数据转换(Dataset Transforms)
文章目录 1.导入必要模块 2.定义数据处理类 3.定义numpy转化为tensor类 4.定义乘法转化类 5.打印结果 5.1.未转化前 5.2.tensor转化 5.3.乘法转化 1.导入必要模块 ...
- Pytorch专题实战——反向传播(Backpropagation)
文章目录 1.前言 2.初识如何更新梯度 3.手动更新梯度 4.自动更新梯度 1.前言 大体分为三步: (1)前向传播,计算loss (2)计算局部梯度 (3)反向传播,用链式求导法则计算梯度 2.初 ...
- 1. 激活函数(Activation functions)
1.1 激活函数(Activation functions) 选择激活函数的经验法则 如果输出是0.1值(二分类问题),则输出层选择sigmoid函数,然后其它的所有单元都选择Relu函数. 这是很多 ...
- 激活函数(Activation Functions)
神经网络结构的输出为所有输入的加权和,这导致整个神经网络是一个线性模型.如果将每一个神经元的输出通过一个非线性函数,那么整个神经网络的模型也就不再是线性的了,使得神经网络可以更好地解决较为复杂的问题. ...
最新文章
- 科学家都解决不了的5个“简单”算法,你不来看看?
- 游戏 AI 相关文章
- 码云机房今晨出现网络故障,已经恢复
- 枚举方式的线程安全的单例
- oracle表空间可以迁移,Oracle 表空间迁移
- python实现mini-batch_Mini-Batch 、Momentum、Adam算法的实现
- 波音737-800座位图哪个好_「科普」汽车哪个座位才安全?很多人都不知道
- 中移4G模块-ML302-OpenCpu开发-GPIO
- java加密算法之DES篇
- 在MAC终端下打开Finder
- Arm 虚拟硬件与新解决方案或将颠覆物联网产品研发
- 利用sublime的package Control安装插件
- 基于NAR神经网络的时间序列预测
- python之parser.add_argument()用法——命令行选项、参数和子命令解析器
- 失传万年的PS致富经典(一)
- 不要抱怨自己的能力没有「用武之地」
- storm部署安装deploy
- 查询跟踪快递物流,筛选因拒收退回的单号
- 如何将普通交换机实现POE供电?
- 化妆品APP开发快速制作
热门文章
- Hadoop之分布式存储HDFS和离线计算MapReduce
- “强化学习说白了,是建立分布到分布之间的映射”?数学角度谈谈个人观点
- 构造一个简单的神经网络,以DQN方式实现小游戏的自动控制
- 观看台式计算机组成观后感,计算机组成原理实验一:运算器实验
- 前端html css3修炼之道,Web前端工程师修炼之道(原书第5版)
- python 多线程笔记(2)-- 锁
- kafka 同步提交 异步_Kafka 位移提交那些事儿
- oracle 正则表达式2
- 计算机应用技术课程本科,《计算机应用技术I》课程教学大纲(共本科29级用)().doc...
- python2.7_call