如何使用Tensorflow匯出使用Python構建的模型?


Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用以實現演算法、深度學習應用程式等等。它用於研究和生產目的。

它具有最佳化技術,有助於快速執行復雜的數學運算。

這是因為它使用了 NumPy 和多維陣列。這些多維陣列也稱為“張量”。該框架支援使用深度神經網路。它具有高度可擴充套件性,並附帶許多流行的資料集。它使用 GPU 計算並自動管理資源。它附帶大量機器學習庫,並且得到良好的支援和記錄。該框架能夠執行深度神經網路模型、訓練它們並建立預測相應資料集相關特徵的應用程式。

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

pip install tensorflow

張量是 TensorFlow 中使用的資料結構。它有助於連線流圖中的邊。此流圖稱為“資料流圖”。張量不過是一個多維陣列或列表。

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

示例

以下是程式碼片段:

print("The model is being exported")
export_model = tf.keras.Sequential(
   [binary_vectorize_layer, binary_model,
   layers.Activation('sigmoid')])
print("The model is being compiled")
export_model.compile(
   loss=losses.SparseCategoricalCrossentropy(from_logits=False),
   optimizer='adam',
   metrics=['accuracy'])
print("The model is being tested with `raw_test_ds`, which resuls in raw strings")
loss, accuracy = export_model.evaluate(raw_test_ds)
print("The accuracy of the model is : {:2.2%}".format(binary_accuracy))

程式碼來源 - https://www.tensorflow.org/tutorials/load_data/text

輸出

The model is being exported
The model is being compiled
The model is being tested with `raw_test_ds`, which resuls in raw strings
250/250 [==============================] - 4s 13ms/step - loss: 0.5296 - accuracy: 0.8078
The accuracy of the model is : 81.10%

解釋

  • 在將資料集饋送到模型之前,會將其應用於“TextVectorization”層。

  • 如果模型需要處理原始字串,則可以在模型內部應用“TextVectorization”層。

  • 為此,將使用訓練期間使用的權重建立一個新模型。

更新於: 2021年1月19日

98 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.