-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathteleportation_coequality_test.py
146 lines (116 loc) · 5.21 KB
/
teleportation_coequality_test.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
import argparse
from typing import Tuple
import torch
import torch.nn as nn
import numpy as np
from torch.utils.data import Dataset
from neuralteleportation.neuralteleportationmodel import NeuralTeleportationModel
from neuralteleportation.training.training import test, train
from neuralteleportation.training.experiment_setup import get_dataset_subsets, get_model
from neuralteleportation.training.config import TrainingConfig, TrainingMetrics
from neuralteleportation.metrics import accuracy
def argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument("-t", type=int, default=2)
return parser.parse_args()
def test_model_with_set_get_weights(model: nn.Module, testset: Dataset,
metric: TrainingMetrics, config: TrainingConfig,
rept: int = 1) -> Tuple[np.ndarray, np.ndarray]:
loss_diff_avg = []
acc_diff_avg = []
for _ in range(rept):
m = NeuralTeleportationModel(model, input_shape=(config.batch_size, 3, 32, 32)).to(device)
w_o, cob_o = m.get_params()
m.random_teleport()
w_t, cob_t = m.get_params()
m.set_params(weights=w_o, cob=cob_o)
res = test(m, testset, metric, config)
loss1, acc1 = res['loss'], res['accuracy']
m.set_params(weights=w_t, cob=cob_t)
res = test(m, testset, metric, config)
loss2, acc2 = res['loss'], res['accuracy']
loss_diff_avg.append(np.abs(loss1 - loss2))
acc_diff_avg.append(np.abs(acc1 - acc2))
print("==========================================")
print("Loss and accuracy diff with set/get was")
print("Loss diff was: {:.6e}".format(np.abs(loss1 - loss2)))
print("Acc diff was: {:.6e}".format(np.abs(acc1 - acc2)))
print("==========================================")
return np.mean(loss_diff_avg), np.mean(acc_diff_avg)
def test_model_without_set_get_weights(model: nn.Module, testset: Dataset,
metric: TrainingMetrics, config: TrainingConfig,
rept: int = 1) -> Tuple[np.ndarray, np.ndarray]:
"""
Test if the model is coequal before and after using model.teleport
"""
loss_diff_avg = []
acc_diff_avg = []
for _ in range(rept):
m = NeuralTeleportationModel(model, input_shape=(config.batch_size, 3, 32, 32)).to(device)
res = test(m, testset, metric, config)
loss1, acc1 = res['loss'], res['accuracy']
m.random_teleport()
res = test(m, testset, metric, config)
loss2, acc2 = res['loss'], res['accuracy']
loss_diff_avg.append(np.abs(loss1 - loss2))
acc_diff_avg.append(np.abs(acc1 - acc2))
print("==========================================")
print("Loss and accuracy diff without set/get was")
print("Loss diff was: {:.6e}".format(np.abs(loss1 - loss2)))
print("Acc diff was: {:.6e}".format(np.abs(acc1 - acc2)))
print("==========================================")
return np.mean(loss_diff_avg), np.mean(acc_diff_avg)
if __name__ == '__main__':
device = 'cuda' if torch.cuda.is_available() else 'cpu'
trainset, _, testset = get_dataset_subsets("cifar10")
args = argument_parser()
metric = TrainingMetrics(
criterion=torch.nn.CrossEntropyLoss(),
metrics=[accuracy]
)
config = TrainingConfig(
batch_size=32,
epochs=1,
device=device
)
model = get_model("cifar10", "resnet18COB")
print("==========================================")
print()
print("==========================================")
print("===========Testing w/o set/get============")
print("==========================================")
print()
print("==========================================")
loss, acc = test_model_without_set_get_weights(model, testset, metric, config, args.t)
loss_mean = np.mean(loss)
acc_mean = np.mean(acc)
success = np.isclose(loss_mean, 0, atol=1e-7) and np.isclose(acc_mean, 0, atol=1e-7)
print("==========================================")
print("Loss and accuracy avg diff without set/get was")
print("Loss diff was: {:.6e}".format(loss_mean))
print("Acc diff was: {:.6e}".format(acc_mean))
if success:
print("TEST SUCCESSFUL")
else:
print("TEST FAILED!")
print()
print("==========================================")
print()
print("==========================================")
print("===========Testing with set/get===========")
print("==========================================")
print()
print("==========================================")
loss, acc = test_model_with_set_get_weights(model, testset, metric, config, args.t)
loss_mean = np.mean(loss)
acc_mean = np.mean(acc)
success = np.isclose(loss_mean, 0, atol=1e-7) and np.isclose(acc_mean, 0, atol=1e-7)
print("==========================================")
print("Loss and accuracy avg diff with set/get was")
print("Loss diff was: {:.6e}".format(loss_mean))
print("Acc diff was: {:.6e}".format(acc_mean))
if success:
print("TEST SUCCESSFUL")
else:
print("TEST FAILED!")
print("==========================================")