-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
36 lines (28 loc) · 1.33 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
import torch
import torch.nn as nn
from transformers import AutoModel
class BertClassifier(nn.Module):
def __init__(self, config):
super(BertClassifier, self).__init__()
# Binary classification problem (num_labels = 2)
self.num_labels = config["num_labels"]
# Pre-trained BERT model
self.bert = AutoModel.from_pretrained("GroNLP/bert-base-dutch-cased")
# Dropout to avoid overfitting
self.dropout = nn.Dropout(config['hidden_dropout_prob'])
# A single layer classifier added on top of BERT to fine tune for binary classification
self.classifier = nn.Sequential(
nn.Linear(config['hidden_size'], config['hidden_size']),
nn.Sigmoid(),
nn.Linear(config['hidden_size'], config['num_labels']),
nn.Softmax(dim=1),
)
self.id_dict = {}
def forward(self, input_ids, token_type_ids=None, attention_mask=None,
position_ids=None, head_mask=None):
# Forward pass through pre-trained BERT
outputs = self.bert(input_ids, position_ids=position_ids, token_type_ids=token_type_ids,
attention_mask=attention_mask, head_mask=head_mask)
pooled_output = outputs[-1]
pooled_output = self.dropout(pooled_output)
return self.classifier(pooled_output)