Keras: Getting IndexError

Created on 27 Oct 2015  路  5Comments  路  Source: keras-team/keras

I am very new to keras. Trying to build a binary classifier for an NLP task. (My code is motivated from imdb example - https://github.com/fchollet/keras/blob/master/examples/imdb_cnn.py)

below is my code snippet:

max_features = 30
maxlen = 30
batch_size = 32
embedding_dims = 30
nb_filter = 250
filter_length = 3
hidden_dims = 250
nb_epoch = 3

(Train_X, Train_Y, Test_X, Test_Y) = load_and_split_data()
model = Sequential()
model.add(Embedding(max_features, embedding_dims, input_length=maxlen))
model.add(Convolution1D(nb_filter=nb_filter,filter_length=filter_length,border_mode="valid",activation="relu",subsample_length=1))
model.add(MaxPooling1D(pool_length=2))
model.add(Flatten())
model.add(Dense(hidden_dims))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', class_mode="binary")
fitlog = model.fit(Train_X, Train_Y, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2)

When I run model.fit(), I get the following:

/.virtualenvs/nnet/lib/python2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    857         t0_fn = time.time()
    858         try:
--> 859             outputs = self.fn()
    860         except Exception:
    861             if hasattr(self.fn, 'position_of_error'):

IndexError: One of the index value is out of bound. Error code: 65535.\n
Apply node that caused the error: GpuAdvancedSubtensor1(<CudaNdarrayType(float32, matrix)>, Elemwise{Cast{int64}}.0)
Toposort index: 47
Inputs types: [CudaNdarrayType(float32, matrix), TensorType(int64, vector)]
Inputs shapes: [(30, 30), (3840,)]
Inputs strides: [(30, 1), (8,)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[GpuReshape{3}(GpuAdvancedSubtensor1.0, MakeVector{dtype='int64'}.0)]]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

Can you please help me resolve this ?

Most helpful comment

model.add(Embedding(max_features, embedding_dims, input_length=maxlen))

Either one of the 3 arguments above isn't matching your training data. Most likely max_features.

All 5 comments

model.add(Embedding(max_features, embedding_dims, input_length=maxlen))

Either one of the 3 arguments above isn't matching your training data. Most likely max_features.

My dataset has 40K records in Train data and 10K records in Test data. Largest vector across both train and test is of size 26. To bring in uniformity, I am padding them up as follows:

set parameters:
max_features = 30
maxlen = 30

Train_X = sequence.pad_sequences(Train_X,maxlen=maxlen)
Test_X = sequence.pad_sequences(Test_X,maxlen=maxlen)

model.add(Embedding(max_features, embedding_dims, input_length=maxlen))

Am I right in setting both max_features = 30, length of sequence = 30, embedding_dims = 200 ??
Happy to share my code file

I got this. Value of max_features was wrong. Closing this thread. Thanks

I am getting the same error, how did you fix it ?

Always add 1 to max_features!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anjishnu picture anjishnu  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments

braingineer picture braingineer  路  3Comments

nryant picture nryant  路  3Comments

snakeztc picture snakeztc  路  3Comments