Different results with same input

Hi,

I have an issue when I pass the same image in the model, I have different output.
I definitively did something wrong, but can you help me?

a = transforms.ToTensor()(transforms.Scale(225)(imagesList[0]))
t = torch.Tensor(2,3,224,224)
t[0] = a
t[1] = a
mod = models.alexnet(pretrained=True)
out = mod(Variable(t))

print(out[0])
print(out[1])
print((out.data[0] == out.data[1]).all())

I have this output :

Variable containing:
-1.5060
0.1141
1.3188

-4.5860
-0.8714
4.6969
[torch.FloatTensor of size 1000]

Variable containing:
-1.6095e+00
-1.8007e-01
2.6349e+00

-5.1909e+00
-2.2386e+00
5.0429e+00
[torch.FloatTensor of size 1000]

False

EDIT : I have the same problem if I do two forward pass with a batch of 1

That’s because there’s Dropout inside the model and it applies a different mask to each batch element at each forward. If you want to disable it call model.eval() to put it in evaluation mode.


As a side note, don’t use == to compare floats. Floating point operations are slightly inaccurate and can differ by very small amounts. Use (tensor1 - tensor2).abs().max() to find the maximal difference between the outputs (and assume they’re equal if it’s very small, e.g. 1e-10).

3 Likes