MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / embedding

Function embedding

python/oneflow/nn/modules/sparse.py:193–263  ·  view source on GitHub ↗

r"""A simple lookup table that looks up embeddings in a fixed dictionary and size. This module is often used to retrieve word embeddings using indices. The input to the module is a list of indices, and the embedding matrix, and the output is the corresponding word embeddings. See :

(
    input,
    weight,
    padding_idx=None,
    max_norm=None,
    norm_type=2.0,
    scale_grad_by_freq=False,
    sparse=False,
)

Source from the content-addressed store, hash-verified

191
192
193def embedding(
194 input,
195 weight,
196 padding_idx=None,
197 max_norm=None,
198 norm_type=2.0,
199 scale_grad_by_freq=False,
200 sparse=False,
201):
202 r"""A simple lookup table that looks up embeddings in a fixed dictionary and size.
203
204 This module is often used to retrieve word embeddings using indices.
205 The input to the module is a list of indices, and the embedding matrix,
206 and the output is the corresponding word embeddings.
207
208 See :class:`oneflow.nn.Embedding` for more details.
209
210 Args:
211 input (oneflow.LongTensor): Tensor containing indices into the embedding matrix
212 weight (Tensor): The embedding matrix with number of rows equal to the maximum possible index + 1,
213 and number of columns equal to the embedding size
214 padding_idx (int, optional): If specified, the entries at :attr:`padding_idx` do not contribute to the gradient;
215 therefore, the embedding vector at :attr:`padding_idx` is not updated during training,
216 i.e. it remains as a fixed "pad".
217 max_norm (float, optional): If given, each embedding vector with norm larger than max_norm is renormalized to have
218 norm max_norm
219 norm_type (float, optional): The p of the p-norm to compute for the max_norm option. Default 2.
220 scale_grad_by_freq (boolean, optional): If given, this will scale gradients by the inverse of
221 frequency of the words in the mini-batch. Default False
222
223 For example:
224
225 .. code-block:: python
226
227 >>> import oneflow as flow
228 >>> import oneflow.nn.functional as F
229
230 >>> # a batch of 2 samples of 4 indices each
231 >>> input = flow.tensor([[1,2,4,5],[4,3,2,9]])
232 >>> # an embedding matrix containing 10 tensors of size 3
233 >>> embedding_matrix = flow.rand(10, 3)
234 >>> output = F.embedding(input, embedding_matrix)
235 >>> output.shape
236 oneflow.Size([2, 4, 3])
237 >>> # example with padding_idx
238 >>> input = flow.tensor([[0,2,0,5]])
239 >>> output = F.embedding(input, embedding_matrix, padding_idx=0)
240 >>> output.shape
241 oneflow.Size([1, 4, 3])
242 """
243
244 assert sparse is False, "Not support sparse=True yet!"
245 if padding_idx is not None:
246 if padding_idx > 0:
247 assert padding_idx < weight.size(
248 0
249 ), "Padding_idx must be within num_embeddings"
250 elif padding_idx < 0:

Callers 4

_test_embeddingFunction · 0.85
test_embedding_implMethod · 0.85
test_embedding_renormMethod · 0.85

Calls 1

sizeMethod · 0.45

Tested by 4

_test_embeddingFunction · 0.68
test_embedding_implMethod · 0.68
test_embedding_renormMethod · 0.68