class_docstr: Task for used with Masked language modelling.
  
  .. todo::
      Describe MLM
  
  :param mask_ratio: Fraction of tokens to mask.
  :param smart_masking: Whether to apply smart masking (use tokens from the same group when choosing randoms).
class_docstr_tok: ["Task","for","used","with","Masked","language","modelling",".","..","todo",":",":","Describe","MLM",":","param","mask_ratio",":","Fraction","of","tokens","to","mask",".",":","param","smart_masking",":","Whether","to","apply","smart","masking","(","use","tokens","from","the","same","group","when","choosing","randoms",")","."]
code: def encode_document(
      self, document: PersonDocument
  ) -> "MLMEncodedDocument":
      prefix_sentence = (
          ["[CLS]"]
          + Background.get_sentence(document.background)
          + ["[SEP]"]
      )
  
      ############################################
      ### CLS TASK
      document, targ_cls = self.cls_task(document)
      ############################################
  
      sentences = [prefix_sentence] + [
          s + ["[SEP]"] for s in document.sentences
      ]
      sentence_lengths = [len(x) for x in sentences]
  
      def expand(x: List[T]) -> List[T]:
          assert len(x) == len(sentence_lengths)
          return list(
              chain.from_iterable(
                  length * [i] for length, i in zip(sentence_lengths, x)
              )
          )
  
      abspos_expanded = expand([0] + document.abspos)
      age_expanded = expand([0.0] + document.age)
      assert document.segment is not None
      segment_expanded = expand([1] + document.segment)
  
      flat_sentences = np.concatenate(sentences)
  
      token2index = self.datamodule.vocabulary.token2index
      unk_id = token2index["[UNK]"]
  
      # print(flat_sentences[500:550])
      token_ids = np.array(
          [token2index.get(x, unk_id) for x in flat_sentences]
      )
      masked_sentences, masked_indx, masked_tokens = self.mlm_mask(
          token_ids.copy()
      )
  
      length = len(token_ids)
  
      input_ids = np.zeros((4, self.max_length))
      input_ids[0, :length] = masked_sentences
      input_ids[1, :length] = abspos_expanded
      input_ids[2, :length] = age_expanded
      input_ids[3, :length] = segment_expanded
  
      padding_mask = np.repeat(False, self.max_length)
      padding_mask[:length] = True
  
      # TODO: Consider renaming, to document/sentences instead of sequence...
      # would require refactoring of the modelling also though
  
      original_sequence = np.zeros(self.max_length)
      original_sequence[:length] = token_ids
  
      sequence_id = np.array(document.person_id)
  
      return MLMEncodedDocument(
          sequence_id=sequence_id,
          input_ids=input_ids,
          padding_mask=padding_mask,
          target_tokens=masked_tokens,
          target_pos=masked_indx,
          target_cls=targ_cls,
          original_sequence=original_sequence,
      )
  
file_path: life2vec/src/tasks/mlm.py
imports: import logging
  from dataclasses import dataclass
  from functools import cached_property
  from itertools import chain
  from typing import List, Tuple, TypeVar, cast
  from xml.dom.minidom import Document
  import numpy
  import torch
  from random import shuffle
  from src.data_new.types import Background, PersonDocument, EncodedDocument
  from src.tasks.base import Task
label_desc: Use image node_id 1 for calling the MLM obj's underlying member method code with example usage: obj.encode_document(document) and returns: MLMEncodedDocument, list