如何使用 Estimator 將 TensorFlow 用於特徵列轉換?


Tensorflow 可以與 Estimator 一起使用來轉換特徵列,方法是首先將資料集的第一行轉換為字典,然後使用獨熱編碼來轉換此特徵列,即“性別”列。

閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?

我們將使用 Keras Sequential API,它有助於構建一個用於處理簡單層堆疊的順序模型,其中每一層都只有一個輸入張量和一個輸出張量。

包含至少一層卷積層的神經網路稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。

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

Estimator 是 TensorFlow 對完整模型的高階表示。它旨在簡化擴充套件和非同步訓練。Estimator 使用特徵列來描述模型如何解釋原始輸入特徵。Estimator 期望一個數值輸入向量,而特徵列將有助於描述模型應該如何轉換資料集中每個特徵。

選擇和使用正確的特徵列集對於學習有效的模型至關重要。

示例

print("An example transformation produced by a feature column")
example = dict(dftrain.head(1))
class_fc = tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list('class', ('First', 'Second', 'Third')))
print('Feature value is: "{}"'.format(example['class'].iloc[0]))
print('One-hot encoded is: ', tf.keras.layers.DenseFeatures([class_fc])(example).numpy())

程式碼來源 -https://www.tensorflow.org/tutorials/estimator/boosted_trees

輸出

An example transformation produced by a feature column
Feature value is: "Third"
One-hot encoded is: [[0. 0. 1.]]

解釋

  • 可以檢視特徵列產生的轉換。

  • 可以在控制檯上檢視 indicator_column 對單個示例的輸出。

更新於: 2021年2月25日

110 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.