class_docstr: Dataset class for NCF
class_docstr_tok: ["Dataset","class","for","NCF"]
docstr: Generator for serving batches of training data. Positive examples are loaded from the
  original training file, to which negative samples are added. Data is loaded in memory into a
  shuffle buffer up to a maximum of shuffle_size rows, before the data is shuffled and released.
  If out-of-memory errors are encountered, try reducing shuffle_size.
  
  Args:
      batch_size (int): Number of examples in each batch.
      shuffle_size (int): Maximum number of examples in shuffle buffer.
      yield_id (bool): If true, return assigned user and item IDs, else return original values.
      write_to (str): Path of file to write full dataset (including negative examples).
  
  Returns:
      list
docstr_tok: ["Generator","for","serving","batches","of","training","data",".","Positive","examples","are","loaded","from","the","original","training","file",",","to","which","negative","samples","are","added",".","Data","is","loaded","in","memory","into","a","shuffle","buffer","up","to","a","maximum","of","shuffle_size","rows",",","before","the","data","is","shuffled","and","released",".","If","out-of-memory","errors","are","encountered",",","try","reducing","shuffle_size",".","Args",":","batch_size","(","int",")",":","Number","of","examples","in","each","batch",".","shuffle_size","(","int",")",":","Maximum","number","of","examples","in","shuffle","buffer",".","yield_id","(","bool",")",":","If","true",",","return","assigned","user","and","item","IDs",",","else","return","original","values",".","write_to","(","str",")",":","Path","of","file","to","write","full","dataset","(","including","negative","examples",")",".","Returns",":","list"]
code: def train_loader(
      self, batch_size, shuffle_size=None, yield_id=False, write_to=None
  ):
      """
      Generator for serving batches of training data. Positive examples are loaded from the
      original training file, to which negative samples are added. Data is loaded in memory into a
      shuffle buffer up to a maximum of shuffle_size rows, before the data is shuffled and released.
      If out-of-memory errors are encountered, try reducing shuffle_size.
  
      Args:
          batch_size (int): Number of examples in each batch.
          shuffle_size (int): Maximum number of examples in shuffle buffer.
          yield_id (bool): If true, return assigned user and item IDs, else return original values.
          write_to (str): Path of file to write full dataset (including negative examples).
  
      Returns:
          list
      """
  
      # if shuffle_size not supplied, use (estimated) full data size i.e. complete in-memory shuffle
      if shuffle_size is None:
          shuffle_size = self.train_len * (self.n_neg + 1)
      if write_to:
          pd.DataFrame(
              columns=[self.col_user, self.col_item, self.col_rating]
          ).to_csv(write_to, header=True, index=False)
      shuffle_buffer = []
  
      with self.train_datafile as train_datafile:
          for user in train_datafile.users:
              user_positive_examples = train_datafile.load_data(user)
              user_positive_item_pool = set(
                  user_positive_examples[self.col_item].unique()
              )
              n_samples = self.n_neg * user_positive_examples.shape[0]
              sampler = NegativeSampler(
                  user,
                  n_samples,
                  user_positive_item_pool,
                  self.train_datafile.items,
                  self.sample_with_replacement,
                  self.print_warnings,
              )
              user_negative_examples = (
                  self._create_negative_examples_df(
                      user, sampler.sample()
                  )
              )
              user_examples = pd.concat(
                  [user_positive_examples, user_negative_examples]
              )
              shuffle_buffer.append(user_examples)
              shuffle_buffer_len = sum(
                  [df.shape[0] for df in shuffle_buffer]
              )
              if shuffle_buffer_len >= shuffle_size:
                  buffer_remainder = (
                      yield from self._release_shuffle_buffer(
                          shuffle_buffer, batch_size, yield_id, write_to
                      )
                  )
                  shuffle_buffer = (
                      [buffer_remainder]
                      if buffer_remainder is not None
                      else []
                  )
          # yield remaining buffer
          yield from self._release_shuffle_buffer(
              shuffle_buffer, batch_size, yield_id, write_to
          )
  
imports: import os
  from collections import OrderedDict
  import random
  import numpy
  import pandas
  import csv
  import logging
  from tqdm import tqdm
  from recommenders.utils.constants import DEFAULT_ITEM_COL, DEFAULT_USER_COL, DEFAULT_RATING_COL
label_desc: Use image node_id 8 for calling the Dataset obj's underlying member method code with example usage: obj.train_loader(batch_size, shuffle_size, yield_id, write_to) without return types