-
Notifications
You must be signed in to change notification settings - Fork 1
/
CL_KAN_vs_MLP.py
279 lines (197 loc) · 10.3 KB
/
CL_KAN_vs_MLP.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# from torchsummary import summary
import torch
from efficientkan import KAN as efficientKAN
from fastkan import FastKAN as fastKAN
from kan import KANLayer
from continual_learning_trainer import ContinualLearningTrainer
from utils import DivideDataset
# CNN model for CIFAR-10 with KANLinear
class EfficientKAN(nn.Module):
def __init__(self, num_classes, dataset_name, init_method='xavier'):
super(EfficientKAN, self).__init__()
if dataset_name == 'CIFAR10':
self.input_size = 3072
elif dataset_name == 'MNIST':
self.input_size = 784
self.efficientKAN = efficientKAN([self.input_size, 256, num_classes])
self.init_weights(init_method=init_method)
def forward(self, x):
x = x.view(-1, self.input_size)
x = self.efficientKAN(x)
return x
## initialize the weights
def init_weights(self, init_method='xavier'):
init_method = {
'xavier': nn.init.xavier_normal_,
'kaiming': nn.init.kaiming_normal_,
'normal': nn.init.normal_,
}
## initialize the weights for the efficientKAN
for m in self.efficientKAN.modules():
for name, param in m.named_parameters():
if name == 'base_weight' or name == 'spline_weight':
init_method[init_method](param)
# CNN model for CIFAR-10 with KANLinear
class MLP(nn.Module):
def __init__(self, num_classes, dataset_name):
super(MLP, self).__init__()
if dataset_name == 'CIFAR10':
self.input_size = 3072
elif dataset_name == 'MNIST':
self.input_size = 784
self.mlp = nn.Sequential(
nn.Linear(self.input_size, 256),
nn.SELU(),
nn.Linear(256, num_classes)
)
def forward(self, x):
x = x.view(-1, self.input_size)
x = self.mlp(x)
return x
# CNN model for CIFAR-10 with fastKAN
class FastKAN(nn.Module):
def __init__(self, num_classes, dataset_name):
super(FastKAN, self).__init__()
if dataset_name == 'CIFAR10':
self.input_size = 3072
elif dataset_name == 'MNIST':
self.input_size = 784
self.fastKAN = fastKAN([self.input_size, 256, num_classes])
def forward(self, x):
x = x.view(-1, self.input_size)
x = self.fastKAN(x)
return x
class KAN_original(nn.Module):
def __init__(self, num_classes, dataset_name, device='cuda'):
super(KAN_original, self).__init__()
if dataset_name == 'CIFAR10':
self.input_size = 3072
elif dataset_name == 'MNIST':
self.input_size = 784
self.first_layer = KANLayer(in_dim=self.input_size, out_dim=256, num=5, k=3, device=device)
self.second_layer = KANLayer(in_dim=256, out_dim=num_classes, num=5, k=3, device=device)
self.activation = nn.SELU()
def forward(self, x):
x = x.view(-1, self.input_size)
x, _, _, _ = self.first_layer(x)
x = self.activation(x)
x ,_, _, _ = self.second_layer(x)
return x
if __name__ == '__main__':
## device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# define models
MLP_model_1 = MLP(num_classes=10, dataset_name='CIFAR10').to(device)
EfficientKAN_model_1 = EfficientKAN(num_classes=10, dataset_name='CIFAR10').to(device)
#FastKAN_model_1 = FastKAN(num_classes=10, dataset_name='CIFAR10').to(device)
#KAN_original_model_1 = KAN_original(num_classes=10, dataset_name='CIFAR10', device=device).to(device)
MLP_model_2 = MLP(num_classes=10, dataset_name='MNIST').to(device)
EfficientKAN_model_2 = EfficientKAN(num_classes=10, dataset_name='MNIST').to(device)
#FastKAN_model_2 = FastKAN(num_classes=10, dataset_name='MNIST').to(device)
#KAN_original_model_2 = KAN_original(num_classes=10, dataset_name='MNIST').to(device)
## batch size
batch_size = 64
## code_version_flag
isMNIST = True
## dataset transform
transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
## dataset and dataloader
train_dataset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.CIFAR10(root='./data', train=False, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
## MNIST dataset and dataloader
transform = transforms.Compose([
transforms.Resize((28, 28)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
train_dataset_MNIST = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset_MNIST = datasets.MNIST(root='./data', train=False, transform=transform)
train_MNIST_task_divider = DivideDataset(train_dataset_MNIST, 5, list(range(10)))
train_MNIST_task_datasets, tasks_classes = train_MNIST_task_divider.get_the_datasets()
num_of_task = 5
train_task_dataset_loader = {}
for task in range(num_of_task):
train_task_dataset_loader[task] = DataLoader(train_MNIST_task_datasets[task], batch_size=batch_size, shuffle=True)
test_MNIST_task_divider = DivideDataset(test_dataset_MNIST, 5, list(range(10)))
test_MNIST_task_datasets, tasks_classes = test_MNIST_task_divider.get_the_datasets()
test_task_dataset_loader = {}
for task in range(num_of_task):
test_task_dataset_loader[task] = DataLoader(test_MNIST_task_datasets[task], batch_size=batch_size, shuffle=False)
# train_loader_MNIST = DataLoader(train_dataset_MNIST, batch_size=batch_size, shuffle=True)
# test_loader_MNIST = DataLoader(test_dataset_MNIST, batch_size=batch_size, shuffle=False)
## define optimizer
MLP_optimizer_1 = optim.AdamW(MLP_model_1.parameters(), lr=1e-3, weight_decay=1e-4)
EfficientKAN_optimizer_1 = optim.AdamW(EfficientKAN_model_1.parameters(), lr=1e-3, weight_decay=1e-4)
#FastKAN_optimizer_1 = optim.AdamW(FastKAN_model_1.parameters(), lr=1e-3, weight_decay=1e-4)
#KAN_original_optimizer_1 = optim.AdamW(KAN_original_model_1.parameters(), lr=1e-3, weight_decay=1e-4)
MLP_optimizer_2 = optim.AdamW(MLP_model_2.parameters(), lr=1e-3, weight_decay=1e-4)
EfficientKAN_optimizer_2 = optim.AdamW(EfficientKAN_model_2.parameters(), lr=1e-3, weight_decay=1e-4)
#FastKAN_optimizer_2 = optim.AdamW(FastKAN_model_2.parameters(), lr=1e-3, weight_decay=1e-4)
#KAN_original_optimizer_2 = optim.AdamW(KAN_original_model_2.parameters(), lr=1e-3, weight_decay=1e-4)
## define loss function
criterion = nn.CrossEntropyLoss()
## training schedular
schedular_MLP_1 = optim.lr_scheduler.ExponentialLR(MLP_optimizer_1, gamma=0.8)
schedular_EfficientKAN_1 = optim.lr_scheduler.ExponentialLR(EfficientKAN_optimizer_1, gamma=0.8)
#schedular_FastKAN_1 = optim.lr_scheduler.ExponentialLR(FastKAN_optimizer_1, gamma=0.8)
#schedular_original_KAN_1 = optim.lr_scheduler.ExponentialLR(KAN_original_optimizer_1, gamma=0.8)
schedular_MLP_2 = optim.lr_scheduler.ExponentialLR(MLP_optimizer_2, gamma=0.8)
schedular_EfficientKAN_2 = optim.lr_scheduler.ExponentialLR(EfficientKAN_optimizer_2, gamma=0.8)
#schedular_FastKAN_2 = optim.lr_scheduler.ExponentialLR(FastKAN_optimizer_2, gamma=0.8)
#schedular_original_KAN_2 = optim.lr_scheduler.ExponentialLR(KAN_original_optimizer_2, gamma=0.8)
if isMNIST:
models = [ MLP_model_2, EfficientKAN_model_2] #, FastKAN_model_2, KAN_original_model_2]
model_names = ['MLP', 'EfficientKAN', 'FastKAN', 'KAN_original']
dataset_name = ['MNIST']
train_dataset_loader = [train_task_dataset_loader]
test_dataset_loader = [test_task_dataset_loader]
optimizers = [MLP_optimizer_2, EfficientKAN_optimizer_2] #, FastKAN_optimizer_2, KAN_original_optimizer_2]
schedulars = [schedular_MLP_2, schedular_EfficientKAN_2] #, schedular_FastKAN_2, schedular_original_KAN_2]
else:
models = [MLP_model_1, EfficientKAN_model_1] #, FastKAN_model_1]
model_names = ['MLP', 'EfficientKAN', 'FastKAN']
dataset_name = ['CIFAR10']
train_dataset_loader = [train_loader]
test_dataset_loader = [test_loader]
optimizers = [MLP_optimizer_1, EfficientKAN_optimizer_1] #, FastKAN_optimizer_1]
schedulars = [schedular_MLP_1, schedular_EfficientKAN_1] #, schedular_FastKAN_1]
epoch_ditribution = {}
for task in range(num_of_task):
if task == 0:
epoch_ditribution[task] = 7
else:
epoch_ditribution[task] = 5
file_path = 'saved_models\\CL_KAN_vs_MLP.txt'
file_path_cl_tasks = 'saved_models\\CL_KAN_vs_MLP_tasks.txt'
args_dict = {}
args_dict['num_models'] = len(models)
args_dict['num_datasets'] = 1
for index in range(args_dict['num_datasets']):
args_dict[('dataset_name', index)] = dataset_name[index]
args_dict[('trainloader', index)] = train_dataset_loader[index]
args_dict[('testloader', index)] = test_dataset_loader[index]
args_dict['record_save_path'] = file_path
args_dict['record_save_path_cl_tasks'] = file_path_cl_tasks
args_dict['epoch_distribution'] = epoch_ditribution
args_dict['device'] = device
args_dict['loss_function'] = criterion
args_dict['weights_save_path'] = 'saved_models'
args_dict['num_tasks'] = num_of_task
for m in range(args_dict['num_models']):
args_dict[('model', m)] = models[m]
args_dict[('model_name', m)] = model_names[m]
args_dict[('optimizers', m)] = optimizers[m]
args_dict[('schedulers', m)] = schedulars[m]
cl_trainer = ContinualLearningTrainer(args_dict)
cl_trainer.train_models()