如何使用Tensorflow和Python對預處理後的資料進行洗牌?


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

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

pip install tensorflow

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

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

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

示例

以下是程式碼片段:

print("Combine the labelled dataset and reshuffle it")
BUFFER_SIZE = 50000
BATCH_SIZE = 64
VALIDATION_SIZE = 5000
all_labeled_data = labeled_data_sets[0]
for labeled_dataset in labeled_data_sets[1:]:
   all_labeled_data = all_labeled_data.concatenate(labeled_dataset)
all_labeled_data = all_labeled_data.shuffle(
   BUFFER_SIZE, reshuffle_each_iteration=False)
print("Displaying a few samples of input data")
for text, label in all_labeled_data.take(8):
   print("The sentence is : ", text.numpy())
   print("The label is :", label.numpy())

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

輸出

Combine the labelled dataset and reshuffle it
Displaying a few samples of input data
The sentence is : b'But I have now both tasted food, and given'
The label is : 0
The sentence is : b'All these shall now be thine: but if the Gods'
The label is : 1
The sentence is : b'Their spiry summits waved. There, unperceived'
The label is : 0
The sentence is : b'"I pray you, would you show your love, dear friends,'
The label is : 1
The sentence is : b'Entering beneath the clavicle the point'
The label is : 0
The sentence is : b'But grief, his father lost, awaits him now,'
The label is : 1
The sentence is : b'in the fore-arm where the sinews of the elbow are united, whereon he'
The label is : 2
The sentence is : b'For, as I think, I have already chased'
The label is : 0

解釋

  • 在預處理資料後,控制檯上會顯示資料集中的幾個樣本。

  • 資料未分組,這意味著“all_labeled_data”中的每個條目都對應一個數據點。

更新於: 2021年1月19日

108 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.