code: def io_iter(
      s_iter,
      uir_tuple,
      n_sample=0,
      sample_alpha=0,
      rng=None,
      batch_size=1,
      shuffle=False,
  ):
      """Paralellize mini-batch of input-output items. Create an iterator over data yielding batch of input item indices, batch of output item indices,
      batch of start masking, batch of end masking, and batch of valid ids (relative positions of current sequences in the last batch).
  
      Parameters
      ----------
      batch_size: int, optional, default = 1
  
      shuffle: bool, optional, default: False
          If `True`, orders of triplets will be randomized. If `False`, default orders kept.
  
      Returns
      -------
      iterator : batch of input item indices, batch of output item indices, batch of starting sequence mask, batch of ending sequence mask, batch of valid ids
  
      """
      rng = rng if rng is not None else get_rng(None)
      start_mask = np.zeros(batch_size, dtype="int")
      end_mask = np.ones(batch_size, dtype="int")
      input_iids = None
      output_iids = None
      l_pool = []
      c_pool = [None for _ in range(batch_size)]
      sizes = np.zeros(batch_size, dtype="int")
      if n_sample > 0:
          item_count = Counter(uir_tuple[1])
          item_indices = np.array(
              [iid for iid, _ in item_count.most_common()], dtype="int"
          )
          item_dist = (
              np.array(
                  [cnt for _, cnt in item_count.most_common()],
                  dtype="float",
              )
              ** sample_alpha
          )
          item_dist = item_dist / item_dist.sum()
      for _, batch_mapped_ids in s_iter(batch_size, shuffle):
          l_pool += batch_mapped_ids
          while len(l_pool) > 0:
              if end_mask.sum() == 0:
                  input_iids = uir_tuple[1][
                      [
                          mapped_ids[-sizes[idx]]
                          for idx, mapped_ids in enumerate(c_pool)
                      ]
                  ]
                  output_iids = uir_tuple[1][
                      [
                          mapped_ids[-sizes[idx] + 1]
                          for idx, mapped_ids in enumerate(c_pool)
                      ]
                  ]
                  sizes -= 1
                  for idx, size in enumerate(sizes):
                      if size == 1:
                          end_mask[idx] = 1
                  if n_sample > 0:
                      negative_samples = rng.choice(
                          item_indices,
                          size=n_sample,
                          replace=True,
                          p=item_dist,
                      )
                      output_iids = np.concatenate(
                          [output_iids, negative_samples]
                      )
                  yield input_iids, output_iids, start_mask, np.arange(
                      batch_size, dtype="int"
                  )
                  start_mask.fill(0)  # reset start masking
              while end_mask.sum() > 0 and len(l_pool) > 0:
                  next_seq = l_pool.pop()
                  if len(next_seq) > 1:
                      idx = np.nonzero(end_mask)[0][0]
                      end_mask[idx] = 0
                      start_mask[idx] = 1
                      c_pool[idx] = next_seq
                      sizes[idx] = len(c_pool[idx])
  
      valid_id = np.ones(batch_size, dtype="int")
      while True:
          for idx, size in enumerate(sizes):
              if size == 1:
                  end_mask[idx] = 1
                  valid_id[idx] = 0
          input_iids = uir_tuple[1][
              [
                  mapped_ids[-sizes[idx]]
                  for idx, mapped_ids in enumerate(c_pool)
                  if sizes[idx] > 1
              ]
          ]
          output_iids = uir_tuple[1][
              [
                  mapped_ids[-sizes[idx] + 1]
                  for idx, mapped_ids in enumerate(c_pool)
                  if sizes[idx] > 1
              ]
          ]
          sizes -= 1
          for idx, size in enumerate(sizes):
              if size == 1:
                  end_mask[idx] = 1
          start_mask = start_mask[np.nonzero(valid_id)[0]]
          end_mask = end_mask[np.nonzero(valid_id)[0]]
          sizes = sizes[np.nonzero(valid_id)[0]]
          c_pool = [
              _ for _, valid in zip(c_pool, valid_id) if valid > 0
          ]
          if n_sample > 0:
              negative_samples = rng.choice(
                  item_indices, size=n_sample, replace=True, p=item_dist
              )
              output_iids = np.concatenate(
                  [output_iids, negative_samples]
              )
          yield input_iids, output_iids, start_mask, np.nonzero(
              valid_id
          )[0]
          valid_id = np.ones(len(input_iids), dtype="int")
          if end_mask.sum() == len(input_iids):
              break
          start_mask.fill(0)
  
imports: from collections import Counter
  import numpy
  import torch
  from torch import nn
  from torch.autograd import Variable
  from torch.optim import Optimizer
  from cornac.utils.common import get_rng
label_desc: Use image node_id 2 for calling a global function with example usage: io_iter(s_iter, uir_tuple, n_sample, sample_alpha, rng, batch_size, shuffle) without return types