我刚刚开始转移到tensorflow2。但是它改变了,原始代码是这样的
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)
,评估指标也不同于tf1.4,所以我现在不知道如何在tf2中进行设置。 预先感谢