-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3532 from sheldon-roberts/deepncm-classifier
Adding a Deep Nearest Class Means Classifier model to Flair
- Loading branch information
Showing
6 changed files
with
454 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
41 changes: 41 additions & 0 deletions
41
flair/trainers/plugins/functional/deepncm_trainer_plugin.py
This file contains 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 collections.abc import Iterable | ||
|
||
import torch | ||
|
||
from flair.models import MultitaskModel | ||
from flair.nn import DeepNCMDecoder | ||
from flair.trainers.plugins.base import TrainerPlugin | ||
|
||
|
||
class DeepNCMPlugin(TrainerPlugin): | ||
"""Plugin for training DeepNCMClassifier. | ||
Handles both multitask and single-task scenarios. | ||
""" | ||
|
||
@property | ||
def decoders(self) -> Iterable[DeepNCMDecoder]: | ||
"""Iterator over all DeepNCMDecoder decoders in the trainer.""" | ||
model = self.trainer.model | ||
|
||
models = model.tasks.values() if isinstance(model, MultitaskModel) else [model] | ||
|
||
for sub_model in models: | ||
if hasattr(sub_model, "decoder") and isinstance(sub_model.decoder, DeepNCMDecoder): | ||
yield sub_model.decoder | ||
|
||
@TrainerPlugin.hook | ||
def after_training_epoch(self, **kwargs): | ||
"""Reset class counts after each training epoch.""" | ||
for decoder in self.decoders: | ||
if decoder.mean_update_method == "condensation": | ||
decoder.class_counts.data = torch.ones_like(decoder.class_counts) | ||
|
||
@TrainerPlugin.hook | ||
def after_training_batch(self, **kwargs): | ||
"""Update prototypes after each training batch.""" | ||
for decoder in self.decoders: | ||
decoder.update_prototypes() | ||
|
||
def __str__(self) -> str: | ||
return "DeepNCMPlugin" |
Oops, something went wrong.