-
-
Notifications
You must be signed in to change notification settings - Fork 292
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 #1596 from gogamid/master
Generative Replay with weighted loss for replayed data
- Loading branch information
Showing
3 changed files
with
93 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,31 +3,27 @@ | |
# Copyrights licensed under the MIT License. # | ||
# See the accompanying LICENSE file for terms. # | ||
# # | ||
# Date: 01-04-2022 # | ||
# Author(s): Florian Mies # | ||
# E-mail: [email protected] # | ||
# Website: avalanche.continualai.org # | ||
# Date: 13-02-2024 # | ||
# Author(s): Imron Gamidli # | ||
# # | ||
################################################################################ | ||
|
||
""" | ||
This is a simple example on how to use the Replay strategy. | ||
This is a simple example on how to use the GenerativeReplay strategy with weighted replay loss. | ||
""" | ||
|
||
import datetime | ||
import argparse | ||
import torch | ||
from torch.nn import CrossEntropyLoss | ||
from torchvision import transforms | ||
from torchvision.transforms import ToTensor, RandomCrop | ||
import torch.optim.lr_scheduler | ||
from avalanche.benchmarks import SplitMNIST | ||
from avalanche.models import SimpleMLP | ||
from avalanche.training.supervised import GenerativeReplay | ||
from avalanche.evaluation.metrics import ( | ||
forgetting_metrics, | ||
accuracy_metrics, | ||
loss_metrics, | ||
) | ||
from avalanche.logging import InteractiveLogger | ||
from avalanche.logging import InteractiveLogger, TextLogger | ||
from avalanche.training.plugins import EvaluationPlugin | ||
|
||
|
||
|
@@ -38,20 +34,23 @@ def main(args): | |
) | ||
|
||
# --- BENCHMARK CREATION | ||
benchmark = SplitMNIST(n_experiences=10, seed=1234) | ||
benchmark = SplitMNIST( | ||
n_experiences=5, seed=1234, fixed_class_order=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
) | ||
# --------- | ||
|
||
# MODEL CREATION | ||
model = SimpleMLP(num_classes=benchmark.n_classes) | ||
model = SimpleMLP(num_classes=benchmark.n_classes, hidden_size=10) | ||
|
||
# choose some metrics and evaluation method | ||
interactive_logger = InteractiveLogger() | ||
file_name = "logs/log_" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + ".log" | ||
text_logger = TextLogger(open(file_name, "a")) | ||
|
||
eval_plugin = EvaluationPlugin( | ||
accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True), | ||
loss_metrics(minibatch=True, epoch=True, experience=True, stream=True), | ||
forgetting_metrics(experience=True), | ||
loggers=[interactive_logger], | ||
accuracy_metrics(experience=True, stream=True), | ||
loss_metrics(minibatch=True), | ||
loggers=[interactive_logger, text_logger], | ||
) | ||
|
||
# CREATE THE STRATEGY INSTANCE (GenerativeReplay) | ||
|
@@ -60,10 +59,13 @@ def main(args): | |
torch.optim.Adam(model.parameters(), lr=0.001), | ||
CrossEntropyLoss(), | ||
train_mb_size=100, | ||
train_epochs=4, | ||
train_epochs=2, | ||
eval_mb_size=100, | ||
device=device, | ||
evaluator=eval_plugin, | ||
is_weighted_replay=True, | ||
weight_replay_loss_factor=2.0, | ||
weight_replay_loss=0.001, | ||
) | ||
|
||
# TRAINING LOOP | ||
|
@@ -87,4 +89,5 @@ def main(args): | |
help="Select zero-indexed cuda device. -1 to use CPU.", | ||
) | ||
args = parser.parse_args() | ||
print(args) | ||
main(args) |