def main(_):
  # If available, resume from the latest checkpoint.
  checkpoint_dir = tf.train.latest_checkpoint(FLAGS.logdir)

  num_alphabets = 20
  task_names = ['Omniglot-%d' % task_id for task_id in range(num_alphabets)]

  task_data, num_classes_for_tasks = data.get_data_for_multitask_omniglot_setup(
      num_alphabets)

  router_kwargs = {}

  if FLAGS.method == 'gumbel_matrix':
    router_kwargs = {
        'probs_init': FLAGS.probs_init,
        'temperature_init': FLAGS.temperature_init,
        'temperature_min': FLAGS.temperature_min
    }

  router_fn = utils.get_router_fn_by_name(
      num_alphabets, FLAGS.method, **router_kwargs)

  training_hparams = tf.contrib.training.HParams(
      num_steps=8_000,
      batch_size=16,
      learning_rate=FLAGS.learning_rate,
      router_learning_rate=FLAGS.router_learning_rate
  )

  pathnet_layers = construct_pathnet(
      training_hparams.num_steps, task_names, num_classes_for_tasks, router_fn)

  run_omniglot_experiment(
      pathnet_layers, training_hparams, task_names, task_data, checkpoint_dir)
Ejemplo n.º 2
0
def main(_):
    mnist, mnist_classes = data.get_mnist()
    mnist_2, mnist_classes_2 = data.get_mnist()
    mnist_rot, mnist_rot_classes = data.get_rotated_mnist()
    mnist_rot_2, mnist_rot_classes_2 = data.get_rotated_mnist()

    task_names = ['MNIST', 'MNIST-2', 'MNIST-Rot', 'MNIST-Rot-2']
    task_data = [mnist, mnist_2, mnist_rot, mnist_rot_2]
    num_classes_for_tasks = [
        mnist_classes, mnist_classes_2, mnist_rot_classes, mnist_rot_classes_2
    ]

    num_tasks = len(task_names)
    router_fn = utils.get_router_fn_by_name(num_tasks, FLAGS.method)

    construct_pathnet_and_run_mnist_experiment(task_names, task_data,
                                               num_classes_for_tasks,
                                               router_fn)
Ejemplo n.º 3
0
def main(_):
  task_names, task_data, num_classes_for_tasks = get_all_tasks(
      num_cifar=20, num_mnist=10, num_fashion_mnist=10)

  router_kwargs = {}

  if FLAGS.method == 'gumbel_matrix':
    router_kwargs = {
        'probs_init': FLAGS.probs_init,
        'temperature_init': FLAGS.temperature_init,
        'temperature_min': FLAGS.temperature_min
    }

  router_fn = utils.get_router_fn_by_name(
      len(task_names), FLAGS.method, **router_kwargs)

  num_steps = 20000
  eval_frequency = 1000
  batch_size = 32

  intermediate_eval_steps = list(range(0, num_steps + 1, eval_frequency))

  training_hparams = tf.contrib.training.HParams(
      num_steps=num_steps,
      batch_size=batch_size,
      learning_rate=FLAGS.learning_rate,
      router_learning_rate=FLAGS.router_learning_rate)

  pathnet_layers = construct_pathnet(
      training_hparams.num_steps, task_names, num_classes_for_tasks, router_fn)

  run_routing_experiment(
      pathnet_layers,
      training_hparams,
      task_names,
      task_data,
      None,
      intermediate_eval_steps)
Ejemplo n.º 4
0
def main(_):
  num_alphabets = 20
  task_names = ['Omniglot-%d' % task_id for task_id in range(num_alphabets)]

  task_data, num_classes = data.get_data_for_multitask_omniglot_setup(
      num_alphabets)

  batch_size = 16
  for task_id in range(num_alphabets):
    task_data[task_id] = data.batch_all(task_data[task_id], batch_size)

  router_fn = utils.get_router_fn_by_name(num_alphabets, FLAGS.method)

  session = tf.Session(graph=tf.get_default_graph())

  tf.train.get_or_create_global_step()

  summary_writer = tf.contrib.summary.create_file_writer(FLAGS.logdir)
  summary_writer.set_as_default()

  tf.contrib.summary.initialize(session=session)

  p_inputs, p_task_id, out_logits = build_pathnet_eval_graph(
      task_names, batch_size, num_classes, router_fn)

  evaluate_on = ['train', 'validation', 'test']

  p_task_accuracies = {}
  accuracy_summary_op = {}

  for data_split in evaluate_on:
    (p_task_accuracies[data_split], accuracy_summary_op[data_split]) =\
        utils.create_accuracy_summary_ops(
            task_names, summary_name_prefix='eval_%s' % data_split)

  # This `Saver` is not used to save variables, only to restore them from
  # the checkpoints.
  saver = tf.train.Saver(tf.global_variables())

  previous_checkpoint_path = ''
  time_waited_for_checkpoints = 0

  while time_waited_for_checkpoints < _MAX_WAIT_FOR_NEW_CHECKPOINTS:
    latest_checkpoint_path = tf.train.latest_checkpoint(FLAGS.logdir)

    if latest_checkpoint_path in [None, previous_checkpoint_path]:
      print('Found no new checkpoints')

      time_waited_for_checkpoints += _CHECK_FOR_CHECKPOINTS_FREQUENCY
      time.sleep(_CHECK_FOR_CHECKPOINTS_FREQUENCY)

      continue
    else:
      time_waited_for_checkpoints = 0

    print('Reloading checkpoint: %s' % latest_checkpoint_path)
    previous_checkpoint_path = latest_checkpoint_path

    saver.restore(session, latest_checkpoint_path)

    for data_split in evaluate_on:
      eval_data = [
          dataset[data_split].make_one_shot_iterator().get_next()
          for dataset in task_data
      ]

      print('Evaluating on: %s' % data_split)

      task_accuracies = utils.run_pathnet_evaluation(
          session, p_inputs, p_task_id, out_logits, task_names, eval_data)

      utils.run_accuracy_summary_ops(
          session,
          p_task_accuracies[data_split],
          task_accuracies,
          accuracy_summary_op[data_split])