如何使用 Python 和 Keras 評估已恢復的模型?


Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用,可以實現演算法、深度學習應用程式等等。它用於研究和生產目的。它具有最佳化技術,有助於快速執行復雜的數學運算。

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

pip install tensorflow

Keras 是作為 ONEIROS(開放式神經電子智慧機器人作業系統)專案研究的一部分開發的。Keras 是一個用 Python 編寫的深度學習 API。它是一個高階 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("The restored model is evaluated")
loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)
print('This is the restored model, with accuracy: {:5.3f}%'.format(100 * acc))
print("Predictions are being made, the dimensions of the predictions are")
print(new_model.predict(test_images).shape)
print("A new model instance is created")

model = create_model()

print("The model is fit to the training data")
model.fit(train_images, train_labels, epochs=7)

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

輸出

解釋

  • 使用“evaluate”方法評估恢復的模型。

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

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

  • 使用“predict”方法進行預測。

  • 測試資料的維度顯示在控制檯上。

  • 使用“create_model”方法建立模型的另一個新例項。

  • 模型在 7 個 epoch 中適應訓練資料。

更新於:2021年1月21日

99 次檢視

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.