如何使用 Estimator 在 TensorFlow 中從訓練好的模型進行預測?
Tensorflow 可以與 estimator 配合使用,透過 `classifier` 方法中的 `predict` 方法對新資料進行輸出預測。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?
我們將使用 Keras Sequential API,它有助於構建一個順序模型,該模型用於處理簡單的層堆疊,其中每一層都只有一個輸入張量和一個輸出張量。
包含至少一層卷積層的神經網路被稱為卷積神經網路。我們可以使用卷積神經網路構建學習模型。
TensorFlow Text 包含一系列與文字相關的類和操作,可用於 TensorFlow 2.0。TensorFlow Text 可用於預處理序列建模。
我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 是基於 Jupyter Notebook 構建的。
Estimator 是 TensorFlow 中對完整模型的高階表示。它旨在簡化擴充套件和非同步訓練。
該模型使用鳶尾花資料集進行訓練。該資料集有 4 個特徵和 1 個標籤。
- 萼片長度
- 萼片寬度
- 花瓣長度
- 花瓣寬度
示例
print(“Generating predictions from model”)
expected = ['Setosa', 'Versicolor', 'Virginica']
predict_x = {
'SepalLength': [5.1, 5.9, 6.9],
'SepalWidth': [3.3, 3.0, 3.1],
'PetalLength': [1.7, 4.2, 5.4],
'PetalWidth': [0.5, 1.5, 2.1],
}
print(“Defining input function for prediction”)
print(“It converts inputs to dataset without labels”)
def input_fn(features, batch_size=256):
return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)
predictions = classifier.predict(
input_fn=lambda: input_fn(predict_x))程式碼來源 - https://www.tensorflow.org/tutorials/estimator/premade#first_things_first
輸出
Generating predictions from model Defining input function for prediction It converts inputs to dataset without labels
解釋
- 訓練好的模型將產生良好的結果。
- 這可以用來根據某些未標記的測量值預測鳶尾花的種類。
- 預測是透過單個函式呼叫完成的。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP