如何在Tensorflow Estimator中每隔1000个步骤重置一次指标?

时间:2019-09-21 13:30:22

标签: python tensorflow

我用tensorflow(1.14.1)估计器创建了一个分类器,并在每个步骤中计算了度量。我使用tensorboard来显示指标,并且每个步骤都将其融合。我想在每个时代重置它。

这适用于运行Python(3.7)和tensorflow(1.14.1)的HPC。

def fn_builder(...):
    def model_fn(features, labels, mode, params):
        # create bert-mlp net
        ids = features['ids']
        words = features['words']
        masks = features['mask']
        label_mask = features['label_mask']

        # model
        is_training = (mode == tf.estimator.ModeKeys.TRAIN)
        loss, prd_label = create_model(...)

        pre = tf.metrics.precision(labels, prd_label, weights=label_mask)

        metrics = {'Precision': pre}
        tf.summary.scalar('p', pre[1])


        if mode == tf.estimator.ModeKeys.TRAIN:
            optimizer = Adam(learning_rate=learning_rate)
            train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
            return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op, eval_metric_ops=metrics)



    return pi_model_fn

if __name__ == "__main__":
    batch_size = 5
    epochs = 30
    num_eval_steps = 20 / batch_size + 1
    num_train_steps = 20 * epochs / batch_size

    pi_checkpointing_config = tf.estimator.RunConfig(
        model_dir='data/pi',
        save_checkpoints_secs=10 * 60,  # Save checkpoints every 10min.
        keep_checkpoint_max=10,  # Retain the 10 most recent checkpoints.

    )

    pi = tf.estimator.Estimator(
        model_fn=fn_builder(...),
        config=pi_checkpointing_config)
    pi.train(
        input_fn=input_fn_creator(...),
        max_steps=num_train_steps
    )

这是我的:https://tva1.sinaimg.cn/large/006y8mN6ly1g77gj8vie2j30m808ut8y.jpg

这就是我想要的:https://i.stack.imgur.com/LraT7.png

0 个答案:

没有答案