class_docstr: Speeds up inference calls to MatrixFactorization models by using
  `Faiss <https://github.com/facebookresearch/faiss>`_ to create approximate nearest neighbours
  indices of the latent factors.
  
  Parameters
  ----------
  model : MatrixFactorizationBase
      A matrix factorization model to use for the factors
  nlist : int, optional
      The number of cells to use when building the Faiss index.
  nprobe : int, optional
      The number of cells to visit to perform a search.
  use_gpu : bool, optional
      Whether or not to enable run Faiss on the GPU. Requires faiss to have been
      built with GPU support.
  approximate_similar_items : bool, optional
      whether or not to build an Faiss index for computing similar_items
  approximate_recommend : bool, optional
      whether or not to build an Faiss index for the recommend call
  
  Attributes
  ----------
  similar_items_index : faiss.IndexIVFFlat
      Faiss index for looking up similar items in the cosine space formed by the latent
      item_factors
  
  recommend_index : faiss.IndexIVFFlat
      Faiss index for looking up similar items in the inner product space formed by the latent
      item_factors
class_docstr_tok: ["Speeds","up","inference","calls","to","MatrixFactorization","models","by","using","`","Faiss","<","https",":","\/\/github.com\/facebookresearch\/faiss",">","`","_","to","create","approximate","nearest","neighbours","indices","of","the","latent","factors",".","Parameters","--","--","--","--","--","model",":","MatrixFactorizationBase","A","matrix","factorization","model","to","use","for","the","factors","nlist",":","int",",","optional","The","number","of","cells","to","use","when","building","the","Faiss","index",".","nprobe",":","int",",","optional","The","number","of","cells","to","visit","to","perform","a","search",".","use_gpu",":","bool",",","optional","Whether","or","not","to","enable","run","Faiss","on","the","GPU",".","Requires","faiss","to","have","been","built","with","GPU","support",".","approximate_similar_items",":","bool",",","optional","whether","or","not","to","build","an","Faiss","index","for","computing","similar_items","approximate_recommend",":","bool",",","optional","whether","or","not","to","build","an","Faiss","index","for","the","recommend","call","Attributes","--","--","--","--","--","similar_items_index",":","faiss.IndexIVFFlat","Faiss","index","for","looking","up","similar","items","in","the","cosine","space","formed","by","the","latent","item_factors","recommend_index",":","faiss.IndexIVFFlat","Faiss","index","for","looking","up","similar","items","in","the","inner","product","space","formed","by","the","latent","item_factors"]
code: def fit(self, Cui, show_progress=True, callback=None):
      self.model.fit(Cui, show_progress, callback=callback)
  
      item_factors = self.model.item_factors
      if implicit.gpu.HAS_CUDA and isinstance(
          item_factors, implicit.gpu.Matrix
      ):
          item_factors = item_factors.to_numpy()
      item_factors = item_factors.astype("float32")
  
      self.factors = item_factors.shape[1]
  
      self.quantizer = faiss.IndexFlat(self.factors)
  
      if self.use_gpu:
          self.gpu_resources = faiss.StandardGpuResources()
  
      if self.approximate_recommend:
          log.debug("Building faiss recommendation index")
  
          # build up a inner product index here
          if self.use_gpu:
              index = faiss.GpuIndexIVFFlat(
                  self.gpu_resources,
                  self.factors,
                  self.nlist,
                  faiss.METRIC_INNER_PRODUCT,
              )
          else:
              index = faiss.IndexIVFFlat(
                  self.quantizer,
                  self.factors,
                  self.nlist,
                  faiss.METRIC_INNER_PRODUCT,
              )
  
          index.train(item_factors)
          index.add(item_factors)
          index.nprobe = self.nprobe
          self.recommend_index = index
  
      if self.approximate_similar_items:
          log.debug("Building faiss similar items index")
  
          # likewise build up cosine index for similar_items, using an inner product
          # index on normalized vectors`
          norms = np.linalg.norm(item_factors, axis=1)
          norms[norms == 0] = 1e-10
  
          normalized = (item_factors.T / norms).T.astype("float32")
          if self.use_gpu:
              index = faiss.GpuIndexIVFFlat(
                  self.gpu_resources,
                  self.factors,
                  self.nlist,
                  faiss.METRIC_INNER_PRODUCT,
              )
          else:
              index = faiss.IndexIVFFlat(
                  self.quantizer,
                  self.factors,
                  self.nlist,
                  faiss.METRIC_INNER_PRODUCT,
              )
  
          index.train(normalized)
          index.add(normalized)
          index.nprobe = self.nprobe
          self.similar_items_index = index
  
file_path: implicit/implicit/ann/faiss.py
imports: import logging
  import warnings
  import faiss
  import numpy
  from scipy.sparse import csr_matrix
  import implicit.gpu
  from implicit.recommender_base import RecommenderBase
  from implicit.utils import _batch_call, _filter_items_from_results
label_desc: Use image node_id 2 for calling the FaissModel obj's underlying member method code with example usage: obj.fit(Cui, show_progress, callback) without return types