-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkpoints.py
31 lines (25 loc) · 1.29 KB
/
checkpoints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import tensorflow as tf
class Checkpoint:
"""Enhanced "tf.train.Checkpoint"."""
def __init__(self,
checkpoint_kwargs, # for "tf.train.Checkpoint"
directory, # for "tf.train.CheckpointManager"
max_to_keep=5,
keep_checkpoint_every_n_hours=None):
self.checkpoint = tf.train.Checkpoint(**checkpoint_kwargs)
self.manager = tf.train.CheckpointManager(self.checkpoint, directory, max_to_keep, keep_checkpoint_every_n_hours)
def restore(self, save_path=None):
save_path = self.manager.latest_checkpoint if save_path is None else save_path
return self.checkpoint.restore(save_path)
def save(self, file_prefix_or_checkpoint_number=None, session=None):
if isinstance(file_prefix_or_checkpoint_number, str):
return self.checkpoint.save(file_prefix_or_checkpoint_number, session=session)
else:
return self.manager.save(checkpoint_number=file_prefix_or_checkpoint_number)
def __getattr__(self, attr):
if hasattr(self.checkpoint, attr):
return getattr(self.checkpoint, attr)
elif hasattr(self.manager, attr):
return getattr(self.manager, attr)
else:
self.__getattribute__(attr) # this will raise an exception