forked from NVIDIA/sentiment-discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.py
403 lines (343 loc) · 16.2 KB
/
transfer.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import argparse
import os
import time
import math
import collections
import pickle as pkl
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.autograd import Variable
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from logreg_utils import train_logreg
from fp16 import FP16_Module, FP16_Optimizer
from reparameterization import apply_weight_norm, remove_weight_norm
from model import RNNFeaturizer, TransformerFeaturizer
from configure_data import configure_data
from arguments import add_general_args, add_model_args, add_classifier_model_args, add_sentiment_transfer_args
def get_data_and_args():
parser = argparse.ArgumentParser(description='PyTorch Sentiment Discovery Transfer Learning')
parser = add_general_args(parser)
parser = add_model_args(parser)
parser = add_classifier_model_args(parser)
data_config, data_parser, sentiment_transfer_parser, parser = add_sentiment_transfer_args(parser)
args = parser.parse_args()
args.cuda = torch.cuda.is_available()
if args.seed is not -1:
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
(train_data, val_data, test_data), tokenizer = data_config.apply(args)
args.data_size = tokenizer.num_tokens
args.padding_idx = tokenizer.command_name_map['pad'].Id
return (train_data, val_data, test_data), tokenizer, args
def get_model(args):
ntokens = args.data_size
concat_pools = [args.concat_max, args.concat_min, args.concat_mean]
if args.model.lower() == 'transformer':
model = TransformerFeaturizer(False, args)
else:
model = RNNFeaturizer(args.model, ntokens, args.emsize, args.nhid, args.nlayers,
0.0, args.all_layers, concat_pools, residuals=args.residuals)
if args.cuda:
model.cuda()
if args.fp16:
model.half()
if args.load is not None and args.load != '':
# load char embedding and recurrent encoder for featurization
with open(args.load, 'rb') as f:
sd = x = torch.load(f)
if 'sd' in sd:
sd = sd['sd']
if 'lm_encoder' in sd:
sd = sd['lm_encoder']
try:
model.load_state_dict(sd)
except:
# if state dict has weight normalized parameters apply and remove weight norm to model while loading sd
if hasattr(model, 'rnn'):
apply_weight_norm(model.rnn)
else:
apply_weight_norm(model)
model.load_state_dict(sd)
remove_weight_norm(model)
return model
def transform(model, text, args):
'''
Apply featurization `model` to extract features from text in data loader.
Featurization model should return cell state not hidden state.
`text` data loader should return tuples of ((text, text length), text label)
Returns labels and features for samples in text.
'''
model.eval()
features = np.array([])
labels = np.array([])
first_feature = True
def get_batch(batch):
'''
Process batch and return tuple of (text, text label, text length) long tensors.
Text is returned in column format with (time, batch) dimensions.
'''
text = batch['text'][0]
timesteps = batch['length']
labels = batch['label']
text = Variable(text).long()
timesteps = Variable(timesteps).long()
labels = Variable(labels).long()
if args.cuda:
text, timesteps, labels = text.cuda(), timesteps.cuda(), labels.cuda()
return text.t(), labels, timesteps-1
def get_outs(text_batch, length_batch):
if args.model.lower() == 'transformer':
cell_out, lm_or_encoder_out = model(text_batch, length_batch, args.get_hidden)
else:
model.rnn.reset_hidden(args.batch_size)
for _ in range(1 + args.num_hidden_warmup):
cell_out, lm_or_encoder_out = model(text_batch, length_batch, args.get_hidden)
return cell_out, lm_or_encoder_out
tstart = start = time.time()
n = 0
len_ds = len(text)
# Use no grad context for improving memory footprint/speed of inference
with torch.no_grad():
for i, data in tqdm(enumerate(text), total=len_ds, unit="batch", desc="transform", position=1, ncols=100):
text_batch, labels_batch, length_batch = get_batch(data)
# get batch size and reset hidden state with appropriate batch size
batch_size = text_batch.size(1)
n += batch_size
# extract batch of features from text batch
cell, _ = get_outs(text_batch, length_batch)
cell = cell.float()
if first_feature:
features = []
first_feature = False
labels = []
labels.append(labels_batch.data.cpu().numpy())
features.append(cell.data.cpu().numpy())
if not first_feature:
features = (np.concatenate(features))
labels = (np.concatenate(labels))
print('%0.3f seconds to transform %d examples' %
(time.time() - tstart, n))
return features, labels
def score_and_predict(model, X, Y):
'''
Given a binary classification model, predict output classification for numpy features `X`
and evaluate accuracy against labels `Y`. Labels should be numpy array of 0s and 1s.
Returns (accuracy, numpy array of classification probabilities)
'''
probs = model.predict_proba(X)[:, 1]
clf = probs > .5
accuracy = (np.squeeze(Y) == np.squeeze(clf)).mean()
return accuracy, probs
def get_top_k_neuron_weights(model, k=1):
"""
Get's the indices of the top weights based on the l1 norm contributions of the weights
based off of https://rakeshchada.github.io/Sentiment-Neuron.html interpretation of
https://arxiv.org/pdf/1704.01444.pdf (Radford et. al)
Args:
weights: numpy arraylike of shape `[d,num_classes]`
k: integer specifying how many rows of weights to select
Returns:
k_indices: numpy arraylike of shape `[k]` specifying indices of the top k rows
"""
weights = model.coef_.T
weight_penalties = np.squeeze(np.linalg.norm(weights, ord=1, axis=1))
if k == 1:
k_indices = np.array([np.argmax(weight_penalties)])
elif k >= np.log(len(weight_penalties)):
# runs O(nlogn)
k_indices = np.argsort(weight_penalties)[-k:][::-1]
else:
# runs O(n+klogk)
k_indices = np.argpartition(weight_penalties, -k)[-k:]
k_indices = (k_indices[np.argsort(weight_penalties[k_indices])])[::-1]
return k_indices
def plot_logits(save_root, X, Y_pred, top_neurons):
"""plot logits and save to appropriate experiment directory"""
save_root = os.path.join(save_root,'logit_vis')
if not os.path.exists(save_root):
os.makedirs(save_root)
print('plotting_logits at', save_root)
for i, n in enumerate(top_neurons):
plot_logit_and_save(trXt, trY, n, os.path.join(save_root, str(i)+'_'+str(n)))
def plot_logit_and_save(logits, labels, logit_index, name):
"""
Plots histogram (wrt to what label it is) of logit corresponding to logit_index.
Saves plotted histogram to name.
Args:
logits:
labels:
logit_index:
name:
"""
logit = logits[:,logit_index]
plt.title('Distribution of Logit Values')
plt.ylabel('# of logits per bin')
plt.xlabel('Logit Value')
plt.hist(logit[labels < .5], bins=25, alpha=0.5, label='neg')
plt.hist(logit[labels >= .5], bins=25, alpha=0.5, label='pos')
plt.legend()
plt.savefig(name+'.png')
plt.clf()
def plot_weight_contribs_and_save(coef, name):
plt.title('Values of Resulting L1 Penalized Weights')
plt.tick_params(axis='both', which='major')
coef = normalize(coef)
plt.plot(range(len(coef[0])), coef.T)
plt.xlabel('Neuron (Feature) Index')
plt.ylabel('Neuron (Feature) weight')
print('saving weight visualization to', name)
plt.savefig(name)
plt.clf()
def normalize(coef):
norm = np.linalg.norm(coef)
coef = coef/norm
return coef
def main():
(train_data, val_data, test_data), tokenizer, args = get_data_and_args()
model = get_model(args)
save_root = '' if args.load is None else args.load
save_root = save_root.replace('.current', '')
save_root = os.path.splitext(save_root)[0]
save_root += '_transfer'
save_root = os.path.join(save_root, args.save_results)
if not os.path.exists(save_root):
os.makedirs(save_root)
print('writing results to '+save_root)
# featurize train, val, test or use previously cached features if possible
print('transforming train')
if not (os.path.exists(os.path.join(save_root, 'trXt.npy')) and args.use_cached):
trXt, trY = transform(model, train_data, args)
np.save(os.path.join(save_root, 'trXt'), trXt)
np.save(os.path.join(save_root, 'trY'), trY)
else:
trXt = np.load(os.path.join(save_root, 'trXt.npy'))
trY = np.load(os.path.join(save_root, 'trY.npy'))
vaXt, vaY = None, None
if val_data is not None:
print('transforming validation')
if not (os.path.exists(os.path.join(save_root, 'vaXt.npy')) and args.use_cached):
vaXt, vaY = transform(model, val_data, args)
np.save(os.path.join(save_root, 'vaXt'), vaXt)
np.save(os.path.join(save_root, 'vaY'), vaY)
else:
vaXt = np.load(os.path.join(save_root, 'vaXt.npy'))
vaY = np.load(os.path.join(save_root, 'vaY.npy'))
teXt, teY = None, None
if test_data is not None:
print('transforming test')
if not (os.path.exists(os.path.join(save_root, 'teXt.npy')) and args.use_cached):
teXt, teY = transform(model, test_data, args)
np.save(os.path.join(save_root, 'teXt'), teXt)
np.save(os.path.join(save_root, 'teY'), teY)
else:
teXt = np.load(os.path.join(save_root, 'teXt.npy'))
teY = np.load(os.path.join(save_root, 'teY.npy'))
# train logistic regression model of featurized text against labels
start = time.time()
metric = 'mcc' if args.mcc else 'acc'
logreg_model, logreg_scores, logreg_probs, c, nnotzero = train_logreg(trXt, trY, vaXt, vaY, teXt, teY, max_iter=args.epochs, eval_test=not args.no_test_eval,
seed=args.seed, report_metric=metric, threshold_metric=metric)
end = time.time()
elapsed_time = end - start
with open(os.path.join(save_root, 'all_neurons_score.txt'), 'w') as f:
f.write(str(logreg_scores))
with open(os.path.join(save_root, 'all_neurons_probs.pkl'), 'wb') as f:
pkl.dump(logreg_probs, f)
with open(os.path.join(save_root, 'neurons.pkl'), 'wb') as f:
pkl.dump(logreg_model.coef_, f)
print('all neuron regression took %s seconds'%(str(elapsed_time)))
print(', '.join([str(score) for score in logreg_scores]), 'train, val, test accuracy for all neuron regression')
print(str(c)+' regularization coefficient used')
print(str(nnotzero) + ' features used in all neuron regression\n')
# save a sentiment classification pytorch model
sd = {}
if not args.fp16:
clf_sd = {'weight': torch.from_numpy(logreg_model.coef_).float(), 'bias': torch.from_numpy(logreg_model.intercept_).float()}
else:
clf_sd = {'weight': torch.from_numpy(logreg_model.coef_).half(), 'bias': torch.from_numpy(logreg_model.intercept_).half()}
sd['classifier'] = clf_sd
model.float().cpu()
sd['lm_encoder'] = model.state_dict()
with open(os.path.join(save_root, 'classifier.pt'), 'wb') as f:
torch.save(sd, f)
model.half()
sd['lm_encoder'] = model.state_dict()
with open(os.path.join(save_root, 'classifier.pt.16'), 'wb') as f:
torch.save(sd, f)
# extract sentiment neuron indices
sentiment_neurons = get_top_k_neuron_weights(logreg_model, args.neurons)
print('using neuron(s) %s as features for regression'%(', '.join([str(neuron) for neuron in list(sentiment_neurons.reshape(-1))])))
# train logistic regression model of features corresponding to sentiment neuron indices against labels
start = time.time()
logreg_neuron_model, logreg_neuron_scores, logreg_neuron_probs, neuron_c, neuron_nnotzero = train_logreg(trXt, trY, vaXt, vaY, teXt, teY, max_iter=args.epochs, eval_test=not args.no_test_eval,
seed=args.seed, neurons=sentiment_neurons, drop_neurons=args.drop_neurons,
report_metric=metric, threshold_metric=metric)
end = time.time()
if args.drop_neurons:
with open(os.path.join(save_root, 'dropped_neurons_score.txt'), 'w') as f:
f.write(str(logreg_neuron_scores))
with open(os.path.join(save_root, 'dropped_neurons_probs.pkl'), 'wb') as f:
pkl.dump(logreg_neuron_probs, f)
print('%d dropped neuron regression took %s seconds'%(args.neurons, str(end-start)))
print(', '.join([str(score) for score in logreg_neuron_scores]), 'train, val, test accuracy for %d dropped neuron regression'%(args.neurons))
print(str(neuron_c)+' regularization coefficient used')
start = time.time()
logreg_neuron_model, logreg_neuron_scores, logreg_neuron_probs, neuron_c, neuron_nnotzero = train_logreg(trXt, trY, vaXt, vaY, teXt, teY, max_iter=args.epochs, eval_test=not args.no_test_eval,
seed=args.seed, neurons=sentiment_neurons, report_metric=metric, threshold_metric=metric)
end = time.time()
print('%d neuron regression took %s seconds'%(args.neurons, str(end-start)))
print(', '.join([str(score) for score in logreg_neuron_scores]), 'train, val, test accuracy for %d neuron regression'%(args.neurons))
print(str(neuron_c)+' regularization coefficient used')
# log model accuracies, predicted probabilities, and weight/bias of regression model
with open(os.path.join(save_root, 'all_neurons_score.txt'), 'w') as f:
f.write(str(logreg_scores))
with open(os.path.join(save_root, 'neurons_score.txt'), 'w') as f:
f.write(str(logreg_neuron_scores))
with open(os.path.join(save_root, 'all_neurons_probs.pkl'), 'wb') as f:
pkl.dump(logreg_probs, f)
with open(os.path.join(save_root, 'neurons_probs.pkl'), 'wb') as f:
pkl.dump(logreg_neuron_probs, f)
with open(os.path.join(save_root, 'neurons.pkl'), 'wb') as f:
pkl.dump(logreg_model.coef_, f)
with open(os.path.join(save_root, 'neuron_bias.pkl'), 'wb') as f:
pkl.dump(logreg_model.intercept_, f)
#Plot feats
use_feats, use_labels = teXt, teY
if use_feats is None:
use_feats, use_labels = vaXt, vaY
if use_feats is None:
use_feats, use_labels = trXt, trY
try:
plot_logits(save_root, use_feats, use_labels, sentiment_neurons)
except:
print('no labels to plot logits for')
plot_weight_contribs_and_save(logreg_model.coef_, os.path.join(save_root, 'weight_vis.png'))
print('results successfully written to ' + save_root)
if args.write_results == '':
exit()
def get_csv_writer(feats, top_neurons, all_proba, neuron_proba):
"""makes a generator to be used in data_utils.datasets.csv_dataset.write()"""
header = ['prob w/ all', 'prob w/ %d neuron(s)'%(len(top_neurons),)]
top_feats = feats[:, top_neurons]
header += ['neuron %s'%(str(x),) for x in top_neurons]
yield header
for i, _ in enumerate(top_feats):
row = []
row.append(all_proba[i])
row.append(neuron_proba[i])
row.extend(list(top_feats[i].reshape(-1)))
yield row
data, use_feats = test_data, teXt
if use_feats is None:
data, use_feats = val_data, vaXt
if use_feats is None:
data, use_feats = train_data, trXt
csv_writer = get_csv_writer(use_feats, sentiment_neurons, logreg_probs[-1], logreg_neuron_probs[-1])
data.dataset.write(csv_writer, path=args.write_results)
if __name__ == '__main__':
main()