如何使用Tensorflow建立輸入函式來訓練模型?


可以使用Tensorflow的‘from_tensor_slices’方法並建立一個包含鳶尾花資料集特徵的字典來建立一個用於訓練或評估模型的輸入函式。

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

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

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

TensorFlow Text包含與文字相關的類和操作的集合,可用於TensorFlow 2.0。TensorFlow Text可用於預處理序列建模

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

示例

print("Input function that is used to train or evaluate")
def input_fn(features, labels, training=True, batch_size=256):
   dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
   if training:
      dataset = dataset.shuffle(1000).repeat()
   return dataset.batch(batch_size)

輸出

Input function that is used to train or evaluate

程式碼來源 −https://www.tensorflow.org/tutorials/estimator/premade#first_things_first

解釋

  • 輸入函式可以生成特徵字典和標籤列表。

  • 使用pandas載入資料,並在此記憶體資料的基礎上構建輸入管道。

更新於: 2021年2月22日

397 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告