如何使用 Estimators 在 Tensorflow 中训练泰坦尼克号数据集的模型?


泰坦尼克号数据集可以通过使用“LinearClassifier”创建模型并使用“train”方法进行训练来进行训练。“train”方法存在于tensorflow库的“estimator”类中。

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

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

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

我们将使用 tf.estimator API 训练一个逻辑回归模型。该模型用作其他算法的基线。我们使用泰坦尼克号数据集,目标是根据性别、年龄、等级等特征预测乘客的生存情况。估计器使用特征列来描述模型如何解释原始输入特征。估计器期望一个数值输入向量,特征列将有助于描述模型如何转换数据集中每个特征。

print("The base features are added")
print("The model is trained")
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)
clear_output()
print(result)

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

输出

The base features are added
The model is trained
{'accuracy': 0.7613636, 'accuracy_baseline': 0.625, 'auc': 0.809244, 'auc_precision_recall': 0.75609726, 'average_loss': 0.5452906, 'label/mean': 0.375, 'loss': 0.5347039, 'precision': 0.75, 'prediction/mean': 0.27201703, 'recall': 0.54545456, 'global_step': 200}

解释

  • 将所有基本特征都添加到模型后,就开始训练模型。
  • 这是使用 tf.estimator API 的单个命令完成的。

更新于: 2021年2月25日

119 次浏览

开启你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.