如何使用TensorFlow和Estimators预测泰坦尼克号数据集的输出?


可以使用TensorFlow和Estimators以及之前创建的估计器“BoostedTreesClassifier”并调用其“predict”方法来预测泰坦尼克号数据集的输出。

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

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

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

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

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

示例

print("The predictions are being made")
pred_dicts = list(est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])
probs.plot(kind='hist', bins=20, title='predicted probabilities')
plt.show()

输出

The predictions are being made
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpyls8bhku/model.ckpt-100
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.

解释

  • 模型训练完成后,可以进行预测。
  • 这是对一名乘客进行的预测,以查看他们是否会幸存。
  • 该乘客属于评估数据集。
  • TensorFlow模型经过优化,可以一次对批次或示例集合进行预测。
  • 预测概率在控制台上显示。

更新于:2021年2月25日

86 次查看

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.