Is there anywhere where the use of save and restore signature functions is used so I can see how to properly use them? I want to setup the model again every time we call startTraining() by calling setupModelPersonalization() but before doing so, saving the weights and then restoring them. Anyone could give feedback on how to do that?
@tf.function(input_signature=[tf.TensorSpec(shape=[], dtype=tf.string)])
def save(self, checkpoint_path):
"""Saves the trainable weights to the given checkpoint file.
Args:
checkpoint_path: A file path to save the model.
Returns:
Map of the checkpoint file path.
"""
tensor_names = [self.ws.name, self.bs.name]
tensors_to_save = [self.ws.read_value(), self.bs.read_value()]
tf.raw_ops.Save(
filename=checkpoint_path,
tensor_names=tensor_names,
data=tensors_to_save,
name='save')
return {'checkpoint_path': checkpoint_path}
@tf.function(input_signature=[tf.TensorSpec(shape=[], dtype=tf.string)])
def restore(self, checkpoint_path):
"""Restores the serialized trainable weights from the given checkpoint file.
Args:
checkpoint_path: A path to a saved checkpoint file.
Returns:
Map of restored weight and bias.
"""
restored_tensors = {}
restored = tf.raw_ops.Restore(
file_pattern=checkpoint_path,
tensor_name=self.ws.name,
dt=np.float32,
name='restore')
self.ws.assign(restored)
restored_tensors['ws'] = restored
restored = tf.raw_ops.Restore(
file_pattern=checkpoint_path,
tensor_name=self.bs.name,
dt=np.float32,
name='restore')
self.bs.assign(restored)
restored_tensors['bs'] = restored
return restored_tensors