PyTorch:不同批次的结果略有不同

时间:2019-04-24 13:49:48

标签: python neural-network computer-vision pytorch

我正在使用VGG19网络对CIFAR10图像进行分类,但是对于不同批次大小的数据,我得到的结果略有不同。代码:

net.eval()

f_image = net.forward(Variable(image, requires_grad=True)).data

f_image2 = net.forward(Variable(image[0].unsqueeze(0), requires_grad=True)).data

f_image3 = net.forward(Variable(image[0].unsqueeze(0), requires_grad=True)).data

print(‘f_image - f_image2 = {}’.format((f_image[0] - f_image2).cpu().data.numpy()))
print(‘f_image - f_image3 = {}’.format((f_image[0] - f_image3).cpu().data.numpy()))
print(‘f_image2 - f_image3 = {}’.format((f_image2 - f_image3).cpu().data.numpy()))

具体地说,图像的大小为[batch_size, n_channels, height, width]。我希望f_image[0].unsqueeze(0)f_image2f_image3相同。但是,它们略有不同:

f_image - f_image2 = [[ 0.0000000e+00 3.7252903e-09 -1.8626451e-09 3.7252903e-09
0.0000000e+00 -3.7252903e-09 3.7252903e-09 -1.8626451e-09
3.7252903e-09 -1.3969839e-09]]
f_image - f_image3 = [[ 0.0000000e+00 3.7252903e-09 -1.8626451e-09 3.7252903e-09
0.0000000e+00 -3.7252903e-09 3.7252903e-09 -1.8626451e-09
3.7252903e-09 -1.3969839e-09]]
f_image2 - f_image3 = [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

差别很小,是PyTorch的错误还是预期的行为?

0 个答案:

没有答案