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