如何使用Tensorflow和伊利亞特資料集,並用Python檢查測試資料的效能?


Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用以實現演算法、深度學習應用程式等等。它用於研究和生產目的。它具有最佳化技術,有助於快速執行復雜的數學運算。這是因為它使用了 NumPy 和多維陣列。這些多維陣列也稱為“張量”。

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

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

我們將使用伊利亞特的資料集,其中包含威廉·考珀、愛德華(德比伯爵)和塞繆爾·巴特勒的三部翻譯作品的文字資料。該模型經過訓練,可以在給出一行文字時識別翻譯者。使用的文字檔案已進行預處理。這包括刪除文件標題和頁尾、行號和章節標題。

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

示例

以下是程式碼片段:

print("Testing the model on new data")
inputs = [
   "the allies, and his armour flashed about him so that he seemed to all",
   "And with loud clangor of his arms he fell.",
   "Join'd to th' Ionians with their flowing robes,",
]
print("The predict method is being called")
predicted_scores = export_model.predict(inputs)
predicted_labels = tf.argmax(predicted_scores, axis=1)
for input, label in zip(inputs, predicted_labels):
   print("The question is : ", input)
   print("The predicted label is : ", label.numpy())

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

輸出

Testing the model on new data
The predict method is being called
The question is : the allies, and his armour flashed about him so that he seemed to all
The predicted label is : 2
The question is : And with loud clangor of his arms he fell.
The predicted label is : 0
The question is : Join'd to th' Ionians with their flowing robes,
The predicted label is : 1

解釋

  • 資料編譯完成後,並擬合訓練資料後,會在從未見過的資料上進行測試。

  • 在測試資料上呼叫“predict”方法。

  • 顯示一些預測標籤的示例及其對應的問題。

更新於:2021年1月19日

59 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.