-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathtest_analyze_traps.py
More file actions
142 lines (116 loc) · 5.71 KB
/
Copy pathtest_analyze_traps.py
File metadata and controls
142 lines (116 loc) · 5.71 KB
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
import unittest
import numpy as np
import pandas as pd
try:
import torch
import torch.nn as nn
TORCH_AVAILABLE = True
except Exception:
TORCH_AVAILABLE = False
import weightwatcher as ww
if TORCH_AVAILABLE:
class TinyTrapNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(16, 12, bias=False)
self.fc2 = nn.Linear(12, 10, bias=False)
with torch.no_grad():
u = torch.linspace(1.0, 2.0, steps=12)
v = torch.linspace(-2.0, 1.0, steps=16)
self.fc1.weight.copy_(35.0 * torch.outer(u, v))
u2 = torch.linspace(1.0, 1.5, steps=10)
v2 = torch.linspace(-1.0, 2.0, steps=12)
self.fc2.weight.copy_(20.0 * torch.outer(u2, v2))
def forward(self, x):
x = self.fc1(x)
x = self.fc2(x)
return x
@unittest.skipUnless(TORCH_AVAILABLE, "torch is required for analyze_traps tests")
class TestAnalyzeTraps(unittest.TestCase):
def setUp(self):
self.model = TinyTrapNet()
self.watcher = ww.WeightWatcher(model=self.model)
def test_analyze_traps_method_exists(self):
self.assertTrue(hasattr(self.watcher, "analyze_traps"))
def test_analyze_traps_returns_dataframe(self):
np.random.seed(123)
df = self.watcher.analyze_traps(plot=False, savefig=False)
self.assertIsInstance(df, pd.DataFrame)
def test_analyze_traps_columns(self):
np.random.seed(123)
df = self.watcher.analyze_traps(plot=False, savefig=False)
expected_cols = {
"layer_id", "name", "trap_index", "perm_mode_index",
"sigma_perm", "mp_bulk_max", "left_top_mass", "right_top_mass"
}
self.assertTrue(expected_cols.issubset(set(df.columns)))
def test_analyze_traps_no_powerlaw_columns_required(self):
np.random.seed(123)
df = self.watcher.analyze_traps(plot=False, savefig=False)
self.assertNotIn("alpha", df.columns)
self.assertNotIn("xmin", df.columns)
self.assertNotIn("xmax", df.columns)
def test_analyze_traps_reproducible_when_seed_fixed(self):
np.random.seed(999)
df1 = self.watcher.analyze_traps(plot=False, savefig=False)
np.random.seed(999)
df2 = self.watcher.analyze_traps(plot=False, savefig=False)
self.assertEqual(len(df1), len(df2))
self.assertListEqual(df1["layer_id"].tolist(), df2["layer_id"].tolist())
self.assertListEqual(df1["perm_mode_index"].tolist(), df2["perm_mode_index"].tolist())
def test_analyze_traps_reproducible_with_rng_seed(self):
df1 = self.watcher.analyze_traps(plot=False, savefig=False, rng=1337)
df2 = self.watcher.analyze_traps(plot=False, savefig=False, rng=1337)
self.assertEqual(len(df1), len(df2))
self.assertListEqual(df1["layer_id"].tolist(), df2["layer_id"].tolist())
self.assertListEqual(df1["perm_mode_index"].tolist(), df2["perm_mode_index"].tolist())
def test_analyze_traps_respects_layer_filter(self):
np.random.seed(123)
all_df = self.watcher.analyze_traps(plot=False, savefig=False)
if len(all_df) == 0:
self.skipTest("No traps detected in this environment")
layer_id = int(all_df["layer_id"].iloc[0])
np.random.seed(123)
layer_df = self.watcher.analyze_traps(layers=[layer_id], plot=False, savefig=False)
self.assertTrue(set(layer_df["layer_id"].unique()).issubset({layer_id}))
def test_analyze_traps_skips_ambiguous_multi_Wmat_layers_safely(self):
conv_model = nn.Conv2d(3, 8, kernel_size=3, bias=False)
watcher = ww.WeightWatcher(model=conv_model)
np.random.seed(123)
df = watcher.analyze_traps(plot=False, savefig=False, pool=True)
self.assertIsInstance(df, pd.DataFrame)
def test_analyze_traps_contains_vector_metric_columns(self):
np.random.seed(123)
df = self.watcher.analyze_traps(plot=False, savefig=False)
required = {
"u_entropy", "u_discrete_entropy", "u_localization_ratio", "u_participation_ratio",
"v_entropy", "v_discrete_entropy", "v_localization_ratio", "v_participation_ratio"
}
self.assertTrue(required.issubset(set(df.columns)))
def test_analyze_traps_contains_order_invariant_stat_columns(self):
np.random.seed(123)
df = self.watcher.analyze_traps(plot=False, savefig=False)
required = {
"u_l2_fourth_moment", "u_effective_support", "u_gini_abs", "u_top10_mass",
"u_squared_amp_entropy", "u_stable_rank_surrogate",
"v_l2_fourth_moment", "v_effective_support", "v_gini_abs", "v_top10_mass",
"v_squared_amp_entropy", "v_stable_rank_surrogate", "trap_balance_ratio",
"trap_diffuseness_score", "trap_risk_score", "trap_assessment"
}
self.assertTrue(required.issubset(set(df.columns)))
def test_order_invariant_stats_are_finite(self):
np.random.seed(123)
df = self.watcher.analyze_traps(plot=False, savefig=False)
if len(df) == 0:
self.skipTest("No traps detected in this environment")
row = df.iloc[0]
for col in [
"u_l2_fourth_moment", "u_l2_sixth_moment", "u_effective_support", "u_gini_abs",
"u_top1_mass", "u_top5_mass", "u_top10_mass", "u_squared_amp_entropy", "u_stable_rank_surrogate",
"v_l2_fourth_moment", "v_l2_sixth_moment", "v_effective_support", "v_gini_abs",
"v_top1_mass", "v_top5_mass", "v_top10_mass", "v_squared_amp_entropy", "v_stable_rank_surrogate",
"trap_balance_ratio",
]:
self.assertTrue(np.isfinite(row[col]))
if __name__ == "__main__":
unittest.main()