-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
24 lines (18 loc) · 871 Bytes
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch
import torch.nn as nn
from torch.nn import CrossEntropyLoss
from transformers import DistilBertForTokenClassification,DistilBertConfig
class NER(nn.Module):
""" NER model for tagging """
def __init__(self,num_labels):
super(NER, self).__init__()
self.num_labels = num_labels
self.config = DistilBertConfig.from_pretrained('distilbert-base-cased',num_labels = self.num_labels) # 导入配置文件
# 单独指定config,在config中指定分类个数
self.distilbert = DistilBertForTokenClassification.from_pretrained('distilbert-base-cased',config= self.config)
def forward(self,input_ids,attention_mask,labels):
out = self.distilbert(input_ids = input_ids , labels = labels, attention_mask = attention_mask)
return out
if __name__ == "__main__":
model = NER(3)
print(model)