PyTorch之nn.ReLU与F.ReLU的区别介绍
作者:鹊踏枝-码农 时间:2021-01-19 04:47:24
我就废话不多说了,大家还是直接看代码吧~
import torch.nn as nn
import torch.nn.functional as F
import torch.nn as nn
class AlexNet_1(nn.Module):
def __init__(self, num_classes=n):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
def forward(self, x):
x = self.features(x)
class AlexNet_2(nn.Module):
def __init__(self, num_classes=n):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
)
def forward(self, x):
x = self.features(x)
x = F.ReLU(x)
在如上网络中,AlexNet_1与AlexNet_2实现的结果是一致的,但是可以看到将ReLU层添加到网络有两种不同的实现,即nn.ReLU和F.ReLU两种实现方法。
其中nn.ReLU作为一个层结构,必须添加到nn.Module容器中才能使用,而F.ReLU则作为一个函数调用,看上去作为一个函数调用更方便更简洁。具体使用哪种方式,取决于编程风格。
在PyTorch中,nn.X都有对应的函数版本F.X,但是并不是所有的F.X均可以用于forward或其它代码段中,因为当网络模型训练完毕时,在存储model时,在forward中的F.X函数中的参数是无法保存的。
也就是说,在forward中,使用的F.X函数一般均没有状态参数,比如F.ReLU,F.avg_pool2d等,均没有参数,它们可以用在任何代码片段中。
补充知识:pytorch小知识点——in-place operation
一、什么是in-place
在pytorch的很多函数中经常看到in-place选项,具体是什么意思一直一知半解。这次专门来学习一下,in-place operation在pytorch中是指改变一个tensor的值的时候,不经过复制操作,而是直接在原来的内存上改变它的值。可以把它称为原地操作符。
在pytorch中经常加后缀“_”来代表原地in-place operation,比如说.add_() 或者.scatter()。我们可以将in_place操作简单的理解类似于python中的"+=","-="等操作。
举个例子,下面是正常的加操作,执行结束后x的值没有变化
import torch
x = torch.rand(2)
x
Out[3]: tensor([0.3486, 0.2924]) #<-----这是x初始值
y = torch.rand(2)
y
Out[5]: tensor([0.6301, 0.0101]) #<-----这是y初始值
x.add(y)
Out[6]: tensor([0.9788, 0.3026]) #<-----这是x+y的结果
x
Out[7]: tensor([0.3486, 0.2924]) #<-----这是执行操作之后x的值
y
Out[8]: tensor([0.6301, 0.0101]) #<-----这是执行操作之后y的值
我们可以发现,在正常操作之后原操作数的值不会发生变化。
下面我们来看看in_place操作
import torch
x = torch.rand(2)
x
Out[3]: tensor([0.3486, 0.2924]) #<-----这是x初始值
y = torch.rand(2)
y
Out[5]: tensor([0.6301, 0.0101]) #<-----这是y初始值
x.add_(y)
Out[9]: tensor([0.9788, 0.3026]) #<-----这是x+y结果
x
Out[10]: tensor([0.9788, 0.3026]) #<-----这是操作后x的值
y
Out[11]: tensor([0.6301, 0.0101]) #<-----这是操作后y的值
通过对比可以发现,in_place操作之后,原操作数等于表达式计算结果。也就是说将计算结果赋给了原操作数。
二、不能使用in-place的情况
对于 requires_grad=True 的 叶子张量(leaf tensor) 不能使用 inplace operation
对于在 求梯度阶段需要用到的张量 不能使用 inplace operation
来源:https://blog.csdn.net/u011501388/article/details/86602275
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用技巧-查找文件
使用python接入微信聊天机器人
Python中pycharm编辑器界面风格修改方法
![](https://img.aspxhome.com/file/2023/3/75263_0s.jpg)
python实现巡检系统(solaris)示例
解决使用export_graphviz可视化树报错的问题
![](https://img.aspxhome.com/file/2023/2/84902_0s.jpg)
python自动登录12306并自动点击验证码完成登录的实现源代码
详解python列表生成式和列表生成式器区别
在pycharm中显示python画的图方法
![](https://img.aspxhome.com/file/2023/0/80600_0s.jpg)
通过VB6将ASP编译封装成DLL组件最简教程 附全部工程源文件
Flask 让jsonify返回的json串支持中文显示的方法
![](https://img.aspxhome.com/file/2023/8/93468_0s.jpg)
完全讲解 使用MSCS建立SQL Server集群
Python 使用指定的网卡发送HTTP请求的实例
asp三天学好ADO对象之第三天
Python项目管理Git常用命令详图讲解
![](https://img.aspxhome.com/file/2023/1/64311_0s.png)
php替换字符串中间字符为省略号的方法
用 Django 开发一个 Python Web API的方法步骤
go语言csrf库使用实现原理示例解析
![](https://img.aspxhome.com/file/2023/7/97167_0s.jpg)
python 密码验证(滑块验证)
![](https://img.aspxhome.com/file/2023/1/67361_0s.png)
虚拟环境下搭建一个Django项目
![](https://img.aspxhome.com/file/2023/8/61168_0s.png)
Python深度学习albumentations数据增强库
![](https://img.aspxhome.com/file/2023/2/75372_0s.jpg)