Restores previously saved variables. This method runs the ops added by the constructor for restoring variables. It requires a session in which the graph was launched. The variables to restore do not have to have been initialized, as restoring is itself a way to initialize variables
(self, sess, save_path)
| 1516 | save_debug_info=save_debug_info) |
| 1517 | |
| 1518 | def restore(self, sess, save_path): |
| 1519 | """Restores previously saved variables. |
| 1520 | |
| 1521 | This method runs the ops added by the constructor for restoring variables. |
| 1522 | It requires a session in which the graph was launched. The variables to |
| 1523 | restore do not have to have been initialized, as restoring is itself a way |
| 1524 | to initialize variables. |
| 1525 | |
| 1526 | The `save_path` argument is typically a value previously returned from a |
| 1527 | `save()` call, or a call to `latest_checkpoint()`. |
| 1528 | |
| 1529 | Args: |
| 1530 | sess: A `Session` to use to restore the parameters. None in eager mode. |
| 1531 | save_path: Path where parameters were previously saved. |
| 1532 | |
| 1533 | Raises: |
| 1534 | ValueError: If save_path is None or not a valid checkpoint. |
| 1535 | """ |
| 1536 | if self._is_empty: |
| 1537 | return |
| 1538 | if save_path is None: |
| 1539 | raise ValueError("Can't load save_path when it is None.") |
| 1540 | |
| 1541 | checkpoint_prefix = compat.as_text(save_path) |
| 1542 | if not checkpoint_management.checkpoint_exists_internal(checkpoint_prefix): |
| 1543 | raise ValueError("The passed save_path is not a valid checkpoint: " + |
| 1544 | checkpoint_prefix) |
| 1545 | |
| 1546 | logging.info("Restoring parameters from %s", checkpoint_prefix) |
| 1547 | try: |
| 1548 | if context.executing_eagerly(): |
| 1549 | self._build_eager(save_path, build_save=False, build_restore=True) |
| 1550 | else: |
| 1551 | sess.run(self.saver_def.restore_op_name, |
| 1552 | {self.saver_def.filename_tensor_name: save_path}) |
| 1553 | except errors.NotFoundError as err: |
| 1554 | # There are three common conditions that might cause this error: |
| 1555 | # 0. The file is missing. We ignore here, as this is checked above. |
| 1556 | # 1. This is an object-based checkpoint trying name-based loading. |
| 1557 | # 2. The graph has been altered and a variable or other name is missing. |
| 1558 | |
| 1559 | # 1. The checkpoint would not be loaded successfully as is. Try to parse |
| 1560 | # it as an object-based checkpoint. |
| 1561 | try: |
| 1562 | names_to_keys = object_graph_key_mapping(save_path) |
| 1563 | except errors.NotFoundError: |
| 1564 | # 2. This is not an object-based checkpoint, which likely means there |
| 1565 | # is a graph mismatch. Re-raise the original error with |
| 1566 | # a helpful message (b/110263146) |
| 1567 | raise _wrap_restore_error_with_msg( |
| 1568 | err, "a Variable name or other graph key that is missing") |
| 1569 | |
| 1570 | # This is an object-based checkpoint. We'll print a warning and then do |
| 1571 | # the restore. |
| 1572 | logging.warning( |
| 1573 | "Restoring an object-based checkpoint using a name-based saver. This " |
| 1574 | "may be somewhat fragile, and will re-build the Saver. Instead, " |
| 1575 | "consider loading object-based checkpoints using " |