Esempio n. 1
0
def main(_):
    random.seed(FLAGS.seed)
    np.random.seed(FLAGS.seed)

    dataset = Dataset(FLAGS.dataset)

    train_input_fn = dataset.get_input_fn('train',
                                          batch_size=FLAGS.batch_size,
                                          num_epochs=FLAGS.num_epochs,
                                          shuffle=True)
    eval_input_fn = dataset.get_input_fn('test',
                                         batch_size=FLAGS.batch_size,
                                         num_epochs=1,
                                         shuffle=False)

    params = {
        'vocab_size': dataset.vocab_size,
        'embedding_size': FLAGS.embedding_size,
        'num_blocks': FLAGS.num_blocks,
        'learning_rate_init': FLAGS.learning_rate,
        'learning_rate_decay_steps': (10000 // FLAGS.batch_size) * 25,
        'learning_rate_decay_rate': 0.5,
        'clip_gradients': FLAGS.clip_gradients,
        'debug': FLAGS.debug,
    }

    eval_metrics = {
        "accuracy":
        tf.contrib.learn.metric_spec.MetricSpec(
            tf.contrib.metrics.streaming_accuracy)
    }

    config = tf.contrib.learn.RunConfig(tf_random_seed=FLAGS.seed,
                                        save_summary_steps=120,
                                        save_checkpoints_secs=600,
                                        keep_checkpoint_max=5,
                                        keep_checkpoint_every_n_hours=1,
                                        log_device_placement=True)

    dataset_name = os.path.splitext(os.path.basename(FLAGS.dataset))[0]
    timestamp = int(time.time())
    model_dir = os.path.join(FLAGS.model_dir, dataset_name, str(timestamp))
    estimator = tf.contrib.learn.Estimator(model_dir=model_dir,
                                           model_fn=model_fn,
                                           config=config,
                                           params=params)

    experiment = tf.contrib.learn.Experiment(estimator,
                                             train_input_fn,
                                             eval_input_fn,
                                             train_steps=None,
                                             eval_steps=None,
                                             eval_metrics=eval_metrics,
                                             train_monitors=None,
                                             local_eval_frequency=1)

    experiment.train_and_evaluate()
Esempio n. 2
0
def main(_):
    dataset = Dataset(FLAGS.dataset)

    input_fn = dataset.get_input_fn('test',
                                    batch_size=FLAGS.batch_size,
                                    num_epochs=1,
                                    shuffle=False)

    params = {
        'vocab_size': dataset.vocab_size,
        'embedding_size': FLAGS.embedding_size,
        'num_blocks': FLAGS.num_blocks,
        'debug': False,
    }

    config = tf.contrib.learn.RunConfig(save_summary_steps=120,
                                        save_checkpoints_secs=600,
                                        keep_checkpoint_max=5,
                                        keep_checkpoint_every_n_hours=1,
                                        log_device_placement=True)

    eval_metrics = {
        "accuracy":
        tf.contrib.learn.metric_spec.MetricSpec(
            tf.contrib.metrics.streaming_accuracy)
    }

    estimator = tf.contrib.learn.Estimator(model_dir=FLAGS.model_dir,
                                           model_fn=model_fn,
                                           config=config,
                                           params=params)

    experiment = tf.contrib.learn.Experiment(estimator,
                                             train_input_fn=None,
                                             eval_input_fn=input_fn,
                                             train_steps=None,
                                             eval_steps=None,
                                             eval_metrics=eval_metrics,
                                             train_monitors=None,
                                             local_eval_frequency=1)

    experiment.evaluate(delay_secs=10)