class_docstr: Training pipeline for a neural matrix factorization model.
  
  ``NeuralCollaborativeFiltering`` models combine a collaborative filtering and multilayer
  perceptron network in a single, unified model. The model consists of two sections: the first
  is a simple matrix factorization that calculates a score by multiplying together user and item
  embeddings (lookups through an embedding table); the second is a MLP network that feeds
  embeddings from a second set of embedding tables (one for user, one for item). Both output
  vectors are combined and sent through a final MLP layer before returning a single recommendation
  score.
  
  The implementation here is meant to mimic its original implementation as specified here:
  https://arxiv.org/pdf/1708.05031.pdf [2]_
  
  All ``NeuralCollaborativeFiltering`` instances are subclasses of the ``LightningModule`` class
  provided by PyTorch Lightning. This means to train a model, you will need a
  ``collie.model.CollieTrainer`` object, but the model can be saved and loaded without this
  ``Trainer`` instance. Example usage may look like:
  
  .. code-block:: python
  
      from collie.model import CollieTrainer, NeuralCollaborativeFiltering
  
  
      model = NeuralCollaborativeFiltering(train=train)
      trainer = CollieTrainer(model)
      trainer.fit(model)
      model.eval()
  
      # do evaluation as normal with ``model``
  
      model.save_model(filename='model.pth')
      new_model = NeuralCollaborativeFiltering(load_model_path='model.pth')
  
      # do evaluation as normal with ``new_model``
  
  Parameters
  ----------
  embedding_dim: int
      Number of latent factors to use for the matrix factorization embedding table. For the MLP
      embedding table, the dimensionality will be calculated with the formula
      ``embedding_dim * (2 ** (num_layers - 1))``
  num_layers: int
      Number of MLP layers to apply. Each MLP layer will have its input dimension calculated with
      the formula ``embedding_dim * (2 ** (``num_layers`` - ``current_layer_number``))``
  final_layer: str or function
      Final layer activation function. Available string options include:
  
      * 'sigmoid'
  
      * 'relu'
  
      * 'leaky_relu'
  
  dropout_p: float
      Probability of dropout on the MLP layers
  optimizer: torch.optim or str
      If a string, one of the following supported optimizers:
  
      * ``'sgd'`` (for ``torch.optim.SGD``)
  
      * ``'adam'`` (for ``torch.optim.Adam``)
  
  References
  ----------
  .. [2] Xiangnan et al. "Neural Collaborative Filtering." Neural Collaborative Filtering |
      Proceedings of the 26th International Conference on World Wide Web, 1 Apr. 2017,
      dl.acm.org/doi/10.1145/3038912.3052569.
class_docstr_tok: ["Training","pipeline","for","a","neural","matrix","factorization","model",".","``","NeuralCollaborativeFiltering","``","models","combine","a","collaborative","filtering","and","multilayer","perceptron","network","in","a","single",",","unified","model",".","The","model","consists","of","two","sections",":","the","first","is","a","simple","matrix","factorization","that","calculates","a","score","by","multiplying","together","user","and","item","embeddings","(","lookups","through","an","embedding","table",")",";","the","second","is","a","MLP","network","that","feeds","embeddings","from","a","second","set","of","embedding","tables","(","one","for","user",",","one","for","item",")",".","Both","output","vectors","are","combined","and","sent","through","a","final","MLP","layer","before","returning","a","single","recommendation","score",".","The","implementation","here","is","meant","to","mimic","its","original","implementation","as","specified","here",":","https",":","\/\/arxiv.org\/pdf\/1708.05031.pdf","[","2","]","_","All","``","NeuralCollaborativeFiltering","``","instances","are","subclasses","of","the","``","LightningModule","``","class","provided","by","PyTorch","Lightning",".","This","means","to","train","a","model",",","you","will","need","a","``","collie.model.CollieTrainer","``","object",",","but","the","model","can","be","saved","and","loaded","without","this","``","Trainer","``","instance",".","Example","usage","may","look","like",":","..","code-block",":",":","python","from","collie.model","import","CollieTrainer",",","NeuralCollaborativeFiltering","model","=","NeuralCollaborativeFiltering","(","train=train",")","trainer","=","CollieTrainer","(","model",")","trainer.fit","(","model",")","model.eval","(",")","#","do","evaluation","as","normal","with","``","model","``","model.save_model","(","filename='model.pth","'",")","new_model","=","NeuralCollaborativeFiltering","(","load_model_path='model.pth","'",")","#","do","evaluation","as","normal","with","``","new_model","``","Parameters","--","--","--","--","--","embedding_dim",":","int","Number","of","latent","factors","to","use","for","the","matrix","factorization","embedding","table",".","For","the","MLP","embedding","table",",","the","dimensionality","will","be","calculated","with","the","formula","``","embedding_dim","*","(","2","*","*","(","num_layers","-","1",")",")","``","num_layers",":","int","Number","of","MLP","layers","to","apply",".","Each","MLP","layer","will","have","its","input","dimension","calculated","with","the","formula","``","embedding_dim","*","(","2","*","*","(","``","num_layers","``","-","``","current_layer_number","``",")",")","``","final_layer",":","str","or","function","Final","layer","activation","function",".","Available","string","options","include",":","*","'sigmoid'","*","'relu'","*","'leaky_relu'","dropout_p",":","float","Probability","of","dropout","on","the","MLP","layers","optimizer",":","torch.optim","or","str","If","a","string",",","one","of","the","following","supported","optimizers",":","*","``","'sgd","'","``","(","for","``","torch.optim.SGD","``",")","*","``","'adam","'","``","(","for","``","torch.optim.Adam","``",")","References","--","--","--","--","--","..","[","2","]","Xiangnan","et","al",".","``","Neural","Collaborative","Filtering",".","''","Neural","Collaborative","Filtering","|","Proceedings","of","the","26th","International","Conference","on","World","Wide","Web",",","1","Apr",".","2017",",","dl.acm.org\/doi\/10.1145\/3038912.3052569","."]
docstr: Forward pass through the model.
  
  Parameters
  ----------
  users: tensor, 1-d
      Array of user indices
  items: tensor, 1-d
      Array of item indices
  
  Returns
  -------
  preds: tensor, 1-d
      Predicted ratings or rankings
docstr_tok: ["Forward","pass","through","the","model",".","Parameters","--","--","--","--","--","users",":","tensor",",","1-d","Array","of","user","indices","items",":","tensor",",","1-d","Array","of","item","indices","Returns","--","--","--","-","preds",":","tensor",",","1-d","Predicted","ratings","or","rankings"]
code: def forward(
      self, users: torch.tensor, items: torch.tensor
  ) -> torch.tensor:
      """
      Forward pass through the model.
  
      Parameters
      ----------
      users: tensor, 1-d
          Array of user indices
      items: tensor, 1-d
          Array of item indices
  
      Returns
      -------
      preds: tensor, 1-d
          Predicted ratings or rankings
  
      """
      user_embedding_cf = self.user_embeddings_cf(users)
      item_embedding_cf = self.item_embeddings_cf(items)
      output_cf = user_embedding_cf * item_embedding_cf
  
      user_embedding_mlp = self.user_embeddings_mlp(users)
      item_embedding_mlp = self.item_embeddings_mlp(items)
      interaction = torch.cat(
          (user_embedding_mlp, item_embedding_mlp), -1
      )
      output_mlp = self.mlp_layers(interaction)
  
      concat = torch.cat((output_cf, output_mlp), -1)
  
      prediction = self.predict_layer(concat)
  
      if callable(self.hparams.final_layer):
          prediction = self.hparams.final_layer(prediction)
      elif self.hparams.final_layer == "sigmoid":
          prediction = torch.sigmoid(prediction)
      elif self.hparams.final_layer == "relu":
          prediction = F.relu(prediction)
      elif self.hparams.final_layer == "leaky_relu":
          prediction = F.leaky_relu(prediction)
      elif self.hparams.final_layer is not None:
          raise ValueError(
              f"{self.hparams.final_layer} not valid final layer value!"
          )
  
      return prediction.view(-1)
  
file_path: collie/collie/model/neural_collaborative_filtering.py
imports: from functools import partial
  from typing import Callable, Dict, Optional, Union
  import torch
  from torch import nn
  import torch.nn.functional
  from torch.optim.lr_scheduler import ReduceLROnPlateau
  from collie.model.base import BasePipeline, INTERACTIONS_LIKE_INPUT, ScaledEmbedding
  from collie.utils import get_init_arguments, merge_docstrings, trunc_normal
label_desc: Use image node_id 3 for calling the NeuralCollaborativeFiltering obj's underlying member method code with example usage: obj.forward(users, items) and returns: prediction