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

Index out of range error #279

Open
RaviTej310 opened this issue Jun 23, 2016 · 4 comments
Open

Index out of range error #279

RaviTej310 opened this issue Jun 23, 2016 · 4 comments

Comments

@RaviTej310
Copy link

RaviTej310 commented Jun 23, 2016

I am trying to implement a different version of this simple recurrent network which accepts three image of 200x200 and predicts a fourth. I modified it as:

require 'rnn'
require 'image'

-- hyper-parameters 
batchSize = 1
rho = 3 -- sequence length
hiddenSize = 7
nIndex = 10
lr = 0.1


-- build simple recurrent neural network
local r = nn.Recurrent(
   hiddenSize, nn.LookupTable(nIndex, hiddenSize), 
   nn.Linear(hiddenSize, hiddenSize), nn.Sigmoid(), 
   rho
)

local rnn = nn.Sequential()
   :add(r)
   :add(nn.Linear(hiddenSize, nIndex))
   :add(nn.LogSoftMax())

-- wrap the non-recurrent module (Sequential) in Recursor.
-- This makes it a recurrent module
-- i.e. Recursor is an AbstractRecurrent instance
rnn = nn.Recursor(rnn, rho)

print(rnn)

-- build criterion

criterion = nn.ClassNLLCriterion()

offsets = {}
for i=1,batchSize do
   table.insert(offsets, math.ceil(math.random()*sequence:size(1)))
end
--print(offsets)
offsets = torch.LongTensor(offsets)
--print(offsets)

-- training
local iteration = 1
count=1
datasetname = '/home/srilatha/Desktop/Research_intern/Data_sets/Cropped_data_set'
j=1
dir_name=datasetname.."/"..j.."/"
image_size=200

while count>0 do
   -- 1. create a sequence of rho time-steps

   local inputs, targets = {}, {}
   for step=1,rho do
      filename=dir_name..step..".JPG"
      filename1=dir_name..(step+1)..".JPG" 
      img1=image.load(filename)
      img1=image.scale(img1,image_size,image_size)
      img1=torch.reshape(img1,1,image_size*image_size)
      inputs[step]=img1
      img2=image.load(filename1)
      img2=image.scale(img2,image_size,image_size)
      img2=torch.reshape(img2,1,image_size*image_size)
      targets[step] = img2
   end
   print(inputs[2][1][1])
   print(targets[1][1][1])

   -- 2. forward sequence through rnn

   rnn:zeroGradParameters() 
   rnn:forget() -- forget all past time-steps

   --print(rnn:forward(inputs[1][1]))
   local outputs, err = {}, 0
   for step=1,rho do
      outputs[step] = rnn:forward(inputs[step])
      print(outputs[step])
      err = err + criterion:forward(outputs[step], targets[step])
   end

   print(string.format("Iteration %d ; NLL err = %f ", iteration, err))

   -- 3. backward sequence through rnn (i.e. backprop through time)

   local gradOutputs, gradInputs = {}, {}
   for step=rho,1,-1 do -- reverse order of forward calls
      gradOutputs[step] = criterion:backward(outputs[step], targets[step])
      gradInputs[step] = rnn:backward(inputs[step], gradOutputs[step])
   end

   -- 4. update

   rnn:updateParameters(lr)

   iteration = iteration + 1
   count=count-1
end

But I'm getting an index out of range error:

/home/srilatha/torch/install/share/lua/5.1/nn/Container.lua:67: 
In 1 module of nn.Sequential:
In 1 module of nn.Sequential:
.../srilatha/torch/install/share/lua/5.1/nn/LookupTable.lua:73: index out of range at /tmp/luarocks_torch-scm-1-3482/torch7/lib/TH/generic/THTensorMath.c:156
stack traceback:
    [C]: in function 'index'
    .../srilatha/torch/install/share/lua/5.1/nn/LookupTable.lua:73: in function <.../srilatha/torch/install/share/lua/5.1/nn/LookupTable.lua:68>
    [C]: in function 'xpcall'
    /home/srilatha/torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors'
    ...e/srilatha/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'updateOutput'
    ...e/srilatha/torch/install/share/lua/5.1/rnn/Recurrent.lua:68: in function <...e/srilatha/torch/install/share/lua/5.1/rnn/Recurrent.lua:64>
    [C]: in function 'xpcall'
    /home/srilatha/torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors'
    ...e/srilatha/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'updateOutput'
    /home/srilatha/torch/install/share/lua/5.1/rnn/Recursor.lua:25: in function 'forward'
    /home/srilatha/Desktop/Research_intern/rnn_simple.lua:75: in main chunk
    [C]: in function 'dofile'
    [string "_RESULT={dofile('/home/srilatha/Desktop/Resea..."]:1: in main chunk
    [C]: in function 'xpcall'
    /home/srilatha/torch/install/share/lua/5.1/trepl/init.lua:651: in function </home/srilatha/torch/install/share/lua/5.1/trepl/init.lua:560>
    [string "require('trepl')()"]:1: in main chunk

What is going wrong and how can I fix this?

@nicholas-leonard
Copy link
Member

nicholas-leonard commented Jun 24, 2016

@RaviTej310 You cannot use a LookupTable with images. You should instead use a Linear or SpatialConvolution. PR #280 is a very rough draft of how to use SpatialConvolution with Recurrence. It does not run, but the part about building the model should give you an idea.

@scalastic
Copy link

scalastic commented Jul 22, 2016

Sorry but I get the same error with LSTM on stock indices (not images) :

In 1 module of nn.Sequential:
...DEV/TOOLS/torch/install/share/lua/5.1/nn/LookupTable.lua:73: index out of range at /Users/jeanjerome/DEV/TOOLS/torch/pkg/torch/lib/TH/generic/THTensorMath.c:156
stack traceback:
    [C]: in function 'index'
    ...DEV/TOOLS/torch/install/share/lua/5.1/nn/LookupTable.lua:73: in function <...DEV/TOOLS/torch/install/share/lua/5.1/nn/LookupTable.lua:68>
    [C]: in function 'xpcall'
    ...e/DEV/TOOLS/torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors'
    .../DEV/TOOLS/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'forward'
    ...anjerome/DEV/PROJECTS/FINAPP/FIN_POC/POC-05/src/main.lua:348: in function 'opfunc'
    ...rome/DEV/TOOLS/torch/install/share/lua/5.1/optim/sgd.lua:44: in function 'optimMethod'
    ...anjerome/DEV/PROJECTS/FINAPP/FIN_POC/POC-05/src/main.lua:372: in function 'train'
    ...anjerome/DEV/PROJECTS/FINAPP/FIN_POC/POC-05/src/main.lua:478: in main chunk
    [C]: at 0x010b097cd0

Almost the same stack :(

The problem occurs when feeding the LookupTable on mini-batch.

Could someone points us to a relevant example (with at least dim-2d input Tensor not torch.randperm() please.......!!!) ?

Many thanks

@nicholas-leonard
Copy link
Member

@scalastic Usually, index out of range error occurs when you have an invalid index. For example, with a lookup table of 100 indices, only indexes between 1 and 100 will be valid. Here is a 2d example:

-- 3 x 4 tensor of indices randomly sampled between 1 and 100
input = torch.LongTensor(3,4):random(1,100)
-- lookup table consisting of 100 embeddings of 5 units each:
lookup = nn.LookupTable(100, 5)
-- forward indices through
lookup:forward(input)
(1,.,.) = 
 -0.3788  0.7058  0.4782 -1.0016  0.3914
  0.6950 -1.1974  1.4737  0.0400  0.3055
 -0.4760 -0.7315  0.3597 -0.8322 -0.8582
  0.1845 -0.9815 -0.6764 -0.4099  2.3405

(2,.,.) = 
 -0.4075 -0.3032  0.4292  0.6594  1.1332
 -0.3993  1.6485 -0.2334 -0.4767 -1.1654
  0.2760  0.8907  0.4681  0.2643  0.7932
 -0.6268  0.6494 -1.2751  2.0526 -0.3467

(3,.,.) = 
 -0.2379 -0.5717 -0.7413  0.9472  0.6435
 -1.1906 -1.0219  1.1680 -0.3493 -0.0208
  0.1248  0.5194 -1.0458  1.8812  0.0879
 -1.4082 -0.2250 -0.4062 -0.8765 -0.2728
[torch.DoubleTensor of size 3x4x5]

Here is an example of a similar error (line 75 instead of 73 because 2d instead of 1d):

-- out of range (102 > 100)
input[1][1] = 102
lookup:forward(input)
/usr/local/share/lua/5.1/nn/LookupTable.lua:75: index out of range at /tmp/luarocks_torch-scm-1-9702/torch7/lib/TH/generic/THTensorMath.c:156
stack traceback:
    [C]: in function 'index'
    /usr/local/share/lua/5.1/nn/LookupTable.lua:75: in function 'forward'
    [string "_RESULT={lookup:forward(input)}"]:1: in main chunk
    [C]: in function 'xpcall'
    /usr/local/share/lua/5.1/trepl/init.lua:651: in function 'repl'
    /usr/local/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
    [C]: at 0x00405e60  

@scalastic
Copy link

@nicholas-leonard Thank you for your answer. I'm on the move right now, I'll watch all this in few days and I'll give you my feedback.

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