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)

参考内容

1\]torch.nn.functional.softmax https://pytorch.org/docs/2.6/generated/torch.nn.functional.softmax.html#torch.nn.functional.softmax \[2\]torch.nn.Softmax https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html

相关推荐
cyyt18 分钟前
深度学习周报(4.27~5.3)
人工智能·深度学习
ting945200033 分钟前
动手学深度学习(PyTorch版)深度详解(10): 优化算法 全解
人工智能·pytorch·深度学习·算法
LaughingZhu35 分钟前
Product Hunt 每日热榜 | 2026-05-01
人工智能·经验分享·深度学习·产品运营
2zcode1 小时前
基于深度学习的口腔疾病自主诊断系统设计与实现(UI界面+训练代码+数据集)
人工智能·深度学习·口腔疾病
ouliten1 小时前
[Triton笔记1]核心概念
笔记·python·深度学习·triton
薛定e的猫咪1 小时前
OOD 感知决策与可信强化学习:从置信度评估到安全回退
人工智能·安全·机器学习·开源
灵机一物1 小时前
灵机一物AI原生电商小程序、PC端(已上线)-AI产业深度解析:Token供需失衡下的算力战争与产业变革
大数据·人工智能·深度学习
MediaTea1 小时前
ML:逻辑回归的基本原理与实现
人工智能·算法·机器学习·数据挖掘·逻辑回归
wayz111 小时前
Day 19:LSTM与时间序列预测
人工智能·深度学习·lstm
索木木1 小时前
Flash Attention反向梯度优化显存
人工智能·机器学习·大模型·attention·训练·显存优化·aiinfra