我对TensorFlow相对陌生,并且在TensorBoard和Estimator API中绝对是新事物。我想从给出的源代码中训练Tensorflow的经过稍微修改的Resnet模型,作为正式模型,它使用Estimator。
我需要测试误差和准确性图。精度图只显示了一个点,根本没有测试错误。我还需要x轴上的历元数,而不是步骤数。使用低级Tensorflow可以轻松实现这一点,但是我需要使用给出的模型。
我在resnet_fn中创建learning_rate,cross_entropy和train_accuracy张量,如下所示。我还在resnet_fn中添加了SummarySaverHook。这也没有帮助。
def resnet_model_fn(...):
...
tf.identity(learning_rate, name='learning_rate')
tf.summary.scalar('learning_rate', learning_rate)
....
summary_hook = tf.train.SummarySaverHook(
flags.epochs_per_eval,
output_dir=FLAGS.model_dir,
summary_op=tf.summary.merge_all())
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op,
eval_metric_ops=metrics,
training_hooks=[summary_hook],
evaluation_hooks=[summary_hook])
这是resnet_main()。我可以在终端上看到那些张量“ eval_cross_entropy”等,但是它们在TensorBoard中根本没有显示。我也正在分享TensorBoard的屏幕截图。
def resnet_main(flags, model_function, input_function):
# Using the Winograd non-fused algorithms provides a small performance boost.
os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'
# Set up a RunConfig to only save checkpoints once per training cycle.
run_config = tf.estimator.RunConfig().replace(save_checkpoints_secs=1e9, save_summary_steps=flags.epochs_per_eval)
classifier = tf.estimator.Estimator(
model_fn=model_function, model_dir=flags.model_dir, config=run_config,
params={
'resnet_size': flags.resnet_size,
'data_format': flags.data_format,
'batch_size': flags.batch_size,
})
for _ in range(flags.train_epochs // flags.epochs_per_eval):
tensors_to_log = {
'learning_rate': 'learning_rate',
'cross_entropy': 'cross_entropy',
'train_accuracy': 'train_accuracy'
}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=flags.epochs_per_eval)
print('Starting a training cycle.')
def input_fn_train():
return input_function(True, flags.data_dir, flags.batch_size,
flags.epochs_per_eval, flags.num_parallel_calls)
classifier.train(input_fn=input_fn_train, hooks=[logging_hook])
tensors_to_log_eval = {
'eval_cross_entropy': 'cross_entropy',
'eval_train_accuracy': 'train_accuracy'
}
logging_hook_eval = tf.train.LoggingTensorHook(
tensors=tensors_to_log_eval, every_n_iter=flags.epochs_per_eval)
print('Starting to evaluate.')
# Evaluate the model and print results
def input_fn_eval():
return input_function(False, flags.data_dir, flags.batch_size,
1, flags.num_parallel_calls)
eval_results = classifier.evaluate(input_fn=input_fn_eval, hooks=[logging_hook_eval])
tensors_to_log_pred = {
'pred_cross_entropy': 'cross_entropy'
}
logging_hook_pred = tf.train.LoggingTensorHook(
tensors=tensors_to_log_pred, every_n_iter=flags.epochs_per_eval)
print('Starting to predict.')
def input_fn_pred():
return input_function(False, flags.data_dir, flags.batch_size,
1, flags.num_parallel_calls)
pred_results = classifier.predict(input_fn=input_fn_pred, hooks=[logging_hook_pred])
return eval_results, pred_results
如何获得带有x轴历元数的测试误差和准确性图?