验证准确性似乎为零。 学习Mnist数据集。我从在线资源中复制了代码,但无法正常工作。
允许发短信的随机文本问题:Lorem ipsum dolor坐在amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invitunt ut labour和dolore magna aliquyam erat,sed diam voluptua。在Vero eos etAccusam和Justo duo dolores等处。 Stet clita kasd gubergren,没有大海,也没有坐在这里。 Lorem ipsum dolor坐着,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invitunt ut labour et dolore magna aliquyam erat,sed diam voluptua在Vero eos etAccusam和Justo duo dolores等处。 Stet clita kasd gubergren,没有大海,也没有坐在这里。 输出
Epoch 1/8
235/235 [==============================] - 13s 55ms/step - loss: 0.7066 - accuracy: 0.8715 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
...
Epoch 8/8
235/235 [==============================] - 13s 55ms/step - loss: 0.0176 - accuracy: 0.9941 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00
Score: [0.052960414439439774, 0.9857000112533569]
代码
imports...
# Dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Cast to np.float32
x_train = x_train.astype(np.float32)
y_train = y_train.astype(np.float32)
x_test = x_test.astype(np.float32)
y_test = y_test.astype(np.float32)
# Reshape the images to a depth dimension
x_train = np.expand_dims(x_train, axis=-1)
x_test = np.expand_dims(x_test, axis=-1)
# Dataset variables
train_size = x_train.shape[0]
test_size = x_test.shape[0]
width, height, depth = x_train.shape[1:]
num_features = width * height * depth
num_classes = 10
# Compute the categorical classes_list
y_train = to_categorical(y_train, num_classes=num_classes)
y_test = to_categorical(y_test, num_classes=num_classes)
# Model params
lr = 0.001
optimizer = Adam(lr=lr)
epochs = 8
batch_size = 256
dropout_rate = 0.1
# Define the DNN
input_img = Input(shape=x_train.shape[1:])
x = Conv2D(filters=8, kernel_size=5, padding='same')(input_img)
...
y_pred = Activation("softmax")(x)
# Build the model
model = Model(inputs=[input_img], outputs=[y_pred])
# Compile and train (fit) the model, afterwards evaluate the model
model.summary()
model.compile(
loss="categorical_crossentropy",
optimizer=optimizer,
metrics=["accuracy"])
model.fit(
x=x_train,
y=y_train,
epochs=epochs,
batch_size=batch_size,
validation_data=[x_test, y_test])
score = model.evaluate(
x_test,
y_test,
verbose=0)
print("Score: ", score)
答案 0 :(得分:0)
即使在注释部分(感谢Medrik)中也提供了解决方案(答案部分),还是为了社区的利益。
validation_data
放在[]
括号中,而不是()
括号中。在修改以下代码后,此问题已解决
model.fit( x=x_train, y=y_train, epochs=epochs, batch_size=batch_size, validation_data=[x_test, y_test])
到
model.fit( x=x_train, y=y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test))