【问题标题】: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?对于 SNN,如何解决`RuntimeError:张量 a (3) 的大小必须与非单维 1 的张量 b (128) 的大小相匹配?
【发布时间】:2022-01-14 18:10:20
【问题描述】:

什么是 PyTorch 中的损失函数,可以让我计算多目标问题的损失?我有三个目标变量。我看到了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())。我实际上并不认为这是损失函数的问题,而是我必须打印批量准确度的函数:

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}%")

我的批次的形状是torch.Size([25, 128, 3]),类型为Float

【问题讨论】:

  • 是您的target 标签索引吗?你能把你的target也贴上来吗

标签: python deep-learning pytorch tensor loss


【解决方案1】:

该错误是由于精度打印机功能不是为多目标分类而设计的。我猜你的目标张量的第一个维度对应于正确类的总数(3),而函数期望第一个暗淡是批量大小(128)。

函数_, idx = output.sum(dim = 0).max(1) 正在返回具有最多尖峰数的神经元。然后根据目标(targets == idx) 进行检查,这意味着正在针对单目标问题测量准确性。这应该被修改以检查所有可能的正确类。

至于您关于合适的损失函数的问题,BCEWithLogitsLoss() 可以应用于累积的输出峰值,并且效果很好。

或者,每个输出神经元可能有一个目标尖峰计数,该目标尖峰计数使用MSELoss()与实际尖峰计数进行比较。

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 2019-11-09
    • 2020-12-18
    • 2020-12-13
    • 2021-09-04
    • 2021-01-26
    • 2020-11-24
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多