-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccs.py
164 lines (124 loc) · 5.25 KB
/
ccs.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
152
153
154
155
156
157
158
159
160
161
162
163
164
import copy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class CCS(object):
def __init__(self, model, x0, x1, nepochs=1000, ntries=10, lr=1e-3, batch_size=-1,
verbose=False, device="cuda", weight_decay=0.01, var_normalize=False):
# data
self.var_normalize = var_normalize
self.x0 = self.normalize(x0)
self.x1 = self.normalize(x1)
self.d = self.x0.shape[-1]
# training
self.nepochs = nepochs
self.ntries = ntries
self.lr = lr
self.verbose = verbose
self.device = device
self.batch_size = batch_size
self.weight_decay = weight_decay
self.model = model
self.reversed = False
self.initialize_probe()
self.best_probe = copy.deepcopy(self.probe)
def initialize_probe(self):
self.probe = nn.Sequential(nn.Linear(self.d, 1), nn.Sigmoid()).to(self.device)
def normalize(self, x):
"""
Mean-normalizes the data x (of shape (n, d))
If self.var_normalize, also divides by the standard deviation
"""
normalized_x = x - x.mean(axis=0, keepdims=True)
if self.var_normalize:
normalized_x /= normalized_x.std(axis=0, keepdims=True)
return normalized_x
def get_tensor_data(self):
"""
Returns x0, x1 as appropriate tensors (rather than np arrays)
"""
x0 = torch.tensor(self.x0, dtype=torch.float, requires_grad=False, device=self.device)
x1 = torch.tensor(self.x1, dtype=torch.float, requires_grad=False, device=self.device)
return x0, x1
def get_loss(self, p0, p1):
"""
Returns the CCS loss for two probabilities each of shape (n,1) or (n,)
"""
informative_loss = (torch.min(p0, p1)**2).mean(0)
consistent_loss = ((p0 - (1-p1))**2).mean(0)
return informative_loss + consistent_loss
def get_acc(self, x0_test, x1_test, y_test):
"""
Computes accuracy for the current parameters on the given test inputs
"""
x0 = torch.tensor(self.normalize(x0_test), dtype=torch.float, requires_grad=False, device=self.device)
x1 = torch.tensor(self.normalize(x1_test), dtype=torch.float, requires_grad=False, device=self.device)
with torch.no_grad():
p0, p1 = self.best_probe(x0), self.best_probe(x1)
avg_confidence = 0.5*(p0 + (1-p1))
predictions = (avg_confidence.detach().cpu().numpy() < 0.5).astype(int)[:, 0]
acc = (predictions == y_test).mean()
acc = max(acc, 1 - acc)
return acc
def train(self):
"""
Does a single training run of nepochs epochs
"""
x0, x1 = self.get_tensor_data()
permutation = torch.randperm(len(x0))
x0, x1 = x0[permutation], x1[permutation]
# set up optimizer
optimizer = torch.optim.AdamW(self.probe.parameters(), lr=self.lr, weight_decay=self.weight_decay)
batch_size = len(x0) if self.batch_size == -1 else self.batch_size
nbatches = len(x0) // batch_size
# Start training (full batch)
for epoch in range(self.nepochs):
for j in range(nbatches):
x0_batch = x0[j*batch_size:(j+1)*batch_size]
x1_batch = x1[j*batch_size:(j+1)*batch_size]
# probe
p0, p1 = self.probe(x0_batch), self.probe(x1_batch)
# get the corresponding loss
loss = self.get_loss(p0, p1)
# update the parameters
optimizer.zero_grad()
loss.backward()
optimizer.step()
return loss.detach().cpu().item()
def repeated_train(self):
best_loss = np.inf
for train_num in range(self.ntries):
self.initialize_probe()
loss = self.train()
if loss < best_loss:
self.best_probe = copy.deepcopy(self.probe)
best_loss = loss
return best_loss
# Orient the probe in a supervised mannger
def orient(self, x0, x1, y):
x0 = torch.tensor(x0, dtype=torch.float, requires_grad=False, device=self.model.device)
x1 = torch.tensor(x1, dtype=torch.float, requires_grad=False, device=self.model.device)
with torch.no_grad():
p0, p1 = self.best_probe(x0), self.best_probe(x1)
avg_confidence = p0 - p1
credences = (avg_confidence.detach().cpu().numpy())[:, 0]
predictions = (credences < 0.5).astype(int)
acc = (predictions == y).mean()
self.reversed = acc < 0.5
def predict_single(self, hs):
classifier_direction = np.squeeze(np.transpose(self.best_probe[0].weight.detach().cpu().numpy()))
def sigmoid(x, temp=1):
return 1/(1 + np.exp(-1 * temp * x))
credence = sigmoid(np.dot(hs,classifier_direction))
if (self.reversed):
return 1 - credence
else:
return credence
def direction_single(self, hs):
classifier_direction = np.squeeze(np.transpose(self.best_probe[0].weight.detach().cpu().numpy()))
res = np.dot(hs,classifier_direction)
if (self.reversed):
return -res
else:
return res