ageng-anugrah commited on
Commit
2f307a4
1 Parent(s): b9c4f65

First model

Browse files
Files changed (4) hide show
  1. README.md +59 -0
  2. config.json +71 -0
  3. pytorch_model.bin +3 -0
  4. vocab.txt +0 -0
README.md ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: id
3
+ tags:
4
+ - indobert
5
+ - indobenchmark
6
+ ---
7
+
8
+ ## How to use
9
+
10
+ ### Load model and tokenizer
11
+ ```python
12
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
13
+
14
+ tokenizer = AutoTokenizer.from_pretrained("ageng-anugrah/indobert-large-p2-finetuned-ner")
15
+ model = AutoModelForTokenClassification.from_pretrained("ageng-anugrah/indobert-large-p2-finetuned-ner")
16
+ ```
17
+
18
+ ### Extract NER Tag
19
+ ```python
20
+ import torch
21
+ def predict(model, tokenizer, sentence):
22
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
+ inputs = tokenizer(sentence.split(),
24
+ is_split_into_words = True,
25
+ return_offsets_mapping=True,
26
+ return_tensors="pt",
27
+ padding='max_length',
28
+ truncation=True,
29
+ max_length=512)
30
+
31
+ model.to(device)
32
+ # move to gpu
33
+ ids = inputs["input_ids"].to(device)
34
+ mask = inputs["attention_mask"].to(device)
35
+
36
+ # forward pass
37
+ outputs = model(ids, attention_mask=mask)
38
+ logits = outputs[0]
39
+
40
+ active_logits = logits.view(-1, model.num_labels) # shape (batch_size * seq_len, num_labels)
41
+ flattened_predictions = torch.argmax(active_logits, axis=1) # shape (batch_size*seq_len,) - predictions at the token level
42
+
43
+ tokens = tokenizer.convert_ids_to_tokens(ids.squeeze().tolist())
44
+ token_predictions = [model.config.id2label[i] for i in flattened_predictions.cpu().numpy()]
45
+ wp_preds = list(zip(tokens, token_predictions)) # list of tuples. Each tuple = (wordpiece, prediction)
46
+
47
+ prediction = []
48
+ for token_pred, mapping in zip(wp_preds, inputs["offset_mapping"].squeeze().tolist()):
49
+ #only predictions on first word pieces are important
50
+ if mapping[0] == 0 and mapping[1] != 0:
51
+ prediction.append(token_pred[1])
52
+ else:
53
+ continue
54
+
55
+ return sentence.split(), prediction
56
+
57
+ sentence = "BJ Habibie adalah Presiden Indonesia ke-3 yang lahir pada tanggl 25 Juni 1936"
58
+ words, labels = predict(model, tokenizer, sentence)
59
+ ```
config.json ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "indobenchmark/indobert-large-p2",
3
+ "_num_labels": 5,
4
+ "architectures": [
5
+ "BertForTokenClassification"
6
+ ],
7
+ "attention_probs_dropout_prob": 0.1,
8
+ "classifier_dropout": null,
9
+ "directionality": "bidi",
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 1024,
13
+ "id2label": {
14
+ "0": "B-ADJP",
15
+ "1": "B-ADVP",
16
+ "2": "B-INTJ",
17
+ "3": "B-NP",
18
+ "4": "B-PP",
19
+ "5": "B-PRT",
20
+ "6": "B-SBAR",
21
+ "7": "B-UCP",
22
+ "8": "B-VP",
23
+ "9": "I-ADJP",
24
+ "10": "I-ADVP",
25
+ "11": "I-NP",
26
+ "12": "I-PP",
27
+ "13": "I-SBAR",
28
+ "14": "I-UCP",
29
+ "15": "I-VP",
30
+ "16": "O"
31
+ },
32
+ "initializer_range": 0.02,
33
+ "intermediate_size": 4096,
34
+ "label2id": {
35
+ "B-ADJP": 0,
36
+ "B-ADVP": 1,
37
+ "B-INTJ": 2,
38
+ "B-NP": 3,
39
+ "B-PP": 4,
40
+ "B-PRT": 5,
41
+ "B-SBAR": 6,
42
+ "B-UCP": 7,
43
+ "B-VP": 8,
44
+ "I-ADJP": 9,
45
+ "I-ADVP": 10,
46
+ "I-NP": 11,
47
+ "I-PP": 12,
48
+ "I-SBAR": 13,
49
+ "I-UCP": 14,
50
+ "I-VP": 15,
51
+ "O": 16
52
+ },
53
+ "layer_norm_eps": 1e-12,
54
+ "max_position_embeddings": 512,
55
+ "model_type": "bert",
56
+ "num_attention_heads": 16,
57
+ "num_hidden_layers": 24,
58
+ "output_past": true,
59
+ "pad_token_id": 0,
60
+ "pooler_fc_size": 768,
61
+ "pooler_num_attention_heads": 12,
62
+ "pooler_num_fc_layers": 3,
63
+ "pooler_size_per_head": 128,
64
+ "pooler_type": "first_token_transform",
65
+ "position_embedding_type": "absolute",
66
+ "torch_dtype": "float32",
67
+ "transformers_version": "4.27.4",
68
+ "type_vocab_size": 2,
69
+ "use_cache": true,
70
+ "vocab_size": 30522
71
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd916fb6015bfd0fe32f058ad8b444c486965293b9c5508e6e1135fac1fb015d
3
+ size 1336577069
vocab.txt ADDED
The diff for this file is too large to render. See raw diff