Python 使用相同的脚本进行培训和服务(Estimator&x2B;hub)

Python 使用相同的脚本进行培训和服务(Estimator&x2B;hub),python,tensorflow,tensorflow-estimator,tensorflow-hub,Python,Tensorflow,Tensorflow Estimator,Tensorflow Hub,我想在训练和发球时使用hub,但我有点困惑如何在同一张图上使用它。也就是说,我有 def build_graph(..., mode, ...): tags_and_args= ... # one for training, one for serving if mode == 'training': hub.create_module_spec(module_fn, tags_and_args=tags_and_args) module_outp

我想在训练和发球时使用hub,但我有点困惑如何在同一张图上使用它。也就是说,我有

def build_graph(..., mode, ...):
    tags_and_args= ... # one for training, one for serving
    if mode == 'training':
        hub.create_module_spec(module_fn, tags_and_args=tags_and_args)
        module_output = hub.Module(...)
        hub.register_module_for_export(module_fn, tags_and_args=tags_and_args)

        loss, output = ...

    else:
        module_output = hub.Module(XXX)
我应该从磁盘重新加载模块吗?因此,
XXX
将是我以前保存它的路径。或者它以某种方式保存为内存中的图形对象

我将调用我的代码作为

estimator.train(...)
exporter = hub.LatestModuleExporter(...)
exporter.export(...)
esimator.export_savedmodel(...)  # for serving

您可以在估计器的模型_fn中使用hub.Module,而无需导出它。在Estimator.train()的开始处,模块的变量将根据其预先训练的值进行初始化(与随机初始化其他变量非常相似)。之后,模块的变量的行为与模型的其他变量非常相似——它们是模型检查点的一部分,并从那里恢复以进行评估、恢复培训或导出到SavedModel以提供服务,就像任何其他变量一样

导出hub.Module仅在您希望创建一个新版本的模块(通过培训更新权重)供另一个单独的估计器使用时才需要