如何使用 Tensorflow 来训练和评估 titanic 数据集?

tensorflowserver side programmingprogramming更新于 2025/5/20 2:52:17

可以使用 Tensorflow 的‘train’方法和‘evaluate’方法训练和评估 titanic 数据集。方法。

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

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

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

我们使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 有助于在浏览器上运行 Python 代码,无需配置,可以免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。

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

示例

linear_est = tf.estimator.LinearClassifier(feature_columns)
print("The model is being trained")
linear_est.train(train_input_fn, max_steps=100)
print("The model is being evaluated")
result = linear_est.evaluate(eval_input_fn)
clear_output()
print(pd.Series(result))

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

输出

accuracy              0.765152
accuracy_baseline     0.625000
auc                   0.832844
auc_precision_recall 0.789631
average_loss         0.478908
label/mean           0.375000
loss                  0.478908
precision             0.703297
prediction/mean     0.350790
recall               0.646465
global_step         100.000000
dtype: float64

解释

  • 模型已初始化。
  • 还提到了特征和超参数。
  • 在 train_input_fn 的帮助下,将训练数据输入到模型中。
  • 使用 train 函数训练模型。
  • 使用评估集确定模型性能。
  • 使用 dfeval DataFrame 确定性能。
  • 通过检查 y_eval 数组中的标签来验证预测。
  • 在训练 Boosted Trees 模型之前,先训练一个线性分类器。
  • 这已经完成了,即从一个简单的模型开始建立基准。

相关文章