-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
123 lines (104 loc) · 4.48 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
# =============================================================================
# Model
# =============================================================================
from src import utils,metric
from torch.utils.data import Dataset,DataLoader
import torch.nn as nn
MAX_LEN = 512
TRAIN_BATCH_SIZE = 32
VALID_BATCH_SIZE = 32
EPOCHS = 20
LEARNING_RATE = 1e-05
WEIGHT_DECAY = 1e-05
num_of_batches_per_epoch = len(X_train)//TRAIN_BATCH_SIZE
history = defaultdict(list)
class DistillBERT(torch.nn.Module):
def __init__(self,num_classes):
super(DistillBERTClass, self).__init__()
self.l1 = DistilBertModel.from_pretrained("distilbert-base-uncased")
self.classifier = torch.nn.Linear(768, 768)
self.dropout = torch.nn.Dropout(0.6)
self.classifier = torch.nn.Linear(768, num_classes)
def forward(self, input_ids, attention_mask):
output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
hidden_state = output_1[0]
bert_last = hidden_state[:, 0]
output = self.classifier(bert_last)
return output
class BERT(torch.nn.Module):
def __init__(self,num_classes):
super(BERTClass, self).__init__()
self.l1 = BertModel.from_pretrained("bert-base-uncased",output_hidden_states=True)
self.classifier = torch.nn.Linear(768, 768)
self.dropout = torch.nn.Dropout(0.6)
self.classifier = torch.nn.Linear(768, num_classes)
def forward(self, input_ids, attention_mask):
output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
hidden_state = output_1[0]
bert_last = hidden_state[:, 0]
output = self.classifier(bert_last)
return output
class RoBERTA(torch.nn.Module):
def __init__(self,num_classes):
super(BibBirdClass, self).__init__()
self.l1 = BigBirdModel.from_pretrained("roberta-base",output_hidden_states=True)
self.classifier = torch.nn.Linear(4096, 1024)
self.dropout = torch.nn.Dropout(0.6)
self.classifier = torch.nn.Linear(1024, num_classes)
def forward(self, input_ids, attention_mask):
output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
hidden_state = output_1[0]
bert_last = hidden_state[:, 0]
output = self.classifier(bert_last)
return output
from transformers import DistilBertConfig,DistilBertTokenizer,DistilBertModel
from transformers import BertConfig,BertTokenizer,BertModel
from transformers import BigBirdConfig,BigBirdTokenizer,BigBirdModel
from transformers import LongformerConfig,LongformerTokenizer,LongformerModel
num_classes = len(df_profile.labels.unique())
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
baseline_model = BERTClass(num_classes)
baseline_model.to(device)
class BertDataFormat(Dataset):
def __init__(self, dataframe, tokenizer, max_len):
self.len = len(dataframe)
self.data = dataframe
self.tokenizer = tokenizer
self.max_len = max_len
def __getitem__(self, index):
cur_doc = str(self.data.doc[index])
cur_doc = " ".join(cur_doc.split())
inputs = self.tokenizer.encode_plus(
cur_doc,
None,
add_special_tokens=True,
max_length=self.max_len,
padding='max_length',
return_token_type_ids=True,
truncation=True
)
ids = inputs['input_ids']
mask = inputs['attention_mask']
return {
'ids': torch.tensor(ids, dtype=torch.long),
'mask': torch.tensor(mask, dtype=torch.long),
'targets': torch.tensor(self.data.labels[index], dtype=torch.long)
}
def __len__(self):
return self.len
training_set = BertDataFormat(train_df, tokenizer, MAX_LEN)
testing_set = BertDataFormat(test_df, tokenizer, MAX_LEN)
train_params = {'batch_size': TRAIN_BATCH_SIZE,
'shuffle': True,
'num_workers': 0
}
test_params = {'batch_size': VALID_BATCH_SIZE,
'shuffle': False,
'num_workers': 0
}
training_loader = DataLoader(training_set, **train_params)
testing_loader = DataLoader(testing_set, **test_params)
testing_set = BertDataFormat(test_df, tokenizer, MAX_LEN)
testing_loader = DataLoader(testing_set, **test_params)
loss_function = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(params = baseline_model.parameters(), lr=LEARNING_RATE,weight_decay=WEIGHT_DECAY)