用户保存:使用与输入大小不同的目标大小(TORCH.SIZE([1]))(TORCH.SIZE([1,1]))
我有此代码:
actual_loes_score_g = actual_loes_score_t.to(self.device, non_blocking=True)
predicted_loes_score_g = self.model(input_g)
loss_func = nn.L1Loss()
loss_g = loss_func(
predicted_loes_score_g,
actual_loes_score_g,
)
其中 prediction_loes_score_g
is tensor([[[ - 24.9374]],grad_fn =< addmmbackward0>)
and code> and mauth_loes_score_score_gore_g
is 张量([20。],dtype = Torch.float64)
。 (我将批次大小用于调试目的。)
我收到此警告:
火炬/nn/模块/损失。 。这可能会导致由于广播而导致不正确的结果。请确保它们的尺寸相同。
如何正确确保它们的尺寸相同?
我认为这可能是答案:
predicted_loes_score = predicted_loes_score_g.detach()[0]
loss_g = loss_func(
predicted_loes_score,
actual_loes_score_g,
)
但是后来我会遇到此错误:
torch/autograd/__init__.py", line 154, in backward
Variable._execution_engine.run_backward(
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是大小[1,1]的
大小[1],
您需要从预测中删除维度或在目标中添加尺寸。我会推荐后者,因为该额外的尺寸与您的批量大小相对应。尝试以下操作:
which is size [1,1]
which is size [1]
You need to either remove a dimension from your prediction or add a dimension to your target. I would recommend the latter because that extra dimension corresponds to your batch size. Try this: