如何使用 Estimators 和 Tensorflow 構建線性模型來載入泰坦尼克號資料集?


可以使用 Estimators 構建線性模型來載入泰坦尼克號資料集,方法是使用 'Pandas' 包中提供的 'read_csv' 方法。此方法獲取儲存泰坦尼克號資料集的 Google API。讀取 API 並將資料儲存為 CSV 檔案格式。

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

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

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

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

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

Estimator 是 TensorFlow 對完整模型的高階表示。它旨在易於擴充套件和非同步訓練。

我們將使用 tf.estimator API 訓練邏輯迴歸模型。該模型用作其他演算法的基線。我們使用泰坦尼克號資料集,目標是預測乘客的生存情況,給定諸如性別、年齡、艙位等特徵。

pip install -q sklearn

示例

import os
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf
print("Load the dataset")
dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv')
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv')
print("Removing feature 'survived'")
y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')

程式碼來源 −https://www.tensorflow.org/tutorials/estimator/linear

輸出

Load the dataset
Removing feature 'survived'

解釋

  • 下載所需的軟體包。
  • 從 API 下載資料。
  • 刪除“survived”列。

更新於: 2021年2月22日

83 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.