当前位置:网站首页>神经网络-非线性激活
神经网络-非线性激活
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函数处理前后的效果还是比较明显的哈。
总结
非线性变换主要目的就是在我们的网络当中去引入一些非线性特征。因为非线性也多的话,你才能训练出符合各种曲线或者说符合各种特征的一个模型,如果大家都是直愣愣的话,模型的泛化能力就不够好。
边栏推荐
- Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
- Summary of acl2021 information extraction related papers
- 1. Mobile terminal touch screen event
- 2022年化工自动化控制仪表操作证考试题库及答案
- [deep learning] (4) decoder mechanism in transformer, complete pytoch code attached
- 2022年煤气考试题库及在线模拟考试
- Ten wastes of software research and development: the other side of research and development efficiency
- Learn Chapter 20 of vue3 (keep alive cache component)
- Task04 | statistiques mathématiques
- 2022危险化学品生产单位安全生产管理人员题库及答案
猜你喜欢

Threejs opening

Introduction to JVM stack and heap

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022

VR线上展览所具备应用及特色

Obtain detailed ideas for ABCDEF questions of 2022 American Games

TCP server communication flow

Pytorch(四) —— 可视化工具 Visdom

2022 t elevator repair question bank and simulation test

Possible problems and solutions of using scroll view to implement slider view
随机推荐
VR线上展览所具备应用及特色
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
[godot] unity's animator is different from Godot's animplayer
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
CUDA development and debugging tool
细数软件研发效能的七宗罪
Grey correlation cases and codes
尺取法:有效三角形的个数
What is uid? What is auth? What is a verifier?
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
PgSQL failed to start after installation
[Master / slave] router election in DD message
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
How to view the changes and opportunities in the construction of smart cities?
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
The design points of voice dialogue system and the importance of multi round dialogue
C language games (I) -- guessing games
软件研发的十大浪费:研发效能的另一面
How to choose the right server for website data collection?
TASK04|數理統計