0

My main doubt if I can easly convert this script to run in a Jupyter notebook whithout have this error. The function tf.app.run() provide a wrapper that handles flag parsing. But looks like the tensorflow code forcefully exits the process in which it's running after main completes.

This is my main function:

def main(_):

    input_fn = make_input_fn


    hparams = tf.contrib.training.HParams(
        learning_rate=.1,
    )    

    config = tf.ConfigProto(
        # allow_soft_placement=True,
        # log_device_placement=True
    )    

    trainingConfig = tf.contrib.learn.RunConfig(        
        save_summary_steps=500,
        save_checkpoints_steps=500,
        model_dir=("/tmp/tf-logs/bucketized-01"),
        session_config=config
    )    

    estimator = tf.estimator.Estimator(
        model_fn=make_model,
        params=hparams,
        config=trainingConfig
    )

    estimator.train(
        input_fn=input_fn,
        steps=TRAIN_EPOCHS,
    )           

When I call in Jupyter notebook:

if __name__ == '__main__':
    tf.app.run(main)

I had this error:

INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from /tmp/tf-logs/bucketized-01/model.ckpt-2001
INFO:tensorflow:Saving checkpoints for 2002 into /tmp/tf-logs/bucketized-01/model.ckpt.
INFO:tensorflow:loss = 11.734686, step = 2002
INFO:tensorflow:global_step/sec: 4.84241
INFO:tensorflow:loss = 11.320501, step = 2102 (20.653 sec)
INFO:tensorflow:global_step/sec: 5.54159
INFO:tensorflow:loss = 9.874545, step = 2202 (18.044 sec)
INFO:tensorflow:global_step/sec: 5.20988
INFO:tensorflow:loss = 11.533301, step = 2302 (19.196 sec)
INFO:tensorflow:Saving checkpoints for 2401 into /tmp/tf-logs/bucketized-01/model.ckpt.
INFO:tensorflow:Loss for final step: 10.57784.
An exception has occurred, use %tb to see the full traceback.

SystemExit
Maxim
  • 52,561
  • 27
  • 155
  • 209
Andre Araujo
  • 2,348
  • 2
  • 27
  • 41
  • 2
    Possible duplicate of [Introductory MNIST example from Tensorflow results in Exception](https://stackoverflow.com/questions/46742441/introductory-mnist-example-from-tensorflow-results-in-exception) – Maxim Jan 22 '18 at 21:49
  • I improved my question, Thanks! – Andre Araujo Jan 23 '18 at 00:32

1 Answers1

1

Here's the full tf.app.run function:

def run(main=None, argv=None):
  """Runs the program with an optional 'main' function and 'argv' list."""
  f = flags.FLAGS

  # Extract the args from the optional `argv` list.
  args = argv[1:] if argv else None

  # Parse the known flags from that list, or from the command
  # line otherwise.
  # pylint: disable=protected-access
  flags_passthrough = f._parse_flags(args=args)
  # pylint: enable=protected-access

  main = main or _sys.modules['__main__'].main

  # Call the main function, passing through any arguments
  # to the final program.
  _sys.exit(main(_sys.argv[:1] + flags_passthrough))

Yes, it explicitly calls sys.exit(), hence it shouldn't be used in Jupyter. If all you need is flag parsing, simply call flags.FLAGS._parse_flags(args=args) or use this version:

import sys
from tensorflow.python.platform import flags

def run(main=None, argv=None):
  args = argv[1:] if argv else None
  flags_passthrough = flags.FLAGS._parse_flags(args=args)
  main = main or sys.modules['__main__'].main
  main(sys.argv[:1] + flags_passthrough)
Maxim
  • 52,561
  • 27
  • 155
  • 209