【发布时间】: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