コード例 #1
0
ファイル: experiment.py プロジェクト: tdgros/google-research
def create_datasets(config, train_split, test_split, data_format='squad'):
  """Setup datasets.

  Args:
    config: a config describing the dataset
    train_split: name of train split
    test_split: name of test split
    data_format: 'squad' or 'squad2'

  Returns:
    A ConfigDict containing 4 keys:
      train_fn, test_fn - Input functions
      train_dataset, test_dataset - instantiated dataset objects
  """
  # Load dataset
  train_config = configurable.merge(config, split_name=train_split)
  eval_config = configurable.merge(config, split_name=test_split)
  dataset_class = configurable.Configurable.load(config)

  train = dataset_class.get_input_fn(
      mode='train', config=train_config, data_format=data_format)
  test = dataset_class.get_input_fn(
      mode='eval', config=eval_config, data_format=data_format)

  train_dataset = dataset_class(
      mode='train', config=train_config, data_format=data_format)
  test_dataset = dataset_class(
      mode='eval', config=eval_config, data_format=data_format)

  return dict(
      train_fn=train,
      test_fn=test,
      train_dataset=train_dataset,
      test_dataset=test_dataset)
コード例 #2
0
    def test_merge(self):
        a = {'a': 1, 'b': {'c': 2}}
        b = {'a': 3, 'b': {'c': 4, 'd': 5}}
        a_b_result = {'a': 3, 'b': {'c': 4, 'd': 5}}
        b_a_result = {'a': 1, 'b': {'c': 2, 'd': 5}}
        a_b_merge = configurable.merge(a, b)
        b_a_merge = configurable.merge(b, a)

        self.assertAllEqual(a_b_result, a_b_merge)
        self.assertAllEqual(b_a_result, b_a_merge)
コード例 #3
0
def main(_):
  """Parse config flag and config files then run experiment."""

  # Load config
  parsed_config = configurable.load_config(FLAGS.config_file, FLAGS.config)

  if 'fn' not in parsed_config:
    # Fill in the default template
    parsed_config = configurable.merge(
        parsed_config, fn='ConfigurableExperiment')

  configurable.save_config(FLAGS.model_dir, parsed_config)
  # TODO(thangluong): consider use FLAGS if we do need to use this often
  parsed_config['log_device_placement'] = False
  parsed_config['allow_soft_placement'] = True
  if 'data_format' not in parsed_config:
    parsed_config['data_format'] = 'squad'
  assert FLAGS.model_dir
  experiment_fn = experiment.create_experiment_fn(
      default_config=parsed_config, return_distributed_spec=False)
  exp = experiment_fn(hparams=None, model_dir=FLAGS.model_dir)
  if FLAGS.mode == 'train':
    exp.train()
  elif FLAGS.mode == 'eval':
    exp.evaluate()
  elif FLAGS.mode == 'train_and_eval':
    exp.train_and_evaluate()
  else:
    raise ValueError('Unknown mode %s' % FLAGS.mode)
コード例 #4
0
ファイル: experiment.py プロジェクト: tdgros/google-research
  def _create_config(hparams):
    """Create trial config and save to disk.

    Args:
      hparams: Nondefault params to merge

    Returns:
      A configurable object `spec` instantiated from the final config.
        spec.config will return the config.
    """
    hparams = hparams or contrib_training.HParams()
    tuned_config = configurable.unflatten_dict(hparams.values())
    pprinter = pprint.PrettyPrinter()
    tf.logging.info('Provided extra params:\n%s',
                    pprinter.pformat(tuned_config))
    try:
      merged_config = configurable.merge(default_config, tuned_config)
      tf.logging.info('Tuned default config:\n%s', merged_config)
    except TypeError as e:
      tf.logging.info(
          'Do not provide same config in both config string and vizier.'
          '  This may lead to type errors.')

      raise e

    # Instantiate a ConfigurableExperiment object.
    experiment_spec = configurable.Configurable.initialize(merged_config)
    tf.logging.info('Final config:\n%s', experiment_spec.config)

    return experiment_spec