(root_dir,
                        restrict_to_architectures,
                        restrict_to_pretrained_source,
                        restrict_to_variants=None)
return event_paths
Returns a dict that maps each variant name to its event file.

  The name of the variant is the basename of the directory where it's stored.
  Assumes the following directory organization root_dir contains a sub-directory
  for every variant where event files can be found.

  There may be more than one event file for each variant, e.g. a new one will be
  created upon restarting an experiment that was pre-empted. So later event
  files contain the summaries for larger values of 'step'. We need all of them
  for determining the global 'best'.

  Args:
    root_dir: A str. The root directory of experiments of all models variants.
    restrict_to_architectures: A list of names of architectures to restrict to
      when choosing the best variant.
    restrict_to_pretrained_source: A string. The pretrained_source to restrict
      to when choosing the best variant.
    restrict_to_variants: Optionally, a set of variant names to restrict to.
Returns a dict that maps each variant name to its event file.
Returns a dict that maps each variant name to its event file .
def get_paths_to_events(root_dir,
                        restrict_to_architectures,
                        restrict_to_pretrained_source,
                        restrict_to_variants=None):
  """Returns a dict that maps each variant name to its event file.

  The name of the variant is the basename of the directory where it's stored.
  Assumes the following directory organization root_dir contains a sub-directory
  for every variant where event files can be found.

  There may be more than one event file for each variant, e.g. a new one will be
  created upon restarting an experiment that was pre-empted. So later event
  files contain the summaries for larger values of 'step'. We need all of them
  for determining the global 'best'.

  Args:
    root_dir: A str. The root directory of experiments of all models variants.
    restrict_to_architectures: A list of names of architectures to restrict to
      when choosing the best variant.
    restrict_to_pretrained_source: A string. The pretrained_source to restrict
      to when choosing the best variant.
    restrict_to_variants: Optionally, a set of variant names to restrict to.
  """
  params_dir = os.path.join(root_dir, 'params')
  summary_dir = os.path.join(root_dir, 'summaries')
  logging.info('Looking for parameters in params_dir: %s', params_dir)
  logging.info('Looking for summaries in summary_dir: %s', summary_dir)

  def get_variant_architecture(name):
    """Return the architecture of the given variant if recorded; o/w None."""
    variant_params_dir = os.path.join(params_dir, name)
    architecture = get_value_from_params_dir(
        variant_params_dir,
        (
            '_gin.Learner.embedding_fn',
            # The following are for backwards compatibility.
            '_gin.Trainer.embedding_network',
            '_gin.LearnerConfig.embedding_network',
        ))

    return architecture

  def get_variant_pretrained_source(name):
    """Return the pretrained src of the given variant if recorded; o/w None."""
    variant_params_dir = os.path.join(params_dir, name)
    pretrained_source = get_value_from_params_dir(
        variant_params_dir, '_gin.Trainer.pretrained_source')

    if not pretrained_source:
      # Backwards compatibility.
      pretrained_source = get_value_from_params_dir(
          variant_params_dir, '_gin.LearnerConfig.pretrained_source')

    return pretrained_source

  def keep_variant(name):
    """Determine if the variant in directory name should be considered."""
    value_error_msg = (
        'Requested to restrict to an architecture or '
        'pretrained_source but the given experiment does not '
        'have its params recorded. Looked in: {}'.format(params_dir))

    if restrict_to_architectures:
      architecture = get_variant_architecture(name)
      if architecture is None:
        raise ValueError(value_error_msg)
    valid_architecture = (not restrict_to_architectures or
                          architecture in restrict_to_architectures)

    if restrict_to_pretrained_source:
      pretrained_source = get_variant_pretrained_source(name)
      if pretrained_source is None:
        raise ValueError(value_error_msg)
    valid_pretrained_source = (
        not restrict_to_pretrained_source or
        pretrained_source == restrict_to_pretrained_source)

    valid_variant_name = True
    if restrict_to_variants is not None:
      valid_variant_name = name in restrict_to_variants

    return (valid_architecture and valid_pretrained_source and
            valid_variant_name)

  variant_names = [
      fname for fname in tf.io.gfile.listdir(summary_dir)
      if tf.io.gfile.isdir(os.path.join(summary_dir, fname))
  ]

  if not variant_names:
    # Maybe there are no variants, and we are already in the directory that
    # contains the summaries. In this case, we consider that the current
    # directory (.) is the only variant.
    variant_names = ['.']

  # Further filter variant names based on the given restrictions.
  variant_names = [name for name in variant_names if keep_variant(name)]

  if not variant_names:
    raise ValueError('Found no subdirectories in {}. Was expecting a '
                     'subdirectory per variant.'.format(summary_dir))
  variant_paths = [
      os.path.join(summary_dir, variant_dir) for variant_dir in variant_names
  ]

  event_paths = {}
  for variant_path, variant_name in zip(variant_paths, variant_names):
    event_filenames = [
        f_name for f_name in tf.io.gfile.listdir(variant_path)
        if f_name.startswith('events.out.tfevents')
    ]

    if len(event_filenames) < 1:
      logging.warn('Skipping empty variant %s.', variant_path)
      logging.info(
          'Was expecting at least one event file '
          'in directory %s. Instead, found %d.', variant_path,
          len(event_filenames))
      continue
    event_paths[variant_name] = [
        os.path.join(variant_path, event_filename)
        for event_filename in event_filenames
    ]

  logging.info('Found event files for variants: %s', list(event_paths.keys()))
  return event_paths
def get_paths_to_events ( root_dir , restrict_to_architectures , restrict_to_pretrained_source , restrict_to_variants = None ) : params_dir = os . path . join ( root_dir , 'params' ) summary_dir = os . path . join ( root_dir , 'summaries' ) logging . info ( 'Looking for parameters in params_dir: %s' , params_dir ) logging . info ( 'Looking for summaries in summary_dir: %s' , summary_dir ) def get_variant_architecture ( name ) : """Return the architecture of the given variant if recorded; o/w None.""" variant_params_dir = os . path . join ( params_dir , name ) architecture = get_value_from_params_dir ( variant_params_dir , ( '_gin.Learner.embedding_fn' , # The following are for backwards compatibility. '_gin.Trainer.embedding_network' , '_gin.LearnerConfig.embedding_network' , ) ) return architecture def get_variant_pretrained_source ( name ) : """Return the pretrained src of the given variant if recorded; o/w None.""" variant_params_dir = os . path . join ( params_dir , name ) pretrained_source = get_value_from_params_dir ( variant_params_dir , '_gin.Trainer.pretrained_source' ) if not pretrained_source : # Backwards compatibility. pretrained_source = get_value_from_params_dir ( variant_params_dir , '_gin.LearnerConfig.pretrained_source' ) return pretrained_source def keep_variant ( name ) : """Determine if the variant in directory name should be considered.""" value_error_msg = ( 'Requested to restrict to an architecture or ' 'pretrained_source but the given experiment does not ' 'have its params recorded. Looked in: {}' . format ( params_dir ) ) if restrict_to_architectures : architecture = get_variant_architecture ( name ) if architecture is None : raise ValueError ( value_error_msg ) valid_architecture = ( not restrict_to_architectures or architecture in restrict_to_architectures ) if restrict_to_pretrained_source : pretrained_source = get_variant_pretrained_source ( name ) if pretrained_source is None : raise ValueError ( value_error_msg ) valid_pretrained_source = ( not restrict_to_pretrained_source or pretrained_source == restrict_to_pretrained_source ) valid_variant_name = True if restrict_to_variants is not None : valid_variant_name = name in restrict_to_variants return ( valid_architecture and valid_pretrained_source and valid_variant_name ) variant_names = [ fname for fname in tf . io . gfile . listdir ( summary_dir ) if tf . io . gfile . isdir ( os . path . join ( summary_dir , fname ) ) ] if not variant_names : # Maybe there are no variants, and we are already in the directory that # contains the summaries. In this case, we consider that the current # directory (.) is the only variant. variant_names = [ '.' ] # Further filter variant names based on the given restrictions. variant_names = [ name for name in variant_names if keep_variant ( name ) ] if not variant_names : raise ValueError ( 'Found no subdirectories in {}. Was expecting a ' 'subdirectory per variant.' . format ( summary_dir ) ) variant_paths = [ os . path . join ( summary_dir , variant_dir ) for variant_dir in variant_names ] event_paths = { } for variant_path , variant_name in zip ( variant_paths , variant_names ) : event_filenames = [ f_name for f_name in tf . io . gfile . listdir ( variant_path ) if f_name . startswith ( 'events.out.tfevents' ) ] if len ( event_filenames ) < 1 : logging . warn ( 'Skipping empty variant %s.' , variant_path ) logging . info ( 'Was expecting at least one event file ' 'in directory %s. Instead, found %d.' , variant_path , len ( event_filenames ) ) continue event_paths [ variant_name ] = [ os . path . join ( variant_path , event_filename ) for event_filename in event_filenames ] logging . info ( 'Found event files for variants: %s' , list ( event_paths . keys ( ) ) ) return event_paths