-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_model_corr.py
221 lines (184 loc) · 6.98 KB
/
cross_model_corr.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
For Hydrogen;
%load_ext autoreload
%autoreload 2
Probe BERT hidden representations for SST (Linguistic correlation analysis)
"""
import argparse
import csv
from pathlib import Path
import logging
from logging import getLogger, StreamHandler, FileHandler
from pympler import asizeof
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
import torch
from pytorch_transformers import BertForSequenceClassification, BertTokenizer, BertConfig
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
config = BertConfig.from_pretrained('bert-base-uncased')
config.output_hidden_states = True
config.output_attentions = True
cache = {}
sns.set()
# model_name = "BERT"
# task_name = "CoLA"
def calc_hid_rep(args, model_name: str, task_name: str):
# Check if the result already exists
output_data_path = Path(
f"probing_data/{model_name}/{task_name}_neuron.npy")
if output_data_path.exists() and not args.override:
logger.info("Hid rep already exists.")
return
# Load model
model = load_model(model_name, task_name)
# Activate the neuron (use SST-2 for now)
data_path = Path(f"glue_data/{args.evaluation_data}/test.tsv")
with data_path.open("r", encoding="utf-8") as f:
csv_reader = csv.reader(f, delimiter="\t")
next(csv_reader)
for i, row in enumerate(csv_reader):
sentence = "[CLS] " + row[1] + " [SEP]"
tokenized = tokenizer.tokenize(sentence)
encoded = tokenizer.encode(sentence)
tens = torch.LongTensor(encoded).view(1, -1)
last_hid, all_hid, all_attention = model(tens)
# Append the neurons to 1D vec
array_list = [hid_j[0].detach().numpy() for hid_j in all_hid]
all_hid_array = np.concatenate(array_list, axis=1)
if i == 0:
result_array = all_hid_array
else:
result_array = np.concatenate((result_array, all_hid_array),
axis=0)
# Stop at the specified length
if args.debug and i == 4:
break
if i % 100 == 0:
logger.info(f"Now at {i}th example")
# shape = (sum(example_num)=num_words,layer_num*hidden_dim)
# debug time: (113,9984)
# Report consumed memory (sample=5 -> 4400MB)
consumed_bytes = asizeof.asizeof(result_array)
logger.info(f"Consumed bytes: {consumed_bytes /1024 / 1024 }MB")
# Save array
np.save(output_data_path, result_array)
return
def load_model(model_name: str, task_name: str):
if model_name not in cache:
cache[model_name] = dict()
if task_name not in cache[model_name]:
model_path = str(Path(f"models/{model_name}/{task_name}/"))
model = BertForSequenceClassification.from_pretrained(model_path,
config=config)
cache[model_name][task_name] = model
return cache[model_name][task_name]
# A = cola_rep.T
# B = sst_rep.T
def calc_corr(A, B):
"""
Pairwise correlation for matrix with shape of (example_num,feature_num)
"""
N = B.shape[0]
sA = A.sum(0)
sB = B.sum(0)
p1 = N * np.dot(B.T, A)
p2 = sA * sB[:, None]
p3 = N * ((B**2).sum(0)) - (sB**2)
p4 = N * ((A**2).sum(0)) - (sA**2)
return ((p1 - p2) / np.sqrt(p4 * p3[:, None]))
# neuron_num = 3985
def highlight_neuron(neuron_num):
# Append to string to color the output
END = '\033[0m'
RED = '\033[41m'
YEL = '\u001b[43m'
BLUE = '\033[44m'
CIAN = '\033[46m'
# # Reindex to the original index
# layer_num,ind = divmod(neuron_num,768)
# Get the hid rep
sst_path = Path(f"probing_data/{model_name}/{task_name2}_neuron.npy")
sst_rep = np.load(sst_path)
rep = sst_rep[:, neuron_num]
# Plot the distribution of this neuron
sns.distplot(rep)
# Get the top 5% activate neuron
mean = np.mean(rep)
std = np.std(rep)
# Show the text where this activation occured
data_path = Path(f"glue_data/{args.evaluation_data}/test.tsv")
with data_path.open("r") as f:
csv_reader = csv.reader(f, delimiter="\t")
next(csv_reader)
for row in csv_reader:
sentence = "[CLS] " + row[1] + " [SEP]"
tokenized = tokenizer.tokenize(sentence)
encoded = tokenizer.encode(sentence)
tens = torch.LongTensor(encoded).view(1, -1)
last_hid, all_hid, all_attention = model(tens)
# get the activation of
layer_num, ind = divmod(neuron_num, all_hid[0].shape[2])
activation = all_hid[0][0][layer_num][ind].item()
if abs(activation) > std * 2:
return
def EDA():
model_name = "BERT"
task_name1 = "SST-2"
task_name2 = "CoLA"
# Load the hid rep
cola_path = Path(f"probing_data/{model_name}/{task_name1}_neuron.npy")
cola_rep = np.load(cola_path)
sst_path = Path(f"probing_data/{model_name}/{task_name2}_neuron.npy")
sst_rep = np.load(sst_path)
# Calc the pairwise corr
corr_path = Path("probing_data/EDA/corr.npy")
if not corr_path.exists():
corr_matrix = calc_corr(cola_rep, sst_rep) # shape==(9984,9984)
np.save(corr_path, corr_matrix)
logger.info("Calculated correlation")
else:
logger.info("Existing correlation matrix exists: use this")
corr_matrix = np.load(corr_path)
sorted_corr = np.sort(corr_matrix, axis=1)
# There are neurons which moves similarily with each neuron
# However layer in the lower layer has this tendency strongly
n = 3
mean_topn = np.mean(sorted_corr[:, -n:], axis=1)
fig, ax = plt.subplots()
ax.plot(range(len(mean_topn)), mean_topn)
# Maybe neuron with low corr are task specific...
low_corr_neuron_ind = np.argsort(mean_topn)[:100]
def EDA2():
return
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--debug",
action='store_true',
help="Debug mode if flagged") # pos class num = 46
parser.add_argument("--override", action="store_true")
parser.add_argument("--evaluation_data",
default="SST-2",
help="The data set to activate the neuron")
args = parser.parse_args()
# args = parser.parse_args(["--debug"])
# Init logger
logger = getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(process)d-%(asctime)s-%(levelname)s-%(message)s',
datefmt='%d-%b-%y %H:%M:%S')
# Logging to stdout
s_handler = StreamHandler()
s_handler.setLevel(logging.INFO)
s_handler.setFormatter(formatter)
if (logger.hasHandlers()):
logger.handlers.clear()
logger.addHandler(s_handler)
# model_list = [
# "CoLA", "SST-2", "STS-B", "MNLI", "MRPC", "QNLI", "QQP", "RTE", "WNLI"
# ]
# calc_hid_rep(args, "BERT", "CoLA")
# calc_hid_rep(args, "BERT", "SST-2")
EDA()