如何使用TensorFlow训练和评估泰坦尼克号数据集?


可以使用TensorFlow的`train`方法和`evaluate`方法分别训练和评估泰坦尼克号数据集。

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

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

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

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

估计器是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://tensorflowcn.cn/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`数组中的标签来验证预测。
  • 在训练提升树模型之前,会训练一个线性分类器。
  • 这样做是为了从简单的模型开始,建立一个基准。

更新于:2021年2月25日

浏览量:184

启动您的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.