-
Notifications
You must be signed in to change notification settings - Fork 5
/
emotion_summarization.py
349 lines (280 loc) · 12.1 KB
/
emotion_summarization.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
from bert_score import score as bert_scr
import pandas as pd
from absl import app
from absl import flags
from datasets import load_metric
import json
import numpy as np
import os
import pandas as pd
import random
import torch
import torch.nn as nn
from datasets import load_dataset, Dataset
from transformers import AutoModel, AutoTokenizer, DataCollatorForSeq2Seq, BartForConditionalGeneration
from tqdm import tqdm
FLAGS = flags.FLAGS
flags.DEFINE_string("emotion", "anger", "Emotion type")
flags.DEFINE_string("training_path",
"data/train_val_test/train_anonymized_post.json",
"Path to training json")
flags.DEFINE_string("validation_path",
"data/train_val_test/val_anonymized_post.json",
"Path to validation json")
flags.DEFINE_string("test_path",
"data/train_val_test/test_anonymized_post.json",
"Path to test json")
flags.DEFINE_string("model", "facebook/bart-large-cnn", "Model ")
flags.DEFINE_string("results_summarization", "summarization", "")
flags.DEFINE_integer("batch_size", 2, "")
flags.DEFINE_integer("gradient_accumulation_steps", 8, "")
flags.DEFINE_float("learning_rate", 0.00005, "")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
rouge_metric = load_metric("rouge")
rouge_names = ["rouge1", "rouge2", "rougeL", "rougeLsum"]
def load_split_to_dataset(filepath, emotion):
with open(filepath) as f:
dataset = json.load(f)
positive_posts = []
summarizations = []
for k in dataset:
emo = 0
summ = []
for annotation in dataset[k]['Annotations']:
for hit in dataset[k]['Annotations'][annotation]:
if hit['Emotion'] == emotion:
emo = 1
summ.append(hit['Abstractive'])
if emo == 0:
continue
else:
for i in range(len(summ)):
positive_posts.append(dataset[k]['Post'])
summarizations.append(summ[i])
return positive_posts, summarizations
def load_test_split_to_dataset(filepath, emotion):
with open(filepath) as f:
dataset = json.load(f)
positive_posts = []
summarizations = []
for k in dataset:
emo = 0
summ = []
for annotation in dataset[k]['Annotations']:
for hit in dataset[k]['Annotations'][annotation]:
if hit['Emotion'] == emotion:
emo = 1
summ.append(hit['Abstractive'])
if emo == 0:
pass
else:
positive_posts.append(dataset[k]['Post'])
summarizations.append(summ)
return positive_posts, summarizations
def convert_positive_examples_to_features(example_batch):
input_encodings = tokenizer(example_batch["Posts"],
max_length=512,
truncation=True,
padding='max_length')
target_encodings = tokenizer(text_target=example_batch['Summaries'],
max_length=128,
truncation=True)
return {
"input_ids": input_encodings["input_ids"],
"attention_mask": input_encodings["attention_mask"],
"labels": target_encodings["input_ids"]
}
def convert_positive_test_examples_to_features(example_batch):
input_encodings = tokenizer(example_batch["Posts"],
max_length=512,
truncation=True,
padding='max_length')
for i in range(len(example_batch['Summary2'])):
if example_batch['Summary2'][i] == None:
example_batch['Summary2'][i] = ''
target_summaries1 = tokenizer(text_target=example_batch['Summary1'],
max_length=128,
truncation=True,
padding='max_length')
target_summaries2 = tokenizer(text_target=example_batch['Summary2'],
max_length=128,
truncation=True,
padding='max_length')
return {
"input_ids": input_encodings["input_ids"],
"attention_mask": input_encodings["attention_mask"],
"summary1": target_summaries1["input_ids"],
"summary2": target_summaries2["input_ids"]
}
def convert_negative_examples_to_features(example_batch):
input_encodings = tokenizer(example_batch["Posts"],
max_length=512,
padding='max_length',
truncation=True)
return {
"input_ids": input_encodings["input_ids"],
"attention_mask": input_encodings["attention_mask"]
}
def evaluate_summaries(dataset,
metric,
model,
tokenizer,
batch_size=8,
device=device,
column_text="Posts"):
article_batches = list(chunks(dataset[column_text], batch_size))
target_batches_s1 = list(chunks(dataset['Summary1'], batch_size))
target_batches_s2 = list(chunks(dataset['Summary2'], batch_size))
zped = list(zip(target_batches_s1, target_batches_s2))
fin_zip = []
for elem in zped:
inner_zip = list(zip(elem[0], elem[1]))
for i in range(len(inner_zip)):
if inner_zip[i][1] == '':
inner_zip[i] = [inner_zip[i][0]]
else:
inner_zip[i] = list(inner_zip[i])
fin_zip.append(inner_zip)
our_summaries = []
target_summaries = []
q = False
for article_batch, target_batch in tqdm(zip(article_batches, fin_zip),
total=len(article_batches)):
inputs = tokenizer(article_batch,
truncation=True,
padding="max_length",
return_tensors="pt")
summaries = model.generate(
input_ids=inputs["input_ids"].to(device),
attention_mask=inputs["attention_mask"].to(device),
length_penalty=0.8,
num_beams=8,
max_length=128)
decoded_summaries = [
tokenizer.decode(s,
skip_special_tokens=True,
clean_up_tokenization_spaces=True)
for s in summaries
]
decoded_summaries = [d.replace("<n>", " ") for d in decoded_summaries]
our_summaries += decoded_summaries
target_summaries += target_batch
metric.add_batch(predictions=decoded_summaries,
references=target_batch)
_, _, F1 = bert_scr(our_summaries,
target_summaries,
model_type='microsoft/deberta-xlarge-mnli',
lang='en',
verbose=True)
new_score = metric.compute()
return new_score['rougeL'].mid.fmeasure, F1.mean()
def chunks(list_of_elements, batch_size):
"""Yield successive batch-sized chunks from list_of_elements."""
for i in range(0, len(list_of_elements), batch_size):
yield list_of_elements[i:i + batch_size]
def prepare_dataset(positive_posts, summarizations):
df_positive = pd.DataFrame(list(zip(positive_posts, summarizations)),
columns=['Posts', 'Summaries'])
df_positive = Dataset.from_pandas(df_positive)
df_positive = df_positive.map(convert_positive_examples_to_features,
batched=True)
df_positive.set_format(type="torch",
columns=["input_ids", "labels", "attention_mask"])
return df_positive
def prepare_test_dataset(positive_posts, summarizations):
s1, s2 = [], []
for elem in summarizations:
if len(elem) == 2:
s1.append(elem[0])
s2.append(elem[1])
else:
s1.append(elem[0])
s2.append(None)
df_positive = pd.DataFrame(list(zip(positive_posts, s1, s2)),
columns=['Posts', 'Summary1', 'Summary2'])
df_positive = Dataset.from_pandas(df_positive)
df_positive = df_positive.map(convert_positive_test_examples_to_features,
batched=True)
df_positive.set_format(
type="torch",
columns=["input_ids", "summary1", "summary2", "attention_mask"])
return df_positive
def return_dataloaders(model, df_positive):
seq2seq_data_collator = DataCollatorForSeq2Seq(tokenizer,
model=model,
padding='longest')
train_positives = torch.utils.data.DataLoader(
df_positive,
batch_size=FLAGS.batch_size,
collate_fn=seq2seq_data_collator,
num_workers=4,
shuffle=True)
return train_positives
def return_test_dataloaders(df_positive):
train_positives = torch.utils.data.DataLoader(df_positive,
batch_size=FLAGS.batch_size,
num_workers=4,
shuffle=False)
return train_positives
def ev_once(model, df_positive):
model.eval()
rL, bert_score = evaluate_summaries(df_positive,
rouge_metric,
model,
tokenizer,
batch_size=8,
device=device)
model.train()
return rL, bert_score
def full_eval(model, df_test_positive, df_validation_positive, results, epoch):
rL, bert_score = ev_once(model, df_test_positive)
results[str(epoch) + '_test'] = (rL, float(bert_score.numpy()))
rL, bert_score = ev_once(model, df_validation_positive)
results[str(epoch) + '_validation'] = (rL, float(bert_score.numpy()))
with open(FLAGS.results_summarization + '_' + FLAGS.emotion + '.json',
'w') as f:
json.dump(results, f)
return rL, bert_score
def main(argv):
global tokenizer
train_positive_posts, train_summarizations = load_split_to_dataset(
FLAGS.training_path, FLAGS.emotion)
dev_positive_posts, dev_summarizations = load_test_split_to_dataset(
FLAGS.validation_path, FLAGS.emotion)
test_positive_posts, test_summarizations = load_test_split_to_dataset(
FLAGS.test_path, FLAGS.emotion)
tokenizer = AutoTokenizer.from_pretrained(FLAGS.model)
df_train_positive = prepare_dataset(train_positive_posts,
train_summarizations)
df_validation_positive = prepare_test_dataset(dev_positive_posts,
dev_summarizations)
df_test_positive = prepare_test_dataset(test_positive_posts,
test_summarizations)
model = BartForConditionalGeneration.from_pretrained(FLAGS.model)
model.to(device)
train_dataloader_positives = return_dataloaders(model, df_train_positive)
optimizer = torch.optim.Adam(model.parameters(), lr=FLAGS.learning_rate)
results = {}
full_eval(model, df_test_positive, df_validation_positive, results, 0)
for epoch in range(10):
ctr = 1
for data_positive in tqdm(train_dataloader_positives):
cuda_tensors_positives = {
key: data_positive[key].to(device)
for key in data_positive
}
seq2seqlmoutput = model(
input_ids=cuda_tensors_positives['input_ids'],
attention_mask=cuda_tensors_positives['attention_mask'],
labels=cuda_tensors_positives['labels'])
seq2seqlmoutput.loss.backward()
if ctr % FLAGS.gradient_accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
ctr += 1
optimizer.step()
optimizer.zero_grad()
full_eval(model, df_test_positive, df_validation_positive, results,
epoch + 1)
if __name__ == "__main__":
app.run(main)