Masked LM#

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

Masked Language Model Scoring, https://arxiv.org/abs/1910.14659

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

[1]:
import malaya
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/ensure_tf_install.py:53: 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:
https://github.com/tensorflow/addons
  warnings.warn(
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/resource_loader.py:72: UserWarning: You are currently using TensorFlow 2.6.0 and trying to load a custom op (custom_ops/seq2seq/_beam_search_ops.so).
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 https://github.com/tensorflow/addons#gpucpu-custom-ops

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: https://github.com/tensorflow/addons
  warnings.warn(
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:35: 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
  warnings.warn(
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:38: UserWarning: check compatible Tensorflow version with Tensorflow Addons at https://github.com/tensorflow/addons/releases
  warnings.warn(
/home/husein/dev/malaya/malaya/tokenizer.py:202: FutureWarning: Possible nested set at position 3361
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))
/home/husein/dev/malaya/malaya/tokenizer.py:202: FutureWarning: Possible nested set at position 3879
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))

Dependency#

Make sure you already installed,

pip3 install transformers

List available MLM models#

[2]:
malaya.language_model.available_mlm()
[2]:
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.

    Parameters
    ----------
    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.

    Returns
    -------
    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.

[3]:
model = malaya.language_model.mlm(model = 'malay-huggingface/bert-tiny-bahasa-cased')
[4]:
model.score('saya suke awak')
[4]:
-41.13948
[5]:
model.score('saya suka awak')
[5]:
-23.288536
[6]:
model.score('najib razak')
[6]:
-45.52244
[7]:
model.score('najib comel')
[7]:
-27.509785