ProgramCheckpoint
ProgramCheckpoint
Bases: Callback
Callback to save the Synalinks program or program variables at some frequency.
ProgramCheckpoint
callback is used in conjunction with training using
program.fit()
to save a program or variables (in a checkpoint file) at some
interval, so the program or variables can be loaded later to continue the
training from the state saved.
A few options this callback provides include:
- Whether to only keep the program that has achieved the "best performance" so far, or whether to save the program at the end of every epoch regardless of performance.
- Definition of "best"; which quantity to monitor and whether it should be maximized or minimized.
- The frequency it should save at. Currently, the callback supports saving at the end of every epoch, or after a fixed number of training batches.
- Whether only variables are saved, or the whole program is saved.
Example:
program.compile(
reward=...,
optimizer=...,
metrics=[
...
],
)
EPOCHS = 10
checkpoint_filepath = '/tmp/synalinks/checkpoint.program.json'
program_checkpoint_callback = synalinks.callbacks.ProgramCheckpoint(
filepath=checkpoint_filepath,
monitor='val_reward',
mode='max',
save_best_only=True,
)
# Program is saved at the end of every epoch, if it's the best seen so far.
program.fit(
x=x_train,
y=y_train,
validation_data=(x_test, y_test),
epochs=EPOCHS,
callbacks=[program_checkpoint_callback]
)
# The program (that are considered the best) can be loaded as -
synalinks.programs.load_program(checkpoint_filepath)
# Alternatively, one could checkpoint just the program variables as -
checkpoint_filepath = '/tmp/synalinks/checkpoint.variables.json'
program_checkpoint_callback = keras.callbacks.ProgramCheckpoint(
filepath=checkpoint_filepath,
save_variables_only=True,
monitor='val_accuracy',
mode='max',
save_best_only=True,
)
# Program variables are saved at the end of every epoch, if it's the best seen
# so far.
program.fit(
x=x_train,
y=y_train,
validation_data=(x_test, y_test),
epochs=EPOCHS,
callbacks=[program_checkpoint_callback]
)
# The program variables (that are considered the best) can be loaded as -
program.load_variables(checkpoint_filepath)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
str | PathLike
|
string or |
required |
monitor
|
str
|
The metric name to monitor. Typically the metrics are set by
the |
'val_reward'
|
verbose
|
str | int
|
Verbosity mode, 0 or 1. Mode 0 is silent, and mode 1 displays messages when the callback takes an action. |
0
|
save_best_only
|
bool
|
if |
False
|
mode
|
str
|
one of { |
'auto'
|
save_variables_only
|
bool
|
if |
False
|
save_freq
|
str | int
|
|
'epoch'
|
initial_value_threshold
|
float
|
Floating point initial "best" value of the
metric to be monitored. Only applies if |
None
|
Source code in synalinks/src/callbacks/program_checkpoint.py
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
|
_checkpoint_exists(filepath)
_get_file_path(epoch, batch, logs)
Returns the file path for checkpoint.
Source code in synalinks/src/callbacks/program_checkpoint.py
_get_most_recently_modified_file_matching_pattern(pattern)
Returns the most recently modified filepath matching pattern.
In the rare case where there are more than one pattern-matching file
having the same modified time that is most recent among all, return the
filepath that is largest (by >
operator, lexicographically using the
numeric equivalents). This provides a tie-breaker when multiple files
are most recent. Note that a larger filepath
can sometimes indicate a
later time of modification (for instance, when epoch/batch is used as
formatting option), but not necessarily (when accuracy or loss is used).
The tie-breaker is put in the logic as best effort to return the most
recent, and to avoid nondeterministic result.
Modified time of a file is obtained with os.path.getmtime()
.
This utility function is best demonstrated via an example:
file_pattern = 'batch{batch:02d}epoch{epoch:02d}.json'
test_dir = self.get_temp_dir()
path_pattern = os.path.join(test_dir, file_pattern)
file_paths = [
os.path.join(test_dir, file_name) for file_name in
['batch03epoch02.json',
'batch02epoch02.json', 'batch01epoch01.json']
]
for file_path in file_paths:
# Write something to each of the files
...
self.assertEqual(
_get_most_recently_modified_file_matching_pattern(path_pattern),
file_paths[-1])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
The file pattern that may optionally contain python
placeholder such as |
required |
Returns:
Type | Description |
---|---|
str
|
The most recently modified file's full filepath matching |
Source code in synalinks/src/callbacks/program_checkpoint.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
|
_save_program(epoch, batch, logs)
Saves the program.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch
|
int
|
the epoch this iteration is in. |
required |
batch
|
int
|
the batch this iteration is in. |
required |
logs
|
dict
|
the |
required |
Source code in synalinks/src/callbacks/program_checkpoint.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
_should_save_on_batch(batch)
Handles batch-level saving logic, supports steps_per_execution.