class_docstr: The ImplicitModelTrainer class implements logics for basic recommender training and evaluation using users' 
  *implicit feedback*. 
  
  Parameters
  ----------
  batch_size: int
      Training batch size.
  test_batch_size: int
      Test/Evaluation batch size (number of users per testing batch).
  train_dataset: Dataset
      Dataset for model training.
  model: Recommender
      The target recommender.
  sampler: Sampler
      The sampler for model training.
  item_serving_size: int, optional
      Test/Evaluation batch size (number of items per testing batch).
  
  Notes  
  -----
  
  The function :code:`train` should be called for model training and evaluation.
class_docstr_tok: ["The","ImplicitModelTrainer","class","implements","logics","for","basic","recommender","training","and","evaluation","using","users","'","*","implicit","feedback","*",".","Parameters","--","--","--","--","--","batch_size",":","int","Training","batch","size",".","test_batch_size",":","int","Test\/Evaluation","batch","size","(","number","of","users","per","testing","batch",")",".","train_dataset",":","Dataset","Dataset","for","model","training",".","model",":","Recommender","The","target","recommender",".","sampler",":","Sampler","The","sampler","for","model","training",".","item_serving_size",":","int",",","optional","Test\/Evaluation","batch","size","(","number","of","items","per","testing","batch",")",".","Notes","--","--","-","The","function",":","code",":","`","train","`","should","be","called","for","model","training","and","evaluation","."]
docstr: Train and evaluate a recommender.
  
  Parameters
  ----------
  num_itr: int
      total number of training iterations.
  display_itr: int
      Evaluation/testing period.
  eval_datasets: list of Dataset
      A list of datasets for evaluation/testing.
  evaluators: list of Evaluator
      A list of evaluators for evaluation/testing.
  num_negatives: int, optional
      If specified, a given number of items NOT interacted with each user will be sampled (as negative items) for evaluations.
docstr_tok: ["Train","and","evaluate","a","recommender",".","Parameters","--","--","--","--","--","num_itr",":","int","total","number","of","training","iterations",".","display_itr",":","int","Evaluation\/testing","period",".","eval_datasets",":","list","of","Dataset","A","list","of","datasets","for","evaluation\/testing",".","evaluators",":","list","of","Evaluator","A","list","of","evaluators","for","evaluation\/testing",".","num_negatives",":","int",",","optional","If","specified",",","a","given","number","of","items","NOT","interacted","with","each","user","will","be","sampled","(","as","negative","items",")","for","evaluations","."]
code: def train(
      self,
      num_itr,
      display_itr,
      eval_datasets=[],
      evaluators=[],
      num_negatives=None,
      seed=10,
  ):
      """Train and evaluate a recommender.
  
      Parameters
      ----------
      num_itr: int
          total number of training iterations.
      display_itr: int
          Evaluation/testing period.
      eval_datasets: list of Dataset
          A list of datasets for evaluation/testing.
      evaluators: list of Evaluator
          A list of evaluators for evaluation/testing.
      num_negatives: int, optional
          If specified, a given number of items NOT interacted with each user will be sampled (as negative items) for evaluations.
      """
      acc_loss = 0
      self._eval_manager = ImplicitEvalManager(evaluators=evaluators)
      self._num_negatives = num_negatives
      self._exclude_positives(eval_datasets)
  
      if self._num_negatives is None:
          eval_func = self._evaluate_full
          print(
              colored(
                  "== Start training with FULL evaluation ==", "blue"
              )
          )
      else:
          eval_func = self._evaluate_partial
          self._sample_negatives(seed=seed)
          print(
              colored(
                  "== Start training with sampled evaluation, sample size: %d =="
                  % num_negatives,
                  "blue",
              )
          )
  
      for itr in range(num_itr):
          batch_data = self._sampler.next_batch()
          loss = self._model.train(batch_data)
          acc_loss += loss
          if itr % (display_itr // 10) == 0 and itr > 0:
              print(colored("[Itr %d] Finished" % itr, "blue"))
          if itr % display_itr == 0 and itr > 0:
              if self._eval_save_prefix:
                  self._model.save(self._eval_save_prefix, itr)
  
              print(
                  colored("[Itr %d]" % itr, "red"),
                  "loss: %f" % (acc_loss / display_itr),
              )
              for dataset in eval_datasets:
                  print(
                      colored(
                          "..(dataset: %s) evaluation" % dataset.name,
                          "green",
                      )
                  )
                  sys.stdout.flush()
                  eval_results = eval_func(eval_dataset=dataset)
                  for key, result in eval_results.items():
                      average_result = np.mean(result, axis=0)
                      if type(average_result) is np.ndarray:
                          print(
                              colored(
                                  "..(dataset: %s)" % dataset.name,
                                  "green",
                              ),
                              key,
                              " ".join(
                                  [str(s) for s in average_result]
                              ),
                          )
                      else:
                          print(
                              colored(
                                  "..(dataset: %s)" % dataset.name,
                                  "green",
                              ),
                              key,
                              average_result,
                          )
              acc_loss = 0
  
file_path: openrec/openrec/tf1/legacy/implicit_model_trainer.py
imports: from __future__ import print_function
  from tqdm import tqdm
  import math
  from termcolor import colored
  import numpy
  from openrec.tf1.legacy.utils.evaluators import ImplicitEvalManager
  import sys
  import json
  import pickle
label_desc: Use image node_id 2 for calling the ImplicitModelTrainer obj's underlying member method code with example usage: obj.train(num_itr, display_itr, eval_datasets, evaluators, num_negatives, seed) without return types