如何使用 TensorFlow 和 Auto MPG 資料集對資料進行歸一化以預測燃油效率?


Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用以實現演算法、深度學習應用程式等等。可以使用以下程式碼行在 Windows 上安裝“tensorflow”包:

pip install tensorflow

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

迴歸問題的目標是預測連續或離散變數的輸出,例如價格、機率、是否會下雨等等。

我們使用的資料集稱為“Auto MPG”資料集。它包含 1970 年代和 1980 年代汽車的燃油效率。它包括重量、馬力、排量等屬性。透過這些屬性,我們需要預測特定車輛的燃油效率。

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

以下是程式碼片段:

示例

print("Separating the label from features")
train_features = train_dataset.copy()
test_features = test_dataset.copy()

train_labels = train_features.pop('MPG')
test_labels = test_features.pop('MPG')
print("The mean and standard deviation of the training dataset : ")
train_dataset.describe().transpose()[['mean', 'std']]
print("Normalize the features since they use different scales")
print("Creating the normalization layer")
normalizer = preprocessing.Normalization()
normalizer.adapt(np.array(train_features))
print(normalizer.mean.numpy())
first = np.array(train_features[3:4])
print("Every feature has been individually normalized")
with np.printoptions(precision=2, suppress=True):
print('First example is :', first)
print()
print('Normalized data :', normalizer(first).numpy())

程式碼來源 - https://www.tensorflow.org/tutorials/keras/regression

輸出

Separating the label from features
The mean and standard deviation of the training dataset :
Normalize the features since they use different scales
Creating the normalization layer
[ 5.467 193.847 104.135 2976.88 15.591 75.934 0.168 0.197
0.635]
Every feature has been individually normalized
First example is : [[ 4. 105. 63. 2125. 14.7 82. 0. 0. 1. ]]

Normalized data : [[−0.87 −0.87 −1.11 −1.03 −0.33 1.65 −0.45 −0.5 0.76]]

解釋

  • 目標值(標籤)與特徵分離。

  • 標籤是需要為預測發生而訓練的值。

  • 特徵被歸一化,以便訓練穩定。

  • Tensorflow 中的“Normalization”函式預處理資料。

  • 建立第一層,並將均值和方差儲存在此層中。

  • 呼叫此層時,它會返回輸入資料,其中每個特徵都已歸一化。

更新於: 20-Jan-2021

161 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.