import tensorflow as tf

    try:
        config = load_config(logdir)
    except RuntimeError:
        print('Failed to load existing config.')
    except IOError:
        config = save_config(config, logdir)
    trainer = trainer_.Trainer(logdir, config=config)
    cleanups = []
    try:
        with tf.variable_scope('graph', use_resource=True):
            data = get_batch(datasets, trainer.phase, trainer.reset)
            score, summary, cleanups = model_fn(data, trainer, config, logdir)
            message = 'Graph contains {} trainable variables.'
            tf.logging.info(message.format(tools.count_weights()))
            if config.test_steps:
                trainer.add_phase(
                    'test', config.test_steps, score, summary,
                    batch_size=config.batch_shape[0],
