如何使用 Tensorflow 与 Estimators 检查 titanic 数据集中的特定列?

tensorflowserver side programmingprogramming更新于 2025/5/20 6:22:17

可以通过访问要检查的列并使用‘DenseFeatures’来检查 titanic 数据集中的特定列并将其转换为 Numpy 数组。

阅读更多: 什么是 TensorFlow,以及 Keras 如何与 TensorFlow 配合使用来创建神经网络?

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

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

Estimator 是 TensorFlow 对完整模型的高级表示。它旨在轻松扩展和异步训练。我们将使用 tf.estimator API 训练逻辑回归模型。该模型用作其他算法的基线。我们使用泰坦尼克号数据集,目标是根据性别、年龄、等级等特征预测乘客的生存情况。

示例

print("Results of a specific column are being inspected")
age_column = feature_columns[7]
tf.keras.layers.DenseFeatures([age_column])(feature_batch).numpy()

代码来源 −https://www.tensorflow.org/tutorials/estimator/linear

输出

Results of a specific column are being inspected
array([[61. ],
   [17. ],
   [19. ],
   [55.5],
   [26. ],
   [20. ],
   [24. ],
   [ 9. ],
   [31. ],
  [28. ]], dtype=float32)

解释

  • 检查特定特征列的结果。
  • 这是在 tf.keras.layers.DenseFeatures 层的帮助下完成的。

相关文章