-
Notifications
You must be signed in to change notification settings - Fork 0
/
ablation.py
153 lines (115 loc) · 4.85 KB
/
ablation.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
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import pathlib
import matplotlib.pyplot as plt
from dataclasses import dataclass
from omegaconf import OmegaConf,DictConfig
import hydra
import pickle
import numpy as np
import pandas as pd
from utils import Logger,compare_beliefs, VariationalDropout
from plots import compare_beliefs,display_images_with_entropies,display_images_with_entropies_single
from termcolor import colored
# Class for the ablative study
from CNN_MNIST_dual import CNN_MNIST_Dual
@dataclass
class Config:
# Reproductibility and hardware
seed : int = 0
device : str = 'cuda' if torch.cuda.is_available() else 'cpu'
load_model : bool = False
job_num : int = 0
# Logging and saving
logdir : str = pathlib.Path.cwd() / 'logs'
savedir : str = pathlib.Path.cwd() / 'saved_models'
save_model : bool = True
# Task hyperparameters
dataset : str = 'MNIST'
epoch : int = 1 # The number of update
# Control hyperparameters
batch_size : int = 30
# Habitual network hyperparameters
temperature : float = 4.0
lr : float = 0.001
# Loses coefficients
kl_coeff : float = 5
# Compression
variational_dropout : bool = False
quantization : bool = False
class DataManager():
def __init__(self,config):
self.c = config
self.dataset = config.dataset
self.batch_size = config.batch_size
def load_MNIST(self):
# Loading MNIST
train_dataset = datasets.MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True,
)
test_dataset = datasets.MNIST(root='./data/',
train=False,
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=self.c.batch_size,
shuffle=True,
pin_memory=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=self.c.batch_size,
shuffle=False,
pin_memory=True)
return train_loader,test_loader
class Eval:
def __init__(self, config:Config):
self.c = config
self.seed = self.c.seed
self.device = self.c.device
self.logger = Logger()
self.train_data, self.test_data = DataManager(self.c).load_MNIST()
self.model = CNN_MNIST_Dual(self.c).to(self.device)
self.model.load_state_dict(torch.load("./saved_models/mnist_cnn.pt"))
self.criterion = nn.CrossEntropyLoss()
self.optimizer = optim.Adam(self.model.parameters(), lr=0.001)
# Hyperparameters
self.T = self.c.temperature
self.kl_coeff = self.c.kl_coeff
def eval(self):
self.model.eval()
test_loss = 0
correct = 0
for data, target in self.test_data:
data = data.to(self.device)
target = target.to(self.device)
z = self.model.encode(data)
logit_h = self.model.forward_h(z)
softmax_h = F.softmax(logit_h, dim=1)
entropies = -torch.sum(softmax_h * torch.log(softmax_h), dim=1, keepdim=True)
display_images_with_entropies_single(data, entropies, num_images_per_row=5)
# sum up batch loss
test_loss += torch.mean(self.criterion(softmax_h, target)).item()
# Compute accuracy
pred = softmax_h.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
test_loss /= len(self.test_data.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(test_loss, correct, len(self.test_data.dataset),100. * correct / len(self.test_data.dataset)))
# Return accuracy
return correct / len(self.test_data.dataset)
#@hydra.main(version_base=None, config_path="conf", config_name="config")
def main() -> None: # cfg : DictConfig
# Load default config
default_config = OmegaConf.structured(Config)
# Merge default config with run config, run config overrides if there is a conflict
#config = OmegaConf.merge(default_config, cfg)
#OmegaConf.save(config, 'config.yaml')
config = default_config
eval = Eval(config)
eval.eval()
if __name__ == '__main__':
main()
print('Finished correctly')