虽然使用Tensorflow的Keras api测试了Mnist数据集的训练,但是我在complile语句中指定 loss=tf.losses.sparse_softmax_cross_entropy
时看到了奇怪的准确性。我只是尝试使用3种不同的方法来指定损失函数,即。
Google Colab link可以更好地解释这一点
这是示例代码
import tensorflow as tf
import numpy as np
from tensorflow import keras
from keras.datasets import mnist
from sklearn.metrics import f1_score
(x_train,y_train),(x_test,y_test) = mnist.load_data()
model = tf.keras.Sequential([
keras.layers.Flatten(),
keras.layers.Dense(units=128,activation=tf.nn.relu),
keras.layers.Dense(10, activation = tf.nn.softmax)
])
# loss='sparse_categorical_crossentropy'
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
hist = model.fit(x_train/255,y_train,epochs=10, verbose=0 )
y_pred1 = model.predict(x_test/255)
y_pred1 = np.argmax(y_pred1, axis=1)
results1 = (np.array([y_pred1 == y_test]).astype(int).reshape(-1,1))
acc1 = np.asscalar(sum(results1)/results1.shape[0])
print("case 1: loss='sparse_categorical_crossentropy' : "+ str(hist.history['acc'][9]))
print("calculated test acc : "+ str(acc1))
print("_________________________________________________")
# loss=tf.losses.sparse_softmax_cross_entropy
model.compile(optimizer=tf.train.AdamOptimizer(),
loss=tf.losses.sparse_softmax_cross_entropy,
metrics=['accuracy'])
hist = model.fit(x_train/255,y_train,epochs=10,verbose=1)
y_pred2 = model.predict(x_test/255)
y_pred2 = np.argmax(y_pred2, axis=1)
results2 = (np.array([y_pred2 == y_test]).astype(int).reshape(-1,1))
acc2 = np.asscalar(sum(results2)/results2.shape[0])
print("case 2: loss=tf.losses.sparse_softmax_cross_entropy : "+ str(hist.history['acc'][9]))
print("calculated test acc : "+ str(acc2))
print("_________________________________________________")
# loss=tf.losses.softmax_cross_entropy
model.compile(optimizer=tf.train.AdamOptimizer(),
loss=tf.losses.softmax_cross_entropy,
metrics=['accuracy'])
from keras.utils import to_categorical
y_train_onehot = to_categorical(y_train)
hist = model.fit(x_train/255,y_train_onehot,epochs=10,verbose=0)
y_pred3 = model.predict(x_test/255)
y_pred3 = np.argmax(y_pred3, axis=1)
results3 = (np.array([y_pred3 == y_test]).astype(int).reshape(-1,1))
acc3 = np.asscalar(sum(results3)/results3.shape[0])
print("case 3: loss=tf.losses.softmax_cross_entropy : "+ str(hist.history['acc'][9]))
print("calculated test acc : "+ str(acc3))
输出如下所示
case 1: loss='sparse_categorical_crossentropy' : 0.99495
calculated test acc : 0.978
_________________________________________________
Epoch 1/10
60000/60000 [==============================] - 5s 79us/sample - loss: 1.4690 - acc: 0.0988
Epoch 2/10
60000/60000 [==============================] - 5s 79us/sample - loss: 1.4675 - acc: 0.0987
Epoch 3/10
60000/60000 [==============================] - 4s 75us/sample - loss: 1.4661 - acc: 0.0988
Epoch 4/10
60000/60000 [==============================] - 4s 74us/sample - loss: 1.4656 - acc: 0.0987
Epoch 5/10
60000/60000 [==============================] - 5s 77us/sample - loss: 1.4652 - acc: 0.0987
Epoch 6/10
60000/60000 [==============================] - 5s 78us/sample - loss: 1.4648 - acc: 0.0988
Epoch 7/10
60000/60000 [==============================] - 4s 75us/sample - loss: 1.4644 - acc: 0.0987
Epoch 8/10
60000/60000 [==============================] - 5s 76us/sample - loss: 1.4641 - acc: 0.0988
Epoch 9/10
60000/60000 [==============================] - 5s 79us/sample - loss: 1.4639 - acc: 0.0987
Epoch 10/10
60000/60000 [==============================] - 5s 76us/sample - loss: 1.4639 - acc: 0.0988
case 2: loss=tf.losses.sparse_softmax_cross_entropy : 0.09876667
calculated test acc : 0.9791
_________________________________________________
case 3: loss=tf.losses.softmax_cross_entropy : 0.99883336
calculated test acc : 0.9784
在第二种情况下, loss=tf.losses.sparse_softmax_cross_entropy
中出现的准确度为 0.0987 ,这没有意义,因为显示了根据训练和测试数据评估模型准确度超过 0.97