如何使用Tensorflow和花卉数据集继续训练模型?


要继续在花卉数据集上训练模型,可以使用“fit”方法。在此方法中,还会指定 epoch 数(用于构建模型的数据训练次数)。一些示例图像也会显示在控制台上。

阅读更多:什么是 TensorFlow,Keras 如何与 TensorFlow 协作创建神经网络?

我们将使用花卉数据集,其中包含数千朵花的图像。它包含 5 个子目录,每个类对应一个子目录。

我们使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助通过浏览器运行 Python 代码,无需任何配置,并可免费访问 GPU(图形处理单元)。Colaboratory 基于 Jupyter Notebook 构建。

print("The data is fit to the model")
model.fit(
   train_ds,
   validation_data=val_ds,
   epochs=3
)

代码来源:https://tensorflowcn.cn/tutorials/load_data/images

输出

The data is fit to the model
Epoch 1/3
92/92 [==============================] - 102s 1s/step - loss: 0.7615 - accuracy: 0.7146 - val_loss: 0.7673 - val_accuracy: 0.7180
Epoch 2/3
92/92 [==============================] - 95s 1s/step - loss: 0.5864 - accuracy: 0.7786 - val_loss: 0.6814 - val_accuracy: 0.7629
Epoch 3/3
92/92 [==============================] - 95s 1s/step - loss: 0.4180 - accuracy: 0.8478 - val_loss: 0.7040 - val_accuracy: 0.7575
<tensorflow.python.keras.callbacks.History at 0x7fda872ea940>

解释

  • 使用 tf.data.Dataset 构建了与这个(使用 keras.preprocessing 构建)类似的数据集。
  • 模型可以一起训练。
  • 训练几个 epoch,这样就不会消耗太多时间。

更新于: 2021年2月19日

121 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.