如何使用 TensorFlow 和 Estimators 来检查 Python 中的泰坦尼克号数据集?


可以通过迭代特征并将特征转换为列表,并在控制台上显示它们,使用 TensorFlow 和 Estimators 检查泰坦尼克号数据集。

阅读更多: 什么是 TensorFlow 以及 Keras 如何与 TensorFlow 协作创建神经网络?

我们将使用 Keras Sequential API,它有助于构建一个顺序模型,该模型用于处理简单的层堆栈,其中每一层只有一个输入张量和一个输出张量。

包含至少一层的神经网络称为卷积层。我们可以使用卷积神经网络来构建学习模型。

我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,无需任何配置,并且可以免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 的基础之上。

Estimator 是 TensorFlow 对完整模型的高级表示。它旨在实现轻松扩展和异步训练。

我们将使用 tf.estimator API 训练一个逻辑回归模型。该模型用作其他算法的基线。我们使用泰坦尼克号数据集,目标是根据性别、年龄、等级等特征预测乘客的生存情况。

Estimator 使用特征列来描述模型如何解释原始输入特征。Estimator 期望一个数值输入向量,特征列将有助于描述模型如何转换数据集中每个特征。选择和使用正确的特征列集对于学习有效的模型至关重要。

示例

print("The dataset is being inspected")
ds = make_input_fn(dftrain, y_train, batch_size=10)()
for feature_batch, label_batch in ds.take(1):
print('Some feature keys are:', list(feature_batch.keys()))
print()
print('A batch of class:', feature_batch['class'].numpy())
print()
print('A batch of Labels:', label_batch.numpy())

代码来源 −https://tensorflowcn.cn/tutorials/estimator/linear

输出

The dataset is being inspected
Some feature keys are: ['sex', 'age', 'n_siblings_spouses', 'parch', 'fare', 'class', 'deck', 'embark_town', 'alone']
A batch of class: [b'First' b'First' b'First' b'Third' b'Third' b'Third' b'First' b'Third'
b'Second' b'Third']
A batch of Labels: [0 1 1 0 0 0 1 0 0 0]

解释

  • 数据集被检查。
  • 特征键、标签和类别在控制台上显示。
  • 这是通过迭代数据集的一批来完成的。

更新于: 2021年2月25日

126 次查看

开启你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.