################################################################################
# Copyright (c) 2021 ContinualAI.                                              #
# Copyrights licensed under the MIT License.                                   #
# See the accompanying LICENSE file for terms.                                 #
#                                                                              #
# Date: 12-10-2020                                                             #
# Author(s): Vincenzo Lomonaco                                                 #
# E-mail: contact@continualai.org                                              #
# Website: avalanche.continualai.org                                           #
################################################################################

"""
This is a simple example on how to use the Replay strategy.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import torch
from torch.nn import CrossEntropyLoss
from torchvision import transforms
from torchvision.transforms import ToTensor, RandomCrop
import torch.optim.lr_scheduler
import matplotlib.pyplot as plt
import numpy as np
from avalanche.benchmarks import SplitMNIST
from avalanche.models import VAE
from avalanche.training.supervised import VAETraining
from avalanche.training.plugins import GenerativeReplayPlugin
from avalanche.logging import InteractiveLogger


def main(args):
    # --- CONFIG
    device = torch.device(
        f"cuda:{args.cuda}"
        if torch.cuda.is_available() and args.cuda >= 0
        else "cpu"
    )
    n_batches = 5
    # ---------

    # --- TRANSFORMATIONS
    train_transform = transforms.Compose(
        [
            RandomCrop(28, padding=4),
            ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,)),
        ]
    )
    test_transform = transforms.Compose(
        [ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
    )
    # ---------

    # --- SCENARIO CREATION
    scenario = SplitMNIST(n_experiences=10, seed=1234)
    # ---------

    # MODEL CREATION
    model = VAE((1, 28, 28), nhid=2)

    # choose some metrics and evaluation method
    interactive_logger = InteractiveLogger()

    # CREATE THE STRATEGY INSTANCE (GenerativeReplay)
    cl_strategy = VAETraining(
        model,
        torch.optim.Adam(model.parameters(), lr=0.001),
        train_mb_size=100,
        train_epochs=4,
        device=device,
        plugins=[GenerativeReplayPlugin()]
    )

    # TRAINING LOOP
    print("Starting experiment...")
    f, axarr = plt.subplots(scenario.n_experiences, 10)
    for k in range(3):  # scenario.train_stream:
        # k = 0
        print("Start of experience ",
              scenario.train_stream[k].current_experience)
        cl_strategy.train(scenario.train_stream[k])
        print("Training completed")

        samples = model.generate(10)
        samples = samples.detach().cpu().numpy()

        for j in range(10):
            axarr[k, j].imshow(samples[j, 0], cmap="gray")
            axarr[k, 4].set_title("Generated images for experience " + str(k))
        np.vectorize(lambda ax: ax.axis('off'))(axarr)

        plt.savefig("VAE_output_per_exp")
        plt.show()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--cuda",
        type=int,
        default=0,
        help="Select zero-indexed cuda device. -1 to use CPU.",
    )
    args = parser.parse_args()
    main(args)

@@ -2666,29 +2666,35 @@
     
-for k in range(3):  #
+k = 0%0A    for experience in
  sce
@@ -2717,24 +2717,8 @@
 am:%0A
-        # k = 0%0A
     
@@ -2769,32 +2769,18 @@
     
-scenario.train_stream%5Bk%5D
+experience
 .cur
@@ -2826,32 +2826,18 @@
 ain(
-scenario.train_stream%5Bk%5D
+experience
 )%0A  
@@ -3179,21 +3179,66 @@
 (axarr)%0A
-%0A
     
+    k += 1%0A%0A    f.subplots_adjust(hspace=1.2)%0A
     plt.
@@ -3267,20 +3267,16 @@
 r_exp%22)%0A
-    
     plt.

Save all plots in a single file.