Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple neural net has different output for inputs in the same batch that are identical #1306

Open
happypepper opened this issue Feb 22, 2018 · 11 comments

Comments

@happypepper
Copy link

In this working standalone code example, an nn is initialized with 2 linear layers.
The inputs are then filled with 0s and passed through the NN.

As you can see, the nn outputs are identical for the first 4 inputs of the batch, but the 5th one is different from the 4th.

require 'nn'
require 'torch'

torch.setdefaulttensortype('torch.FloatTensor')
torch.manualSeed(0)

local my_nn = nn.Sequential()
my_nn:add(nn.Linear(1009, 500))
my_nn:add(nn.Linear(500, 1008))

inputs = torch.FloatTensor(5,1009):fill(0)
values = torch.FloatTensor(5,1008)

values:copy(my_nn:forward(inputs))
print(torch.all(torch.eq(values[1], values[2])))
print(torch.all(torch.eq(values[2], values[3])))
print(torch.all(torch.eq(values[3], values[4])))
print(torch.all(torch.eq(values[4], values[5])))
print(values[4][1008])
print(values[5][1008])

output:

true	
true	
true	
false	
-0.0064480220898986	
-0.0064480230212212
@tastyminerals
Copy link
Contributor

You should read about comparing float numbers first. The output of network is correct.

@happypepper
Copy link
Author

Why is the output correct? What reason is there for the 5th output in the batch to be different than the 4th output in the batch, but 1st - 4th outputs are identical?

There's nothing wrong with floating point comparison, it will just compare to see if the binary representations of each floating point number is equal. In this case, they are indeed equal for 1st - 4th outputs.

@tastyminerals
Copy link
Contributor

tastyminerals commented Feb 22, 2018

Sorry, yeah, haven't read your code.
Running on my machine.

true	
true	
true	
true	
-0.0064480295404792	
-0.0064480295404792

How did you compile it?

@happypepper
Copy link
Author

That is intriguing, do you have an idea why this would happen?

@tastyminerals
Copy link
Contributor

Well, first thing is to make sure you have compiled and installed torch according to official instructions.

@tastyminerals
Copy link
Contributor

Lua5.1, gcc-4.9 linked against system gcc.

@happypepper
Copy link
Author

happypepper commented Feb 22, 2018

I've installed with Lua 5.2, I believe it is supported as per http://torch.ch/docs/getting-started.html

running the code with th example.lua

@tastyminerals
Copy link
Contributor

Since I never used Lua 5.2 with Torch I can only suggest you compile and install Torch with Lua 5.1 and check again. There is absolutely no reason to have Lua 5.2 unless you want to deal with such unexpected behaviour.

@happypepper
Copy link
Author

I just reinstalled with Lua 5.1, but still getting the same result. I'm using mac OS.
How can I check the gcc version used?

@tastyminerals
Copy link
Contributor

tastyminerals commented Feb 22, 2018

gcc --version in terminal.
You wouldn't be able to compile it with newer gcc version so whatever is installed should be correct.
Hmm, strange indeed.

@happypepper
Copy link
Author

happypepper commented Feb 22, 2018

I just ran the same code on linux with Lua 5.2 installed and it gave the correct result.
Can someone else with Mac OS confirm the code also gives incorrect result on their machine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants