Tensorflow评估频率

时间:2018-04-23 20:59:56

标签: tensorflow frequency evaluate

我在tensorflow中使用train_and_evaluate函数,并希望更频繁地进行eval步骤(通过全局步骤或经过的时间)。这是我的代码(模型函数未显示)。

def get_classifier(batch_size):
    config = tf.estimator.RunConfig(
        model_dir="models/shape_model_cnn_3",
        save_checkpoints_secs=300,
        save_summary_steps=100)

    params = tf.contrib.training.HParams(
        batch_size=batch_size,
        num_conv=[48,64,96], # Sizes of each convolutional layer
        conv_len=[2,3,4], # Kernel size of each convolutional layer
        num_nodes=128, # Number of LSTM nodes for each LSTM layer
        num_layers=3, # Number of LSTM layers
        num_classes=7, # Number of classes in final layer
        learning_rate=0.0001,
        gradient_clipping_norm=9.0,
        dropout=0.3)

    classifier = tf.estimator.Estimator(
        model_fn=my_model,
        config=config,
        params=params
    )

    return classifier

classifier = get_classifier(8)

train_spec = tf.estimator.TrainSpec(
    input_fn=lambda:input.batch_dataset("dataset/shape-train-???.tfrecords", tf.estimator.ModeKeys.TRAIN, 8),
    max_steps=100000
)

eval_spec = tf.estimator.EvalSpec(
    input_fn=lambda:input.batch_dataset("dataset/shape-eval-???.tfrecords", tf.estimator.ModeKeys.EVAL, 8)
)

tf.estimator.train_and_evaluate(classifier, train_spec, eval_spec)

我已尝试在start_delay_secs中使用EvalSpec参数,我不确定这是不是它的用途但是它似乎没有任何影响

4 个答案:

答案 0 :(得分:0)

您可以将max_steps设置为较低的数字,以便更快地进行评估。

这将重置输入功能。目前,没有办法暂停输入功能并使用估算器在相同状态下恢复。我们正在考虑添加此功能。

答案 1 :(得分:0)

我发现EvalSpec中有一个参数,`throttle_secs'在几秒钟后开始评估阶段。或者,如果您想根据许多步骤进行评估,可以使用for循环并按照@Kathy Wu的建议逐步增加max_steps。

答案 2 :(得分:0)

改为使用tf.contrib.learn.Experiment

例如:

experiment = tf.contrib.learn.Experiment(

    estimator=estimator,  # Estimator

    train_input_fn=train_input_fn,  # First-class function

    eval_input_fn=eval_input_fn,  # First-class function

    train_steps=params.train_steps,  # Minibatch steps

    min_eval_frequency=params.min_eval_frequency,  # Eval frequency

    train_monitors=[train_input_hook],  # Hooks for training

    eval_hooks=[eval_input_hook],  # Hooks for evaluation

    eval_steps=None  # Use evaluation feeder until its empty

)

learn_runner.run(

    experiment_fn=experiment,  # First-class function

    run_config=run_config,  # RunConfig

    schedule="train_and_evaluate",  # What to run

    hparams=params  # HParams

)

答案 3 :(得分:0)

设置save_checkpoints_steps时,它会在指定的步骤数后运行评估;配置:

tf.estimator.RunConfig(save_summary_steps=5, log_step_count_steps=3, save_checkpoints_steps=40)

每个40 steps都进行一次评估。