这是一个错误吗? torch.__version__ 是 '0.1.11+b13b701' 。
使用(几乎)最新版本( '0.1.11+8aa1cef'
)对我来说很好
import torch
import torch.nn as nn
from torch.autograd import Variable
y = Variable(torch.rand(5, 3), requires_grad=True)
t = Variable(torch.LongTensor(5).random_(0, 2))
m = nn.MultiMarginLoss()
loss = m(y, t)
loss.backward()
print(y.grad)
输出
Variable containing:
-0.1333 0.0667 0.0667
0.0667 -0.1333 0.0667
0.0667 -0.1333 0.0667
0.0667 -0.1333 0.0667
0.0667 -0.1333 0.0667
[torch.FloatTensor of size 5x3]
你好,
nn.Module
没有后向(它们都没有),它们的前向是使用符合 autograd 的方法实现的,因此会自动区分。
如果你想找到MultiMarginLoss
的实现,这里用 c 实现。
谢谢。 我刚刚开始使用 PyTorch。 我现在明白了。
最有用的评论
你好,
nn.Module
没有后向(它们都没有),它们的前向是使用符合 autograd 的方法实现的,因此会自动区分。如果你想找到
MultiMarginLoss
的实现,这里用 c 实现。