-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel.py
114 lines (90 loc) · 3.53 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
import torch
import param
import torch.nn as nn
from transformers import BertModel, DistilBertModel, RobertaModel
class BertEncoder(nn.Module):
def __init__(self):
super(BertEncoder, self).__init__()
self.encoder = BertModel.from_pretrained('bert-base-uncased')
def forward(self, x, mask=None):
outputs = self.encoder(x, attention_mask=mask)
feat = outputs[1]
return feat
class DistilBertEncoder(nn.Module):
def __init__(self):
super(DistilBertEncoder, self).__init__()
self.encoder = DistilBertModel.from_pretrained('distilbert-base-uncased')
self.pooler = nn.Linear(param.hidden_size, param.hidden_size)
def forward(self, x, mask=None):
outputs = self.encoder(x, attention_mask=mask)
hidden_state = outputs[0]
pooled_output = hidden_state[:, 0]
feat = self.pooler(pooled_output)
return feat
class RobertaEncoder(nn.Module):
def __init__(self):
super(RobertaEncoder, self).__init__()
self.encoder = RobertaModel.from_pretrained('roberta-base')
def forward(self, x, mask=None):
outputs = self.encoder(x, attention_mask=mask)
sequence_output = outputs[0]
feat = sequence_output[:, 0, :]
return feat
class DistilRobertaEncoder(nn.Module):
def __init__(self):
super(DistilRobertaEncoder, self).__init__()
self.encoder = RobertaModel.from_pretrained('distilroberta-base')
self.pooler = nn.Linear(param.hidden_size, param.hidden_size)
def forward(self, x, mask=None):
outputs = self.encoder(x, attention_mask=mask)
sequence_output = outputs[0]
feat = sequence_output[:, 0, :]
return feat
class BertClassifier(nn.Module):
def __init__(self, dropout=0.1):
super(BertClassifier, self).__init__()
self.dropout = nn.Dropout(p=dropout)
self.classifier = nn.Linear(param.hidden_size, param.num_labels)
self.apply(self.init_bert_weights)
def forward(self, x):
x = self.dropout(x)
out = self.classifier(x)
return out
def init_bert_weights(self, module):
""" Initialize the weights.
"""
if isinstance(module, (nn.Linear, nn.Embedding)):
module.weight.data.normal_(mean=0.0, std=0.02)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
class RobertaClassifier(nn.Module):
"""Head for sentence-level classification tasks."""
def __init__(self, dropout=0.1):
super(RobertaClassifier, self).__init__()
self.pooler = nn.Linear(param.hidden_size, param.hidden_size)
self.dropout = nn.Dropout(p=dropout)
self.classifier = nn.Linear(param.hidden_size, param.num_labels)
def forward(self, x):
x = self.dropout(x)
x = self.pooler(x)
x = torch.tanh(x)
x = self.dropout(x)
out = self.classifier(x)
return out
class Discriminator(nn.Module):
"""Discriminator model for source domain."""
def __init__(self):
"""Init discriminator."""
super(Discriminator, self).__init__()
self.layer = nn.Sequential(
nn.Linear(param.hidden_size, param.intermediate_size),
nn.LeakyReLU(),
nn.Linear(param.intermediate_size, param.intermediate_size),
nn.LeakyReLU(),
nn.Linear(param.intermediate_size, 1),
nn.Sigmoid()
)
def forward(self, x):
"""Forward the discriminator."""
out = self.layer(x)
return out