parameters: (checkpoint_dir, assignment_map)
docstring: Using assingment map initializes current variables with loaded tensors.
  
    Note: This overrides default initialization ops of specified variables and
    redefines dtype.
  
    Assignment map supports following syntax:
      `'checkpoint_scope_name/': 'scope_name/'` - will load all variables in
        current `scope_name` from `checkpoint_scope_name` with matching variable
        names.
      `'checkpoint_scope_name/some_other_variable': 'scope_name/variable_name'` -
        will initalize `scope_name/variable_name` variable
        from `checkpoint_scope_name/some_other_variable`.
      `'scope_variable_name': variable` - will initialize given `tf.Variable`
        object with variable from the checkpoint.
      `'scope_variable_name': list(variable)` - will initialize list of
        partitioned variables with variable from the checkpoint.
      `'scope_name/': '/'` - will load all variables in current `scope_name` from
        checkpoint's root (e.g. no scope).
  
    Supports loading into partitioned variables, which are represented as
    '<variable>/part_<part #>'.
  
    Example:
    ```python
      # Create variables.
      with tf.variable_scope('test'):
        m = tf.get_variable('my_var')
      with tf.variable_scope('test2'):
        var2 = tf.get_variable('my_var')
      var3 = tf.get_variable(name="my1", shape=[100, 100],
                             partitioner=lambda shape, dtype: [5, 1])
      ...
      # Specify which variables to intialize from checkpoint.
      init_from_checkpoint(checkpoint_dir, {
        'some_var': 'test/my_var',
        'some_scope/': 'test2/'})
      ...
      # Or use `Variable` objects to identify what to initialize.
      init_from_checkpoint(checkpoint_dir, {
        'some_scope/var2': var2,
      })
      # Initialize partitioned variables
      init_from_checkpoint(checkpoint_dir, {
        'some_var_from_ckpt': 'part_var',
      })
      # Or specifying the list of `Variable` objects.
      init_from_checkpoint(checkpoint_dir, {
        'some_var_from_ckpt': var3._get_variable_list(),
      })
      ...
      # Initialize variables as usual.
      session.run(tf.get_all_variables())
    ```
  
    Args:
      checkpoint_dir: Directory with checkpoints file or path to checkpoint.
      assignment_map: Dict, where keys are names of the variables in the
        checkpoint and values are current variables or names of current variables
        (in default graph).
  
    Raises:
      tf.errors.OpError: If missing checkpoints or tensors in checkpoints.
      ValueError: If missing variables in current graph.
docstring_summary: Using assingment map initializes current variables with loaded tensors.
docstring_tokens: Using assingment map initializes current variables with loaded tensors .
function: def init_from_checkpoint(checkpoint_dir, assignment_map):
    """Using assingment map initializes current variables with loaded tensors.
  
    Note: This overrides default initialization ops of specified variables and
    redefines dtype.
  
    Assignment map supports following syntax:
      `'checkpoint_scope_name/': 'scope_name/'` - will load all variables in
        current `scope_name` from `checkpoint_scope_name` with matching variable
        names.
      `'checkpoint_scope_name/some_other_variable': 'scope_name/variable_name'` -
        will initalize `scope_name/variable_name` variable
        from `checkpoint_scope_name/some_other_variable`.
      `'scope_variable_name': variable` - will initialize given `tf.Variable`
        object with variable from the checkpoint.
      `'scope_variable_name': list(variable)` - will initialize list of
        partitioned variables with variable from the checkpoint.
      `'scope_name/': '/'` - will load all variables in current `scope_name` from
        checkpoint's root (e.g. no scope).
  
    Supports loading into partitioned variables, which are represented as
    '<variable>/part_<part #>'.
  
    Example:
    ```python
      # Create variables.
      with tf.variable_scope('test'):
        m = tf.get_variable('my_var')
      with tf.variable_scope('test2'):
        var2 = tf.get_variable('my_var')
      var3 = tf.get_variable(name="my1", shape=[100, 100],
                             partitioner=lambda shape, dtype: [5, 1])
      ...
      # Specify which variables to intialize from checkpoint.
      init_from_checkpoint(checkpoint_dir, {
        'some_var': 'test/my_var',
        'some_scope/': 'test2/'})
      ...
      # Or use `Variable` objects to identify what to initialize.
      init_from_checkpoint(checkpoint_dir, {
        'some_scope/var2': var2,
      })
      # Initialize partitioned variables
      init_from_checkpoint(checkpoint_dir, {
        'some_var_from_ckpt': 'part_var',
      })
      # Or specifying the list of `Variable` objects.
      init_from_checkpoint(checkpoint_dir, {
        'some_var_from_ckpt': var3._get_variable_list(),
      })
      ...
      # Initialize variables as usual.
      session.run(tf.get_all_variables())
    ```
  
    Args:
      checkpoint_dir: Directory with checkpoints file or path to checkpoint.
      assignment_map: Dict, where keys are names of the variables in the
        checkpoint and values are current variables or names of current variables
        (in default graph).
  
    Raises:
      tf.errors.OpError: If missing checkpoints or tensors in checkpoints.
      ValueError: If missing variables in current graph.
    """
    filepattern = _get_checkpoint_filename(checkpoint_dir)
    reader = load_checkpoint(checkpoint_dir)
    variable_map = reader.get_variable_to_shape_map()
    for tensor_name_in_ckpt, current_var_or_name in six.iteritems(assignment_map):
      var = None
      # Check if this is Variable object or list of Variable objects (in case of
      # partitioned variables).
      is_var = lambda x: isinstance(x, variables.Variable)
      if is_var(current_var_or_name) or (
          isinstance(current_var_or_name, list)
          and all(is_var(v) for v in current_var_or_name)):
        var = current_var_or_name
      else:
        var_scope = vs._get_default_variable_store()
        # Check if this variable is in var_store.
        var = var_scope._vars.get(current_var_or_name, None)
        # Also check if variable is partitioned as list.
        if var is None:
          if current_var_or_name + "/part_0" in var_scope._vars:
            var = []
            i = 0
            while current_var_or_name + "/part_%d" % i in var_scope._vars:
              var.append(var_scope._vars[current_var_or_name + "/part_%d" % i])
              i += 1
      if var is not None:
        # If 1 to 1 mapping was provided, find variable in the checkpoint.
        if tensor_name_in_ckpt not in variable_map:
          raise ValueError("Tensor %s is not found in %s checkpoint" % (
              tensor_name_in_ckpt, checkpoint_dir
          ))
        if is_var(var):
          # Additional at-call-time checks.
          if not var.get_shape().is_compatible_with(
              variable_map[tensor_name_in_ckpt]):
            raise ValueError(
                "Shape of variable %s (%s) doesn't match with shape of "
                "tensor %s (%s) from checkpoint reader." % (
                    var.name, str(var.get_shape()),
                    tensor_name_in_ckpt, str(variable_map[tensor_name_in_ckpt])
                ))
          var_name = var.name
        else:
          var_name = ",".join([v.name for v in var])
        _set_variable_or_list_initializer(var, filepattern, tensor_name_in_ckpt)
        logging.info("Initialize variable %s from checkpoint %s with %s" % (
            var_name, checkpoint_dir, tensor_name_in_ckpt
        ))
      else:
        scopes = ""
        # TODO(vihanjain): Support list of 'current_var_or_name' here.
        if "/" in current_var_or_name:
          scopes = current_var_or_name[:current_var_or_name.rindex("/")]
        if not tensor_name_in_ckpt.endswith("/"):
          raise ValueError(
              "Assignment map with scope only name {} should map to scope only "
              "{}. Should be 'scope/': 'other_scope/'.".format(
                  scopes, tensor_name_in_ckpt))
        # If scope to scope mapping was provided, find all variables in the scope.
        for var_name in var_scope._vars:
          if var_name.startswith(scopes):
            # Lookup name with specified prefix and suffix from current variable.
            # If tensor_name given is '/' (root), don't use it for full name.
            if tensor_name_in_ckpt != "/":
              full_tensor_name = tensor_name_in_ckpt + var_name[len(scopes) + 1:]
            else:
              full_tensor_name = var_name[len(scopes) + 1:]
            if full_tensor_name not in variable_map:
              raise ValueError(
                  "Tensor %s (%s in %s) is not found in %s checkpoint" % (
                      full_tensor_name, var_name[len(scopes) + 1:],
                      tensor_name_in_ckpt, checkpoint_dir
                  ))
            var = var_scope._vars[var_name]
            _set_variable_or_list_initializer(var, filepattern, full_tensor_name)
            logging.info("Initialize variable %s from checkpoint %s with %s" % (
                var_name, checkpoint_dir, full_tensor_name
            ))
function_tokens: def init_from_checkpoint ( checkpoint_dir , assignment_map ) : filepattern = _get_checkpoint_filename ( checkpoint_dir ) reader = load_checkpoint ( checkpoint_dir ) variable_map = reader . get_variable_to_shape_map ( ) for tensor_name_in_ckpt , current_var_or_name in six . iteritems ( assignment_map ) : var = None # Check if this is Variable object or list of Variable objects (in case of # partitioned variables). is_var = lambda x : isinstance ( x , variables . Variable ) if is_var ( current_var_or_name ) or ( isinstance ( current_var_or_name , list ) and all ( is_var ( v ) for v in current_var_or_name ) ) : var = current_var_or_name else : var_scope = vs . _get_default_variable_store ( ) # Check if this variable is in var_store. var = var_scope . _vars . get ( current_var_or_name , None ) # Also check if variable is partitioned as list. if var is None : if current_var_or_name + "/part_0" in var_scope . _vars : var = [ ] i = 0 while current_var_or_name + "/part_%d" % i in var_scope . _vars : var . append ( var_scope . _vars [ current_var_or_name + "/part_%d" % i ] ) i += 1 if var is not None : # If 1 to 1 mapping was provided, find variable in the checkpoint. if tensor_name_in_ckpt not in variable_map : raise ValueError ( "Tensor %s is not found in %s checkpoint" % ( tensor_name_in_ckpt , checkpoint_dir ) ) if is_var ( var ) : # Additional at-call-time checks. if not var . get_shape ( ) . is_compatible_with ( variable_map [ tensor_name_in_ckpt ] ) : raise ValueError ( "Shape of variable %s (%s) doesn't match with shape of " "tensor %s (%s) from checkpoint reader." % ( var . name , str ( var . get_shape ( ) ) , tensor_name_in_ckpt , str ( variable_map [ tensor_name_in_ckpt ] ) ) ) var_name = var . name else : var_name = "," . join ( [ v . name for v in var ] ) _set_variable_or_list_initializer ( var , filepattern , tensor_name_in_ckpt ) logging . info ( "Initialize variable %s from checkpoint %s with %s" % ( var_name , checkpoint_dir , tensor_name_in_ckpt ) ) else : scopes = "" # TODO(vihanjain): Support list of 'current_var_or_name' here. if "/" in current_var_or_name : scopes = current_var_or_name [ : current_var_or_name . rindex ( "/" ) ] if not tensor_name_in_ckpt . endswith ( "/" ) : raise ValueError ( "Assignment map with scope only name {} should map to scope only " "{}. Should be 'scope/': 'other_scope/'." . format ( scopes , tensor_name_in_ckpt ) ) # If scope to scope mapping was provided, find all variables in the scope. for var_name in var_scope . _vars : if var_name . startswith ( scopes ) : # Lookup name with specified prefix and suffix from current variable. # If tensor_name given is '/' (root), don't use it for full name. if tensor_name_in_ckpt != "/" : full_tensor_name = tensor_name_in_ckpt + var_name [ len ( scopes ) + 1 : ] else : full_tensor_name = var_name [ len ( scopes ) + 1 : ] if full_tensor_name not in variable_map : raise ValueError ( "Tensor %s (%s in %s) is not found in %s checkpoint" % ( full_tensor_name , var_name [ len ( scopes ) + 1 : ] , tensor_name_in_ckpt , checkpoint_dir ) ) var = var_scope . _vars [ var_name ] _set_variable_or_list_initializer ( var , filepattern , full_tensor_name ) logging . info ( "Initialize variable %s from checkpoint %s with %s" % ( var_name , checkpoint_dir , full_tensor_name ) )