I was working on generating an XOR gate dataset with torch7. But when i printed the dataset i saw that the data was wrong, but i could not find the bug. There seems to be nothing wrong with the code. But I'm new to torch, so mistakes can happen.
So, here is my code
input = torch.Tensor (4,2)
input:random(0,1)
output = torch.Tensor(1)
dataset={};
function dataset:size() return 4 end
for i=1,dataset:size() do
if input[i][1]==input[i][2] then
output[1] = 0
else
output[1] = 1
end
print("original")
print(input[i][1].." "..input[i][2].." "..output[1]) -- the values that are going to dataset
dataset[i] = {input[i], output}
print("dataset")
print(dataset[i][1][1].." "..dataset[i][1][2].." "..dataset[i][2][1]) -- for double checking i read from dataset again
end
print("Why dataset is different now?")
for i=1,4 do
print(dataset[i][1][1].." "..dataset[i][1][2].." "..dataset[i][2][1]) -- So, why this is different?
end
As you can see, I printed the values that are being inserted into the dataset
list and for double checking i read from dataset
again.
And finally i checked from dataset
after full insertion. The dataset was different somehow. I ran couple of times. Every time it was different. Like it was stuck on 1 or 0.
So here is my output
original
1 0 1
dataset
1 0 1
original
0 0 0
dataset
0 0 0
original
1 1 0
dataset
1 1 0
original
0 0 0
dataset
0 0 0
Why dataset is different now?
1 0 0
0 0 0
1 1 0
0 0 0
As you can see, the format is like this
input input output
I printed original when i read from input[i] and output.
I printed dataset when i read from dataset, after being inserted.
Also you can see that the first set of values are different when i printed. It should be 1 0 1. But it is 1 0 0.
I could not find the bug in my code. Can anyone help? If the question is not clear please let me know.
Problem is here: dataset[i] = {input[i], output}
You're not saving calculated result, you're saving reference to value that is changed with subsequent calculations for 'xor' function.
Naturally, when you read result, you're always getting the same number - last result written to output[1]
To fix it, either change output
variable to store actual temporary value (not table), or at least read actual value from output table when saving to dataset[i], do not just save link to table, you won't get deep copy that way.