如何使用TensorFlow和Estimators預測泰坦尼克號資料集的輸出?


可以使用TensorFlow和Estimators以及之前建立的估計器“BoostedTreesClassifier”並呼叫其“predict”方法來預測泰坦尼克號資料集的輸出。

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

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

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

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

Estimator是TensorFlow對完整模型的高階表示。它旨在實現輕鬆擴充套件和非同步訓練。我們將使用tf.estimator API訓練一個邏輯迴歸模型。該模型用作其他演算法的基線。我們使用泰坦尼克號資料集,目標是根據性別、年齡、等級等特徵預測乘客的生存情況。

示例

print("The predictions are being made")
pred_dicts = list(est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])
probs.plot(kind='hist', bins=20, title='predicted probabilities')
plt.show()

輸出

The predictions are being made
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpyls8bhku/model.ckpt-100
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.

解釋

  • 模型訓練完成後,可以進行預測。
  • 這是對一名乘客進行的預測,以檢視他們是否會倖存。
  • 該乘客屬於評估資料集。
  • TensorFlow模型經過最佳化,可以一次對批次或示例集合進行預測。
  • 預測機率在控制檯上顯示。

更新於:2021年2月25日

86 次檢視

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.