Я пытаюсь обучить весь набор данных Mnist. Я основываю свой код на этом учебнике . Ниже мой код
import tensorflow as tf
mnist = tf.keras.datasets.mnist
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
x_test = x_test.reshape(x_test.shape[0], 28, 28,1)
mean_px = x_train.mean().astype(np.float32)
std_px = x_train.std().astype(np.float32)
def standardize(x):
return (x-mean_px)/std_px
y_train= tf.keras.utils.to_categorical(y_train)
num_classes = y_train.shape[1]
seed = 43
np.random.seed(seed)
model= tf.keras.models.Sequential()
model.add(tf.keras.layers.Lambda(standardize,input_shape=(28,28,1)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
gen = tf.keras.preprocessing.image.ImageDataGenerator()
batches = gen.flow(x_train, y_train, batch_size=64)
val_batches=gen.flow(x_test, y_test, batch_size=64)
history=model.fit_generator(generator=batches, steps_per_epoch=batches.n, epochs=3,validation_data=val_batches, validation_steps=val_batches.n)
Однако я получаю сообщение об ошибке ValueError: Error when checking target: expected dense to have shape (10,) but got array with shape (1,)
Что я делаю не так?