ContextManager that restores variables on creation. When save_path is None (e.g. No checkpoint), does nothing. Otherwise, it preloads all values from checkpoint. When the corresponding variable is first created, it assigns the checkpoint value to the variable. ```python wit
(save_path, map_func=None)
| 46 | |
| 47 | @contextlib.contextmanager |
| 48 | def restore_variables_on_create(save_path, map_func=None): |
| 49 | """ContextManager that restores variables on creation. |
| 50 | |
| 51 | When save_path is None (e.g. No checkpoint), does nothing. |
| 52 | Otherwise, it preloads all values from checkpoint. When the |
| 53 | corresponding variable is first created, it assigns the checkpoint |
| 54 | value to the variable. |
| 55 | |
| 56 | ```python |
| 57 | with restore_variables_on_create( |
| 58 | tf.train.latest_checkpoint(checkpoint_dir)): |
| 59 | ``` |
| 60 | |
| 61 | Args: |
| 62 | save_path: The checkpoint file prefix. |
| 63 | map_func: A function that given the variable name as argument |
| 64 | and returns a variable name in checkpoint for restore. If |
| 65 | None, use the variable with the same name in checkpoint to restore. |
| 66 | It's an error that the mapped variable name doesn't exist in |
| 67 | checkpoint. |
| 68 | |
| 69 | Yields: |
| 70 | Nothing. |
| 71 | |
| 72 | Raises: |
| 73 | NotFoundError: If the variable is not found in checkpoint. |
| 74 | ValueError: If not used in eager mode or map_func is not callable. |
| 75 | """ |
| 76 | if not context.executing_eagerly(): |
| 77 | raise ValueError( |
| 78 | "Currently, restore_variables_on_create can only be used with " |
| 79 | "eager execution enabled.") |
| 80 | if save_path: |
| 81 | if map_func is None: |
| 82 | map_func_wrapper = lambda self, x: x |
| 83 | else: |
| 84 | if not callable(map_func): |
| 85 | raise ValueError("map_func must be callable.") |
| 86 | map_func_wrapper = lambda self, x: map_func(x) |
| 87 | |
| 88 | ckpt_var_cache = {} |
| 89 | reader = checkpoint_utils.load_checkpoint(save_path) |
| 90 | for k, _ in checkpoint_utils.list_variables(save_path): |
| 91 | ckpt_var_cache[k] = reader.get_tensor(k) |
| 92 | |
| 93 | old_init = getattr(resource_variable_ops.ResourceVariable, |
| 94 | "_init_from_args", None) |
| 95 | assert old_init, "ResourceVariable misses _init_from_args method." |
| 96 | setattr(resource_variable_ops.ResourceVariable, "_init_from_args", |
| 97 | _init_from_checkpoint) |
| 98 | setattr(resource_variable_ops.ResourceVariable, "_old_init", old_init) |
| 99 | setattr(resource_variable_ops.ResourceVariable, "_map_func", |
| 100 | map_func_wrapper) |
| 101 | setattr(resource_variable_ops.ResourceVariable, "_ckpt_var_cache", |
| 102 | ckpt_var_cache) |
| 103 | try: |
| 104 | yield |
| 105 | except Exception as e: |
nothing calls this directly
no test coverage detected