Masked LM#

This tutorial is available as an IPython notebook at Malaya/example/mlm.

Masked Language Model Scoring,

We are able to use BERT, ALBERT and RoBERTa from HuggingFace to do text scoring.

import malaya
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/ UserWarning: Tensorflow Addons supports using Python ops for all Tensorflow versions above or equal to 2.3.0 and strictly below 2.5.0 (nightly versions are not supported).
 The versions of TensorFlow you are currently using is 2.6.0 and is not supported.
Some things might work, some things might not.
If you were to encounter a bug, do not file an issue.
If you want to make sure you're using a tested and supported configuration, either change the TensorFlow version or the TensorFlow Addons's version.
You can find the compatibility matrix in TensorFlow Addon's readme:
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/ UserWarning: You are currently using TensorFlow 2.6.0 and trying to load a custom op (custom_ops/seq2seq/
TensorFlow Addons has compiled its custom ops against TensorFlow 2.4.0, and there are no compatibility guarantees between the two versions.
This means that you might get segfaults when loading the custom op, or other kind of low-level errors.
 If you do, do not file an issue on Github. This is a known limitation.

It might help you to fallback to pure Python ops with TF_ADDONS_PY_OPS . To do that, see

You can also change the TensorFlow version installed on your system. You would need a TensorFlow version equal to or above 2.4.0 and strictly below 2.5.0.
 Note that nightly versions of TensorFlow, as well as non-pip TensorFlow like `conda install tensorflow` or compiled from source are not supported.

The last solution is to find the TensorFlow Addons version that has custom ops compatible with the TensorFlow installed on your system. To do that, refer to the readme:
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/ UserWarning: Cannot import beam_search_ops from Tensorflow Addons, ['malaya.jawi_rumi.deep_model', 'malaya.phoneme.deep_model', 'malaya.rumi_jawi.deep_model', 'malaya.stem.deep_model'] will not available to use, make sure Tensorflow Addons version >= 0.12.0
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/ UserWarning: check compatible Tensorflow version with Tensorflow Addons at
/home/husein/dev/malaya/malaya/ FutureWarning: Possible nested set at position 3361
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))
/home/husein/dev/malaya/malaya/ FutureWarning: Possible nested set at position 3879
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))


Make sure you already installed,

pip3 install transformers

List available MLM models#

Size (MB)
malay-huggingface/bert-base-bahasa-cased 310.0
malay-huggingface/bert-tiny-bahasa-cased 66.1
malay-huggingface/albert-base-bahasa-cased 45.9
malay-huggingface/albert-tiny-bahasa-cased 22.6
mesolitica/roberta-base-bahasa-cased 443.0
mesolitica/roberta-tiny-bahasa-cased 66.1

Load MLM model#

def mlm(model: str = 'malay-huggingface/bert-tiny-bahasa-cased', force_check: bool = True, **kwargs):
    Load Masked language model.

    model: str, optional (default='malay-huggingface/bert-tiny-bahasa-cased')
        Check available models at `malaya.language_model.available_mlm()`.
    force_check: bool, optional (default=True)
        Force check model one of malaya model.
        Set to False if you have your own huggingface model.

    result: malaya.torch_model.mask_lm.MLMScorer class

If you have other models from huggingface and want to load it on malaya.torch_model.mask_lm.MLMScorer, set force_check=False.

model = malaya.language_model.mlm(model = 'malay-huggingface/bert-tiny-bahasa-cased')
model.score('saya suke awak')
model.score('saya suka awak')
model.score('najib razak')
model.score('najib comel')