BackUpAndRestore
BackupAndRestore
Bases: Callback
Callback to back up and restore the training state.
BackupAndRestore callback is intended to recover training from an
interruption that has happened in the middle of a Program.fit execution, by
backing up the training states in a temporary checkpoint file, at the end of
each epoch. Each backup overwrites the previously written checkpoint file,
so at any given time there is at most one such checkpoint file for
backup/restoring purpose.
If training restarts before completion, the training state (which includes
the Program weights and epoch number) is restored to the most recently saved
state at the beginning of a new Program.fit run. At the completion of a
Program.fit run, the temporary checkpoint file is deleted.
Note that the user is responsible to bring jobs back after the interruption. This callback is important for the backup and restore mechanism for fault tolerance purpose, and the program to be restored from a previous checkpoint is expected to be the same as the one used to back up. If user changes arguments passed to compile or fit, the checkpoint saved for fault tolerance can become invalid.
Example:
class InterruptingCallback(synalinks.callbacks.Callback):
def on_epoch_begin(self, epoch, logs=None):
if epoch == 4:
raise RuntimeError('Interrupting!')
callback = synalinks.callbacks.BackupAndRestore(backup_dir="/tmp/backup")
program = synalinks.programs.Sequential(
[synalinks.Generator(data_model=Answer)]
)
program.compile(
synalinks.optimizers.RandomFewShot(),
reward=synalinks.reward.ExactMatch(),
)
program.build(Query)
try:
program.fit(..., callbacks=[callback, InterruptingCallback()], verbose=0)
except:
pass
history = program.fit(
..., epochs=10, batch_size=1, callbacks=[callback], verbose=0
)
# Only 6 more epochs are run, since first training got interrupted at
# zero-indexed epoch 4, second training will continue from 4 to 9.
len(history.history['reward']) # Returns 6
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backup_dir
|
str
|
Path of directory where to store the data
needed to restore the program. The directory
cannot be reused elsewhere to store other files, e.g. by the
|
required |
save_freq
|
str | int
|
|
'epoch'
|
double_checkpoint
|
bool
|
If enabled, |
False
|
delete_checkpoint
|
bool
|
This |
True
|
Source code in synalinks/src/callbacks/backup_and_restore.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |