-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
126 lines (110 loc) · 4.19 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
import argparse
from torch.cuda import is_available
from utils import Permutator
from ewc import EWC
from torchvision import transforms
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
import torch
from model import Net
from torch.optim import Adam
import numpy as np
import torch.nn as nn
import wandb
from datetime import datetime
from tqdm import tqdm
# argparse
parser = argparse.ArgumentParser(
description="This is an implementation of Elastic Weight Consolidation"
)
parser.add_argument("--num_tasks", type=int, default=3, help="number of tasks")
parser.add_argument("--epochs", type=int, default=3, help="Number of epochs to train")
parser.add_argument("--batch-size", type=int, default=64, help="Batch size")
parser.add_argument("--use-ewc", type=bool, default=True, help="Use EWC")
parser.add_argument("--device", type=str, default=None, help="Device to use")
parser.add_argument("--lr", type=float, default=0.001, help="Learning rate")
parser.add_argument("--lmda", type=float, default=1000, help="Lambda")
parser.add_argument(
"--wandb-project", type=str, default="ewc", help="Wandb project name"
)
args = parser.parse_args()
if args.device is None:
DEVICE = "cuda:0" if is_available() else "cpu"
else:
DEVICE = args.device
BATCH_SIZE = args.batch_size
NUM_EPOCHS = args.epochs
NUM_TASKS = args.num_tasks
LMDA = args.lmda
USE_EWC = args.use_ewc
LR = args.lr
# dataloaders
trans = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
)
train_ds = MNIST("./data", train=True, download=True, transform=trans)
test_ds = MNIST("./data", train=False, download=True, transform=trans)
train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=False)
# instantiate model
net = Net().to(DEVICE)
optimizer = Adam(net.parameters(), lr=LR)
criterion = nn.CrossEntropyLoss()
permutator = Permutator(NUM_TASKS)
ewc = EWC(net, train_loader, DEVICE, NUM_TASKS, criterion, permutator)
def calc_accuracies(net, task_id):
accuracies = []
for batch in test_loader:
net.eval()
X, y = permutator.permute_batch(batch, task_id)
X, y = X.to(DEVICE), y.to(DEVICE)
logits = net(X)
accuracies.append((torch.argmax(logits, dim=1) == y).float().mean().item())
accuracy = np.mean(accuracies)
print(f"task {task_id} accuracy: {accuracy}")
return accuracy
def train(net, task_id, ep, lmda=100, use_ewc=True):
net.train()
for i, batch in enumerate(tqdm(train_loader, desc=f"epoch {ep+1}/{NUM_EPOCHS}")):
net.zero_grad()
X, y = permutator.permute_batch(batch, task_id)
X, y = X.to(DEVICE), y.to(DEVICE)
logits = net(X)
loss = criterion(logits, y)
# add fishers from previous tasks
if use_ewc:
for prev_task_id in range(task_id):
for n, p in net.named_parameters():
if p.requires_grad:
loss += (
lmda
/ 2
* torch.sum(
ewc.fisher_matrices[prev_task_id][n]
* (p - ewc.best_params[prev_task_id][n]) ** 2
)
)
loss.backward()
optimizer.step()
wandb.log(
{
f"Loss/train_{task_id}": loss,
f"Acc/train_{task_id}": (torch.argmax(logits, dim=1) == y)
.float()
.mean()
.item(),
}
)
tags = [f"lambda_{LMDA}", "ewc"] if USE_EWC else []
tmstp = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
name = f"ewc-lambda_{LMDA}-{tmstp}" if USE_EWC else f"baseline-{tmstp}"
wandb.init(project=args.wandb_project, tags=tags, name=name)
for task_id in range(NUM_TASKS):
print(f"--- Task {task_id} Training ---")
for ep in range(NUM_EPOCHS):
train(net, task_id, ep, lmda=LMDA, use_ewc=USE_EWC)
for id in range(NUM_TASKS):
acc = calc_accuracies(net, id)
wandb.log({"epoch": ep + NUM_EPOCHS * task_id, f"Acc/test_{id}": acc})
ewc.update_fisher(task_id)
ewc.save_params(task_id)