请注意:我不想改进以下示例。我知道你可以获得超过 99% 的准确率。整个代码都在问题中。当我尝试这个简单的代码时,我得到了大约 95% 的准确率,如果我简单地将激活函数从 sigmoid 更改为 relu,它会下降到不到 50%。发生这种情况有理论上的原因吗?
我在网上找到了以下示例:
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
Y_train = np_utils.to_categorical(Y_train, classes)
Y_test = np_utils.to_categorical(Y_test, classes)
batch_size = 100
epochs = 15
model = Sequential()
model.add(Dense(100, input_dim=784))
model.add(Activation('sigmoid'))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='sgd')
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)
score = model.evaluate(X_test, Y_test, verbose=1)
print('Test accuracy:', score[1])
这给出了大约 95% 的准确率,但如果我用 ReLU 更改 sigmoid,我得到的准确率不到 50%。这是为什么?