-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
333 lines (256 loc) · 10.9 KB
/
training.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
# -*- coding: utf-8 -*-
"""Training
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1mqsFeFVF6wesNc3s-h1GE-xuTWYRymTP
"""
!pip install transformers
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import numpy as np
import spacy
import logging
logging.getLogger().setLevel(logging.CRITICAL)
import warnings
warnings.filterwarnings('ignore')
device = 'cpu'
if torch.cuda.is_available():
device = 'cuda'
tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium')
model = GPT2LMHeadModel.from_pretrained('gpt2-medium')
model = model.to(device)
from google.colab import drive
drive.mount('/content/drive')
file_path = '/content/drive/My Drive/490G1 Final Project/tweets.csv'
import pandas as pd
dataframe = pd.read_csv(file_path)
dataframe
dataframe = dataframe[['author', 'content']]
import re
def remove_line(text):
return re.sub(r'http\S+', '', text)
dataframe['content'] = dataframe['content'].apply(lambda x: remove_line(x))
name_set = set(dataframe['author'].tolist())
tokenizer(dataframe['content'].tolist()[12])
print(name_set)
for (idx, row) in dataframe.iterrows():
author = row.author
content = row.content.strip()
from torch.utils.data import Dataset
from torch.utils.data import Dataset, DataLoader
import os
import json
import csv
# only trump this time
class TrumpDataset(Dataset):
def __init__(self, file_path = '/content/drive/My Drive/490G1 Final Project/trump_tweet.txt'):
super().__init__()
# Trump part
# self.tweet_list = []
# self.end_of_text_token = "<|endoftext|>"
# self.person_list = ['realDonaldTrump']
# with open(file_path) as tweets:
# for line in tweets.readlines():
# line = line.strip('\n')
# self.tweet_list.append(f"realDonaldTrump:{line}{self.end_of_text_token}")
for (idx, row) in dataframe.iterrows():
author = row.author
content = row.content.strip()
if content == '':
continue
else:
self.person_list.append(str(author))
self.tweet_list.append(f"{author}:{content}{self.end_of_text_token}")
def __len__(self):
return len(self.tweet_list)
def __getitem__(self, item):
return self.tweet_list[item]
def get_person(self):
return self.person_list
from torch.utils.data import Dataset
from torch.utils.data import Dataset, DataLoader
import os
import json
import csv
# only trump this time
class TwitterDataset(Dataset):
def __init__(self, author_name):
super().__init__()
self.tweet_list = []
self.end_of_text_token = "<|endoftext|>"
for (idx, row) in dataframe.iterrows():
author = row.author
content = row.content.strip()
if content == '' and author != author_name:
continue
else:
self.tweet_list.append(f"{author}:{content}{self.end_of_text_token}")
def __len__(self):
return len(self.tweet_list)
def __getitem__(self, item):
return self.tweet_list[item]
dataset = TrumpDataset()
trump_loader = DataLoader(dataset, batch_size=8, shuffle=True)
BATCH_SIZE = 16
EPOCHS = 5
LEARNING_RATE = 3e-5
WARMUP_STEPS = 2000
TRAINING_STEPS = -1
MAX_SEQ_LEN = 400
from transformers import AdamW, get_linear_schedule_with_warmup
device = 'cpu'
if torch.cuda.is_available():
device = 'cuda'
from tqdm import tqdm
model = model.to(device)
model.train()
optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=WARMUP_STEPS, num_training_steps=TRAINING_STEPS)
proc_seq_count = 0
sum_loss = 0.0
batch_count = 0
tmp_trump_tens = None
models_folder = "/content/drive/My Drive/490G1 Final Project/trained_models"
if not os.path.exists(models_folder):
os.mkdir(models_folder)
for epoch in range(EPOCHS):
print(f"EPOCH {epoch} started" + '=' * 30)
for idx,tweet in tqdm(enumerate(trump_loader)):
trump_tens = torch.tensor(tokenizer.encode(tweet[0])).unsqueeze(0).to(device)
#Skip sample from dataset if it is longer than MAX_SEQ_LEN
if trump_tens.size()[1] > MAX_SEQ_LEN:
continue
#The first trump sequence in the sequence
if not torch.is_tensor(tmp_trump_tens):
tmp_trump_tens = trump_tens
continue
else:
#The next joke does not fit in so we process the sequence and leave the last joke
#as the start for next sequence
if tmp_trump_tens.size()[1] + trump_tens.size()[1] > MAX_SEQ_LEN:
work_trump_tens = tmp_trump_tens
tmp_trump_tens = trump_tens
else:
#Add the joke to sequence, continue and try to add more
tmp_trump_tens = torch.cat([tmp_trump_tens, trump_tens[:,1:]], dim=1)
continue
################## Sequence ready, process it trough the model ##################
outputs = model(work_trump_tens, labels=work_trump_tens)
loss, logits = outputs[:2]
loss.backward()
sum_loss = sum_loss + loss.detach().data
proc_seq_count = proc_seq_count + 1
if proc_seq_count == BATCH_SIZE:
proc_seq_count = 0
batch_count += 1
optimizer.step()
scheduler.step()
optimizer.zero_grad()
model.zero_grad()
if batch_count == 1:
print(f"sum loss {sum_loss}")
batch_count = 0
sum_loss = 0.0
# Store the model after each epoch to compare the performance of them
torch.save(model.state_dict(), os.path.join(models_folder, f"gpt2_medium_trump_{epoch}.pt"))
from tqdm import tqdm
EPOCHS = 15
for id, name in enumerate(name_set):
dataset = TwitterDataset(name)
data_loader = DataLoader(dataset, batch_size=8, shuffle=True)
model = GPT2LMHeadModel.from_pretrained('gpt2-medium')
model = model.to(device)
model.train()
optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=WARMUP_STEPS, num_training_steps=TRAINING_STEPS)
proc_seq_count = 0
sum_loss = 0.0
batch_count = 0
tmp_trump_tens = None
models_folder = "/content/drive/My Drive/490G1 Final Project/trained_models"
if not os.path.exists(models_folder):
os.mkdir(models_folder)
for epoch in range(EPOCHS):
print(f"EPOCH {epoch} for {id}{name} started" + '=' * 30)
for idx,tweet in tqdm(enumerate(data_loader)):
trump_tens = torch.tensor(tokenizer.encode(tweet[0])).unsqueeze(0).to(device)
#Skip sample from dataset if it is longer than MAX_SEQ_LEN
if trump_tens.size()[1] > MAX_SEQ_LEN:
continue
#The first trump sequence in the sequence
if not torch.is_tensor(tmp_trump_tens):
tmp_trump_tens = trump_tens
continue
else:
#The next joke does not fit in so we process the sequence and leave the last joke
#as the start for next sequence
if tmp_trump_tens.size()[1] + trump_tens.size()[1] > MAX_SEQ_LEN:
work_trump_tens = tmp_trump_tens
tmp_trump_tens = trump_tens
else:
#Add the joke to sequence, continue and try to add more
tmp_trump_tens = torch.cat([tmp_trump_tens, trump_tens[:,1:]], dim=1)
continue
################## Sequence ready, process it trough the model ##################
outputs = model(work_trump_tens, labels=work_trump_tens)
loss, logits = outputs[:2]
loss.backward()
sum_loss = sum_loss + loss.detach().data
proc_seq_count = proc_seq_count + 1
if proc_seq_count == BATCH_SIZE:
proc_seq_count = 0
batch_count += 1
optimizer.step()
scheduler.step()
optimizer.zero_grad()
model.zero_grad()
if batch_count == 1:
print(f"sum loss {sum_loss}")
batch_count = 0
sum_loss = 0.0
# Store the model after each epoch to compare the performance of them
torch.save(model.state_dict(), os.path.join(models_folder, f"gpt2_medium_{name}_final.pt"))
def choose_from_top(probs, n=5):
ind = np.argpartition(probs, -n)[-n:]
top_prob = probs[ind]
top_prob = top_prob / np.sum(top_prob) # Normalize
choice = np.random.choice(n, 1, p = top_prob)
token_id = ind[choice][0]
return int(token_id)
MODEL_EPOCH = 4
person_list = list(set(dataset.get_person()))
models_folder = "/content/drive/My Drive/490G1 Final Project/trained_models"
model_path = os.path.join(models_folder, f"gpt2_medium_trump_{MODEL_EPOCH}.pt")
model.load_state_dict(torch.load(model_path))
trump_output_file_path = f'generated_trump_tweet_{MODEL_EPOCH}.txt'
output_file_path = f'generated_tweet_{MODEL_EPOCH}.txt'
model.eval()
if os.path.exists(trump_output_file_path):
os.remove(trump_output_file_path)
tweet_num = 0
with torch.no_grad():
for person in person_list:
for tweet_idx in range(10):
tweet_finished = False
cur_ids = torch.tensor(tokenizer.encode(f'{person}:')).unsqueeze(0).to(device)
for i in range(100):
outputs = model(cur_ids, labels=cur_ids)
loss, logits = outputs[:2]
softmax_logits = torch.softmax(logits[0,-1], dim=0) #Take the first(from only one in this case) batch and the last predicted embedding
if i < 3:
n = 20
else:
n = 3
next_token_id = choose_from_top(softmax_logits.to('cpu').numpy(), n=n) #Randomly(from the topN probability distribution) select the next word
cur_ids = torch.cat([cur_ids, torch.ones((1,1)).long().to(device) * next_token_id], dim = 1) # Add the last word to the running sequence
if next_token_id in tokenizer.encode('<|endoftext|>'):
tweet_finished = True
break
if tweet_finished:
tweet_num = tweet_num + 1
output_list = list(cur_ids.squeeze().to('cpu').numpy())
output_text = tokenizer.decode(output_list)
#print(f"{tweet_num}:{output_text} \n")
with open(trump_output_file_path, 'a') as f:
print(f"{tweet_num}:{output_text} \n")
f.write(f"{tweet_num}:{output_text} \n")