![](/img/trans.png)
[英]RuntimeError: The size of tensor a (128) must match the size of tensor b (256) at non-singleton dimension 3
[英]How to resolve `RuntimeError: The size of tensor a (3) must match the size of tensor b (128) at non-singleton dimension 1` for SNN?
什么是 PyTorch 中的损失 function 可以让我计算多目标问题的损失? 我有三个目标变量。 我看到了BCEWithLogitsLoss()
的建议,但它产生了这个错误:
RuntimeError: The size of tensor a (3) must match the size of tensor b (128) at non-singleton dimension 1
我也在研究一个尖峰神经网络。 上面的RunTimeError
被抛出acc = np.mean((targets == idx).detach().cpu().numpy())
。 我实际上这不是 function 的损失问题,而是 function 我必须打印批量精度:
def print_batch_accuracy(data, targets, train = False):
output, _ = net(data.view(batch_size, -1))
_, idx = output.sum(dim = 0).max(1)
print(targets)
acc = np.mean((targets == idx).detach().cpu().numpy())
if train:
print(f"Train set accuracy for a single minibatch: {acc * 100:.2f}%")
else:
print(f"Test set accuracy for a single minibatch: {acc * 100:.2f}%")
我的批次的形状是Float
类型的torch.Size([25, 128, 3])
。
该错误是由于精度打印机 function 不是为多目标分类而设计的。 我猜你的目标张量的第一个维度对应于正确类的总数(3),而 function 预计第一个暗淡是批量大小(128)。
function _, idx = output.sum(dim = 0).max(1)
返回具有最大数量的尖峰的神经元。 然后根据目标(targets == idx)
进行检查,这意味着正在测量单目标问题的准确性。 这应该被修改以检查所有可能的正确类。
至于您关于合适的损失函数的问题, BCEWithLogitsLoss()
可以应用于累积的 output 尖峰并且效果很好。
或者,每个 output 神经元可能有一个目标尖峰计数,该目标尖峰计数使用MSELoss()
与实际尖峰计数进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.