当前位置:网站首页>PyTorch中 torch.nn与torch.nn.functional的区别
PyTorch中 torch.nn与torch.nn.functional的区别
2022-07-26 14:31:00 【代码的路】
torch.nn
pytorch中文文档链接:torch.nn
在__init__()函数里定义,定义的是一个类:
torch.nn.functional
pytorch中文文档链接:torch.nn.functional
在__forward()__函数里定义,定义的是一个函数:
两者的区别
torch.nn中是一个定义的类,以class xx来定义的,可以提取变化的学习参数。
torch.nn.functional是一个函数,由def function( )定义,是一个固定的运算公式。
深度学习中会有很多权重是在不断更新的,所以需要采用类的方式,以确保能在参数发生变化时仍能使用我们之前定好的运算步骤。因此如果模型有可学习的参数,应该使用nn.Module,否则两个没有区别。但是简单的计算不需要新建一个类来做,所以使用nn.functional定义函数就可以。
即:层内有variable的情况用nn定义,否则用nn.functional定义。
学习更多编程知识,请关注我的公众号:

边栏推荐
- Use cpolar to build a commercial website (apply for website security certificate)
- Would you please refer to the document of Database specification?
- Iscc2021 lock problem solution
- Learning basic knowledge of Android security
- 『云原生』KubeSphere可插拔组件之DevOps系统
- Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11
- 请问下大家,flink sql有没有办法不输出update_before?
- [ostep] 04 virtualized CPU - process scheduling strategy
- [Yugong series] July 2022 go teaching course 017 - if of branch structure
- 1对1直播源码——1对1语音聊天源码
猜你喜欢

Realize the full link grayscale based on Apache APIs IX through MSE

1-to-1 live broadcast source code - 1-to-1 voice chat source code

GOM登录器配置免费版生成图文教程

SiamRPN++:深层网络连体视觉跟踪的演变

MLX90640 红外热成像仪测温传感器模块开发笔记(六)

Unity learning notes – infinite map

智能家居行业发展,密切关注边缘计算和小程序容器技术

《MySQL高级篇》五、InnoDB数据存储结构

目标跟踪相关知识总结

Pdf translation, which translation company in Beijing is good
随机推荐
uni-app从创建到运行到微信开发者工具
Tips for unity transparent channel
基于CAS的SSO单点登录环境搭建
Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
目标跟踪相关知识总结
unity透明通道的小技巧
Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11
Mysql-03 database operation
嵌入式开发:调试嵌入式软件的技巧
Keyboard shortcut to operate the computer (I won't encounter it myself)
Summary and analysis of image level weakly supervised image semantic segmentation
UE4 智能指针和弱指针
基于CAS的SSO单点服务端配置
UE4 smart pointer and weak pointer
Low power multi-channel wfas1431 wireless data acquisition and transmission instrument operation process description
Parsing XML files using Dom4j
Sqldeveloper tools quick start
Annotation and reflection
【文件上传漏洞-06】分布式配置文件攻击实验—以upload-labs-4为例
研发了 5 年的时序数据库,到底要解决什么问题?