-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.py
153 lines (129 loc) · 5.5 KB
/
models.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 modules #
##################
from transformers import AutoConfig, AutoModelForSequenceClassification, RobertaModel
from transformers.models.roberta.modeling_roberta import RobertaPreTrainedModel
import torch
import torch.nn as nn
from torch.utils.data import Sampler
#######################
# functions & classes #
#######################
class MyRobertaClassificationHead(nn.Module):
"""Head for sentence-level classification tasks."""
def __init__(self, config):
super().__init__()
self.dense1 = nn.Linear(config.hidden_size*3, config.hidden_size*2)
self.dense2 = nn.Linear(config.hidden_size*2, config.hidden_size)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
def forward(self, features, entity_location):
subject_entity_avgs = []
oject_entity_avgs = []
for idx in range(entity_location.shape[0]):
subject_entity_avg = features[:, entity_location[idx][0]:entity_location[idx][1], :]
subject_entity_avg = torch.mean(subject_entity_avg, axis=1)
subject_entity_avgs.append(subject_entity_avg.cpu().detach().numpy())
oject_entity_avg = features[:, entity_location[idx][2]:entity_location[idx][3], :]
oject_entity_avg = torch.mean(oject_entity_avg, axis=1)
oject_entity_avgs.append(oject_entity_avg.cpu().detach().numpy())
subject_entity_avgs = torch.tensor(subject_entity_avgs)
oject_entity_avgs = torch.tensor(oject_entity_avgs)
x = torch.cat([features[:, 0, :], subject_entity_avg, oject_entity_avg], axis = 1)
x = self.dropout(x)
x = self.dense1(x)
x = torch.tanh(x)
x = self.dense2(x)
x = torch.tanh(x)
x = self.out_proj(x)
return x
class MyRobertaForSequenceClassification(RobertaPreTrainedModel):
_keys_to_ignore_on_load_missing = [r"position_ids"]
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.roberta = RobertaModel(config, add_pooling_layer=False)
self.classification = MyRobertaClassificationHead(config)
self.init_weights()
def forward(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
entity_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
entity_location=None,
return_dict=None,
):
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
outputs = self.roberta(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
entity_type_ids=entity_type_ids
)
sequence_output = outputs[0]
logits = self.classification(sequence_output, entity_location)
loss = None
if labels is not None:
loss = 0
#if not return_dict:
output = (logits,) + outputs[2:]
return ((loss,) + output) if loss is not None else output
class StratifiedSampler(Sampler):
"""
Stratified Sampling
Provides equal representation of target classes in each batch
"""
def __init__(self, class_vector, batch_size):
"""
Arguments
---------
class_vector : torch tensor
a vector of class labels
batch_size : integer
batch_size
"""
self.n_splits = int(class_vector.size(0) / batch_size)
self.class_vector = class_vector
def gen_sample_array(self):
try:
from sklearn.model_selection import StratifiedShuffleSplit
except:
print('Need scikit-learn for this functionality')
import numpy as np
s = StratifiedShuffleSplit(n_splits=self.n_splits, test_size=0.5)
X = torch.randn(self.class_vector.size(0),2).numpy()
y = self.class_vector.numpy()
s.get_n_splits(X, y)
train_index, test_index = next(s.split(X, y))
return np.hstack([train_index, test_index])
def __iter__(self):
return iter(self.gen_sample_array())
def __len__(self):
return len(self.class_vector)
def get_model(model_name, num_classes):
if model_name == 'custom_robert_base':
model_config = AutoConfig.from_pretrained('./roberta-retrained/base/')
model_config.num_labels = num_classes
model = MyRobertaForSequenceClassification.from_pretrained('./roberta-retrained/base/', config=model_config)#'klue/roberta-base', config=model_config)
elif model_name == 'custom_robert_large':
model_config = AutoConfig.from_pretrained('./roberta-retrained/large/')
model_config.num_labels = num_classes
model = MyRobertaForSequenceClassification.from_pretrained('./roberta-retrained/large/', config=model_config)#'klue/roberta-base', config=model_config)
else:
model_config = AutoConfig.from_pretrained(model_name)
model_config.num_labels = num_classes
model = AutoModelForSequenceClassification.from_pretrained(model_name, config=model_config)
return model