Tensorflow Keras 模型和 Tensorflow Estimators 都能够训练神经网络模型并使用它们来预测新数据。它们都是位于低级核心 TensorFlow API 之上的高级 API。那么我什么时候应该使用其中一种而不是另一种呢?
最佳答案
背景
Estimators API 已添加到 1.1 版的 Tensorflow 中,并为较低级别的 Tensorflow 核心操作提供了高级抽象。它与 Estimator 实例配合使用,Estimator 实例是 TensorFlow 对完整模型的高级表示。
Keras与 Estimators API 类似,它抽象了层、激活函数和优化器等深度学习模型组件,以方便开发人员使用。它是一个模型级库,不处理低级操作,而低级操作是张量操作库或后端的工作。 Keras 支持三个后端 - Tensorflow , Theano和 CNTK .
Keras 直到 Release 1.4.0 才成为 Tensorflow 的一部分(2017 年 11 月 2 日)。现在,当您使用 tf.keras(或谈论“Tensorflow Keras”)时,您只需使用 Keras 接口(interface)和 Tensorflow 后端来构建和训练您的模型。
因此,Estimator API 和 Keras API 都在低级核心 Tensorflow API 上提供了高级 API,您可以使用其中任何一个来训练模型。但在大多数情况下,如果您使用 Tensorflow,则出于下面列出的原因,您会希望使用 Estimators API。
分布
您可以使用 Estimators API 跨多个服务器进行分布式训练,但不能使用 Keras API。
来自Tensorflow Keras Guide ,它说:
The Estimators API is used for training models for distributed environments.
来自Tensorflow Estimators Guide ,它说:
You can run Estimator-based models on a local host or on a distributed multi-server environment without changing your model. Furthermore, you can run Estimator-based models on CPUs, GPUs, or TPUs without recoding your model.
预制估算器
虽然 Keras 提供的抽象使构建模型变得更加容易,但您仍然需要编写代码来构建模型。通过估算器,Tensorflow 提供了预制估算器,这些模型您只需插入超参数即可立即使用。
预制估算器与您使用 scikit-learn
的方式类似。 。例如,tf.estimator.LinearRegressor
来自 Tensorflow 类似于 sklearn.linear_model.LinearRegression
来自scikit-learn
。
与其他 Tensorflow 工具集成
Tensorflow 提供了一个名为 TensorBoard 的可视化工具这可以帮助您可视化图表和统计数据。通过使用估算器,您可以轻松保存摘要,以便使用 Tensorboard 进行可视化。
将 Keras 模型转换为估算器
要将 Keras 模型迁移到 Estimator,请使用 tf.keras.estimator.model_to_estimator
方法。
关于tensorflow - Tensorflow Keras 模型和估算器有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455863/