我如何通过广播对具有不同维度的 2 个张量进行元素化处理?

How can I element-wise 2 Tensors with different dimensions with Broadcasting?

我有一个名为 inputs 的张量,其 sizetorch.Size([20, 1, 161, 199]),另一个 mask 大小为 torch.Size([20, 1, 199])。我想把它们相乘。

我试过了:

masked_inputs = inputs * mask[..., None]

但出现错误:

RuntimeError: The size of tensor a (161) must match the size of tensor b (199) at non-singleton dimension 2

我不太确定该怎么做?

做到了:

masked_inputs = inputs * mask.unsqueeze(2)