-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
29 lines (18 loc) · 874 Bytes
/
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
import numpy as np
import torch
from torch import nn
from transformers import AutoModel
import random
class bert(nn.Module):
def __init__(self, name = None, num_classes = None, seed = None):
super(bert, self).__init__()
if not (seed == None):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
self.bert = AutoModel.from_pretrained(name)
self.linear1 = nn.Linear(768, num_classes)
def forward(self, input_ids = None, attention_mask = None):
op = self.bert(input_ids = input_ids, attention_mask = attention_mask)# sequence_output has the following shape: (batch_size, sequence_length, 768)
linear1_output = self.linear1(op[0][:, 0, :]) # take the embeddings of the CLS token
return linear1_output