TensorFlow 2.3 模型评估model.evaluate()时accuracy丢失的坑

37 篇文章 1 订阅

问题描述

对手写数据集 MNIST 进行简单的模型训练并评估

import tensorflow as tf

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])
model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy']
)
model.fit(train_images, train_labels, epochs=2)
loss, accuracy = model.evaluate(test_images, test_labels, verbose=2)
print('loss: {:.2f} accuracy: {:.2f}% '.format(loss, accuracy * 100))
model.save('model.h5')
# Epoch 1/2
# 1875/1875 [==============================] - 2s 1ms/step - loss: 0.2618 - accuracy: 0.9251
# Epoch 2/2
# 1875/1875 [==============================] - 2s 1000us/step - loss: 0.1149 - accuracy: 0.9657
# 313/313 - 0s - loss: 0.0977 - accuracy: 0.9694
# loss: 0.10 accuracy: 96.94%

加载模型再评估

import tensorflow as tf

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
test_images = test_images / 255.0
model = tf.keras.models.load_model('model.h5')
loss, accuracy = model.evaluate(test_images, test_labels, verbose=2)
print('loss: {:.2f} accuracy: {:.2f}% '.format(loss, accuracy * 100))
# loss: 0.10 accuracy: 9.89%

惊奇地发现 loss 是正确的,但 accuracy 丢失了

这是一个 BUG ,当损失函数使用 sparse_categorical_crossentropy ,而度量指标是 accuracy 时出现




训练时把 accuracy 改为 sparse_categorical_accuracy

import tensorflow as tf

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])
model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['sparse_categorical_accuracy']
)
model.fit(train_images, train_labels, epochs=2)
loss, accuracy = model.evaluate(test_images, test_labels, verbose=2)
print('loss: {:.2f} accuracy: {:.2f}% '.format(loss, accuracy * 100))
model.save('model.h5')
# Epoch 1/2
# 1875/1875 [==============================] - 1s 654us/step - loss: 0.2642 - sparse_categorical_accuracy: 0.9239
# Epoch 2/2
# 1875/1875 [==============================] - 2s 848us/step - loss: 0.1159 - sparse_categorical_accuracy: 0.9657
# 313/313 - 0s - loss: 0.1132 - sparse_categorical_accuracy: 0.9663
# loss: 0.11 accuracy: 96.63%

再评估就正确了

import tensorflow as tf

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
test_images = test_images / 255.0
model = tf.keras.models.load_model('model.h5')
loss, accuracy = model.evaluate(test_images, test_labels, verbose=2)
print('loss: {:.2f} accuracy: {:.2f}% '.format(loss, accuracy * 100))




使用 TensorFlow 2.4 或 新版本的 tf-nightly

pip install tensorflow-gpu==2.4.0




参考文献

  1. Accuracy is lost after save/load
  2. TF website tutorial “Save and load” fails on Google Colab for entire models
  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值