diff --git a/torch/nn/functional.py b/torch/nn/functional.py index e7c50305527..823a82a06ef 100644 --- a/torch/nn/functional.py +++ b/torch/nn/functional.py @@ -1976,7 +1976,7 @@ def embedding( Shape: - Input: LongTensor of arbitrary shape containing the indices to extract - Weight: Embedding matrix of floating point type with shape `(V, embedding_dim)`, - where V = maximum index + 1 and embedding_dim = the embedding size + where V = maximum index + 1 and embedding_dim = the embedding size - Output: `(*, embedding_dim)`, where `*` is the input shape Examples:: @@ -2082,33 +2082,25 @@ def embedding_bag( :attr:`offsets`, if those are not None. include_last_offset (bool, optional): if ``True``, the size of offsets is equal to the number of bags + 1. - The last element is the size of the input, or the ending index position of the last bag (sequence). + The last element is the size of the input, or the ending index position of the last bag (sequence). Shape: - - :attr:`input` (LongTensor) and :attr:`offsets` (LongTensor, optional) - - If :attr:`input` is 2D of shape `(B, N)`, + - If :attr:`input` is 2D of shape `(B, N)`, it will be treated as ``B`` bags (sequences) + each of fixed length ``N``, and this will return ``B`` values aggregated in a way + depending on the :attr:`mode`. :attr:`offsets` is ignored and required to be ``None`` in this case. - it will be treated as ``B`` bags (sequences) each of fixed length ``N``, and - this will return ``B`` values aggregated in a way depending on the :attr:`mode`. - :attr:`offsets` is ignored and required to be ``None`` in this case. + - If :attr:`input` is 1D of shape `(N)`, it will be treated as a concatenation of + multiple bags (sequences). :attr:`offsets` is required to be a 1D tensor containing + the starting index positions of each bag in :attr:`input`. Therefore, for :attr:`offsets` + of shape `(B)`, :attr:`input` will be viewed as having ``B`` bags. + Empty bags (i.e., having 0-length) will have returned vectors filled by zeros. - - If :attr:`input` is 1D of shape `(N)`, + - :attr:`weight` (Tensor): the learnable weights of the module of shape `(num_embeddings, embedding_dim)` - it will be treated as a concatenation of multiple bags (sequences). - :attr:`offsets` is required to be a 1D tensor containing the - starting index positions of each bag in :attr:`input`. Therefore, - for :attr:`offsets` of shape `(B)`, :attr:`input` will be viewed as - having ``B`` bags. Empty bags (i.e., having 0-length) will have - returned vectors filled by zeros. - - - :attr:`weight` (Tensor): the learnable weights of the module of - shape `(num_embeddings, embedding_dim)` - - - :attr:`per_sample_weights` (Tensor, optional). Has the same shape as - :attr:`input`. + - :attr:`per_sample_weights` (Tensor, optional). Has the same shape as :attr:`input`. - :attr:`output`: aggregated embedding values of shape `(B, embedding_dim)` diff --git a/torch/nn/modules/sparse.py b/torch/nn/modules/sparse.py index 283afb70c24..6499f788361 100644 --- a/torch/nn/modules/sparse.py +++ b/torch/nn/modules/sparse.py @@ -231,7 +231,7 @@ class EmbeddingBag(Module): EmbeddingBag also supports per-sample weights as an argument to the forward pass. This scales the output of the Embedding before performing a weighted - reduction as specified by ``mode``. If :attr:`per_sample_weights`` is passed, the + reduction as specified by ``mode``. If :attr:`per_sample_weights` is passed, the only supported ``mode`` is ``"sum"``, which computes a weighted sum according to :attr:`per_sample_weights`. @@ -259,34 +259,6 @@ class EmbeddingBag(Module): weight (Tensor): the learnable weights of the module of shape `(num_embeddings, embedding_dim)` initialized from :math:`\mathcal{N}(0, 1)`. - Inputs: :attr:`input` (IntTensor or LongTensor), :attr:`offsets` (IntTensor or LongTensor, optional), and - :attr:`per_index_weights` (Tensor, optional) - - - :attr:`input` and :attr:`offsets` have to be of the same type, either int or long - - - If :attr:`input` is 2D of shape `(B, N)`, - - it will be treated as ``B`` bags (sequences) each of fixed length ``N``, and - this will return ``B`` values aggregated in a way depending on the :attr:`mode`. - :attr:`offsets` is ignored and required to be ``None`` in this case. - - - If :attr:`input` is 1D of shape `(N)`, - - it will be treated as a concatenation of multiple bags (sequences). - :attr:`offsets` is required to be a 1D tensor containing the - starting index positions of each bag in :attr:`input`. Therefore, - for :attr:`offsets` of shape `(B)`, :attr:`input` will be viewed as - having ``B`` bags. Empty bags (i.e., having 0-length) will have - returned vectors filled by zeros. - - per_sample_weights (Tensor, optional): a tensor of float / double weights, or None - to indicate all weights should be taken to be ``1``. If specified, :attr:`per_sample_weights` - must have exactly the same shape as input and is treated as having the same - :attr:`offsets`, if those are not ``None``. Only supported for ``mode='sum'``. - - - Output shape: `(B, embedding_dim)` - Examples:: >>> # an Embedding module containing 10 tensors of size 3 @@ -336,6 +308,36 @@ class EmbeddingBag(Module): init.normal_(self.weight) def forward(self, input: Tensor, offsets: Optional[Tensor] = None, per_sample_weights: Optional[Tensor] = None) -> Tensor: + """Forward pass of EmbeddingBag. + + Args: + input (Tensor): Tensor containing bags of indices into the embedding matrix. + offsets (Tensor, optional): Only used when :attr:`input` is 1D. :attr:`offsets` determines + the starting index position of each bag (sequence) in :attr:`input`. + per_sample_weights (Tensor, optional): a tensor of float / double weights, or None + to indicate all weights should be taken to be ``1``. If specified, :attr:`per_sample_weights` + must have exactly the same shape as input and is treated as having the same + :attr:`offsets`, if those are not ``None``. Only supported for ``mode='sum'``. + + Returns: + Tensor output shape of `(B, embedding_dim)`. + + .. note:: + + A few notes about ``input`` and ``offsets``: + + - :attr:`input` and :attr:`offsets` have to be of the same type, either int or long + + - If :attr:`input` is 2D of shape `(B, N)`, it will be treated as ``B`` bags (sequences) + each of fixed length ``N``, and this will return ``B`` values aggregated in a way + depending on the :attr:`mode`. :attr:`offsets` is ignored and required to be ``None`` in this case. + + - If :attr:`input` is 1D of shape `(N)`, it will be treated as a concatenation of + multiple bags (sequences). :attr:`offsets` is required to be a 1D tensor containing the + starting index positions of each bag in :attr:`input`. Therefore, for :attr:`offsets` of shape `(B)`, + :attr:`input` will be viewed as having ``B`` bags. Empty bags (i.e., having 0-length) will have + returned vectors filled by zeros. + """ return F.embedding_bag(input, self.weight, offsets, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.mode, self.sparse,