简体   繁体   中英

Implementing my first keras model: Why am I getting a mismatch in input arrays?

I am trying to implement my first keras model. I followed this tutorial

I am running the following versions of modules:

scipy: 1.4.1
numpy: 1.18.1
matplotlib: 3.1.3
pandas: 1.0.3
statsmodels: 0.11.0
sklearn: 0.22.1
Using TensorFlow backend.
keras: 2.3.1

This is the code of my createmodel.py

from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

img_rows, img_cols = 28, 28

x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)

x_train = x_train / 255
x_test = x_test / 255

from keras.utils import to_categorical
num_classes = 10

y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
     activation='relu',
     input_shape=(img_rows, img_cols, 1)))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy',
      optimizer='adam',
      metrics=['accuracy'])

batch_size = 128
epochs = 10

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("test_model.h5")

But when I run the script, I get the following Error, when the model is trained (at line model.fit):

ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (10,)

I understand that I have a mismatch in the array dimensions. But as I am new to keras and python I would need a kickstart from you. Thanks for helping!

Ok I will answer my own question right away, so that it helps somebody else in my situation.

As stated here there is an error in the tutorial when compiling the model - you need to use

categorical_crossentropy

when compiling with a one-hot-encoded vector.

The difference is described here

model.compile(loss='sparse_categorical_crossentropy',
      optimizer='adam',
      metrics=['accuracy'])

Instead of loss='sparse_categorical_crossentropy' use loss=keras.losses.categorical_crossentropy

model.compile(loss=keras.losses.categorical_crossentropy,
      optimizer='adam',
      metrics=['accuracy'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM