Tensorflow使用save保存模型后,使用evaluate时accuracy数据与原来差距太大

参考链接:https://blog.csdn.net/lly1122334/article/details/118901557
学习模型保存时,使用保存完整模型的save方法后,再使用evaluate函数评估模型,得到的loss结果与原来一致,但是accuracy结果却相去甚远,寻求半天解决办法后得知,当损失函数使用 sparse_categorical_crossentropy,但是正确率的度量指标为metrics=['acc]就会出现这样的错误。
因此!!!当训练时损失函数使用 sparse_categorical_crossentropy时,把metrics设置改为sparse_categorical_accuracy就可以啦!!
在这里插入图片描述
原来的评估结果:
在这里插入图片描述
加载保存模型后的评估结果:
在这里插入图片描述
又解决一个麻烦,开心!!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里是一个使用 TensorFlow 训练图像识别模型的示例代码,数据使用的是 cifar10,你可以在 TensorFlow使用它进行测试。以下是代码: ``` import tensorflow as tf from tensorflow import keras import numpy as np # 加载 CIFAR-10 数据集 cifar10 = keras.datasets.cifar10 (train_images, train_labels), (test_images, test_labels) = cifar10.load_data() # 数据预处理 train_images = train_images / 255.0 test_images = test_images / 255.0 # 定义模型 model = keras.Sequential([ keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), keras.layers.MaxPooling2D((2, 2)), keras.layers.Conv2D(64, (3, 3), activation='relu'), keras.layers.MaxPooling2D((2, 2)), keras.layers.Conv2D(64, (3, 3), activation='relu'), keras.layers.Flatten(), keras.layers.Dense(64, activation='relu'), 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=10, validation_data=(test_images, test_labels)) # 评估模型 test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print('\nTest accuracy:', test_acc) # 使用模型进行预测 probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()]) predictions = probability_model.predict(test_images) ``` 你可以在 TensorFlow 的官方 GitHub 仓库中找到 cifar10 数据集。同,也可以使用以下代码在 TensorFlow 中加载 cifar10 数据集: ``` import tensorflow as tf from tensorflow import keras # 加载 CIFAR-10 数据集 cifar10 = keras.datasets.cifar10 (train_images, train_labels), (test_images, test_labels) = cifar10.load_data() ``` 希望这可以帮助你开始训练自己的图像识别模型

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值