-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
180 lines (141 loc) · 7.64 KB
/
model.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
from transformers import AutoModel, AutoConfig
import torch.nn as nn
import torch
import torch.functional as F
import numpy as np
class AttentionModel(nn.Module):
def __init__(self, checkpoint_base, checkpoint_sentiment, num_labels, freeze_base=True, device='cuda'):
super(AttentionModel,self).__init__()
self.num_labels = num_labels
#Load Model with given checkpoint and extract its body
base_config = AutoConfig.from_pretrained(checkpoint_base, output_attentions=False,output_hidden_states=False)
self.model_base = AutoModel.from_pretrained(checkpoint_base,config=base_config)
sentiment_config = AutoConfig.from_pretrained(checkpoint_sentiment, output_attentions=False,output_hidden_states=False)
self.model_sentiment = AutoModel.from_pretrained(checkpoint_sentiment,config=sentiment_config)
# Attention Layer to combine both outputs
self.attn_layer = AttentionLayer()
# Fully Connected Layer for classification
self.ffn_layer = nn.Linear(768, 1)
# Loss function
self.loss_fn = nn.BCEWithLogitsLoss()
if freeze_base:
for name, param in self.model_base.named_parameters():
param.requires_grad = False
for name, param in self.model_sentiment.named_parameters():
param.requires_grad = False
def forward(self, input_ids=None, attention_mask=None, labels=None, loss=True):
#Extract outputs from the body
outputs_base = self.model_base(input_ids=input_ids, attention_mask=attention_mask)['last_hidden_state']
outputs_sentiment = self.model_sentiment(input_ids=input_ids, attention_mask=attention_mask)['last_hidden_state']
x = self.attn_layer(outputs_base, outputs_sentiment, attention_mask)
x = self.ffn_layer(x)
if loss:
x = self.loss_fn(x, labels.unsqueeze(1).float())
return x
class AttentionLayer(nn.Module):
def __init__(self, token_dim=768, cross_dim=768):
super(AttentionLayer, self).__init__()
self.cross_token_param = nn.Linear(token_dim * 2, cross_dim)
self.ind_token_param = nn.Linear(token_dim * 2, 1)
self.att_token_param = torch.nn.Parameter(torch.FloatTensor(cross_dim, 1), requires_grad=True)
torch.nn.init.xavier_uniform(self.att_token_param)
def masked_softmax(self, x, mask, dim=1):
x_masked = x.clone()
x_masked[mask == 0] = -float("inf")
return torch.softmax(x_masked, dim=dim)
def forward(self, outputs_base, outputs_sentiment, attention_mask=None):
# Concatenate the outputs of both models
hidden_concat = torch.cat((outputs_base, outputs_sentiment), dim=2)
# Apply cross-token attention
cross_token = torch.tanh(self.cross_token_param(hidden_concat))
# Apply individual token attention
ind_token = torch.sigmoid(self.ind_token_param(hidden_concat))
# Apply attention weights
att_token = torch.squeeze(torch.matmul(cross_token, self.att_token_param))
# Apply softmax to get attention weights and apply attention mask
att_token = self.masked_softmax(att_token, attention_mask, 1)
# Expand attention weights to match the hidden states dimension
att_token = att_token.unsqueeze(2)
att_token = att_token.expand(-1, -1, 768)
# Get the inter token representation
inter_token = (1 - ind_token) * outputs_base + ind_token * outputs_sentiment
# Apply attention weights to the hidden states
final_representation = torch.mul(att_token, inter_token).sum(dim=1)
return final_representation
class AttentionModelSingle(nn.Module):
def __init__(self, checkpoint_base, num_labels, freeze_base=True, device='cuda'):
super(AttentionModelSingle,self).__init__()
self.num_labels = num_labels
#Load Model with given checkpoint and extract its body
base_config = AutoConfig.from_pretrained(checkpoint_base, output_attentions=False,output_hidden_states=False)
self.model_base = AutoModel.from_pretrained(checkpoint_base,config=base_config)
# Attention Layer to combine both outputs
self.attn_layer = AttentionLayerSingle()
# Fully Connected Layer for classification
self.ffn_layer = nn.Linear(768, 1)
# Loss function
self.loss_fn = nn.BCEWithLogitsLoss()
if freeze_base:
for name, param in self.model_base.named_parameters():
param.requires_grad = False
def forward(self, input_ids=None, attention_mask=None, labels=None, loss=True):
#Extract outputs from the body
outputs_base = self.model_base(input_ids=input_ids, attention_mask=attention_mask)['last_hidden_state']
x = self.attn_layer(outputs_base, attention_mask)
x = self.ffn_layer(x)
if loss:
x = self.loss_fn(x, labels.unsqueeze(1).float())
return x
class AttentionLayerSingle(nn.Module):
def __init__(self, token_dim=768, cross_dim=768):
super(AttentionLayerSingle, self).__init__()
self.cross_token_param = nn.Linear(token_dim, cross_dim)
self.att_token_param = torch.nn.Parameter(torch.FloatTensor(cross_dim, 1), requires_grad=True)
torch.nn.init.xavier_uniform(self.att_token_param)
def masked_softmax(self, x, mask, dim=1):
x_masked = x.clone()
x_masked[mask == 0] = -float("inf")
return torch.softmax(x_masked, dim=dim)
def forward(self, outputs_base, attention_mask=None):
# Apply cross-token attention
cross_token = torch.tanh(self.cross_token_param(outputs_base))
# Apply attention weights
att_token = torch.squeeze(torch.matmul(cross_token, self.att_token_param))
# Apply softmax to get attention weights and apply attention mask
att_token = self.masked_softmax(att_token, attention_mask, 1)
# Expand attention weights to match the hidden states dimension
att_token = att_token.unsqueeze(2)
att_token = att_token.expand(-1, -1, 768)
# Apply attention weights to the hidden states
final_representation = torch.mul(att_token, outputs_base).sum(dim=1)
return final_representation
class TwoCLSModel(nn.Module):
def __init__(self, checkpoint_base, checkpoint_sentiment, num_labels, freeze_base=True, device='cuda'):
super(TwoCLSModel,self).__init__()
self.num_labels = num_labels
#Load Model with given checkpoint and extract its body
base_config = AutoConfig.from_pretrained(checkpoint_base, output_attentions=False,output_hidden_states=False)
self.model_base = AutoModel.from_pretrained(checkpoint_base,config=base_config)
sentiment_config = AutoConfig.from_pretrained(checkpoint_sentiment, output_attentions=False,output_hidden_states=False)
self.model_sentiment = AutoModel.from_pretrained(checkpoint_sentiment,config=sentiment_config)
# Fully Connected Layer for classification
self.ffn_layer = nn.Linear(2*768, 768)
self.ffn_layer2 = nn.Linear(768, 1)
# Loss function
self.loss_fn = nn.BCEWithLogitsLoss()
if freeze_base:
for name, param in self.model_base.named_parameters():
param.requires_grad = False
for name, param in self.model_sentiment.named_parameters():
param.requires_grad = False
def forward(self, input_ids=None, attention_mask=None, labels=None, loss=True):
#Extract outputs from the body
outputs_base = self.model_base(input_ids=input_ids, attention_mask=attention_mask)['last_hidden_state'][:,0,:] #CLS token
outputs_sentiment = self.model_sentiment(input_ids=input_ids, attention_mask=attention_mask)['last_hidden_state'][:,0,:] #CLS token
# Concatenate the outputs of both models
x = torch.cat((outputs_base, outputs_sentiment), dim=1)
x = torch.sigmoid(self.ffn_layer(x))
x = self.ffn_layer2(x)
if loss:
x = self.loss_fn(x, labels.unsqueeze(1).float())
return x