bert icon indicating copy to clipboard operation
bert copied to clipboard

how to see loss per steps or epoch during train?

Open minsuk-heo opened this issue 8 years ago • 35 comments

I successfully ran run_squad.py, but from tf.logging, I only see below, while I want to see loss is going down. how to make logs for loss or acc from training?

INFO:tensorflow:global_step/sec: 1.73561 INFO:tensorflow:examples/sec: 20.8273 INFO:tensorflow:global_step/sec: 1.73487 INFO:tensorflow:examples/sec: 20.8184 INFO:tensorflow:global_step/sec: 1.73578 INFO:tensorflow:examples/sec: 20.8294 INFO:tensorflow:global_step/sec: 1.73657 INFO:tensorflow:examples/sec: 20.8389 INFO:tensorflow:global_step/sec: 1.73621 INFO:tensorflow:examples/sec: 20.8345 INFO:tensorflow:global_step/sec: 1.73602 INFO:tensorflow:examples/sec: 20.8322 INFO:tensorflow:global_step/sec: 1.73591 INFO:tensorflow:examples/sec: 20.831 INFO:tensorflow:global_step/sec: 1.7353 INFO:tensorflow:examples/sec: 20.8236 INFO:tensorflow:global_step/sec: 1.73526 INFO:tensorflow:examples/sec: 20.8231

minsuk-heo avatar Nov 07 '18 00:11 minsuk-heo

Unfortunately this is a bug TPUEstimator where it doesn't print the loss when training on CPU/GPU. However what you can do is is to run eval mode in a separate terminal on the training directory. Basically it's the same command but --do_train=False and --do_predict=True. Although that will give you predictions rather than the loss. If you only have one GPU you can run the eval on the CPU (it will just be slow).

The hackiest way to print the loss is just to use loss = tf.Print(loss, [loss])) before returning the loss.

jacobdevlin-google avatar Nov 07 '18 00:11 jacobdevlin-google

@minsuk-heo I can add training hook in TPUEstimatorSpec:

logging_hook = tf.train.LoggingTensorHook({"loss": total_loss}, every_n_iter=10) output_spec = tf.contrib.tpu.TPUEstimatorSpec( mode=mode, loss=total_loss, train_op=train_op, training_hooks=[logging_hook], scaffold_fn=scaffold_fn)

avostryakov avatar Nov 07 '18 07:11 avostryakov

thanks @avostryakov, your solution works perfectly!

minsuk-heo avatar Nov 07 '18 21:11 minsuk-heo

thanks @avostryakov, your solution works perfectly!

Did you solve this problem? I can not see the logs for loss by add training hook

FuYanzhe2 avatar Dec 13 '18 08:12 FuYanzhe2

@FuYanzhe2 me too.

agent-1900 avatar Dec 19 '18 09:12 agent-1900

@avostryakov I try it, but when it run, it occured : TypeError: new() got an unexpected keyword argument 'training_hooks'

Please help me, my tensorflow version is 1.10

xiongma avatar Dec 28 '18 08:12 xiongma

@avostryakov I try it, but when it run, it occured : TypeError: new() got an unexpected keyword argument 'training_hooks'

Please help me, my tensorflow version is 1.10

use tf 1.11

zheolong avatar Jan 30 '19 12:01 zheolong

INFO:tensorflow:loss = 232.23836 just loss,no step!

hantaozi avatar Mar 11 '19 13:03 hantaozi

@FuYanzhe2 me too.

You should add tf.logging.set_verbosity(tf.logging.INFO) first

Dikea avatar Jun 03 '19 08:06 Dikea

@avostryakov Did you succeed with LoggingTensorHook? I tried but got an error saying "loss/Mean" marked as unfetchable. It seems like a TPU issue.

RuibinMa avatar Jun 22 '19 01:06 RuibinMa

Is there any way to add a early stopping hook? How do I pass estimator to the TPUEstimatorSpec?

abhi060698 avatar Jul 23 '19 01:07 abhi060698

@abhi060698 If you want to use a early_stopping_hook, please use estimator.train_and_evaluate api.

train_hooks_list = [early_stopping_hook()] # implemented by yourself
train_spec = tf.estimator.TrainSpec(train_input_fn, hooks=train_hooks_list)
eval_spec =  # implemented by yourself
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

stevewyl avatar Jul 30 '19 07:07 stevewyl

@abhi060698 If you want to use a early_stopping_hook, please use estimator.train_and_evaluate api.

train_hooks_list = [early_stopping_hook()] # implemented by yourself
train_spec = tf.estimator.TrainSpec(train_input_fn, hooks=train_hooks_list)
eval_spec =  # implemented by yourself
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

这个不是tpu的吧 @stevewyl

dh12306 avatar Aug 23 '19 08:08 dh12306

Mark

guotong1988 avatar Sep 05 '19 02:09 guotong1988

@dh12306 It's just for original Estimator API. Maybe TPU doesn't work.

stevewyl avatar Sep 05 '19 23:09 stevewyl

I've been having all sorts of issues trying to get TPUEstimator to play nice with summaries and logging, so it would be really nice to get just this working Running @avostryakov 's code in TF 1.14 on a TPUv3 throws the following error:

tensorflow[24580] ERROR Error recorded from outfeed: Attempted to use a closed Session. tensorflow[24580] ERROR Error recorded from infeed: Attempted to use a closed Session. tensorflow[24580] ERROR Error recorded from training_loop: Operation 'add_5' has been marked as not fetchable.

AlLongley avatar Oct 05 '19 07:10 AlLongley

can anybody tell me what does global_step/sec and examples/sec means? what's difference between them? and how they are helpful?

also what can I do if I dont want to see them during training

Ashbajawed avatar Oct 15 '19 04:10 Ashbajawed

what about if i want to get the training time for each epoch?

elvinjgalarza avatar Nov 10 '19 07:11 elvinjgalarza

train_hooks_list = [early_stopping_hook()] # implemented by yourself train_spec = tf.estimator.TrainSpec(train_input_fn, hooks=train_hooks_list) eval_spec = # implemented by yourself tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

does not work on TPU

check https://github.com/tensorflow/tpu/issues/606

prokok avatar Dec 12 '19 19:12 prokok

@Allong12 did u solve this problem, I trained my model on TPUV3-8, it appeared same problem.

xiongma avatar Dec 23 '19 08:12 xiongma