(model, ckpt_path, renamed_variable_generator)
return replacement_mapping
Return a mapping of variable names in `ckpt_path` to variables in `model`.

  #### Examples

  ```python
  net = ResNet18V1(output_dim=None)
  net.build((None, 84, 84, 3))
  var_mapping = parameter_adapter(
      model=net,
      ckpt_path='CKPT_PATH/model_27500.ckpt',
      renamed_variable_generator=meta_dataset_variable_generator)
  ```

  The returned dictionary is suitable for instantiating a `tf.train.Saver`
  instance that restores the corresponding variables of `model` to values from
  `ckpt_path`:

  ```python
  ckpt_to_restore = 'CKPT_PATH/model_46000.ckpt',
  net = WideResNet16V2(
      width_multiplier=2, large_input_kernel=False, output_dim=None)
  net.build((None, 84, 84, 3))
  var_mapping = parameter_adapter(
      net,
      ckpt_to_restore,
      renamed_variable_generator=meta_dataset_variable_generator)

  saver = tf.train.Saver(var_mapping)
  with tf.Session() as sess:
    tf.global_variables_initializer().run()
    saver.restore(sess, ckpt_to_restore)
  ```

  Args:
    model: A subclass of `tf.keras.Model`; the model for which to load variable
      values from `ckpt_path`.
    ckpt_path: An absolute path to the checkpoint from which to load variables.
    renamed_variable_generator: A callable that takes in a subclass of
      `tf.Module` and and a list of variable names and yields elements of a
      one-to-one mapping of elements of the list to `tf.Variable`s of the
      `tf.Module`.

  Returns:
    A dictionary that maps variable names from the checkpoint at `ckpt_path` to
    variables in `model`, where the mapping is determined by
    `renamed_variable_generator`.
Return a mapping of variable names in `ckpt_path` to variables in `model`.
Return a mapping of variable names in ckpt_path to variables in model .
def ckpt_parameter_adapter(model, ckpt_path, renamed_variable_generator):
  """Return a mapping of variable names in `ckpt_path` to variables in `model`.

  #### Examples

  ```python
  net = ResNet18V1(output_dim=None)
  net.build((None, 84, 84, 3))
  var_mapping = parameter_adapter(
      model=net,
      ckpt_path='CKPT_PATH/model_27500.ckpt',
      renamed_variable_generator=meta_dataset_variable_generator)
  ```

  The returned dictionary is suitable for instantiating a `tf.train.Saver`
  instance that restores the corresponding variables of `model` to values from
  `ckpt_path`:

  ```python
  ckpt_to_restore = 'CKPT_PATH/model_46000.ckpt',
  net = WideResNet16V2(
      width_multiplier=2, large_input_kernel=False, output_dim=None)
  net.build((None, 84, 84, 3))
  var_mapping = parameter_adapter(
      net,
      ckpt_to_restore,
      renamed_variable_generator=meta_dataset_variable_generator)

  saver = tf.train.Saver(var_mapping)
  with tf.Session() as sess:
    tf.global_variables_initializer().run()
    saver.restore(sess, ckpt_to_restore)
  ```

  Args:
    model: A subclass of `tf.keras.Model`; the model for which to load variable
      values from `ckpt_path`.
    ckpt_path: An absolute path to the checkpoint from which to load variables.
    renamed_variable_generator: A callable that takes in a subclass of
      `tf.Module` and and a list of variable names and yields elements of a
      one-to-one mapping of elements of the list to `tf.Variable`s of the
      `tf.Module`.

  Returns:
    A dictionary that maps variable names from the checkpoint at `ckpt_path` to
    variables in `model`, where the mapping is determined by
    `renamed_variable_generator`.
  """
  if not model.built:
    raise ValueError(
        '`model` must have been built before parameters can be mapped.')

  # Read the checkpoint.
  reader = tf.compat.v1.train.NewCheckpointReader(ckpt_path)

  var_to_shape_map = reader.get_variable_to_shape_map()
  ckpt_vars = var_to_shape_map.keys()
  ckpt_vars = list(filter(functional_backbones.is_backbone_variable, ckpt_vars))

  replacement_mapping = []
  for new_tensor, old_tensor_name in renamed_variable_generator(
      model, ckpt_vars):

    if not reader.has_tensor(old_tensor_name):
      raise ValueError('Tensor %s not found in checkpoint.' % old_tensor_name)

    old_tensor = reader.get_tensor(old_tensor_name)

    old_tensor_shape = tuple(var_to_shape_map[old_tensor_name])
    new_tensor_shape = tuple(new_tensor.shape.as_list())
    if old_tensor_shape != new_tensor_shape:
      old_tensor = old_tensor.reshape(new_tensor.shape)

    replacement_mapping += [(new_tensor, old_tensor)]

  return replacement_mapping
def ckpt_parameter_adapter ( model , ckpt_path , renamed_variable_generator ) : if not model . built : raise ValueError ( '`model` must have been built before parameters can be mapped.' ) # Read the checkpoint. reader = tf . compat . v1 . train . NewCheckpointReader ( ckpt_path ) var_to_shape_map = reader . get_variable_to_shape_map ( ) ckpt_vars = var_to_shape_map . keys ( ) ckpt_vars = list ( filter ( functional_backbones . is_backbone_variable , ckpt_vars ) ) replacement_mapping = [ ] for new_tensor , old_tensor_name in renamed_variable_generator ( model , ckpt_vars ) : if not reader . has_tensor ( old_tensor_name ) : raise ValueError ( 'Tensor %s not found in checkpoint.' % old_tensor_name ) old_tensor = reader . get_tensor ( old_tensor_name ) old_tensor_shape = tuple ( var_to_shape_map [ old_tensor_name ] ) new_tensor_shape = tuple ( new_tensor . shape . as_list ( ) ) if old_tensor_shape != new_tensor_shape : old_tensor = old_tensor . reshape ( new_tensor . shape ) replacement_mapping += [ ( new_tensor , old_tensor ) ] return replacement_mapping