The exit shape of the keras loss function sparse_categorical_crossentropy did not match

I have a dataset that has 3570 labels. When I use sparse_categorical_crossentropy

as a loss function, the output form does not match.

model = Sequential()
model.add(Dense(1024, input_dim=79, activation='relu'))
model.add(Dense(2048, activation='relu'))
model.add(Dense(3570, activation='sigmoid'))

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(x_train, y_train,
          epochs=10,
          batch_size=1,
          validation_data=(x_valid, y_valid))

      

and the output is ValueError: Error when checking model target: expected dense_42 to have shape (None, 1) but got array with shape (1055, 3570)

Then I create this question in C # 2444 and used np.expand_dims(y, -1)

to change the code. But there was still a mistake.

model = Sequential()
model.add(Dense(1024, input_dim=79, activation='relu'))
model.add(Dense(2048, activation='relu'))
model.add(Dense(3570, activation='sigmoid'))

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(x_train, np.expand_dims(y_train, -1),
          epochs=10,
          batch_size=1,
          validation_data=(x_valid, np.expand_dims(y_valid, -1)))

      

mistake ValueError: Error when checking model target: expected dense_45 to have 2 dimensions, but got array with shape (1055, 3570, 1)

How can I change the code?

+3


source to share


1 answer


What are the initial dimensions of y_train?

Most likely your y_train is of the form (1055,). You need to use one-hot code y_train to measure (1055,3570). Then the original code should work. Keras does not accept one column y, using multiple classes, it must be single-hot.



You can find the following:

from keras.utils.np_utils import to_categorical

y_cat = to_categorical(y, num_classes=None)

      

+1


source







All Articles