-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuna.py
194 lines (164 loc) · 8.79 KB
/
una.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
import copy
import logging
from loguru import logger
import os
import numpy as np
import torch
import torch.nn.functional as F
from scipy.stats import spearmanr
from torch.cuda.amp import GradScaler
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from utils import save_config_file
'''
thanks to: https://github.com/vdogmcgee/SimCSE-Chinese-Pytorch/blob/main/simcse_unsup.py
'''
class SimCSE(object):
def __init__(self, *args, **kwargs):
self.args = kwargs['args']
self.model = kwargs['model'].to(self.args.device)
self.optimizer = kwargs['optimizer']
self.scheduler = kwargs['scheduler']
self.tokenizer = kwargs['tokenizer']
if self.args.do_train:
os.makedirs(self.args.output_path, exist_ok=True)
log_dir = self.args.output_path
if not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
self.writer = SummaryWriter(log_dir=log_dir)
logging.basicConfig(filename=os.path.join(self.writer.log_dir, 'training.log'), level=logging.DEBUG)
else:
pass
# bsz : batch size (number of positive pairs)
# d : latent dim
# x : Tensor, shape=[bsz, d]
# latents for one side of positive pairs
# y : Tensor, shape=[bsz, d]
# latents for the other side of positive pairs
# Thanks to https://github.com/SsnL/align_uniform
def simcse_unsup_loss(self, y_pred: 'tensor') -> 'tensor':
"""
loss function for self-supervised training
y_pred (tensor): bert output, [batch_size * 2, 768]
"""
# Get the label for each prediction, [1, 0, 3, 2, ..., batch_size-1, batch_size-2]
y_true = torch.arange(y_pred.shape[0], device=self.args.device)
labels = (y_true - y_true % 2 * 2) + 1
# Calculate the similarity matrix
sim = F.cosine_similarity(y_pred.unsqueeze(1), y_pred.unsqueeze(0), dim=-1)
# Discard the diagnosis (we don't want the self-similarity)
sim = sim - torch.eye(y_pred.shape[0], device=self.args.device) * 1e12
# similarity divide by temperature
logits = sim / self.args.temperature
return logits, labels
def get_perm(self, x):
"""get random permutation"""
size = x.size()[0]
if self.args.device == "cuda":
index = torch.randperm(size).cuda()
else:
index = torch.randperm(size)
return index
def tokenize(self, data, step):
anchor = data["anchor"]
positive = data["positive"]
if step % self.args.aug_every_n_steps == 0:
if self.args.do_tf_idf and self.args.do_hard_negatives:
anchor_hn = data["anchor_hn"]
positive_hn = data["positive_hn"]
sample = [self.tokenizer([anchor[i], positive[i], anchor_hn[i], positive_hn[i]],
max_length=self.args.max_len,
truncation=True,
padding="max_length",
return_tensors='pt') for i in range(len(anchor))]
return sample
sample = [self.tokenizer([anchor[i], positive[i]],
max_length=self.args.max_len,
truncation=True,
padding="max_length",
return_tensors='pt') for i in range(len(anchor))]
return sample
def train(self, train_loader, eval_loader):
logger.info("start training")
self.model.train()
device = self.args.device
best = 0
best_step = 0
total_steps = len(train_loader)*self.args.epochs
scaler = GradScaler(enabled=self.args.fp16_precision)
save_config_file(self.writer.log_dir, self.args)
logging.info(f"Start SimCSE training for {self.args.epochs} epochs.")
logging.info(f"Training with gpu: {self.args.disable_cuda}.")
for epoch in range(self.args.epochs):
pbar = tqdm(train_loader)
for batch_idx, sample in enumerate(pbar):
step = epoch * len(train_loader) + batch_idx
# [batch, n, seq_len] -> [batch * n, sql_len]
data = self.tokenize(sample, step)
input_ids = torch.cat([data[i]['input_ids'] for i in range(len(data))]).to(device)
attention_mask = torch.cat([data[i]['attention_mask'] for i in range(len(data))]).to(device)
if self.args.arch == 'roberta':
out = self.model(input_ids, attention_mask=attention_mask)
else:
token_type_ids = torch.cat([data[i]['token_type_ids'] for i in range(len(data))]).to(device)
out = self.model(input_ids, attention_mask, token_type_ids)
logits, labels = self.simcse_unsup_loss(out)
loss = F.cross_entropy(logits, labels)
self.optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(self.optimizer)
scaler.update()
step += 1
self.scheduler.step()
pbar.set_description(
"Progress: {:.2%} | Loss: {:.3} | lr: {:.3}".format(step / total_steps, loss, self.scheduler.get_last_lr()[0]))
if (step) % self.args.log_every_n_steps == 0:
corrcoef = self.evaluate(model=self.model, dataloader=eval_loader)
logger.info('loss:{:.3}, corrcoef: {:.3} in step {} epoch {}'.format(loss, corrcoef, step, epoch + 1))
logging.debug(
"Progress:{:.2%}\tEpoch: {}\tStep: {}\tlr: {:.3}\tLoss: {:.4}\tcorrcoef: {:.4}\tBest score {:.4} at step: {}".format(
step / total_steps, epoch, step, self.scheduler.get_last_lr()[0], loss, corrcoef, float(best), best_step))
self.writer.add_scalar('loss', loss, step)
self.writer.add_scalar('corrcoef', corrcoef, step)
self.model.train()
# Save the model if the evalaute result outperform the previous
if best < corrcoef:
best = corrcoef
best_step = step
if self.args.save_data:
torch.save(self.model.state_dict(), os.path.join(self.args.output_path, 'simcse.pt'))
checkpoint_name = 'checkpoint_best'
try:
self.model.model.save_pretrained(os.path.join(self.writer.log_dir, checkpoint_name))
except:
self.model.module.model.save_pretrained(os.path.join(self.writer.log_dir, checkpoint_name))
logger.info('higher corrcoef: {}'.format(best))
logging.debug(f"Best corrcoef {best} at step: {best_step}")
def evaluate(self, model, dataloader):
model.eval()
sim_tensor = torch.tensor([], device=self.args.device)
label_array = np.array([])
with torch.no_grad():
for source, target, label in dataloader:
# source [batch, 1, seq_len] -> [batch, seq_len]
source_input_ids = source.get('input_ids').squeeze(1).to(self.args.device)
source_attention_mask = source.get('attention_mask').squeeze(1).to(self.args.device)
if self.args.arch == 'roberta':
source_pred = model(source_input_ids, source_attention_mask, is_train=False)
else:
source_token_type_ids = source.get('token_type_ids').squeeze(1).to(self.args.device)
source_pred = model(source_input_ids, source_attention_mask, source_token_type_ids, is_train=False)
# target [batch, 1, seq_len] -> [batch, seq_len]
target_input_ids = target.get('input_ids').squeeze(1).to(self.args.device)
target_attention_mask = target.get('attention_mask').squeeze(1).to(self.args.device)
if self.args.arch == 'roberta':
target_pred = model(target_input_ids, target_attention_mask, is_train=False)
else:
target_token_type_ids = target.get('token_type_ids').squeeze(1).to(self.args.device)
target_pred = model(target_input_ids, target_attention_mask, target_token_type_ids, is_train=False)
# concat
sim = F.cosine_similarity(source_pred, target_pred, dim=-1)
sim_tensor = torch.cat((sim_tensor, sim), dim=0)
label_array = np.append(label_array, np.array(label))
# corrcoef
return spearmanr(label_array, sim_tensor.cpu().numpy()).correlation