当前位置:网站首页>神经网络-非线性激活
神经网络-非线性激活
2022-07-01 04:35:00 【booze-J】
概述:
非线性激活主要是为了给我们的神经网络去引入一些非线性的特质,比较常用的非线性激活有两个分别是Sigmoid和RELU。
RELU
示例代码:
import torch
from torch import nn
from torch.nn import ReLU
input = torch.tensor([[1,-0.5],
[-1,3]])
input = torch.reshape(input,(-1,1,2,2))
print(input.shape)
# 搭建神经网络
class Booze(nn.Module):
def __init__(self):
super(Booze, self).__init__()
# inplace参数的意义就是是否将原来的变量的值替换成处理后的结果值 若inplace=True则对原来的变量进行替换,原来变量的值就变了,变成处理后的结果值,若inplace=False则不对原来的变量进行替换,产生的结果需要一个新的变量去接收
# 通常情况下,建议将inplace传入False,这样可以保证原始数据不丢失
self.relu1 = ReLU()
# 重写forword方法
def forward(self,input):
output = self.relu1(input)
return output
obj = Booze()
output = obj(input)
print(output)
代码运行结果:
对于ReLU接口的使用,该接口只有一个参数需要传入,那就是inplace参数。
torch.nn.ReLU(inplace=False)
inplace参数的意义就是是否将原来的变量的值替换成处理后的结果值 若inplace=True则对原来的变量进行替换,原来变量的值就变了,变成处理后的结果值,若inplace=False则不对原来的变量进行替换,产生的结果需要一个新的变量去接收。
通常情况下,建议将inplace传入False,这样可以保证原始数据不丢失。
上面这张图是ReLU激活函数的图像,对上面这张图的解释:小于零进行截断,大于零则输出原有值。
Sigmoid
示例代码:
import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("./CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)
# 搭建神经网络
class Booze(nn.Module):
def __init__(self):
super(Booze, self).__init__()
# inplace参数的意义就是是否将原来的变量的值替换成处理后的结果值 若inplace=True则对原来的变量进行替换,原来变量的值就变了,变成处理后的结果值,若inplace=False则不对原来的变量进行替换,产生的结果需要一个新的变量去接收
# 通常情况下,建议将inplace传入False,这样可以保证原始数据不丢失
self.relu1 = ReLU()
self.sigmoid1 = Sigmoid()
# 重写forward方法
def forward(self,input):
# output = self.relu1(input)
output = self.sigmoid1(input)
return output
obj = Booze()
# 使用tensorboard进行可视化
writer = SummaryWriter('logs')
step = 0
for data in dataloader:
imgs,targets = data
writer.add_images("input",imgs,step)
# 使用神经网络对图片进行处理
output = obj(imgs)
writer.add_images("output",output,step)
step+=1
writer.close()
Sigmoid和RELU的使用方法类似这里就不再赘述。
上述代码运行结果在tensorboard中可视化如下图所示:
非线性激活sigmoid函数处理前后的效果还是比较明显的哈。
总结
非线性变换主要目的就是在我们的网络当中去引入一些非线性特征。因为非线性也多的话,你才能训练出符合各种曲线或者说符合各种特征的一个模型,如果大家都是直愣愣的话,模型的泛化能力就不够好。
边栏推荐
- 2022危险化学品生产单位安全生产管理人员题库及答案
- How to use maixll dock
- [difficult] sqlserver2008r2, can you recover only some files when recovering the database?
- Seven crimes of counting software R & D Efficiency
- ThreeJS开篇
- 数据加载及预处理
- Difference between cookie and session
- Obtain detailed ideas for ABCDEF questions of 2022 American Games
- Daily question - line 10
- Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
猜你喜欢

TASK04|數理統計

The design points of voice dialogue system and the importance of multi round dialogue

The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears

VIM简易使用教程

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

Odeint et GPU

2022危险化学品生产单位安全生产管理人员题库及答案

How to use maixll dock

2022年T电梯修理题库及模拟考试

Dual contractual learning: text classification via label aware data augmentation reading notes
随机推荐
VIM easy to use tutorial
Basic usage, principle and details of session
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
Task04 mathematical statistics
Pytest automated testing - compare robotframework framework
Difference between cookie and session
Daily question - line 10
2022年聚合工艺考试题及模拟考试
2022 polymerization process test questions and simulation test
数据加载及预处理
Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I
为什么香港服务器最适合海外建站使用
Why is Hong Kong server most suitable for overseas website construction
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
Extension fragment
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling