Views
No views yet
1import torch
2from transformers import AlbertForMaskedLM, AlbertTokenizer
3import sentencepiece as spm
4
5# Load model and tokenizer
6model = AlbertForMaskedLM.from_pretrained("seanghay/albert-khmer-small")
7tokenizer = AlbertTokenizer.from_pretrained("seanghay/albert-khmer-small")
8sp = spm.SentencePieceProcessor()
9sp.load(tokenizer.vocab_file)
10
11text = "ភ្នំពេញគឺជា[MASK]នៃប្រទេសកម្ពុជា។"
12pieces = sp.encode_as_pieces(text)
13ids = sp.encode_as_ids(text)
14input_ids = torch.LongTensor([2] + ids + [3]).unsqueeze(0) # [CLS] + ids + [SEP]
15attention_mask = torch.zeros_like(input_ids)
16
17# Perform inference
18with torch.no_grad():
19 outputs = model(**inputs)
20 logits = outputs.logits
21
22# Locate the [MASK] token and extract predictions
23mask_token_index = torch.where(inputs.input_ids == tokenizer.mask_token_id)[1]
24mask_token_logits = logits[0, mask_token_index, :]
25
26top_5_tokens = torch.topk(mask_token_logits, 5, dim=1).indices[0].tolist()
27
28print(f"Original text: {text}")
29print(f"Decoded input text embedding (should match original text): {sp.decode_ids(input_ids.squeeze().tolist())})
30for i, token_id in enumerate(top_5_tokens):
31 predicted_token = tokenizer.decode([token_id])
32 print(f"{i + 1}. {text.replace('[MASK]', predicted_token)}")
33| Parameter | Value |
|---|---|
hidden_size | 768 |
embedding_size | 128 |
num_hidden_layers | 12 |
num_attention_heads | 12 |
intermediate_size | 3072 |
max_position_embeddings | 512 |
vocab_size | 16,000 |
| Token Rank | Predicted Word | Full Sentence |
|---|---|---|
| 1 | បេះដូង | ភ្នំពេញគឺជាបេះដូងនៃប្រទេសកម្ពុជា។ |
| 2 | ទឹកដី | ភ្នំពេញគឺជាទឹកដីនៃប្រទេសកម្ពុជា។ |
| 3 | រាជធានី | ភ្នំពេញគឺជារាជធានីនៃប្រទេសកម្ពុជា។ |
@misc{seanghay2024albertkhmersmall,
author = {Seanghay Yath},
title = {ALBERT Khmer Small: An efficient ALBERT model for the Khmer language},
year = {2024},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/seanghay/albert-khmer-small}},
note = {11.9M parameters, trained on 13M Khmer sentences}
}