且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Tensorflow Keras模型和Estimator有什么区别?

更新时间:2023-12-02 19:50:10

背景

Estimators API已在1.1版中添加到Tensorflow中,并提供了对较低层Tensorflow核心操作的高层抽象.它可以与Estimator实例一起使用,该实例是TensorFlow完整模型的高级表示.

Background

The Estimators API was added to Tensorflow in Release 1.1, and provides a high-level abstraction over lower-level Tensorflow core operations. It works with an Estimator instance, which is TensorFlow's high-level representation of a complete model.

Keras 与Estimators API相似,因为它抽象了深度学习模型组件,例如层,激活函数和优化器,使开发人员更容易.它是一个模型级库,不处理低级操作,这是张量操纵库后端的工作. Keras支持三个后端- Tensorflow CNTK .

Keras is similar to the Estimators API in that it abstracts deep learning model components such as layers, activation functions and optimizers, to make it easier for developers. It is a model-level library, and does not handle low-level operations, which is the job of tensor manipulation libraries, or backends. Keras supports three backends - Tensorflow, Theano and CNTK.

直到发布1.4.0 ( 2017年11月2日).现在,当您使用tf.keras(或谈论"Tensorflow Keras")时,您只需将Keras接口与Tensorflow后端一起使用即可构建和训练模型.

Keras was not part of Tensorflow until Release 1.4.0 (2 Nov 2017). Now, when you use tf.keras (or talk about 'Tensorflow Keras'), you are simply using the Keras interface with the Tensorflow backend to build and train your model.

因此,Estimator API和Keras API都提供了比低级核心Tensorflow API更高的API,您可以使用其中任何一种来训练模型.但是在大多数情况下,如果您正在使用Tensorflow,出于以下原因,您将希望使用Estimators API.

So both the Estimator API and Keras API provides a high-level API over low-level core Tensorflow API, and you can use either to train your model. But in most cases, if you are working with Tensorflow, you'd want to use the Estimators API for the reasons listed below.

您可以使用Estimators API在多台服务器上进行分布式培训,而不能使用Keras API.

You can conduct distributed training across multiple servers with the Estimators API, but not with Keras API.

Tensorflow Keras指南中说:

Estimators API用于分布式环境的训练模型.

Tensorflow估算器指南中说:

您可以在本地主机或分布式多服务器环境上运行基于Estimator的模型,而无需更改模型.此外,您可以在CPU,GPU或TPU上运行基于Estimator的模型,而无需重新编码模型.

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提供了使抽象的模型更容易构建的抽象,但是您仍然必须编写代码来构建模型.借助Estimators,Tensorflow提供了预制的Estimators ,您只需插入超参数即可直接使用这些模型.

Pre-made Estimator

Whilst Keras provides abstractions that makes building your models easier, you still have to write code to build your model. With Estimators, Tensorflow provides Pre-made Estimators, which are models which you can use straight away, simply by plugging in the hyperparameters.

预制估算器类似于您使用 scikit-learn 的方式.例如,来自Tensorflow的 tf.estimator.LinearRegressor 中的href ="http://scikit-learn.org/stable/modules/generation/sklearn.linear_model.LinearRegression.html" rel ="noreferrer"> sklearn.linear_model.LinearRegression .

Pre-made Estimators are similar to how you'd work with scikit-learn. For example, the tf.estimator.LinearRegressor from Tensorflow is similar to the sklearn.linear_model.LinearRegression from scikit-learn.

Tensorflow提供了一个称为 TensorBoard 的可视化工具,可帮助您可视化图形和统计数据.通过使用Estimator,您可以轻松保存摘要以使用Tensorboard进行可视化.

Tensorflow provides a vistualzation tool called TensorBoard that helps you visualize your graph and statistics. By using an Estimator, you can easily save summaries to be visualized with Tensorboard.

要将Keras模型迁移到Estimator,请使用

To migrate a Keras model to an Estimator, use the tf.keras.estimator.model_to_estimator method.