File size: 23,013 Bytes
2d06dcc |
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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
from operator import mod
import torch
import torch.nn.functional as F
from torch import nn
from transformers import BertPreTrainedModel, BertModel, AutoModelForMaskedLM, BertForMaskedLM
from torch.nn.parameter import Parameter
from .utils import PairEnum
from sentence_transformers import SentenceTransformer
from losses import SupConLoss
activation_map = {'relu': nn.ReLU(), 'tanh': nn.Tanh()}
class Bert_SCCL(BertPreTrainedModel):
def __init__(self, config, args):
super(Bert_SCCL, self).__init__(config)
self.bert = None
self.contrast_head = None
self.cluster_centers = None
def init_model(self, cluster_centers=None, alpha=1.0):
self.emb_size = self.bert.config.hidden_size
self.alpha = alpha
# Instance-CL head
self.contrast_head = nn.Sequential(
nn.Linear(self.emb_size, self.emb_size),
nn.ReLU(inplace=True),
nn.Linear(self.emb_size, 128))
# Clustering head
initial_cluster_centers = torch.tensor(
cluster_centers, dtype=torch.float, requires_grad=True)
self.cluster_centers = Parameter(initial_cluster_centers)
def forward(self, input_ids, attention_mask, task_type):
if task_type == "evaluate":
return self.get_mean_embeddings(input_ids, attention_mask)
elif task_type == "explicit":
input_ids_1, input_ids_2, input_ids_3 = torch.unbind(input_ids, dim=1)
attention_mask_1, attention_mask_2, attention_mask_3 = torch.unbind(attention_mask, dim=1)
mean_output_1 = self.get_mean_embeddings(input_ids_1, attention_mask_1)
mean_output_2 = self.get_mean_embeddings(input_ids_2, attention_mask_2)
mean_output_3 = self.get_mean_embeddings(input_ids_3, attention_mask_3)
return mean_output_1, mean_output_2, mean_output_3
def get_mean_embeddings(self, input_ids, attention_mask):
bert_output = self.bert.forward(input_ids=input_ids, attention_mask=attention_mask)
attention_mask = attention_mask.unsqueeze(-1)
mean_output = torch.sum(bert_output[0]*attention_mask, dim=1) / torch.sum(attention_mask, dim=1)
return mean_output
def get_cluster_prob(self, embeddings):
norm_squared = torch.sum((embeddings.unsqueeze(1) - self.cluster_centers) ** 2, 2)
numerator = 1.0 / (1.0 + (norm_squared / self.alpha))
power = float(self.alpha + 1) / 2
numerator = numerator ** power
return numerator / torch.sum(numerator, dim=1, keepdim=True)
def local_consistency(self, embd0, embd1, embd2, criterion):
p0 = self.get_cluster_prob(embd0)
p1 = self.get_cluster_prob(embd1)
p2 = self.get_cluster_prob(embd2)
lds1 = criterion(p1, p0)
lds2 = criterion(p2, p0)
return lds1+lds2
def contrast_logits(self, embd1, embd2=None):
feat1 = F.normalize(self.contrast_head(embd1), dim=1)
if embd2 != None:
feat2 = F.normalize(self.contrast_head(embd2), dim=1)
return feat1, feat2
else:
return feat1
class BERT_MTP_Pretrain(nn.Module):
def __init__(self, args):
super(BERT_MTP_Pretrain, self).__init__()
self.num_labels = args.num_labels
self.bert = AutoModelForMaskedLM.from_pretrained(args.pretrained_bert_model)
self.dropout = nn.Dropout(0.1) #0.1
self.classifier = nn.Linear(args.feat_dim, args.num_labels)
def forward(self, X, ):
outputs = self.bert(**X, output_hidden_states=True)
CLSEmbedding = outputs.hidden_states[-1][:,0]
CLSEmbedding = self.dropout(CLSEmbedding)
logits = self.classifier(CLSEmbedding)
output_dir = {"logits": logits}
output_dir["hidden_states"] = outputs.hidden_states[-1][:, 0]
return output_dir
def mlmForward(self, X, Y = None):
outputs = self.bert(**X, labels = Y)
return outputs.loss
def loss_ce(self, logits, Y):
loss = nn.CrossEntropyLoss()
output = loss(logits, Y)
return output
class BERT_MTP(nn.Module):
def __init__(self, args):
super(BERT_MTP, self).__init__()
self.bert = AutoModelForMaskedLM.from_pretrained(args.pretrained_bert_model)
self.dropout = nn.Dropout(0.1)
#self.classifier = nn.Linear(args.feat_dim, args.num_labels)
self.head = nn.Sequential(
nn.Linear(args.feat_dim, args.feat_dim),
nn.ReLU(inplace=True),
nn.Dropout(0.1),
nn.Linear(args.feat_dim, args.mlp_head_feat_dim)
)
def forward(self, X):
"""logits are not normalized by softmax in forward function"""
outputs = self.bert(**X, output_hidden_states=True, output_attentions=True)
cls_embed = outputs.hidden_states[-1][:,0]
features = F.normalize(self.head(cls_embed), dim=1)
output_dir = {"features": features}
output_dir["hidden_states"] = cls_embed
return output_dir
def loss_cl(self, embds, label=None, mask=None, temperature=0.07, base_temperature=0.07, device=None):
"""compute contrastive loss"""
loss = SupConLoss()
output = loss(embds, labels=label, mask=mask, temperature = temperature, device=device)
return output
def save_backbone(self, save_path):
self.bert.save_pretrained(save_path)
class BERT_GCD(BertPreTrainedModel):
def __init__(self,config, args):
super(BERT_GCD, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.mlp_head = nn.Sequential(
nn.Linear(args.feat_dim, args.feat_dim),
nn.ReLU(inplace=True),
nn.Linear(args.feat_dim, args.mlp_head_feat_dim)
)
self.init_weights()
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None,
feature_ext = False, mode = None, loss_fct = None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
last_output_tokens = encoded_layer_12[-1]
features = last_output_tokens.mean(dim = 1)
return features
class BERT_CC(BertPreTrainedModel):
def __init__(self,config, args):
super(BERT_CC, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.cluster_num = args.num_labels
self.instance_projector = nn.Sequential(
nn.Linear(config.hidden_size, config.hidden_size),
nn.ReLU(),
nn.Linear(config.hidden_size, config.hidden_size),
)
self.cluster_projector = nn.Sequential(
nn.Linear(config.hidden_size, config.hidden_size),
nn.ReLU(),
nn.Linear(config.hidden_size, self.cluster_num),
nn.Softmax(dim=1)
)
self.init_weights()
def get_features(self, h_i, h_j):
z_i = F.normalize(self.instance_projector(h_i), dim=1)
z_j = F.normalize(self.instance_projector(h_j), dim=1)
c_i = self.cluster_projector(h_i)
c_j = self.cluster_projector(h_j)
return z_i, z_j, c_i, c_j
def forward_cluster(self, x):
c = self.cluster_projector(x)
c = torch.argmax(c, dim=1)
return c
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None,
feature_ext = False, mode = None, loss_fct = None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
last_output_tokens = encoded_layer_12[-1]
features = last_output_tokens.mean(dim = 1)
return features
class BERTForDeepAligned(BertPreTrainedModel):
def __init__(self,config, args):
super(BERTForDeepAligned, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None,
feature_ext = False, mode = None, loss_fct = None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
pooled_output = outputs.pooler_output
pooled_output = self.dense(encoded_layer_12[-1].mean(dim = 1))
pooled_output = self.activation(pooled_output)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
if feature_ext:
return pooled_output
else:
if mode == 'train':
loss = loss_fct(logits, labels)
return loss
else:
return pooled_output, logits
class BERT_USNID(BertPreTrainedModel):
def __init__(self, config, args):
super(BERT_USNID, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.args = args
if args.pretrain or (not args.wo_self):
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.mlp_head = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , feature_ext = False):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
last_output_tokens = encoded_layer_12[-1]
features = last_output_tokens.mean(dim = 1)
features = self.dense(features)
pooled_output = self.activation(features)
pooled_output = self.dropout(features)
if self.args.pretrain or (not self.args.wo_self):
logits = self.classifier(pooled_output)
mlp_outputs = self.mlp_head(pooled_output)
if feature_ext:
if self.args.pretrain or (not self.args.wo_self):
return features, logits
else:
return features, mlp_outputs
else:
if self.args.pretrain or (not self.args.wo_self):
return mlp_outputs, logits
else:
return mlp_outputs, mlp_outputs
class BERT_USNID_UNSUP(BertPreTrainedModel):
def __init__(self, config, args):
super(BERT_USNID_UNSUP, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.args = args
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.mlp_head = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None, weights = None,
feature_ext = False, mode = None, loss_fct = None, aug_feats=None, use_aug = False):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
last_output_tokens = encoded_layer_12[-1]
features = last_output_tokens.mean(dim = 1)
features = self.dense(features)
pooled_output = self.activation(features)
pooled_output = self.dropout(features)
logits = self.classifier(pooled_output)
mlp_outputs = self.mlp_head(pooled_output)
if feature_ext:
return features, mlp_outputs
else:
return mlp_outputs, logits
class BertForConstrainClustering(BertPreTrainedModel):
def __init__(self, config, args):
super(BertForConstrainClustering, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
# train
self.dense = nn.Linear(config.hidden_size, config.hidden_size) # Pooling-mean
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
# finetune
self.alpha = 1.0
self.cluster_layer = Parameter(torch.Tensor(args.num_labels, args.num_labels))
torch.nn.init.xavier_normal_(self.cluster_layer.data)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None,
feature_ext = False, u_threshold=None, l_threshold=None, mode=None, semi=False):
eps = 1e-10
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
pooled_output = outputs.pooler_output
pooled_output = self.dense(encoded_layer_12[-1].mean(dim = 1))
pooled_output = self.activation(pooled_output)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
if feature_ext:
return logits
else:
if mode=='train':
logits_norm = F.normalize(logits, p=2, dim=1)
sim_mat = torch.matmul(logits_norm, logits_norm.transpose(0, -1))
label_mat = labels.view(-1,1) - labels.view(1,-1)
label_mat[label_mat!=0] = -1
label_mat[label_mat==0] = 1
label_mat[label_mat==-1] = 0
if not semi:
pos_mask = (label_mat > u_threshold).type(torch.cuda.FloatTensor)
neg_mask = (label_mat < l_threshold).type(torch.cuda.FloatTensor)
pos_entropy = -torch.log(torch.clamp(sim_mat, eps, 1.0)) * pos_mask
neg_entropy = -torch.log(torch.clamp(1-sim_mat, eps, 1.0)) * neg_mask
loss = (pos_entropy.mean() + neg_entropy.mean()) * 5
return loss
else:
label_mat[labels==-1, :] = -1
label_mat[:, labels==-1] = -1
label_mat[label_mat==0] = 0
label_mat[label_mat==1] = 1
pos_mask = (sim_mat > u_threshold).type(torch.cuda.FloatTensor)
neg_mask = (sim_mat < l_threshold).type(torch.cuda.FloatTensor)
pos_mask[label_mat==1] = 1
neg_mask[label_mat==0] = 1
pos_entropy = -torch.log(torch.clamp(sim_mat, eps, 1.0)) * pos_mask
neg_entropy = -torch.log(torch.clamp(1-sim_mat, eps, 1.0)) * neg_mask
loss = pos_entropy.mean() + neg_entropy.mean() + u_threshold - l_threshold
return loss
else:
q = 1.0 / (1.0 + torch.sum(torch.pow(logits.unsqueeze(1) - self.cluster_layer, 2), 2) / self.alpha)
q = q.pow((self.alpha + 1.0) / 2.0)
q = (q.t() / torch.sum(q, 1)).t()
return logits, q
class BertForDTC(BertPreTrainedModel):
def __init__(self, config, args):
super(BertForDTC, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
#train
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
#finetune
self.alpha = 1.0
self.cluster_layer = Parameter(torch.Tensor(args.num_labels, args.num_labels))
torch.nn.init.xavier_normal_(self.cluster_layer.data)
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None,
feature_ext = False, mode = None, loss_fct=None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
pooled_output = outputs.pooler_output
pooled_output = self.dense(encoded_layer_12[-1].mean(dim = 1))
pooled_output = self.activation(pooled_output)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
if feature_ext:
return pooled_output
elif mode == 'train':
loss = loss_fct(logits, labels)
return loss
else:
q = 1.0 / (1.0 + torch.sum(torch.pow(logits.unsqueeze(1) - self.cluster_layer, 2), 2) / self.alpha)
q = q.pow((self.alpha + 1.0) / 2.0)
q = (q.t() / torch.sum(q, 1)).t()
return logits, q
class BertForKCL_Similarity(BertPreTrainedModel):
def __init__(self, config, args):
super(BertForKCL_Similarity,self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.dense = nn.Linear(config.hidden_size * 2, config.hidden_size * 4)
self.normalization = nn.BatchNorm1d(config.hidden_size * 4)
self.activation = activation_map[args.activation]
self.classifier = nn.Linear(config.hidden_size * 4, args.num_labels)
self.init_weights()
def forward(self, input_ids, token_type_ids = None, attention_mask=None, labels=None, loss_fct=None, mode = None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
pooled_output = outputs.pooler_output
feat1,feat2 = PairEnum(encoded_layer_12[-1].mean(dim = 1))
feature_cat = torch.cat([feat1,feat2], 1)
pooled_output = self.dense(feature_cat)
pooled_output = self.normalization(pooled_output)
pooled_output = self.activation(pooled_output)
logits = self.classifier(pooled_output)
if mode == 'train':
loss = loss_fct(logits.view(-1,self.num_labels), labels.view(-1))
return loss
else:
return pooled_output, logits
class BertForKCL(BertPreTrainedModel):
def __init__(self, config, args):
super(BertForKCL, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None, mode = None,
simi = None, loss_fct = None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
pooled_output = outputs.pooler_output
pooled_output = self.dense(encoded_layer_12[-1].mean(dim = 1))
pooled_output = self.activation(pooled_output)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
if mode == 'train':
probs = F.softmax(logits,dim=1)
prob1, prob2 = PairEnum(probs)
loss_KCL = loss_fct(prob1, prob2, simi)
flag = len(labels[labels != -1])
if flag != 0:
loss_ce = nn.CrossEntropyLoss()(logits[labels != -1], labels[labels != -1])
loss = loss_ce + loss_KCL
else:
loss = loss_KCL
return loss
else:
return pooled_output, logits
class BertForMCL(BertPreTrainedModel):
def __init__(self, config, args):
super(BertForMCL, self).__init__(config)
self.num_labels = args.num_labels
self.bert = BertModel(config)
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = activation_map[args.activation]
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, args.num_labels)
self.init_weights()
def forward(self, input_ids = None, token_type_ids = None, attention_mask=None , labels = None, mode = None, loss_fct = None):
outputs = self.bert(
input_ids, token_type_ids=token_type_ids, attention_mask=attention_mask, output_hidden_states=True)
encoded_layer_12 = outputs.hidden_states
pooled_output = outputs.pooler_output
pooled_output = self.dense(encoded_layer_12[-1].mean(dim = 1))
pooled_output = self.activation(pooled_output)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
probs = F.softmax(logits, dim = 1)
if mode == 'train':
flag = len(labels[labels != -1])
prob1, prob2 = PairEnum(probs)
simi = torch.matmul(probs, probs.transpose(0, -1)).view(-1)
simi[simi > 0.5] = 1
simi[simi < 0.5] = -1
loss_MCL = loss_fct(prob1, prob2, simi)
if flag != 0:
loss_ce = nn.CrossEntropyLoss()(logits[labels != -1], labels[labels != -1])
loss = loss_ce + loss_MCL
else:
loss = loss_MCL
return loss
else:
return pooled_output, logits |