class_docstr: An embedding layer that compresses the number of embedding
  parameters required by using bloom filter-like hashing.
  
  Parameters
  ----------
  
  num_embeddings: int
      Number of entities to be represented.
  embedding_dim: int
      Latent dimension of the embedding.
  compression_ratio: float, optional
      The underlying number of rows in the embedding layer
      after compression. Numbers below 1.0 will use more
      and more compression, reducing the number of parameters
      in the layer.
  num_hash_functions: int, optional
      Number of hash functions used to compute the bloom filter indices.
  bag: bool, optional
      Whether to use the ``EmbeddingBag`` layer for the underlying embedding.
      This should be faster in principle, but currently seems to perform
      very poorly.
  
  Notes
  -----
  
  Large embedding layers are a performance problem for fitting models:
  even though the gradients are sparse (only a handful of user and item
  vectors need parameter updates in every minibatch), PyTorch updates
  the entire embedding layer at every backward pass. Computation time
  is then wasted on applying zero gradient steps to whole embedding matrix.
  
  To alleviate this problem, we can use a smaller underlying embedding layer,
  and probabilistically hash users and items into that smaller space. With
  good hash functions, collisions should be rare, and we should observe
  fitting speedups without a decrease in accuracy.
  
  The idea follows the RecSys 2017 "Getting recommenders fit"[1]_
  paper. The authors use a bloom-filter-like approach to hashing. Their approach
  uses one-hot encoded inputs followed by fully connected layers as
  well as softmax layers for the output, and their hashing reduces the
  size of the fully connected layers rather than embedding layers as
  implemented here; mathematically, however, the two formulations are
  identical.
  
  The hash function used is murmurhash3, hashing the indices with a different
  seed for every hash function, modulo the size of the compressed embedding layer.
  The hash mapping is computed once at the start of training, and indexed
  into for every minibatch.
  
  References
  ----------
  
  .. [1] Serra, Joan, and Alexandros Karatzoglou.
     "Getting deep recommenders fit: Bloom embeddings
     for sparse binary input/output networks."
     arXiv preprint arXiv:1706.03993 (2017).
class_docstr_tok: ["An","embedding","layer","that","compresses","the","number","of","embedding","parameters","required","by","using","bloom","filter-like","hashing",".","Parameters","--","--","--","--","--","num_embeddings",":","int","Number","of","entities","to","be","represented",".","embedding_dim",":","int","Latent","dimension","of","the","embedding",".","compression_ratio",":","float",",","optional","The","underlying","number","of","rows","in","the","embedding","layer","after","compression",".","Numbers","below","1.0","will","use","more","and","more","compression",",","reducing","the","number","of","parameters","in","the","layer",".","num_hash_functions",":","int",",","optional","Number","of","hash","functions","used","to","compute","the","bloom","filter","indices",".","bag",":","bool",",","optional","Whether","to","use","the","``","EmbeddingBag","``","layer","for","the","underlying","embedding",".","This","should","be","faster","in","principle",",","but","currently","seems","to","perform","very","poorly",".","Notes","--","--","-","Large","embedding","layers","are","a","performance","problem","for","fitting","models",":","even","though","the","gradients","are","sparse","(","only","a","handful","of","user","and","item","vectors","need","parameter","updates","in","every","minibatch",")",",","PyTorch","updates","the","entire","embedding","layer","at","every","backward","pass",".","Computation","time","is","then","wasted","on","applying","zero","gradient","steps","to","whole","embedding","matrix",".","To","alleviate","this","problem",",","we","can","use","a","smaller","underlying","embedding","layer",",","and","probabilistically","hash","users","and","items","into","that","smaller","space",".","With","good","hash","functions",",","collisions","should","be","rare",",","and","we","should","observe","fitting","speedups","without","a","decrease","in","accuracy",".","The","idea","follows","the","RecSys","2017","``","Getting","recommenders","fit","''","[","1","]","_","paper",".","The","authors","use","a","bloom-filter-like","approach","to","hashing",".","Their","approach","uses","one-hot","encoded","inputs","followed","by","fully","connected","layers","as","well","as","softmax","layers","for","the","output",",","and","their","hashing","reduces","the","size","of","the","fully","connected","layers","rather","than","embedding","layers","as","implemented","here",";","mathematically",",","however",",","the","two","formulations","are","identical",".","The","hash","function","used","is","murmurhash3",",","hashing","the","indices","with","a","different","seed","for","every","hash","function",",","modulo","the","size","of","the","compressed","embedding","layer",".","The","hash","mapping","is","computed","once","at","the","start","of","training",",","and","indexed","into","for","every","minibatch",".","References","--","--","--","--","--","..","[","1","]","Serra",",","Joan",",","and","Alexandros","Karatzoglou",".","``","Getting","deep","recommenders","fit",":","Bloom","embeddings","for","sparse","binary","input\/output","networks",".","''","arXiv","preprint","arXiv:1706.03993","(","2017",")","."]
code: def __init__(
      self,
      num_embeddings,
      embedding_dim,
      compression_ratio=0.2,
      num_hash_functions=4,
      bag=False,
      padding_idx=0,
  ):
      super(BloomEmbedding, self).__init__()
  
      self.num_embeddings = num_embeddings
      self.embedding_dim = embedding_dim
      self.compression_ratio = compression_ratio
      self.compressed_num_embeddings = int(
          compression_ratio * num_embeddings
      )
      self.num_hash_functions = num_hash_functions
      self.padding_idx = padding_idx
      self._bag = bag
  
      if num_hash_functions > len(SEEDS):
          raise ValueError(
              "Can use at most {} hash functions ({} requested)".format(
                  len(SEEDS), num_hash_functions
              )
          )
  
      self._masks = SEEDS[: self.num_hash_functions]
  
      if self._bag:
          self.embeddings = ScaledEmbeddingBag(
              self.compressed_num_embeddings,
              self.embedding_dim,
              mode="sum",
          )
      else:
          self.embeddings = ScaledEmbedding(
              self.compressed_num_embeddings,
              self.embedding_dim,
              padding_idx=self.padding_idx,
          )
  
      # Hash cache. We pre-hash all the indices, and then just
      # map the indices to their pre-hashed values as we go
      # through the minibatches.
      self._hashes = None
      self._offsets = None
  
file_path: spotlight/spotlight/layers.py
imports: import numpy
  from sklearn.utils import murmurhash3_32
  import torch
  import torch.nn
label_desc: Use image node_id 1 to create a new BloomEmbedding object from inherited base classes: nn with example: obj = BloomEmbedding(num_embeddings, embedding_dim, compression_ratio, num_hash_functions, bag, padding_idx)