Saves a training checkpoint and provides basic checkpoint management. The saved checkpoint includes variables created by this object and any trackable objects it depends on at the time `Checkpoint.save()` is called. `save` is a basic convenience wrapper around the `write` method,
(self, file_prefix, session=None)
| 1506 | return self._save_counter |
| 1507 | |
| 1508 | def save(self, file_prefix, session=None): |
| 1509 | """Saves a training checkpoint and provides basic checkpoint management. |
| 1510 | |
| 1511 | The saved checkpoint includes variables created by this object and any |
| 1512 | trackable objects it depends on at the time `Checkpoint.save()` is |
| 1513 | called. |
| 1514 | |
| 1515 | `save` is a basic convenience wrapper around the `write` method, |
| 1516 | sequentially numbering checkpoints using `save_counter` and updating the |
| 1517 | metadata used by `tf.train.latest_checkpoint`. More advanced checkpoint |
| 1518 | management, for example garbage collection and custom numbering, may be |
| 1519 | provided by other utilities which also wrap `write` |
| 1520 | (`tf.contrib.checkpoint.CheckpointManager` for example). |
| 1521 | |
| 1522 | Args: |
| 1523 | file_prefix: A prefix to use for the checkpoint filenames |
| 1524 | (/path/to/directory/and_a_prefix). Names are generated based on this |
| 1525 | prefix and `Checkpoint.save_counter`. |
| 1526 | session: The session to evaluate variables in. Ignored when executing |
| 1527 | eagerly. If not provided when graph building, the default session is |
| 1528 | used. |
| 1529 | |
| 1530 | Returns: |
| 1531 | The full path to the checkpoint. |
| 1532 | """ |
| 1533 | graph_building = not context.executing_eagerly() |
| 1534 | if graph_building: |
| 1535 | if ops.inside_function(): |
| 1536 | raise NotImplementedError( |
| 1537 | "Calling tf.train.Checkpoint.save() from a function is not " |
| 1538 | "supported, as save() modifies saving metadata in ways not " |
| 1539 | "supported by TensorFlow Operations. Consider using " |
| 1540 | "tf.train.Checkpoint.write(), a lower-level API which does not " |
| 1541 | "update metadata. tf.train.latest_checkpoint and related APIs will " |
| 1542 | "not see this checkpoint.") |
| 1543 | if session is None: |
| 1544 | session = get_session() |
| 1545 | if self._save_counter is None: |
| 1546 | # When graph building, if this is a new save counter variable then it |
| 1547 | # needs to be initialized before assign_add. This is only an issue if |
| 1548 | # restore() has not been called first. |
| 1549 | session.run(self.save_counter.initializer) |
| 1550 | if not graph_building or self._save_assign_op is None: |
| 1551 | with ops.colocate_with(self.save_counter): |
| 1552 | assign_op = self.save_counter.assign_add(1, read_value=True) |
| 1553 | if graph_building: |
| 1554 | self._save_assign_op = data_structures.NoDependency(assign_op) |
| 1555 | if graph_building: |
| 1556 | checkpoint_number = session.run(self._save_assign_op) |
| 1557 | else: |
| 1558 | checkpoint_number = assign_op.numpy() |
| 1559 | file_path = self.write( |
| 1560 | "%s-%d" % (file_prefix, checkpoint_number), session=session) |
| 1561 | checkpoint_management.update_checkpoint_state_internal( |
| 1562 | save_dir=os.path.dirname(file_prefix), |
| 1563 | model_checkpoint_path=file_path, |
| 1564 | all_model_checkpoint_paths=[file_path], |
| 1565 | save_relative_paths=True) |