Как настроить глобальные шаги и eval_metrics в пользовательском оценщике Tensorflow2 - PullRequest
0 голосов
/ 01 ноября 2019

Я только начинаю переходить на tenorflow2. Но он меняется, и исходный код, подобный этому

    def build_model_fn(self,params):
        def model_fn(features,labels,mode):
            feature=features['features']
            target=None if mode==tf.estimator.ModeKeys.PREDICT else features['labels']
            model = self.model(params, is_training=mode == tf.estimator.ModeKeys.TRAIN)

            with tf.GradientTape() as g:
                model_outputs=model(feature)
                predictions={'predictions':model_outputs}
                tf.summary.histogram('predictions',model_outputs)

                if mode == tf.estimator.ModeKeys.PREDICT:
                    return tf.estimator.EstimatorSpec(mode, predictions=predictions)

                loss = self.create_loss(target, model_outputs)

            if mode==tf.estimator.ModeKeys.EVAL:
                metrics={'eval_loss':tf.losses.mean_squared_error(target,model_outputs)}
                return tf.estimator.EstimatorSpec(mode,loss=loss,eval_metric_ops=metrics)
            else:
                logging.info('*'*10+'Start To Train'+'*'*10)
                training_hooks=tf.estimator.LoggingTensorHook(tensors={'loss':loss},every_n_iter=100)
                gradients=g.gradient(loss,model.trainable_variables)
                train_op=tf.optimizers.Adam(3e-4).apply_gradients(zip(gradients,model.trainable_variables))#self.create_optimizer(loss,var_list=model.trainable_variables)
                return tf.estimator.EstimatorSpec(mode,loss=loss,train_op=train_op,training_hooks=[training_hooks])
        return model_fn

, но он сообщит об ошибке или предупреждающей информации, подобной этой

WARNING:tensorflow:It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
2019-11-01 10:58:18,221 - tensorflow - WARNING - It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
WARNING:tensorflow:It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
2019-11-01 10:58:18,246 - tensorflow - WARNING - It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
WARNING:tensorflow:It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
2019-11-01 10:58:18,265 - tensorflow - WARNING - It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
WARNING:tensorflow:It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
2019-11-01 10:58:18,285 - tensorflow - WARNING - It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
WARNING:tensorflow:It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
2019-11-01 10:58:18,304 - tensorflow - WARNING - It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize.
INFO:tensorflow:loss = 0.42501944, step = 0 (2.332 sec)

, и показатели eval также отличаются от tf1.4,так что я не знаю, как настроить в TF2 сейчас. Заранее спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...