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)
| 1840 | return self._save_counter |
| 1841 | |
| 1842 | def save(self, file_prefix): |
| 1843 | """Saves a training checkpoint and provides basic checkpoint management. |
| 1844 | |
| 1845 | The saved checkpoint includes variables created by this object and any |
| 1846 | trackable objects it depends on at the time `Checkpoint.save()` is |
| 1847 | called. |
| 1848 | |
| 1849 | `save` is a basic convenience wrapper around the `write` method, |
| 1850 | sequentially numbering checkpoints using `save_counter` and updating the |
| 1851 | metadata used by `tf.train.latest_checkpoint`. More advanced checkpoint |
| 1852 | management, for example garbage collection and custom numbering, may be |
| 1853 | provided by other utilities which also wrap `write` |
| 1854 | (`tf.contrib.checkpoint.CheckpointManager` for example). |
| 1855 | |
| 1856 | Args: |
| 1857 | file_prefix: A prefix to use for the checkpoint filenames |
| 1858 | (/path/to/directory/and_a_prefix). Names are generated based on this |
| 1859 | prefix and `Checkpoint.save_counter`. |
| 1860 | |
| 1861 | Returns: |
| 1862 | The full path to the checkpoint. |
| 1863 | """ |
| 1864 | graph_building = not context.executing_eagerly() |
| 1865 | if graph_building: |
| 1866 | if ops.inside_function(): |
| 1867 | raise NotImplementedError( |
| 1868 | "Calling tf.train.Checkpoint.save() from a function is not " |
| 1869 | "supported, as save() modifies saving metadata in ways not " |
| 1870 | "supported by TensorFlow Operations. Consider using " |
| 1871 | "tf.train.Checkpoint.write(), a lower-level API which does not " |
| 1872 | "update metadata. tf.train.latest_checkpoint and related APIs will " |
| 1873 | "not see this checkpoint.") |
| 1874 | session = get_session() |
| 1875 | if self._save_counter is None: |
| 1876 | # When graph building, if this is a new save counter variable then it |
| 1877 | # needs to be initialized before assign_add. This is only an issue if |
| 1878 | # restore() has not been called first. |
| 1879 | session.run(self.save_counter.initializer) |
| 1880 | if not graph_building or self._save_assign_op is None: |
| 1881 | with ops.colocate_with(self.save_counter): |
| 1882 | assign_op = self.save_counter.assign_add(1, read_value=True) |
| 1883 | if graph_building: |
| 1884 | self._save_assign_op = data_structures.NoDependency(assign_op) |
| 1885 | if graph_building: |
| 1886 | checkpoint_number = session.run(self._save_assign_op) |
| 1887 | else: |
| 1888 | checkpoint_number = assign_op.numpy() |
| 1889 | file_path = self.write("%s-%d" % (file_prefix, checkpoint_number)) |
| 1890 | checkpoint_management.update_checkpoint_state_internal( |
| 1891 | save_dir=os.path.dirname(file_prefix), |
| 1892 | model_checkpoint_path=file_path, |
| 1893 | all_model_checkpoint_paths=[file_path], |
| 1894 | save_relative_paths=True) |
| 1895 | return file_path |
| 1896 | |
| 1897 | def restore(self, save_path): |
| 1898 | """Restore a training checkpoint. |