如何使用Tensorflow來測試、重置模型並載入最新的檢查點?


Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用,用於實現演算法、深度學習應用程式等等。它用於研究和生產目的。它具有最佳化技術,有助於快速執行復雜的數學運算。這是因為它使用了 NumPy 和多維陣列。這些多維陣列也稱為“張量”。該框架支援使用深度神經網路。它具有高度可擴充套件性,並附帶許多流行的資料集。

可以使用以下程式碼行在 Windows 上安裝“tensorflow”包:

pip install tensorflow

Keras 是作為 ONEIROS(開放式神經電子智慧機器人作業系統)專案研究的一部分開發的。Keras 是一個深度學習 API,是用 Python 編寫的。它是一個高階 API,具有高效的介面,有助於解決機器學習問題。它在 Tensorflow 框架之上執行。它旨在幫助快速進行實驗。它提供了開發和封裝機器學習解決方案所必需的基本抽象和構建塊。

它具有高度可擴充套件性,並具有跨平臺功能。這意味著 Keras 可以執行在 TPU 或 GPU 叢集上。Keras 模型也可以匯出以在 Web 瀏覽器或手機上執行。Keras 已經存在於 Tensorflow 包中。可以使用以下程式碼行訪問它:

import tensorflow
from tensorflow import keras

我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助透過瀏覽器執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 是在 Jupyter Notebook 之上構建的。

以下是程式碼:

示例

print("A new model instance is created")
model = create_model()
print("The previously saved weights are loaded")
model.load_weights(latest)
print("The model is being re−evaluated")
loss, acc = model.evaluate(test_images, test_labels, verbose=2)
print("This is the restored model, with accuracy: {:5.3f}%".format(100 * acc))

程式碼來源 -  https://www.tensorflow.org/tutorials/keras/save_and_load

輸出

A new model instance is created
The previously saved weights are loaded
The model is being re-evaluated
32/32 - 0s - loss: 0.4828 - sparse_categorical_accuracy: 0.8770
This is the restored model, with accuracy:87.700%

解釋

  • 再次,使用“create_model”方法建立例項的新模型。

  • 使用“load_weights”方法將先前儲存的權重載入到此例項。

  • 使用“evaluate”方法評估此新模型。

  • 確定其在訓練期間的準確性和損失。

  • 這些值顯示在控制檯上。

更新於: 2021年1月20日

179 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告