[d129b2]: / medicalbert / classifiers / standard / fast_text_classifier.py

Download this file

57 lines (38 with data), 1.7 kB

 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
import torch
from classifiers.standard.classifier import Classifier
from torch import nn
from torch.nn import CrossEntropyLoss
class FastTextClassifier(Classifier):
def __init__(self, config):
self.config = config
self.model = FastText(config)
self.optimizer = torch.optim.Adam(self.model.parameters(), self.config['learning_rate'])
self.epochs = 0
# PyTorch implementation of FastText (so its not very fast!)
class FastText(nn.Module):
def __init__(self, config, word_embeddings=None):
super(FastText, self).__init__()
self.config = config
self.num_labels = 2
# Embedding Layer
self.embeddings = nn.Embedding(self.config['vocab_size'], self.config['embed_size'])
if word_embeddings:
self.embeddings.weight = nn.Parameter(word_embeddings, requires_grad=False)
# Hidden Layer
self.fc1 = nn.Linear(self.config['embed_size'], 10)
# Output Layer
self.fc2 = nn.Linear(10, 2)
# Softmax non-linearity
self.softmax = nn.Softmax(dim=1)
def forward(self, input_ids=None, attention_mask=None, token_type_ids=None,
position_ids=None, head_mask=None, inputs_embeds=None, labels=None):
embedded_sent = self.embeddings(input_ids)
h = self.fc1(embedded_sent.mean(1))
z = self.fc2(h)
logits = self.softmax(z)
outputs = (logits,)
if labels is not None:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
outputs = (loss,) + outputs
return outputs