如何使用 Keras 從檢查點載入權重並使用 Python 重新評估模型?
TensorFlow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用,可以實現演算法、深度學習應用程式等等。它用於研究和生產目的。
可以使用以下程式碼行在 Windows 上安裝 “tensorflow” 包:
pip install tensorflow
張量 (Tensor) 是 TensorFlow 中使用的資料結構。它有助於連線流圖中的邊。此流圖稱為“資料流圖”。張量只不過是多維陣列或列表。它們可以使用三個主要屬性來識別:
秩 - 它表示張量的維數。可以理解為張量的階數或已定義張量的維數。
型別 - 它表示與張量元素關聯的資料型別。它可以是一維、二維或 n 維張量。
形狀 - 它是行數和列數的總和。
Keras 在希臘語中意為“角”。Keras 是作為 ONEIROS 專案(開放式神經電子智慧機器人作業系統)研究的一部分開發的。Keras 是一個用 Python 編寫的深度學習 API。它是一個高階 API,具有高效的介面,有助於解決機器學習問題。它執行在 TensorFlow 框架之上。它是為了幫助快速實驗而構建的。它提供了開發和封裝機器學習解決方案所必需的基本抽象和構建塊。
Keras 已經存在於 TensorFlow 包中。可以使用以下程式碼行訪問它。
import tensorflow from tensorflow import keras
我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 基於 Jupyter Notebook 構建。以下是程式碼:
示例
print("The weights are loaded")
model.load_weights(checkpoint_path)
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
輸出
The Weights are loaded The model is beign re-evaluated 32/32 - 0 - loss:0.4066 - sparse_categorical_accuracy:0.8740 This is the restored model, with accuracy:87.400%
解釋
這個新模型用於將權重對映到它。
“evaluate” 方法用於檢查模型在新資料上的效能。
此外,還確定了模型訓練時的損失和模型的準確性。
損失和準確性將列印在控制檯上。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP