Testys commited on
Commit
dfe40cb
1 Parent(s): e6d77e2

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +21 -0
  2. config.json +1 -0
  3. custom_modeling.py +12 -0
  4. modeling_cnn_ner.py +32 -0
  5. pytorch_model.bin +3 -0
README.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+ language: en
4
+ tags:
5
+ - ner
6
+ - pytorch
7
+ license: mit
8
+ ---
9
+
10
+ # CNN for Named Entity Recognition
11
+
12
+ This model is a CNN-based model for Named Entity Recognition (NER) built on top of a pre-trained transformer model.
13
+
14
+ ## Model description
15
+
16
+ The model uses a pre-trained transformer as a base and adds convolutional layers on top for NER tasks.
17
+
18
+ ## Intended uses & limitations
19
+
20
+ This model is intended for Named Entity Recognition tasks. It should be used on Yoruba text data.
21
+
config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"model_type": "CNNForYorubaNER", "num_classes": 9, "max_length": 128, "pretrained_model_name": "masakhane/afroxlmr-large-ner-masakhaner-1.0_2.0"}
custom_modeling.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import AutoConfig, AutoModel
3
+ from modeling_cnn_ner import CNNForNER
4
+
5
+ def get_model(pretrained_model_name_or_path):
6
+ config = AutoConfig.from_pretrained(pretrained_model_name_or_path)
7
+ model = CNNForNER(
8
+ pretrained_model_name=config.pretrained_model_name,
9
+ num_classes=config.num_classes,
10
+ max_length=config.max_length
11
+ )
12
+ return model
modeling_cnn_ner.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ from transformers import AutoModelForTokenClassification
6
+
7
+ class CNNForNER(nn.Module):
8
+ def __init__(self, pretrained_model_name, num_classes, max_length=128):
9
+ super(CNNForNER, self).__init__()
10
+ self.transformer = AutoModelForTokenClassification.from_pretrained(pretrained_model_name)
11
+ self.max_length = max_length
12
+
13
+ # Get the number of labels from the pretrained model
14
+ pretrained_num_labels = self.transformer.num_labels
15
+
16
+ self.conv1 = nn.Conv1d(in_channels=pretrained_num_labels, out_channels=256, kernel_size=3, padding=1)
17
+ self.conv2 = nn.Conv1d(in_channels=256, out_channels=128, kernel_size=3, padding=1)
18
+ self.dropout = nn.Dropout(0.3)
19
+ self.fc = nn.Linear(in_features=128, out_features=num_classes)
20
+
21
+ def forward(self, input_ids, attention_mask):
22
+ outputs = self.transformer(input_ids=input_ids, attention_mask=attention_mask)
23
+ logits = outputs.logits # Shape: (batch_size, sequence_length, pretrained_num_labels)
24
+
25
+ # Apply CNN layers
26
+ logits = logits.permute(0, 2, 1) # Shape: (batch_size, pretrained_num_labels, sequence_length)
27
+ conv1_out = F.relu(self.conv1(logits))
28
+ conv2_out = F.relu(self.conv2(conv1_out))
29
+ conv2_out = self.dropout(conv2_out)
30
+ conv2_out = conv2_out.permute(0, 2, 1) # Shape: (batch_size, sequence_length, 128)
31
+ final_logits = self.fc(conv2_out) # Shape: (batch_size, sequence_length, num_classes)
32
+ return final_logits
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f6df5d161a93b1a7470e4e0cb4f1d8a9dbeb74a9fb56b72d318dad0731c9a379
3
+ size 2236003390