Estimators
TensorFlow 高级API 的 Estimator 极大的简化了机器学习编程。Estimators 封装了以下操作:
- training
- evaluation
- prediction
- export for serving
Estimators 的优点
Estimators 提供以下好处:
- 你可以运行基于 Estimators 的模型在一个本地或者分布式的多服务器环境之上并不用修改你的模型。更进一步的讲,你可以在CPUs, GPUs或者是 TPUs上,运行 基于 Estimators 的模型而不需要重新编写你的模型。
- Estimators 简化共享执行在模型开发者之间。
- 你可以使用高级直观的代码来开发艺术模型的一个状态。简短的说,用 Estimators 创建模型比用低级 TensorFlow APIs 简单了太多太多。
- Estimators 是自创建在
tf.layers
上的,这样简化了自定义化。 - Estimators 为你创建
graph
,换句话说,你不用自己去创建graph
- Estimators 提供了一个安全的分布式训练循环:
- 创建
graph
- 初始化变量
- 开始排队
- 解决异常
- 创建
checkpoint
文件和恢复 - 为TensorBoard 保存概要
- 创建
当用 Estimators 编写一个应用时,你必须把输入数据管道从模型里分开。这一分离简化了实验室使用的不同数据集。
预制的 Estimators
预制的 Estimators 能够使你致力于更高的概念等级。你不再需要担心创建计算图或者 sessions
,因为 Estimators 为你解决了所有的基础工作。预制的 Estimators 为你创建并管理 Graph
和 Session
对象。进一步讲,预制的 Estimators 允许你用不同的模型架构进行试验通过最小的代码修改。例如
DNNClassifier
是一个预制的 Estimator 类,它训练分类模型通过密集的,向前传播神经网络。
一个预制的 Estimators 编程结构
一个预制的 Estimator 编程过程典型的由接下来的四个步骤组成:
-
写一个或者多个数据集导入方法。例如,你可能创建一个方法来导入训练数据集和另一个方法来导入测试数据集。每个数据集导入方法必须返还两个对象:
- 一个 ‘keys’ 是 feature 名字和 ‘values’ 是 Tensors 的字典,其包含对应的特征数据。
- 一个 Tensor 包含一个或者多个标签。
例如:下面的代码为一个输入方法展示一个基础框架:
def input_fn(dataset):
... # manipulate dataset, extracting feature names and the label
return feature_dict, label
-
定义特征列。每个
tf.feature_column
识别一个特征名称,它的类型,和任何输入预处理。例如,下面的代码创建三个特征列它们保留着整数型或者浮点型数据。前两个特征列简单地识别特征的名字和类型。第三个特征列也调用一个指定的 lambda 方程来缩放原数据。
# Define three numeric feature columns.
population = tf.feature_column.numeric_column('population')
crime_rate = tf.feature_column.numeric_column('crime_rate')
median_education = tf.feature_column.numeric_column('median_education',
normalizer_fn='lambda x: x - global_education_mean')
-
实例化相关的预制 Estimators。例如:这里有个
LinearClassifier
预制 Estimator 例子。
# Instantiate an estimator, passing the feature columns.
estimator = tf.estimator.Estimator.LinearClassifier(
feature_columns=[population, crime_rate, median_education],
)
-
调用 training, evaluation, 或者 inference 函数。例如:所有的 Estimators 提供一个
train
方法来训练模型。
# my_training_set is the function created in Step 1
estimator.train(input_fn=my_training_set, steps=2000)
预制 Estimators 的好处
预制 Estimators 编码的最优方案,提供以下几点好处:
- 最佳实践于决定位于计算图的不同的部分应该运行,实施策略在一个单一的机器或者集群。
- 最佳实践于 event(summary) 写入和有用的概括。
如果不用,你必须执行这些特征通过你自己。
自定义 Estimators
无论是自定义还是预制的 Estimator,每个 Estimator 的中心点是它的 model function,它是一个方法为训练,评估和预测来创建图。当你正在使用一个预制的 Estimator,其他某个人已经执行模型方法。当依赖一个自定义 Estimator,你必须自己写这个模型方法。 这个指南文件解释如何写模型方程。
推荐的工作流程
我们推荐以下工作流程:
1. 假设存在合适的预制 Estimator,请使用它构建你的第一个模型并使用其结果建立一个基线。
2. 使用此预制 Estimator 构建和测试你的整体流程管道,包括你的数据的完整性和可靠性。
3. 如果有合适的替代预制 Estimator 可用,则运行实验以确定哪种预制估算器能够产生最佳结果。
4. 可能通过构建您自己的自定义估算器来进一步改进您的模型。
从 Keras 模型创建 Estimators
你可以将现有的 Keras 模型转换为 Estimators。 这样做可以让你的 Keras 模型访问 Estimator 的优点,例如分布式训练。调用 tf.keras.estimator.model_to_estimator
,如下例所示:
# Instantiate a Keras inception v3 model.
keras_inception_v3 = tf.keras.applications.inception_v3.InceptionV3(weights=None)
# Compile model with the optimizer, loss, and metrics you'd like to train with.
keras_inception_v3.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy',
metric='accuracy')
# Create an Estimator from the compiled Keras model. Note the initial model
# state of the keras model is preserved in the created Estimator.
est_inception_v3 = tf.keras.estimator.model_to_estimator(keras_model=keras_inception_v3)
# Treat the derived Estimator as you would with any other Estimator.
# First, recover the input name(s) of Keras model, so we can use them as the
# feature column name(s) of the Estimator input function:
keras_inception_v3.input_names # print out: ['input_1']
# Once we have the input name(s), we can create the input function, for example,
# for input(s) in the format of numpy ndarray:
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"input_1": train_data},
y=train_labels,
num_epochs=1,
shuffle=False)
# To train, we call Estimator's train function:
est_inception_v3.train(input_fn=train_input_fn, steps=2000)
Note that the names of feature columns and labels of a keras estimator come from the corresponding compiled keras model. For example, the input key names for train_input_fn above can be obtained from keras_inception_v3.input_names, and similarly, the predicted output names can be obtained from keras_inception_v3.output_names.
For more details, please refer to the documentation for tf.keras.estimator.model_to_estimator
.
更新:一月 27, 2018