Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras to_categorical adds additional value

I have 4 classes which I need to predict, am using keras' to_categorical to achieve that, I expected to get a 4 one-hot-encoded array, but it seems I get 5 values instead, an additional [0] value appears for all rows

dict = {'word': 1, 'feature_name': 2, 'feature_value': 3, 'part_number': 4}
    Y = dataset['class'].apply(lambda label: dict[label])
    print(Y.unique()) #prints [1 4 2 3]
    train_x, test_x, train_y, test_y = model_selection.train_test_split(X, Y, test_size=0.2, random_state=0)
    train_y = to_categorical(train_y)
    print(train_y[0])# prints [0. 0. 1. 0. 0.]

the model am trying to build is as follows

model = Sequential()
model.add(Dense(10, input_dim=input_dim, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(4, activation='softmax'))

but then it keeps throwing

ValueError: Error when checking target: expected dense_5 to have shape (4,) but got array with shape (5,)
like image 599
Exorcismus Avatar asked May 22 '19 09:05

Exorcismus


2 Answers

Your need to number classes starting with 0, like this:

dict = {'word': 0, 'feature_name': 1, 'feature_value': 2, 'part_number': 3}

You can get description of the function with help() command

help(np_utils.to_categorical)

:

Help on function to_categorical in module keras.utils.np_utils:

to_categorical(y, num_classes=None, dtype='float32')
Converts a class vector (integers) to binary class matrix.

E.g. for use with categorical_crossentropy.

# Arguments
    y: class vector to be converted into a matrix
        (integers from 0 to num_classes).
    num_classes: total number of classes.
    dtype: The data type expected by the input, as a string
        (`float32`, `float64`, `int32`...)

# Returns
    A binary matrix representation of the input. The classes axis
    is placed last.
like image 122
Anna K. Avatar answered Oct 12 '22 11:10

Anna K.


Could be a keras version error. Try to update it because this works for me:

dict = {'word': 1, 'feature_name': 2, 'feature_value': 3, 'part_number': 4}
Y = np.random.randint(4, size=10)
print(np.unique(Y)) #prints [0 1 2 3]
train_y = np_utils.to_categorical(Y, num_classes=4)
print(train_y[0]) # prints [0. 0. 1. 0.]

Try starting your dictionary from 0 because when Keras reads your data take 0 as the reference.

dict = {'word': 0, 'feature_name': 1, 'feature_value': 2, 'part_number': 3}

If it does not work, try to force the number of classes:

train_y = to_categorical(train_y, num_classes = 4)
like image 28
Alvaro Romero Diaz Avatar answered Oct 12 '22 10:10

Alvaro Romero Diaz