This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Configure validation frequency #5534
Merged
epwalsh
merged 13 commits into
allenai:main
from
JohnGiorgi:configure-validation-frequency
Jan 4, 2022
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
40e944c
Add boolean flag for validation
JohnGiorgi 765b9df
Add callback for configuring validation frequency
JohnGiorgi 13c0bae
Add test for ShouldValidateCallback
JohnGiorgi d4a1eb0
Import ShouldValidateCallback in callbacks
JohnGiorgi 39ee4a7
Rename start_validation to validation_start
JohnGiorgi 35c429f
Test that model trains with ShouldValidateCallback
JohnGiorgi 90d7b01
Update changelog
JohnGiorgi 2cfd658
Pass ShouldValidateCallback to trainer
JohnGiorgi 327a5e3
make this_epoch_val_metric optional with default value None
JohnGiorgi b7ba113
Don't check if best so far if not validating
JohnGiorgi e81d844
Re-arrange boolean expression for consistency
JohnGiorgi 8902b75
Remove unnecessary check for should_validate_this_epoch
JohnGiorgi f1d43cc
Merge branch 'main' into configure-validation-frequency
epwalsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Dict, Any, TYPE_CHECKING, Optional | ||
|
||
from allennlp.training.callbacks.callback import TrainerCallback | ||
|
||
if TYPE_CHECKING: | ||
from allennlp.training.gradient_descent_trainer import GradientDescentTrainer | ||
|
||
|
||
@TrainerCallback.register("should_validate_callback") | ||
class ShouldValidateCallback(TrainerCallback): | ||
""" | ||
A callback that you can pass to the `GradientDescentTrainer` to change the frequency of | ||
validation during training. If `start_validation` is not `None`, validation will not occur until | ||
`start_validation` epochs have elapsed. If `validation_interval` is not `None`, validation will | ||
run every `validation_interval` number of epochs epochs. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
serialization_dir: str, | ||
start_validation: Optional[int] = None, | ||
validation_interval: Optional[int] = None, | ||
) -> None: | ||
super().__init__(serialization_dir) | ||
self._start_validation = start_validation | ||
self._validation_interval = validation_interval | ||
|
||
def on_epoch( | ||
self, | ||
trainer: "GradientDescentTrainer", | ||
metrics: Dict[str, Any], | ||
epoch: int, | ||
is_primary: bool = True, | ||
**kwargs, | ||
) -> None: | ||
if self._start_validation is not None and epoch < self._start_validation: | ||
trainer._should_validate_this_epoch = False | ||
elif self._validation_interval is not None and epoch % self._validation_interval != 0: | ||
trainer._should_validate_this_epoch = False | ||
else: | ||
trainer._should_validate_this_epoch = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
validation_start
sounds a little better thanstart_validation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed! Yeah, I was struggling with the naming, if you have any other name change suggestions let me know!