【发布时间】:2019-11-11 06:53:46
【问题描述】:
当我使用BCELoss 作为我的神经网络的损失函数时,得到ValueError: Target and input must have the same number of elements。
这是我的测试阶段代码(这是一个非常典型的测试阶段代码):
network.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = network(data)
output = output.to(device)
test_loss += loss_function(output, target).item() # error happens here
_, predicted = torch.max(output.data, 1)
correct += (predicted == target).sum().item()
变量output 的形状是[1000, 10],因为有10 目标类(在MNIST 数据集中),变量target 的形状是[1000]它包含测试批次的目标类(测试的批次大小设置为10)。那么,问题是如何应用BCELoss 作为CNN 网络的损失函数?
附言我使用的数据集是由torchvision 库提供的MNIST 数据集。
附言The answer provided to a similar question here 没有针对我的情况提出解决方案。
【问题讨论】:
标签: machine-learning conv-neural-network pytorch mnist torchvision