Restore the Network from a checkpoint. If variables have already been created (typically when some or all of the `Network` is built), they are assigned values from the checkpoint immediately, overwriting any existing values (in graph mode the default session is used for the assignments).
(network, save_path, map_func=None)
| 973 | "Please inherit from tf.keras.Model instead of tfe.Network, and use " |
| 974 | "tf.keras.Model.load_weights.")) |
| 975 | def restore_network_checkpoint(network, save_path, map_func=None): |
| 976 | """Restore the Network from a checkpoint. |
| 977 | |
| 978 | If variables have already been created (typically when some or all of the |
| 979 | `Network` is built), they are assigned values from the checkpoint immediately, |
| 980 | overwriting any existing values (in graph mode the default session is used for |
| 981 | the assignments). |
| 982 | |
| 983 | If there are checkpoint entries which do not correspond to any existing |
| 984 | variables in the `Network`, these values are saved for deferred restoration; |
| 985 | their initial values will be the checkpointed values once they are |
| 986 | created. Requests for multiple deferred restorations behave the same way as |
| 987 | immediate restorations, in that later requests will take priority over earlier |
| 988 | requests relevant to the same variable. |
| 989 | |
| 990 | If this `Network` shares `Layer`s with another network, those `Layer`s will |
| 991 | also have their variables restored from the checkpoint. |
| 992 | |
| 993 | Args: |
| 994 | network: A Network object to restore. |
| 995 | save_path: The return value of `tfe.save_network_checkpoint`, or a directory |
| 996 | to search for a checkpoint. |
| 997 | map_func: A function mapping fully qualified variable names (e.g. |
| 998 | 'my_network_1/dense_1/kernel') to names in the checkpoint. By default (if |
| 999 | `map_func=None`), the variable prefix for the network being restored |
| 1000 | (`Network.scope_name + '/'`, e.g. 'my_network_1/') is stripped and all |
| 1001 | other variable names (shared with other Networks) are left unchanged. Note |
| 1002 | that this is the _same_ map_func as `tfe.save_network_checkpoint`, not an |
| 1003 | inverse mapping. |
| 1004 | """ |
| 1005 | network._finalize_name(parent_network=False) |
| 1006 | network._set_scope() # scope_name should be available to map_funcs |
| 1007 | if os.path.isdir(save_path): |
| 1008 | # If we don't have a name yet, set no parent. |
| 1009 | save_path = os.path.join(save_path, network.name.replace("/", "_")) |
| 1010 | user_map_func = map_func |
| 1011 | if map_func is None: |
| 1012 | map_func = _make_prefix_stripping_map_fn(network.scope_name) |
| 1013 | # Step one is to restore any existing variables from the checkpoint. |
| 1014 | existing_variables_by_checkpoint_name = _restore_existing_variables( |
| 1015 | network=network, |
| 1016 | save_path=save_path, |
| 1017 | map_func=map_func, |
| 1018 | user_map_func=user_map_func) |
| 1019 | # Step two is to set a custom getter which restores variables on creation, |
| 1020 | # for those variables which have not been added to sub-Layers yet. |
| 1021 | _set_restore_on_create( |
| 1022 | network=network, |
| 1023 | save_path=save_path, |
| 1024 | map_func=map_func, |
| 1025 | user_map_func=user_map_func, |
| 1026 | existing_variables_by_checkpoint_name=( |
| 1027 | existing_variables_by_checkpoint_name)) |
nothing calls this directly
no test coverage detected