【问题标题】:Getting different accuracy in deep learning model with same code使用相同代码在深度学习模型中获得不同的准确性
【发布时间】:2019-07-09 03:39:33
【问题描述】:

我正在关注深度学习书籍中的一个示例(使用 keras ch1 进行深度学习) 这就是我正在关注的例子

from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras.utils import np_utils

import matplotlib.pyplot as plt

np.random.seed(1671)  # for reproducibility

# network and training
NB_EPOCH = 250
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10   # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
DROPOUT = 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 784
RESHAPED = 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 /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_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 softmax

model = 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 history
print(history.history.keys())

# summarize history for accuracy
plt.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 loss
plt.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

【问题讨论】:

    标签: tensorflow machine-learning keras deep-learning google-colaboratory


    【解决方案1】:

    我刚看了你的笔记本,发现你执行了两次归一化单元,导致结果不好。

    # normalize 
    X_train /= 255
    X_test /= 255
    print(X_train.shape[0], 'train samples')
    print(X_test.shape[0], 'test samples')
    

    【讨论】:

    • 即使我重新运行笔记本也不会得到更好的结果。如果它是由于一个单元格执行两次,那么当我重新运行笔记本执行单元格一次时,它不应该发生。
    • 我的意思是代码单元格出现了两次。您的笔记本中有 2 个相同的代码单元(用于规范化的代码单元)。
    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2018-06-29
    • 2018-12-18
    相关资源
    最近更新 更多