-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
154 lines (113 loc) · 4.54 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
import math
import numpy as np
import pandas as pd
import random
import re
import tqdm
import os
import argparse
import torch
import urllib.request
from torch.utils.data import DataLoader, Dataset
from transformers import PreTrainedTokenizerFast
from transformers import GPT2LMHeadModel
from transformers.optimization import AdamW, get_cosine_schedule_with_warmup
parser = argparse.ArgumentParser()
parser.add_argument("--traindata", type=str, help="train data")
parser.add_argument("--pretrain", type=str, help="pretrain weight")
parser.add_argument("--epoch", type=int, help="epoch")
args = parser.parse_args()
Chatbot_Data = pd.read_csv("args.traindata")
Chatbot_Data = Chatbot_Data.dropna(axis=0, how="any")
BOS = "</s>"
EOS = "</s>"
UNK = "<unk>"
PAD = "<pad>"
MASK = "<unused0>"
ENTER = "<ENTER>"
Q_TKN = "<usr>"
A_TKN = "<sys>"
SENT = "<unused1>"
tokenizer = PreTrainedTokenizerFast.from_pretrained("skt/kogpt2-base-v2",
bos_token=BOS, eos_token=EOS, unk_token=UNK, pad_token=PAD, mask_token=MASK)
class ChatbotDataset(Dataset):
def __init__(self, chats, max_len=60):
self._data = chats
self.max_len = max_len
self.q_token = Q_TKN
self.a_token = A_TKN
self.sent_token = SENT
self.eos = EOS
self.mask = MASK
self.tokenizer = tokenizer
def __len__(self):
return len(self._data)
def __getitem__(self, idx):
turn = self._data.iloc[idx]
q = turn["Q"]
a = turn["A"]
q_toked = self.tokenizer.tokenize(self.q_token + q + self.sent_token)
q_len = len(q_toked)
a_toked = self.tokenizer.tokenize(self.a_token + a + self.eos)
a_len = len(a_toked)
if q_len > self.max_len:
a_len = self.max_len - q_len
if a_len <= 0:
q_toked = q_toked[-(int(self.max_len / 2)):]
q_len = len(q_toked)
a_len = self.max_len - q_len
a_toked = a_toked[:a_len]
a_len = len(a_toked)
if q_len + a_len > self.max_len:
a_len = self.max_len - q_len
if a_len <= 0:
q_toked = q_toked[-(int(self.max_len / 2)):]
q_len = len(q_toked)
a_len = self.max_len - q_len
a_toked = a_toked[:a_len]
a_len = len(a_toked)
labels = [self.mask,] * q_len + a_toked[1:]
mask = [0] * q_len + [1] * a_len + [0] * (self.max_len - q_len - a_len)
labels_ids = self.tokenizer.convert_tokens_to_ids(labels)
while len(labels_ids) < self.max_len:
labels_ids += [self.tokenizer.pad_token_id]
token_ids = self.tokenizer.convert_tokens_to_ids(q_toked + a_toked)
while len(token_ids) < self.max_len:
token_ids += [self.tokenizer.pad_token_id]
return (token_ids, np.array(mask), labels_ids)
def collate_batch(batch):
data = [item[0] for item in batch]
mask = [item[1] for item in batch]
label = [item[2] for item in batch]
return torch.LongTensor(data), torch.LongTensor(mask), torch.LongTensor(label)
train_set = ChatbotDataset(Chatbot_Data, max_len=60)
train_dataloader = DataLoader(train_set, batch_size=128, num_workers=2, shuffle=True, collate_fn=collate_batch)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = GPT2LMHeadModel.from_pretrained("skt/kogpt2-base-v2")
if args.pretrain:
state_dict = torch.load("args.pretrain")
new_state_dict = {}
for k, v in state_dict.items():
name = k[7:] if k.startswith("module.") else k
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model = torch.nn.DataParallel(model)
model = model.to(device)
learning_rate = 3e-5
criterion = torch.nn.CrossEntropyLoss(reduction="none")
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
Sneg = -1e18
for epoch in range(args.epoch):
for token_ids, mask, label in tqdm.tqdm(train_dataloader):
token_ids, mask, label = token_ids.to(device), mask.to(device), label.to(device)
out = model(token_ids)
out = out.logits
mask_3d = mask.unsqueeze(dim=2).repeat_interleave(repeats=out.shape[2], dim=2)
mask_out = torch.where(mask_3d == 1, out, Sneg * torch.ones_like(out))
loss = criterion(mask_out.transpose(2, 1), label)
avg_loss = loss.sum() / mask.sum()
optimizer.zero_grad()
avg_loss.backward()
optimizer.step()
torch.save(model.state_dict(), f"./model_weight/train/fintuning_{epoch}.pt")
print("LOSS", avg_loss)