用户保存:使用与输入大小不同的目标大小(TORCH.SIZE([1]))(TORCH.SIZE([1,1]))

发布于 2025-01-25 00:53:08 字数 1111 浏览 3 评论 0 原文

我有此代码:

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

I have this code:

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,
)

where predicted_loes_score_g is tensor([[-24.9374]], grad_fn=<AddmmBackward0>) and actual_loes_score_g is tensor([20.], dtype=torch.float64). (I am using a batch size of 1 for debugging purposes.)

I am getting this warning:

torch/nn/modules/loss.py:96: UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([1, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

How do I correctly ensure they have the same size?

I thought this might be the answer:

predicted_loes_score = predicted_loes_score_g.detach()[0]
loss_g = loss_func(
    predicted_loes_score,
    actual_loes_score_g,
)

but then I get this error later:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2025-02-01 00:53:08
predicted_loes_score_g = tensor([[-24.9374]], grad_fn=<AddmmBackward0>)

这是大小[1,1]的

actual_loes_score_g = tensor([20.], dtype=torch.float64)

大小[1],

您需要从预测中删除维度或在目标中添加尺寸。我会推荐后者,因为该额外的尺寸与您的批量大小相对应。尝试以下操作:

actual_loes_score_g =  actual_loes_score_g.unsqueeze(1)
predicted_loes_score_g = tensor([[-24.9374]], grad_fn=<AddmmBackward0>)

which is size [1,1]

actual_loes_score_g = tensor([20.], dtype=torch.float64)

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:

actual_loes_score_g =  actual_loes_score_g.unsqueeze(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文