torch.nn和torch.nn.function的区别

torch.nn一般作为class提供,需要实例化后使用

torch.nn.functional一般作为函数提供,无需实例化可直接使用

例:

python 复制代码
import torch
import torch.nn.functional as F

x = torch.tensor([[1,2,3], [1,2,3], [1,2,3], [1,2,3]], dtype = torch.double)
nn_softmax = torch.nn.Softmax(dim=0)

out_nn = nn_softmax(x)
out_F = F.softmax(x, dim=0)

参考内容

1torch.nn.functional.softmax

https://pytorch.org/docs/2.6/generated/torch.nn.functional.softmax.html#torch.nn.functional.softmax

2torch.nn.Softmax

https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html

相关推荐
好评笔记2 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466852 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
小糖学代码3 小时前
LLM系列:环境搭建:5.Python-dotenv 环境变量管理
人工智能·python·深度学习·神经网络
Omics Pro4 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
voidmort4 小时前
3. 微调(Fine-tuning)与强化学习(RL)的核心思想
python·深度学习·算法
keyanbanyungong5 小时前
告别杂乱病历!临床科研AI工具实测
人工智能·深度学习
明志数科6 小时前
工业场景数据标注跟实验室标注有什么不同
人工智能·机器学习
xiaoxiaoxiaolll6 小时前
《Light: Science & Applications》合并BIC实现80倍阈值单模运行:超紧凑光子晶体激光器新突破
人工智能·算法·机器学习
悟乙己7 小时前
因果机器学习DML效果与应用场景探索
人工智能·机器学习
z小猫不吃鱼7 小时前
13 Scaling Law 入门:模型规模、数据规模和计算量是什么关系?
人工智能·深度学习·机器学习