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

attempt to call method 'random' (a nil value) #127

Open
jKhurana opened this issue Jan 23, 2017 · 1 comment
Open

attempt to call method 'random' (a nil value) #127

jKhurana opened this issue Jan 23, 2017 · 1 comment

Comments

@jKhurana
Copy link

Below is my code

require 'dpnn'
 require 'cunn'
 
 local deviceNumber = tonumber(os.getenv("CUDA_CARD_NR"))
 cutorch.setDevice(deviceNumber)
 
 local module = nn.Sequential():cuda()
 module:add(nn.Linear(2,1):cuda())
 module:add(nn.Sigmoid():cuda())
 
 criterion = nn.BCECriterion():cuda() -- Binary Cross Entorpy Criteria
 
 local targets = torch.CudaTensor(10):random(0,1)
 local inputs = torch.CudaTensor(10,2):uniform(-1,1)
 
 function trainEpoch(module,criterion,inputs,targets)
   for i=1,inputs:size(1) do
     local idx = math.random(1,inputs:size(1))
     local input, target = inputs[idx], targets:narrow(1,idx,1)
     -- forward
     local output= module:forward(input)
     local loss= criterion:forward(output,target)
     -- backward
     local gradOutput = criterion:backward(output,target)
     module:zeroGradParameters()
     local gradInput = module:backward(input,gradOutput)
     --update
     module:updateGradParameters(0.9) -- momentum
     module:updateParameters(0.1) -- W = W -0.1*dL/dW
   end
 end
 
 for i=1,100 do
   trainEpoch(module,criterion,inputs,targets)
 end

I am running above using the following command

`CUDA_CARD_NR=1 luajit feedforwad.lua 

`
It gives the following error

    luajit: feedforwad.lua:13: attempt to call method 'random' (a nil value)
    stack traceback:
    	feedforwad.lua:13: in main chunk
    	[C]: at 0x004064f0

I know that there is some error in the line

`local targets = torch.CudaTensor(10):random(0,1)`

But I am not able to figure out. Any help would be appreciated.
Thanks in advance.

@Germanunkol
Copy link
Owner

Hi,
I've only used torch.Tensor, nt CudaTensor.
I found this issue where they mention that the random API is not fully implemented in CudaTorch.
Can you try to use targets = torch.Tensor( .. ) instead, and later transfer it to the GPU by running targets:cuda()? That's how I've done training on the GPU.
Just as a note: None of this has anything to do with trAInsported code, though. You'll probably be getting more helpful comments on torch forums.

P.S. Training the trains with torch - awesome idea! I look forward to seeing the results! :)

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