0

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?

9
  • What is model:forward returning? Is it returning a static table? I assume you are saying you get the same value for all entries in test_result and not just that those two are the same (since without knowing the data it is certainly possible for those two to legitimately be the same I would think). Commented Jul 11, 2014 at 18:05
  • HI the pred is a float vector and it did change every time, but when I insert them to table, the table contains all duplicate items Commented Jul 11, 2014 at 18:09
  • How did you test that you were getting different vectors each time? Is the vector value you are ending up with the first vector? Some random vector? The last vector? Commented Jul 11, 2014 at 20:13
  • I did print 'pred' out every step and the table ends up with many last 'pred' Commented Jul 11, 2014 at 20:38
  • Shouldn't testData.data[t] be testData[t].data? Commented Jul 11, 2014 at 20:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

everytime pred is different, but after save/load they are all same
@Coderzelf Do you mean that the content of pred is different or the pred "pointer" is different. Could you update your question to show what pred looks like just after return from model:forward and again just after criterion:forward?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.