1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""SaveableHook, for running callbacks at save and restore time.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20from tensorflow.python.framework import constant_op 21from tensorflow.python.training.tracking import base 22 23 24class SaveableHook(base.NoRestoreSaveable): 25 """Base class for running callbacks at Save/Restore time. 26 27 Subclasses should override one or both methods to modify or read variables 28 during the saving process. No guarantees are made regarding the precedence 29 of execution between multiple `SaveableHook` objects, but execution is 30 guaranteed to occur before or after the respective event. 31 32 Users should emit the SaveableHook alongside other SaveableObjects, such as 33 in Trackable._gather_saveables_for_checkpoint(). 34 35 Saves a single constant in order to be compliant with the SaveableObject API. 36 """ 37 38 def __init__(self, name): 39 """Creates a `SaveableHook` object. 40 41 Args: 42 name: the name to save the object under. 43 """ 44 super(SaveableHook, self).__init__( 45 tensor=constant_op.constant(0), 46 name=name, 47 ) 48 49 @property 50 def device(self): 51 return self.op.device 52 53 def before_save(self): 54 """This method will be called before iterating devices for saving.""" 55 pass 56 57 def after_restore(self): 58 """This method will be called after each device is restored.""" 59 pass 60