如何使用 Python 和 TensorFlow 檢視向量化資料的樣本?


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

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

pip install tensorflow

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

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

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

示例

print("Look at sample data after processing it")
example_text, example_label = next(iter(all_labeled_data))

print("The sentence is : ", example_text.numpy())
vectorized_text, example_label = preprocess_text(example_text, example_label)

print("The vectorized sentence is : ", vectorized_text.numpy())
print("Run the pre-process function on the data")

all_encoded_data = all_labeled_data.map(preprocess_text)

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

輸出

Look at sample data after processing it
The sentence is : b'But I have now both tasted food, and given'
The vectorized sentence is : [ 20 21 58 49 107 3497 909 2 4 540]
Run the pre-process function on the data

解釋

  • 資料向量化後,所有標記都將轉換為整數。

  • 將它們轉換為整數,以便模型可以解釋輸入。

更新於: 2021年1月19日

88 次檢視

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.