在上一篇文章Tensorflow Dataset API详解里面,我们说Datasets API和Estimators API是TensorFlow 1.3开始引入的两个高级API。其中,Estimators API 提供了训练模型、测试模型准确率和生成预测的方法。在这篇文章里面,我准备对Estimator API做一个基本讲解,其中主要内容还是来源于官方文档。
首先,还是先看看tensorflow中Estimators API的组成图:
从上图我们可以知道:Estimators分为Pre-made Estimators和custom Estimators两大类。其中,tf.estimator.Estimators是基类(base class),pre-made Estimators是基类的子类,而custom Estimators则是基类的实例(instance)。
Pre-made Estimators和custom Estimators差异主要在于tensorflow中是否有它们可以直接使用的模型函数(model function or model_fn)d的实现。对于前者,tensorflow中已经有写好的model function,因而直接调用即可;而后者的model function需要自己编写。因此,Pre-made Estimators使用方便,但使用范围小,灵活性差;custom Estimators则正好相反。
Your model function could implement a wide range of algorithms, defining all sorts ofhidden layers andmetrics. Like input functions, all model functions must accept a standard group of input parameters and return a standard group of output values. Just as input functions can leverage the Dataset API,model functions can leverage the Layers API and the Metrics API.
总体来说,模型是由三部分构成:Input functions、Model functions 和Estimators(评估控制器,main function)。
- Input functions:主要是由Dataset API组成