ageng-anugrah commited on
Commit
66c7de4
1 Parent(s): 7011ac5

First model version

Browse files
Files changed (4) hide show
  1. README.md +59 -0
  2. config.json +43 -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,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-TIMEX",
15
+ "1": "I-TIMEX",
16
+ "2": "O"
17
+ },
18
+ "initializer_range": 0.02,
19
+ "intermediate_size": 4096,
20
+ "label2id": {
21
+ "B-TIMEX": 0,
22
+ "I-TIMEX": 1,
23
+ "O": 2
24
+ },
25
+ "layer_norm_eps": 1e-12,
26
+ "max_position_embeddings": 512,
27
+ "model_type": "bert",
28
+ "num_attention_heads": 16,
29
+ "num_hidden_layers": 24,
30
+ "output_past": true,
31
+ "pad_token_id": 0,
32
+ "pooler_fc_size": 768,
33
+ "pooler_num_attention_heads": 12,
34
+ "pooler_num_fc_layers": 3,
35
+ "pooler_size_per_head": 128,
36
+ "pooler_type": "first_token_transform",
37
+ "position_embedding_type": "absolute",
38
+ "torch_dtype": "float32",
39
+ "transformers_version": "4.27.4",
40
+ "type_vocab_size": 2,
41
+ "use_cache": true,
42
+ "vocab_size": 30522
43
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e5b9dd1146b641d2e76875c14cb51d4a023cb6dabaef39fee97a591f0b0dd2ee
3
+ size 1336519661
vocab.txt ADDED
The diff for this file is too large to render. See raw diff