-
Notifications
You must be signed in to change notification settings - Fork 0
/
part3_whitening.py
47 lines (39 loc) · 1.35 KB
/
part3_whitening.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
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from part3_optimization import Test, Train, train_and_test
from part3_regularization_decay import CNNetwork
from sklearn.decomposition import PCA
class WhitenedNetwork(nn.Module):
def __init__(self, model, whiten):
super(WhitenedNetwork, self).__init__()
self.model = model
self.whiten_lambda_transformation = whiten
def forward(self, x):
whiten = self.whiten_lambda_transformation
x = self.whiten(x)
x = self.model(x)
return x
from part3_baseline import epochs, loss_fn, batch_size
from part3_optimization import lr, momentum, std
def pca_batch(X):
# Perform PCA
pca = PCA(n_components=X.shape[1], whiten=True)
pca.fit(X)
eigenvectors = torch.from_numpy(pca.components_).float()
# Define the whitening transform
whiten = lambda x: torch.mm(x, eigenvectors.T)
def activate_whitening():
model = CNNetwork(std)
whitened_model = WhitenedNetwork(model, pca_batch)
return train_and_test(
model=model,
loss_fn=nn.CrossEntropyLoss(),
optimizer=torch.optim.SGD(
whitened_model.parameters(),
lr=lr,
momentum=momentum),
epochs=epochs,
batch_size=batch_size)
#activate_whitening()