-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_models_method_2.py
67 lines (49 loc) · 2.21 KB
/
analyze_models_method_2.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
import gc
import logging
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import torch
from classes import AE
from funcs import calculate_correlation_matrix
def analyze_models_method_2(
device, external_layer_size: int, iterator_ctrl, iterator_not_ctrl, parameter_names
):
def iterate_model_set(device, external_layer_size, iterator, parameter_names):
for element in iterator:
model_name, bottleneck_size = element[0], element[1]
logging.info(f"Now analyzing {model_name}")
# initiate a model
model = AE(external_layer_size, bottleneck_size).to(device)
with torch.no_grad():
model.load_state_dict(torch.load(f"model_{model_name}.pt"))
model.eval()
for param_name, param in model.named_parameters():
if param_name == "decoder.0.weight":
weight_matrix = param.detach().cpu().numpy()
break
# calculate correlation matrix
corr_matrix = pd.DataFrame(
calculate_correlation_matrix(weight_matrix, external_layer_size),
index=parameter_names,
columns=parameter_names,
)
corr_matrix.to_csv(f"analysis_method_2_corr_matrix_{model_name}.csv")
plt.figure(figsize=(15, 15), dpi=600)
sns.clustermap(corr_matrix)
plt.savefig(f"analysis_method_2_clustermap_{model_name}.png")
logging.info(f"Model {model_name} analysis (m. 2) result saved")
# clear memory
del model
del weight_matrix
del corr_matrix
torch.cuda.empty_cache()
gc.collect()
break
# analyze every model
logging.info("Analysis by method #2 started")
iterate_model_set(device, external_layer_size, iterator_ctrl, parameter_names)
logging.info("Control models analysed (m. 2)")
iterate_model_set(device, external_layer_size, iterator_not_ctrl, parameter_names)
logging.info("Not control models analysed (m. 2)")
logging.info("Analysis by method #2 finished")