如何使用 Python 中的 TensorFlow 查詢資料集預處理層的狀態?


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

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

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

pip install tensorflow

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

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

示例

print("A text-only dataset without labels is prepared")
train_text = raw_train_ds.map(lambda text, labels: text)
print("The adapt method is called")
binary_vectorize_layer.adapt(train_text)
int_vectorize_layer.adapt(train_text)
print("The result is displayed on the console")
def binary_vectorize_text(text, label):
   text = tf.expand_dims(text, -1)
   return binary_vectorize_layer(text), label

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

輸出

A text-only dataset without labels is prepared
The adapt method is called
The result is displayed on the console

解釋

  • 準備了不使用標籤的資料集。

  • 在此資料上呼叫了一個名為“adapt”的方法。

  • 這將使用模型的“二進位制”格式對資料集進行向量化。

更新於:2021年1月18日

86 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.