gpt4 book ai didi

python - 如何从 tf.estimator.Estimator 获取最后一个 global_step

转载 作者:行者123 更新时间:2023-12-02 02:35:35 26 4
gpt4 key购买 nike

train(...) 完成后,如何从 tf.estimator.Estimator 获取最后一个 global_step ?例如,典型的基于估算器的训练例程可能如下设置: n_epochs = 10 model_dir = '/path/to/model_dir'

def model_fn(features, labels, mode, params):
# some code to build the model
pass

def input_fn():
ds = tf.data.Dataset() # obviously with specifying a data source
# manipulate the dataset
return ds

run_config = tf.estimator.RunConfig(model_dir=model_dir)
estimator = tf.estimator.Estimator(model_fn=model_fn, config=run_config)

for epoch in range(n_epochs):
estimator.train(input_fn=input_fn)
# Now I want to do something which requires to know the last global step, how to get it?
my_custom_eval_method(global_step)

evaluate()方法返回一个包含 global_step 作为字段的字典。如果由于某种原因我不能或不想使用此方法,如何获取 global_step

最佳答案

只需在训练循环之前创建一个钩子(Hook):

class GlobalStepHook(tf.train.SessionRunHook):
def __init__(self):
self._global_step_tensor = None
self.value = None

def begin(self):
self._global_step_tensor = tf.train.get_global_step()

def after_run(self, run_context, run_values):
self.value = run_context.session.run(self._global_step_tensor)

def __str__(self):
return str(self.value)

global_step = GlobalStepHook()
for epoch in range(n_epochs):
estimator.train(input_fn=input_fn, hooks=[global_step])
# Now the global_step hook contains the latest value of global_step
my_custom_eval_method(global_step.value)

关于python - 如何从 tf.estimator.Estimator 获取最后一个 global_step,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com