Keras: IndexError: index 197 is out of bounds for axis 1 with size 2

Created on 21 Aug 2015  路  3Comments  路  Source: keras-team/keras

Hey there,

I'm having some issues with np_utils.py.

Traceback (most recent call last):
  File "/home/osheak/Documents/neural-network-plant-trait-classification/software/neuralNetworkSoftware/serialiseAllTheData/serialise.py", line 27, in <module>
    (directory,num_classes,custom_height,custom_width, split, True)
  File "../../pyvec/pyvec/images/dataset.py", line 212, in vectorise
    label = np_utils.to_categorical(train_label, nb_classes)
  File "/usr/local/lib/python2.7/dist-packages/keras/utils/np_utils.py", line 16, in to_categorical
    Y[i, y[i]] = 1.
IndexError: index 197 is out of bounds for axis 1 with size 2

Process finished with exit code 1

Naughty code.

def to_categorical(y, nb_classes=None):
    '''Convert class vector (integers from 0 to nb_classes)
    to binary class matrix, for use with categorical_crossentropy
    '''
    y = np.asarray(y, dtype='int32')
    if not nb_classes:
        nb_classes = np.max(y)+1
    Y = np.zeros((len(y), nb_classes))
    for i in range(len(y)):
        Y[i, y[i]] = 1.
    return Y

Most helpful comment

I think to_categorical expects y to be 0-indexed and consecutive. You can leave nb_classes=None, but then you'll have some column(s) that are all 0.

I would take a look at the unique values of your train_label variable

All 3 comments

I think to_categorical expects y to be 0-indexed and consecutive. You can leave nb_classes=None, but then you'll have some column(s) that are all 0.

I would take a look at the unique values of your train_label variable

Extra all zero column was easy to understand but hard to find. Had to exclude it before using the data into model %>% fit()

Can please someone guide me about that extra column. why I am getting it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fredtcaroli picture fredtcaroli  路  3Comments

nryant picture nryant  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

anjishnu picture anjishnu  路  3Comments

StevenLOL picture StevenLOL  路  3Comments