aabujamra
aabujamra

Reputation: 4636

Python/Keras/Theano - Index out of bounds

I'm new to Keras and having some trouble with shapes, specially when it comes to RNNs and LSTMs.

I'm running this code:

model=Sequential()
model.add(Embedding(input_dim=col,output_dim=70))
model.add(SimpleRNN(init='uniform',output_dim=30))
model.add(Dropout(0.5))
model.add(Dense(1))
model.compile(loss="mse", optimizer="sgd")
model.fit(X=predictor_train, y=target_train, nb_epoch=5, batch_size=1,show_accuracy=True)

I'm having this error:

IndexError: index 143 is out of bounds for size 80
Apply node that caused the error: AdvancedSubtensor1(<TensorType(float32, matrix)>, Flatten{1}.0)
Inputs types: [TensorType(float32, matrix), TensorType(int32, vector)]
Inputs shapes: [(80, 70), (80,)]
Inputs strides: [(280, 4), (4,)]
Inputs values: ['not shown', 'not shown']

I don't understand where is that "index 143" coming from and how do I fix it.

Anyone available to enlight my journey?

Extra info below.

-- EDIT -- This "index 143" actually varies everytime I run the code. The numbers do not follow any apparent logic, the only thing I could notice is that, coincidentally or not, the smallest number that appeared was 80 (I ran the code more than 20 times)




EXTRA INFO


About predictor_train (X)

type: 'numpy.ndarray'

shape: (119,80)

dtype: float64

About target_train (Y)

type: class 'pandas.core.series.Series'

shape: (119,)

dtype: float64

Date
2004-10-01    0.003701
2005-05-01    0.001715
2005-06-01    0.002031
2005-07-01    0.002818
...
2015-05-01   -0.007597
2015-06-01   -0.007597
2015-07-01   -0.007597
2015-08-01   -0.007597

model.summary()

--------------------------------------------------------------------------------
Initial input shape: (None, 80)
--------------------------------------------------------------------------------
Layer (name)                  Output Shape                  Param #             
--------------------------------------------------------------------------------
Embedding (Unnamed)           (None, None, 70)              5600                
SimpleRNN (Unnamed)           (None, 30)                    3030                
Dropout (Unnamed)             (None, 30)                    0                   
Dense (Unnamed)               (None, 1)                     31                  
--------------------------------------------------------------------------------
Total params: 8661
--------------------------------------------------------------------------------

FULL TRACEBACK

File "/Users/file.py", line 1523, in Pred
model.fit(X=predictor_train, y=target_train, nb_epoch=5, batch_size=1,show_accuracy=True)
File "/Library/Python/2.7/site-packages/keras/models.py", line 581, in fit
shuffle=shuffle, metrics=metrics)
File "/Library/Python/2.7/site-packages/keras/models.py", line 239, in _fit
outs = f(ins_batch)
File "/Library/Python/2.7/site-packages/keras/backend/theano_backend.py", line 365, in __call__
return self.function(*inputs)
File "/Library/Python/2.7/site-packages/theano/compile/function_module.py", line 595, in __call__
outputs = self.fn()
File "/Library/Python/2.7/site-packages/theano/gof/vm.py", line 233, in __call__
link.raise_with_op(node, thunk)
File "/Library/Python/2.7/site-packages/theano/gof/vm.py", line 229, in __call__
thunk()
File "/Library/Python/2.7/site-packages/theano/gof/op.py", line 768, in rval
r = p(n, [x[0] for x in i], o)
File "/Library/Python/2.7/site-packages/theano/tensor/subtensor.py", line 1657, in perform
out[0] = x.take(i, axis=0, out=o)
IndexError: index 143 is out of bounds for size 80
Apply node that caused the error: AdvancedSubtensor1(<TensorType(float32, matrix)>, Flatten{1}.0)
Inputs types: [TensorType(float32, matrix), TensorType(int32, vector)]
Inputs shapes: [(80, 70), (80,)]
Inputs strides: [(280, 4), (4,)]
Inputs values: ['not shown', 'not shown']

Upvotes: 2

Views: 1308

Answers (1)

basaundi
basaundi

Reputation: 1795

Your X variable probably contains the value 143. The Embedding layer will be of dimensions 80x70.

I'm assuming this is in the NLP field. This means you have a vocabulary size of 80 words each represented by vectors of length 70. Your X variable represents 119 sentences of length 80 (or 80 sentences of length 119) with its contents representing indices to your vocabulary. If it contains a word index greater than 80 this error will pop-up.

A more common value for your col variable is above 10.000. Of course, it depends on what you are doing.

Upvotes: 3

Related Questions