class_docstr: Session-based Recommendations with Recurrent Neural Networks
  
  Parameters
  ----------
  name: string, default: 'GRU4Rec'
      The name of the recommender model.
  
  layers: list of int, optional, default: [100]
      The number of hidden units in each layer
  
  loss: str, optional, default: 'cross-entropy'
      Select the loss function.
  
  batch_size: int, optional, default: 512
      Batch size
  
  dropout_p_embed: float, optional, default: 0.0
      Dropout ratio for embedding layers
  
  dropout_p_hidden: float, optional, default: 0.0
      Dropout ratio for hidden layers
  
  learning_rate: float, optional, default: 0.05
      Learning rate for the optimizer
  
  momentum: float, optional, default: 0.0
      Momentum for adaptive learning rate
  
  sample_alpha: float, optional, default: 0.5
      Tradeoff factor controls the contribution of negative sample towards final loss
  
  n_sample: int, optional, default: 2048
      Number of negative samples
  
  embedding: int, optional, default: 0
  
  constrained_embedding: bool, optional, default: True
  
  n_epochs: int, optional, default: 10
  
  bpreg: float, optional, default: 1.0
      Regularization coefficient for 'bpr-max' loss.
  
  elu_param: float, optional, default: 0.5
      Elu param for 'bpr-max' loss
  
  logq: float, optional, default: 0,
      LogQ correction  to offset the sampling bias affecting 'cross-entropy' loss.
  
  device: str, optional, default: 'cpu'
      Set to 'cuda' for GPU support.
  
  trainable: boolean, optional, default: True
      When False, the model will not be re-trained, and input of pre-trained parameters are required.
  
  verbose: boolean, optional, default: True
      When True, running logs are displayed.
  
  seed: int, optional, default: None
      Random seed for weight initialization.
  
  References
  ----------
  Hidasi, B., Karatzoglou, A., Baltrunas, L., & Tikk, D. (2015).
  Session-based recommendations with recurrent neural networks.
  arXiv preprint arXiv:1511.06939.
code: def fit(self, train_set, val_set=None):
      super().fit(train_set, val_set)
      import torch
  
      from .gru4rec import GRU4RecModel, IndexedAdagradM, io_iter
  
      item_freq = Counter(self.train_set.uir_tuple[1])
      P0 = (
          torch.tensor(
              [
                  item_freq[iid]
                  for (_, iid) in self.train_set.iid_map.items()
              ],
              dtype=torch.float32,
              device=self.device,
          )
          if self.logq > 0
          else None
      )
  
      self.model = GRU4RecModel(
          n_items=self.total_items,
          P0=P0,
          layers=self.layers,
          dropout_p_embed=self.dropout_p_embed,
          dropout_p_hidden=self.dropout_p_hidden,
          embedding=self.embedding,
          constrained_embedding=self.constrained_embedding,
          logq=self.logq,
          sample_alpha=self.sample_alpha,
          bpreg=self.bpreg,
          elu_param=self.elu_param,
          loss=self.loss,
      ).to(self.device)
  
      self.model._reset_weights_to_compatibility_mode()
  
      opt = IndexedAdagradM(
          self.model.parameters(), self.learning_rate, self.momentum
      )
  
      progress_bar = trange(
          1, self.n_epochs + 1, disable=not self.verbose
      )
      for _ in progress_bar:
          H = []
          for i in range(len(self.layers)):
              H.append(
                  torch.zeros(
                      (self.batch_size, self.layers[i]),
                      dtype=torch.float32,
                      requires_grad=False,
                      device=self.device,
                  )
              )
          total_loss = 0
          cnt = 0
          for inc, (
              in_iids,
              out_iids,
              start_mask,
              valid_id,
          ) in enumerate(
              io_iter(
                  s_iter=self.train_set.s_iter,
                  uir_tuple=self.train_set.uir_tuple,
                  n_sample=self.n_sample,
                  sample_alpha=self.sample_alpha,
                  rng=self.rng,
                  batch_size=self.batch_size,
                  shuffle=True,
              )
          ):
              for i in range(len(H)):
                  H[i][np.nonzero(start_mask)[0], :] = 0
                  H[i].detach_()
                  H[i] = H[i][valid_id]
              in_iids = torch.tensor(
                  in_iids, requires_grad=False, device=self.device
              )
              out_iids = torch.tensor(
                  out_iids, requires_grad=False, device=self.device
              )
              self.model.zero_grad()
              R = self.model.forward(
                  in_iids, H, out_iids, training=True
              )
              L = self.model.loss_function(
                  R, out_iids, len(in_iids)
              ) / len(in_iids)
              L.backward()
              opt.step()
              total_loss += L.cpu().detach().numpy() * len(in_iids)
              cnt += len(in_iids)
              if inc % 10 == 0:
                  progress_bar.set_postfix(loss=(total_loss / cnt))
      return self
  
file_path: cornac/cornac/models/gru4rec/recom_gru4rec.py
imports: import numpy
  from tqdm.auto import trange
  from collections import Counter
  from cornac.models.recommender import NextItemRecommender
  from ...utils import get_rng
label_desc: Use image node_id 2 for calling the GRU4Rec obj's underlying member method code with example usage: obj.fit(train_set, val_set) and returns: self