我是Keras的新手,并且正在使用它来构建普通的神经网络来对MNIST数据集进行分类。
之前,我已经将数据分为3个部分:55000进行训练,5000进行评估和10000进行测试,并且我已经缩小了像素密度(将其除以255.0)
我的模型如下:
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
model.add(keras.layers.Dense(100, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))
这是编译:
model.compile(loss='sparse_categorical_crossentropy',
optimizer = 'Adam',
metrics=['accuracy'])
我训练模型:
his = model.fit(xTrain, yTrain, epochs = 20, validation_data=(xValid, yValid))
起初,val_loss减小,然后增加,尽管精度提高了。
Train on 55000 samples, validate on 5000 samples
Epoch 1/20
55000/55000 [==============================] - 5s 91us/sample - loss: 0.2822 - accuracy: 0.9199 - val_loss: 0.1471 - val_accuracy: 0.9588
Epoch 2/20
55000/55000 [==============================] - 5s 82us/sample - loss: 0.1274 - accuracy: 0.9626 - val_loss: 0.1011 - val_accuracy: 0.9710
Epoch 3/20
55000/55000 [==============================] - 5s 83us/sample - loss: 0.0899 - accuracy: 0.9734 - val_loss: 0.0939 - val_accuracy: 0.9742
Epoch 4/20
55000/55000 [==============================] - 5s 84us/sample - loss: 0.0674 - accuracy: 0.9796 - val_loss: 0.0760 - val_accuracy: 0.9770
Epoch 5/20
55000/55000 [==============================] - 5s 94us/sample - loss: 0.0541 - accuracy: 0.9836 - val_loss: 0.0842 - val_accuracy: 0.9742
Epoch 15/20
55000/55000 [==============================] - 4s 82us/sample - loss: 0.0103 - accuracy: 0.9967 - val_loss: 0.0963 - val_accuracy: 0.9788
Epoch 16/20
55000/55000 [==============================] - 5s 84us/sample - loss: 0.0092 - accuracy: 0.9973 - val_loss: 0.0956 - val_accuracy: 0.9774
Epoch 17/20
55000/55000 [==============================] - 5s 82us/sample - loss: 0.0081 - accuracy: 0.9977 - val_loss: 0.0977 - val_accuracy: 0.9770
Epoch 18/20
55000/55000 [==============================] - 5s 85us/sample - loss: 0.0076 - accuracy: 0.9977 - val_loss: 0.1057 - val_accuracy: 0.9760
Epoch 19/20
55000/55000 [==============================] - 5s 83us/sample - loss: 0.0063 - accuracy: 0.9980 - val_loss: 0.1108 - val_accuracy: 0.9774
Epoch 20/20
55000/55000 [==============================] - 5s 85us/sample - loss: 0.0066 - accuracy: 0.9980 - val_loss: 0.1056 - val_accuracy: 0.9768
当我评估损失太大时:
model.evaluate(xTest, yTest)
结果:
10000/10000 [==============================] - 0s 41us/sample - loss: 25.7150 - accuracy: 0.9740
[25.714989705941953, 0.974]
这样可以吗,还是过度拟合的标志?我应该做些改进吗?预先感谢。
答案 0 :(得分:0)
通常,这不好。您希望损失率尽可能小。您的结果通常是过度拟合的。您的网络“知道”其训练数据,但无法分析新图像。您可能要添加一些图层。也许是卷积层,辍学层...另一个想法是扩大您的训练图像。 Keras提供的ImageDataGenerator
类可以在这里为您提供帮助
要查看的另一件事可能是您的超参数。为什么在第一dense
层中使用100个节点?如果您想从dense
图层开始,也许像784(28 * 28)之类的东西看起来更有趣。我建议使用Convolutional
-Dropout
-Dense
的组合。然后您的dense
层可能不需要那么多节点...