支持 TPU 的估算器。
警告:不建议将估算器用于新代码。估算器运行tf.compat.v1.Session-style 代码更难正确编写,并且可能出现意外行为,尤其是与 TF 2 代码结合使用时。估算器确实属于我们的兼容性保证,但不会收到除安全漏洞以外的任何修复。见迁移指南详情。
继承自:Estimator
用法
tf.compat.v1.estimator.tpu.TPUEstimator(
model_fn=None, model_dir=None, config=None, params=None, use_tpu=True,
train_batch_size=None, eval_batch_size=None, predict_batch_size=None,
batch_axis=None, eval_on_tpu=True, export_to_tpu=True, export_to_cpu=True,
warm_start_from=None, embedding_config_spec=None,
export_saved_model_api_version=ExportSavedModelApiVersion.V1
)参数
-
model_fnEstimator要求的模型函数,它返回 EstimatorSpec 或 TPUEstimatorSpec。training_hooks、'evaluation_hooks' 和prediction_hooks不得在 model_fn 内捕获任何 TPU 张量。 -
model_dir保存模型参数、图形等的目录。这也可用于将检查点从目录加载到估计器中,以继续训练先前保存的模型。如果None,如果设置,将使用config中的 model_dir。如果两者都设置,则它们必须相同。如果两者都是None,将使用临时目录。 -
configtpu_config.RunConfig配置对象。不能是None。 -
params超参数的可选dict将被传递到input_fn和model_fn。键是参数的名称,值是基本的 Python 类型。TPUEstimator有保留键,包括'batch_size'。 -
use_tpu一个布尔值,指示是否启用 TPU 支持。目前,-TPU 训练和评估尊重这一位,但 eval_on_tpu 可以覆盖 eval 的执行。见下文。 -
train_batch_size一个表示全局训练批量大小的 int。当调用input_fn和model_fn时,TPUEstimator 将此全局批量大小转换为 per-shard 批量大小,作为参数 ['batch_size']。如果use_tpu是True,则不能是None。必须能被副本总数整除。 -
eval_batch_size表示评估批量大小的 int。必须能被副本总数整除。 -
predict_batch_size表示预测批量大小的 int。必须能被副本总数整除。 -
batch_axis一个 int 值的 Python 元组,说明了 Estimatorinput_fn生成的每个张量应如何在 TPU 计算分片中拆分。例如,如果您的 input_fn 生成(图像、标签),其中图像张量为HWCN格式,则您的分片尺寸将为 [3, 0],其中 3 对应于图像张量的N尺寸, 0 对应于分割标签以匹配相应图像的维度。如果提供 None 并且 per_host_input_for_training 为 True,则将根据主要维度对批次进行分片。如果 tpu_config.per_host_input_for_training 为 False 或PER_HOST_V2,则忽略 batch_axis。 -
eval_on_tpu如果为 False,则评估在 CPU 或 GPU 上运行。在这种情况下,当mode作为EVAL调用时,model_fn 必须返回EstimatorSpec。 -
export_to_tpu如果为 True,export_saved_model()会导出一个元图以在 TPU 上提供服务。请注意,不支持的导出模式(例如 EVAL)将被忽略。对于这些模式,只会导出一个 CPU 模型。目前,export_to_tpu 仅支持 PREDICT。 -
export_to_cpu如果为 True,export_saved_model()会导出一个元图以在 CPU 上提供服务。 -
warm_start_from检查点或 SavedModel 的可选字符串文件路径以进行热启动,或tf.estimator.WarmStartSettings对象以完全配置热启动。如果提供了字符串文件路径而不是WarmStartSettings,则所有变量都是热启动的,并且假定词汇表和张量名称未更改。 -
embedding_config_spec可选的 EmbeddingConfigSpec 实例以支持使用 TPU 嵌入。 -
export_saved_model_api_version整数:1 或 2。1 对应 V1,2 对应 V2。 (默认为 V1)。在 V1 中,export_saved_model()为用户添加了rewrite() 和TPUPartitionedCallOp();而在 v2 中,用户应在其model_fn 中添加rewrite()、TPUPartitionedCallOp() 等。
抛出
-
ValueErrorparams已保留 key 。
属性
-
config -
model_dir -
model_fn返回绑定到self.params的model_fn。 -
params
迁移到 TF2
警告:这个 API 是为 TensorFlow v1 设计的。继续阅读有关如何从该 API 迁移到本机 TensorFlow v2 等效项的详细信息。见TensorFlow v1 到 TensorFlow v2 迁移指南有关如何迁移其余代码的说明。
TPU Estimator 管理自己的 TensorFlow 图和会话,因此与 TF2 行为不兼容。我们建议您迁移到较新的 tf.distribute.TPUStrategy 。有关详细信息,请参阅 TPU 指南。
TPUEstimator 还支持在 CPU 和 GPU 上进行训练。您无需定义单独的 tf.estimator.Estimator 。
TPUEstimator 处理在 TPU 设备上运行的许多细节,例如为每个内核复制输入和模型,以及定期返回主机以运行挂钩。
TPUEstimator 在调用 input_fn 和 model_fn 时将参数中的全局批量大小转换为 per-shard 批量大小。用户需要在构造函数中指定全局batch size,然后通过params['batch_size']获取input_fn和model_fn中每个shard的batch size。
对于训练,
model_fn得到 per-core 批量大小;input_fn可能会得到 per-core 或 per-host 批量大小,具体取决于TPUConfig中的per_host_input_for_training(有关详细信息,请参阅 TPUConfig 的文档字符串)。对于评估和预测,
model_fn获取 per-core 批量大小,input_fn获取 per-host 批量大小。
评估
model_fn 应该返回 TPUEstimatorSpec ,它需要 eval_metrics 用于 TPU 评估。如果eval_on_tpu 为 False,评估将在 CPU 或 GPU 上执行;在这种情况下,以下关于 TPU 评估的讨论不适用。
TPUEstimatorSpec.eval_metrics 是 metric_fn 和 tensors 的元组,其中 tensors 可以是 Tensor 的任何嵌套结构的列表(有关详细信息,请参见 TPUEstimatorSpec)。 metric_fn 接受 tensors 并将度量字符串名称中的 dict 返回到调用度量函数的结果,即 (metric_tensor, update_op) 元组。
可以将use_tpu设置为False进行测试。所有训练、评估和预测都将在 CPU 上执行。 input_fn 和 model_fn 将收到 train_batch_size 或 eval_batch_size 未修改为 params['batch_size'] 。
当前限制:
TPU 评估仅适用于单个主机(一个 TPU 工作器),广播模式除外。
用于评估的
input_fn不应引发end-of-input 异常(OutOfRangeError或StopIteration)。并且所有评估步骤和所有批次都应该具有相同的大小。
示例(MNIST):
# The metric Fn which runs on CPU.
def metric_fn(labels, logits):
predictions = tf.argmax(logits, 1)
return {
'accuracy':tf.compat.v1.metrics.precision(
labels=labels, predictions=predictions),
}
# Your model Fn which runs on TPU (eval_metrics is list in this example)
def model_fn(features, labels, mode, config, params):
...
logits = ...
if mode = tf.estimator.ModeKeys.EVAL:
return tpu_estimator.TPUEstimatorSpec(
mode=mode,
loss=loss,
eval_metrics=(metric_fn, [labels, logits]))
# or specify the eval_metrics tensors as dict.
def model_fn(features, labels, mode, config, params):
...
final_layer_output = ...
if mode = tf.estimator.ModeKeys.EVAL:
return tpu_estimator.TPUEstimatorSpec(
mode=mode,
loss=loss,
eval_metrics=(metric_fn, {
'labels':labels,
'logits':final_layer_output,
}))
预测
TPU 上的预测是支持大批量推理的实验性函数。它不是为latency-critical 系统设计的。此外,由于一些可用性问题,对于小数据集的预测,CPU .predict ,即使用 use_tpu=False 创建一个新的 TPUEstimator 实例可能更方便。
注意:与 TPU 训练/评估相比,input_fn用于预测应该引发 end-of-input 异常(OutOfRangeError或者StopIteration),作为停止信号TPUEstimator.准确地说,由input_fn产生一批数据。这predict()API 一次处理一批。当到达数据源的末尾时,这些操作之一应引发end-of-input 异常。用户通常不需要手动执行此操作。只要数据集不会永远重复,tf.dataAPI 将在生产最后一批后自动引发 end-of-input 异常。
注意:Estimator.predict 返回一个 Python 生成器。请消耗来自生成器的所有数据,以便 TPUEstimator 可以为用户正确关闭 TPU 系统。
当前限制:
TPU 预测仅适用于单个主机(一个 TPU 工作器)。
input_fn必须返回Dataset实例而不是features。事实上,.train() 和 .evaluate() 也支持 Dataset 作为返回值。
示例(MNIST):
height = 32
width = 32
total_examples = 100
def predict_input_fn(params):
batch_size = params['batch_size']
images = tf.random.uniform(
[total_examples, height, width, 3], minval=-1, maxval=1)
dataset = tf.data.Dataset.from_tensor_slices(images)
dataset = dataset.map(lambda images:{'image':images})
dataset = dataset.batch(batch_size)
return dataset
def model_fn(features, labels, params, mode):
# Generate predictions, called 'output', from features['image']
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.contrib.tpu.TPUEstimatorSpec(
mode=mode,
predictions={
'predictions':output,
'is_padding':features['is_padding']
})
tpu_est = TPUEstimator(
model_fn=model_fn,
...,
predict_batch_size=16)
# Fully consume the generator so that TPUEstimator can shutdown the TPU
# system.
for item in tpu_est.predict(input_fn=input_fn):
# Filter out item if the `is_padding` is 1.
# Process the 'predictions'
导出
export_saved_model 导出 2 个元图,一个带有 saved_model.SERVING ,另一个带有 saved_model.SERVING 和 saved_model.TPU 标签。在服务时,这些标签用于选择要加载的适当元图。
在 TPU 上运行图之前,需要初始化 TPU 系统。如果使用 TensorFlow Serving model-server,则会自动完成。如果没有,请使用 session.run(tpu.initialize_system()) 。
API 有两个版本:1 或 2。
在 V1 中,导出的 CPU 图表原样为model_fn。导出的 TPU 图将 tpu.rewrite() 和 TPUPartitionedCallOp 包在 model_fn 周围,因此默认情况下 model_fn 在 TPU 上。要将操作放在 CPU 上,可以使用tpu.outside_compilation(host_call, logits)。
例子:
def model_fn(features, labels, mode, config, params):
...
logits = ...
export_outputs = {
'logits':export_output_lib.PredictOutput(
{'logits':logits})
}
def host_call(logits):
class_ids = math_ops.argmax(logits)
classes = string_ops.as_string(class_ids)
export_outputs['classes'] =
export_output_lib.ClassificationOutput(classes=classes)
tpu.outside_compilation(host_call, logits)
...
在 V2 中,export_saved_model() 设置 params['use_tpu'] 标志以让用户知道代码是否正在导出到 TPU(或没有)。当 params['use_tpu'] 为 True 时,用户需要调用 tpu.rewrite() , TPUPartitionedCallOp 和/或 batch_function() 。
提示:推荐使用 V2,因为它更灵活(例如:批处理等)。
相关用法
- Python tf.compat.v1.estimator.tpu.RunConfig用法及代码示例
- Python tf.compat.v1.estimator.tpu.experimental.EmbeddingConfigSpec用法及代码示例
- Python tf.compat.v1.estimator.DNNEstimator用法及代码示例
- Python tf.compat.v1.estimator.experimental.KMeans用法及代码示例
- Python tf.compat.v1.estimator.regressor_parse_example_spec用法及代码示例
- Python tf.compat.v1.estimator.BaselineRegressor用法及代码示例
- Python tf.compat.v1.estimator.inputs.numpy_input_fn用法及代码示例
- Python tf.compat.v1.estimator.LinearRegressor用法及代码示例
- Python tf.compat.v1.estimator.BaselineEstimator用法及代码示例
- Python tf.compat.v1.estimator.BaselineClassifier用法及代码示例
- Python tf.compat.v1.estimator.LinearClassifier用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedRegressor用法及代码示例
- Python tf.compat.v1.estimator.DNNClassifier用法及代码示例
- Python tf.compat.v1.estimator.DNNRegressor用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedEstimator用法及代码示例
- Python tf.compat.v1.estimator.Estimator用法及代码示例
- Python tf.compat.v1.estimator.LinearEstimator用法及代码示例
- Python tf.compat.v1.estimator.classifier_parse_example_spec用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedClassifier用法及代码示例
- Python tf.compat.v1.enable_eager_execution用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.estimator.tpu.TPUEstimator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
