如何使用 Python 和 TensorFlow 將與 Stack Overflow 問題資料集關聯的文字資料向量化?


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

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

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

pip install tensorflow

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

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

示例

以下是向量化文字資料的程式碼片段:

print("The vectorize function is defined")
def int_vectorize_text(text, label):
   text = tf.expand_dims(text, -1)
   return int_vectorize_layer(text), label
print(" A batch of the dataset is retrieved")
text_batch, label_batch = next(iter(raw_train_ds))
first_question, first_label = text_batch[0], label_batch[0]
print("Question is : ", first_question)
print("Label is : ", first_label)

print("'binary' vectorized question is :",
   binary_vectorize_text(first_question, first_label)[0])
print("'int' vectorized question is :",

   int_vectorize_text(first_question, first_label)[0])

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

輸出

The vectorize function is defined
A batch of the dataset is retrieved
Question is : tf.Tensor(b'"function expected error in blank for dynamically created check box
when it is clicked i want to grab the attribute value.it is working in ie 8,9,10 but not working in ie
11,chrome shows function expected error..<input type=checkbox checked=\'checked\'
id=\'symptomfailurecodeid\' tabindex=\'54\' style=\'cursor:pointer;\' onclick=chkclickevt(this);
failurecodeid=""1"" >...function chkclickevt(obj) { .
alert(obj.attributes(""failurecodeid""));.}"\n', shape=(), dtype=string)
Label is : tf.Tensor(2, shape=(), dtype=int32)
'binary' vectorized question is : tf.Tensor([[1. 1. 1. ... 0. 0. 0.]], shape=(1, 10000), dtype=float32)
'int' vectorized question is : tf.Tensor(
[[ 37 464 65 7  16 12 879 262 181 448 44 10 6  700
   3  46  4 2085 2 473 1   6  156  7  478 1 25 20
  156 7  478 1  499 37 464 1 1846 1666 1  1  1  1
   1  1   1  1    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0 0 0
   0  0   0  0    0 0    0 0    0    0 0  0]], shape=(1, 250), dtype=int64)

解釋

  • 二進位制模式返回一個指示標記是否存在(的)陣列。

  • 在 int 模式下,每個標記都將替換為一個整數。

  • 這樣,順序將被保留。

  • 定義了 vectorize 函式。

  • 對資料樣本進行了向量化,並在控制檯中顯示了向量化的“二進位制”和“int”模式。

  • 可以使用該特定層上的“get_vocabulary”方法查詢字串。

更新於:2021年1月18日

233 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

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