docstr: Perturb the instances in an autoregressive fashion (sequential).
  
  Parameters
  ----------
  num_samples
      Number of samples to be generated.
  raw
      Array of mask templates. Has `mask_templates` rows.
  data
      Binary array having 0 where the tokens are masked. Has `mask_templates` rows.
  top_n:
      Use the top n words when sampling.
  batch_size_lm:
      Batch size used for language model.
  temperature
      Sample weight hyper-parameter.
  use_proba
      Bool whether to sample according to the predicted words distribution.
  **kwargs
      Other arguments. Not used.
  
  Returns
  -------
  sampled_tokens
      Array containing the ids of the sampled tokens. Has `num_samples` rows.
  sampled_data
      Binary array having 0 where the tokens were masked. Has `num_samples` rows.
docstr_tok: ["Perturb","the","instances","in","an","autoregressive","fashion","(","sequential",")",".","Parameters","--","--","--","--","--","num_samples","Number","of","samples","to","be","generated",".","raw","Array","of","mask","templates",".","Has","`","mask_templates","`","rows",".","data","Binary","array","having","0","where","the","tokens","are","masked",".","Has","`","mask_templates","`","rows",".","top_n",":","Use","the","top","n","words","when","sampling",".","batch_size_lm",":","Batch","size","used","for","language","model",".","temperature","Sample","weight","hyper-parameter",".","use_proba","Bool","whether","to","sample","according","to","the","predicted","words","distribution",".","*","*","kwargs","Other","arguments",".","Not","used",".","Returns","--","--","--","-","sampled_tokens","Array","containing","the","ids","of","the","sampled","tokens",".","Has","`","num_samples","`","rows",".","sampled_data","Binary","array","having","0","where","the","tokens","were","masked",".","Has","`","num_samples","`","rows","."]
code: def _perturb_instance_ar(
      self,
      num_samples: int,
      raw: np.ndarray,
      data: np.ndarray,
      top_n: int = 100,
      batch_size: int = 32,
      temperature: float = 1.0,
      use_proba: bool = False,
      **kwargs,
  ) -> Tuple[np.ndarray, np.ndarray]:
      """
      Perturb the instances in an autoregressive fashion (sequential).
  
      Parameters
      ----------
      num_samples
          Number of samples to be generated.
      raw
          Array of mask templates. Has `mask_templates` rows.
      data
          Binary array having 0 where the tokens are masked. Has `mask_templates` rows.
      top_n:
          Use the top n words when sampling.
      batch_size_lm:
          Batch size used for language model.
      temperature
          Sample weight hyper-parameter.
      use_proba
          Bool whether to sample according to the predicted words distribution.
      **kwargs
          Other arguments. Not used.
  
      Returns
      -------
      sampled_tokens
          Array containing the ids of the sampled tokens. Has `num_samples` rows.
      sampled_data
          Binary array having 0 where the tokens were masked. Has `num_samples` rows.
      """
      # number of samples to generate per mask template
      assert num_samples == raw.shape[0]
  
      # tokenize instances
      tokens_plus = self.model.tokenizer.batch_encode_plus(
          list(raw), padding=True, return_tensors="tf"
      )
      tokens = tokens_plus[
          "input_ids"
      ].numpy()  # (mask_template x max_length_sentence)
  
      # store the column indices for each row where a token is a mask
      masked_idx = []
      max_len_idx = -1
      mask_pos = tf.where(tokens == self.model.mask_id)
      mask_row, mask_col = mask_pos[:, 0], mask_pos[:, 1]
  
      for i in range(tokens.shape[0]):
          # get the columns indexes and store them in the buffer
          idx = tf.reshape(tf.where(mask_row == i), shape=-1)
          cols = tf.gather(mask_col, idx)
          masked_idx.append(cols)
  
          # update maximum length
          max_len_idx = max(max_len_idx, len(cols))
  
      # iterate through all possible columns indexes
      for i in range(max_len_idx):
          masked_rows, masked_cols = [], []
  
          # iterate through all possible examples
          for row in range(tokens.shape[0]):
              # this means that the row does not have any more masked columns
              if len(masked_idx[row]) <= i:
                  continue
  
              masked_rows.append(row)
              masked_cols.append(masked_idx[row][i])
  
          # compute logits
          logits = self.model.predict_batch_lm(
              x=tokens_plus,
              vocab_size=self.model.tokenizer.vocab_size,
              batch_size=batch_size,
          )
  
          # select only the logits of the first masked word in each row
          logits_mask = logits[masked_rows, masked_cols, :]
  
          # mask out words according to the subword_mask
          logits_mask[:, self.subwords_mask] = -np.inf
  
          # select top n tokens from each distribution
          top_k = tf.math.top_k(logits_mask, top_n)
          top_k_logits, top_k_tokens = top_k.values, top_k.indices
          top_k_logits = (
              (top_k_logits / temperature)
              if use_proba
              else (top_k_logits * 0)
          )
  
          # Sample indices
          ids_k = tf.reshape(
              tf.random.categorical(top_k_logits, 1), shape=-1
          )
  
          # replace masked tokens with the sampled one
          tokens[masked_rows, masked_cols] = tf.gather(
              top_k_tokens, ids_k, batch_dims=1
          )
          tokens_plus["input_ids"] = tf.convert_to_tensor(tokens)
      return tokens, data
  
imports: import string
  from functools import partial
  from typing import Dict, List, Optional, Tuple
  import numpy
  import tensorflow
  from alibi.utils.lang_model import LanguageModel
  from alibi.explainers.anchors.text_samplers import AnchorTextSampler
label_desc: Use image node_id 12 for calling the LanguageModelSampler obj's underlying member method code with example usage: obj._perturb_instance_ar(num_samples, raw, data, top_n, batch_size, temperature, use_proba) and returns: tokens,  data