1 # Copyright 2017 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 """Iterator ops.""" 16 from __future__ import absolute_import 17 from __future__ import division 18 from __future__ import print_function 19 20 from tensorflow.python.framework import ops 21 from tensorflow.python.ops import gen_dataset_ops 22 from tensorflow.python.training import saver 23 24 25 def make_saveable_from_iterator(iterator): 26 """Returns a SaveableObject for saving/restore iterator state using Saver. 27 28 Args: 29 iterator: Iterator. 30 31 For example: 32 33 ```python 34 with tf.Graph().as_default(): 35 ds = tf.data.Dataset.range(10) 36 iterator = ds.make_initializable_iterator() 37 # Build the iterator SaveableObject. 38 saveable_obj = tf.contrib.data.make_saveable_from_iterator(iterator) 39 # Add the SaveableObject to the SAVEABLE_OBJECTS collection so 40 # it can be automatically saved using Saver. 41 tf.add_to_collection(tf.GraphKeys.SAVEABLE_OBJECTS, saveable_obj) 42 saver = tf.train.Saver() 43 44 while continue_training: 45 ... Perform training ... 46 if should_save_checkpoint: 47 saver.save() 48 ``` 49 50 Note: When restoring the iterator, the existing iterator state is completely 51 discarded. This means that any changes you may have made to the Dataset 52 graph will be discarded as well! This includes the new Dataset graph 53 that you may have built during validation. So, while running validation, 54 make sure to run the initializer for the validation input pipeline after 55 restoring the checkpoint. 56 57 Note: Not all iterators support checkpointing yet. Attempting to save the 58 state of an unsupported iterator will throw an error. 59 """ 60 return _Saveable(iterator._iterator_resource) # pylint: disable=protected-access 61 62 63 class _Saveable(saver.BaseSaverBuilder.SaveableObject): 64 """SaveableObject for saving/restoring iterator state.""" 65 66 def __init__(self, iterator_resource): 67 serialized_iterator = gen_dataset_ops.serialize_iterator(iterator_resource) 68 specs = [ 69 saver.BaseSaverBuilder.SaveSpec(serialized_iterator, "", 70 iterator_resource.name + "-state") 71 ] 72 super(_Saveable, self).__init__(iterator_resource, specs, 73 iterator_resource.name) 74 75 def restore(self, restored_tensors, unused_restored_shapes): 76 with ops.colocate_with(self.op): 77 return gen_dataset_ops.deserialize_iterator(self.op, restored_tensors[0]) 78