-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_BERT.py
29 lines (24 loc) · 900 Bytes
/
model_BERT.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
"""
model作成
"""
import torch.nn as nn
from transformers import AdamW, AutoModel, AutoTokenizer
# BERT-model
class Classifier(nn.Module):
def __init__(self, model_name, num_classes=4):
super().__init__()
self.bert = AutoModel.from_pretrained(model_name)
self.dropout = nn.Dropout(0.1)
self.linear = nn.Linear(768, num_classes)
nn.init.normal_(self.linear.weight, std=0.02)
nn.init.zeros_(self.linear.bias)
def forward(self, input_ids, attention_mask, token_type_ids):
output, _ = self.bert(
input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
return_dict=False) # Pythonの実行上必要なので加筆しました。
output = output[:, 0, :]
output = self.dropout(output)
output = self.linear(output)
return output