我正在按照一本深度学习书籍(深度学习与Keras第1章)的示例进行操作,这是我所遵循的示例
from __future__ import print_functionimport numpy as npfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers.core import Dense, Dropout, Activationfrom keras.optimizers import SGDfrom keras.utils import np_utilsimport matplotlib.pyplot as pltnp.random.seed(1671) # for reproducibility# network and trainingNB_EPOCH = 250BATCH_SIZE = 128VERBOSE = 1NB_CLASSES = 10 # number of outputs = number of digitsOPTIMIZER = SGD() # optimizer, explained later in this chapterN_HIDDEN = 128VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATIONDROPOUT = 0.3# data: shuffled and split between train and test sets(X_train, y_train), (X_test, y_test) = mnist.load_data()#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784RESHAPED = 784#X_train = X_train.reshape(60000, RESHAPED)X_test = X_test.reshape(10000, RESHAPED)X_train = X_train.astype('float32')X_test = X_test.astype('float32')# normalize X_train /= 255X_test /= 255print(X_train.shape[0], 'train samples')print(X_test.shape[0], 'test samples')# convert class vectors to binary class matricesY_train = np_utils.to_categorical(y_train, NB_CLASSES)Y_test = np_utils.to_categorical(y_test, NB_CLASSES)# M_HIDDEN hidden layers# 10 outputs# final stage is softmaxmodel = Sequential()model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))model.add(Activation('relu'))model.add(Dropout(DROPOUT))model.add(Dense(N_HIDDEN))model.add(Activation('relu'))model.add(Dropout(DROPOUT))model.add(Dense(NB_CLASSES))model.add(Activation('softmax'))model.summary()model.compile(loss='categorical_crossentropy', optimizer=OPTIMIZER, metrics=['accuracy'])history = model.fit(X_train, Y_train, batch_size=BATCH_SIZE, epochs=NB_EPOCH, verbose=VERBOSE, validation_split=VALIDATION_SPLIT)score = model.evaluate(X_test, Y_test, verbose=VERBOSE)print("\nTest score:", score[0])print('Test accuracy:', score[1])# list all data in historyprint(history.history.keys())# summarize history for accuracyplt.plot(history.history['acc'])plt.plot(history.history['val_acc'])plt.title('model accuracy')plt.ylabel('accuracy')plt.xlabel('epoch')plt.legend(['train', 'test'], loc='upper left')plt.show()# summarize history for lossplt.plot(history.history['loss'])plt.plot(history.history['val_loss'])plt.title('model loss')plt.ylabel('loss')plt.xlabel('epoch')plt.legend(['train', 'test'], loc='upper left')plt.show()
如果我将这个示例粘贴到https://colab.research.google.com,我得到的准确率是0.9779
但是我在colab中编写了相同的示例(相同的模型,参数,种子),我的准确率大约是0.6755。使用相同的模型和参数,结果不应该有这么大的差异。但我无法找出我遗漏了什么
我还尝试了一行一行地检查,但仍然无法找出我在代码示例中遗漏了什么,导致准确率如此低
这是我在colab中编写的代码:
https://github.com/anandvimal/deeplearning-experiments/blob/master/mnist_keras_1_2.ipynb
回答:
我刚刚阅读了您的笔记本,发现您执行了两次归一化单元格,导致了不良结果。
# normalize X_train /= 255X_test /= 255print(X_train.shape[0], 'train samples')print(X_test.shape[0], 'test samples')