Replaces `tf.Variable` initializers so they load from a checkpoint file. Values are not loaded immediately, but when the initializer is run (typically by running a `tf.compat.v1.global_variables_initializer` op). Note: This overrides default initialization ops of specified variables and re
(ckpt_dir_or_file, assignment_map, reset_version=False)
| 205 | |
| 206 | @tf_export(v1=["train.init_from_checkpoint"]) |
| 207 | def init_from_checkpoint(ckpt_dir_or_file, assignment_map, reset_version=False): |
| 208 | """Replaces `tf.Variable` initializers so they load from a checkpoint file. |
| 209 | |
| 210 | Values are not loaded immediately, but when the initializer is run |
| 211 | (typically by running a `tf.compat.v1.global_variables_initializer` op). |
| 212 | |
| 213 | Note: This overrides default initialization ops of specified variables and |
| 214 | redefines dtype. |
| 215 | |
| 216 | Assignment map supports following syntax: |
| 217 | |
| 218 | * `'checkpoint_scope_name/': 'scope_name/'` - will load all variables in |
| 219 | current `scope_name` from `checkpoint_scope_name` with matching tensor |
| 220 | names. |
| 221 | * `'checkpoint_scope_name/some_other_variable': 'scope_name/variable_name'` - |
| 222 | will initialize `scope_name/variable_name` variable |
| 223 | from `checkpoint_scope_name/some_other_variable`. |
| 224 | * `'scope_variable_name': variable` - will initialize given `tf.Variable` |
| 225 | object with tensor 'scope_variable_name' from the checkpoint. |
| 226 | * `'scope_variable_name': list(variable)` - will initialize list of |
| 227 | partitioned variables with tensor 'scope_variable_name' from the checkpoint. |
| 228 | * `'/': 'scope_name/'` - will load all variables in current `scope_name` from |
| 229 | checkpoint's root (e.g. no scope). |
| 230 | |
| 231 | Supports loading into partitioned variables, which are represented as |
| 232 | `'<variable>/part_<part #>'`. |
| 233 | |
| 234 | Example: |
| 235 | |
| 236 | ```python |
| 237 | |
| 238 | # Say, '/tmp/model.ckpt' has the following tensors: |
| 239 | # -- name='old_scope_1/var1', shape=[20, 2] |
| 240 | # -- name='old_scope_1/var2', shape=[50, 4] |
| 241 | # -- name='old_scope_2/var3', shape=[100, 100] |
| 242 | |
| 243 | # Create new model's variables |
| 244 | with tf.compat.v1.variable_scope('new_scope_1'): |
| 245 | var1 = tf.compat.v1.get_variable('var1', shape=[20, 2], |
| 246 | initializer=tf.compat.v1.zeros_initializer()) |
| 247 | with tf.compat.v1.variable_scope('new_scope_2'): |
| 248 | var2 = tf.compat.v1.get_variable('var2', shape=[50, 4], |
| 249 | initializer=tf.compat.v1.zeros_initializer()) |
| 250 | # Partition into 5 variables along the first axis. |
| 251 | var3 = tf.compat.v1.get_variable(name='var3', shape=[100, 100], |
| 252 | initializer=tf.compat.v1.zeros_initializer(), |
| 253 | partitioner=lambda shape, dtype: [5, 1]) |
| 254 | |
| 255 | # Initialize all variables in `new_scope_1` from `old_scope_1`. |
| 256 | init_from_checkpoint('/tmp/model.ckpt', {'old_scope_1/': 'new_scope_1'}) |
| 257 | |
| 258 | # Use names to specify which variables to initialize from checkpoint. |
| 259 | init_from_checkpoint('/tmp/model.ckpt', |
| 260 | {'old_scope_1/var1': 'new_scope_1/var1', |
| 261 | 'old_scope_1/var2': 'new_scope_2/var2'}) |
| 262 | |
| 263 | # Or use tf.Variable objects to identify what to initialize. |
| 264 | init_from_checkpoint('/tmp/model.ckpt', |
nothing calls this directly
no test coverage detected