Coderzelf
Coderzelf

Reputation: 752

Lua table insert get same element

this is my code:

 30   for t = 1,testData:size() do
 33       -- get new sample
 34       local input = testData.data[t]
 35       if opt.type == 'double' then input = input:double()
 36       elseif opt.type == 'cuda' then input = input:cuda() end
 37       local target = testData.labels[t]
 38       -- test sample
 39       local pred = model:forward(input)
 40       test_result[t]=pred
 41       
 42       local err = criterion:forward(pred,target)
 43       te_error = te_error+err 
 44    end
 45    print(test_result[1])
 46    print(test_result[2])

and I get the same elements, so my table only have stored the last element, why?

Upvotes: 0

Views: 168

Answers (1)

Oliver
Oliver

Reputation: 29493

I wager that the model:forward(input) is returning a global table. So all testResult will point to the same global table. You can check this by printing pred after it is received: if a global table, it will always have the same "value" (pointer). Verify that model:forward returns a table local to that function.

Upvotes: 1

Related Questions