embedding

paddle.nn.functional. embedding ( x, weight, padding_idx=None, sparse=False, name=None ) [source]

The operator is used to lookup embeddings vector of ids provided by x .

The shape of output Tensor is generated by appending the last dimension of the input Tensor shape with embedding size.

Note: The id in x must satisfy \(0 =< id < weight.shape[0]\) , otherwise the program will throw an exception and exit.

Case 1:
    x is a Tensor.
        padding_idx = -1
        x.data = [[1, 3], [2, 4], [4, 127]]
        x.shape = [3, 2]
        weight.shape = [128, 16]
    output is a Tensor:
        out.shape = [3, 2, 16]
        out.data = [[[0.129435295, 0.244512452, ..., 0.436322452],
                    [0.345421456, 0.524563927, ..., 0.144534654]],
                    [[0.345249859, 0.124939536, ..., 0.194353745],
                    [0.945345345, 0.435394634, ..., 0.435345365]],
                    [[0.945345345, 0.435394634, ..., 0.435345365],
                    [0.0,         0.0,         ..., 0.0        ]]]  # padding data

    The input padding_idx is less than 0, it is automatically converted to padding_idx = -1 + 128 = 127
    It will pad all-zero data when id is 127.
Parameters
  • x (Tensor) – A Tensor with type int32/int64, which contains the id information. The value of the input id should satisfy \(0<= id < weight.shape[0]\) .

  • weight (Tensor) – The weight. A Tensor with shape of lookup table parameter. It should have two elements which indicates the size of the dictionary of embeddings and the size of each embedding vector respectively.

  • sparse (bool) – The flag indicating whether to use sparse update. This parameter only affects the performance of the backwards gradient update. It is recommended to set True because sparse update is faster. But some optimizers does not support sparse update, such as Adadelta , Adamax , Lamb. In these cases, sparse must be False. Default: False.

  • padding_idx (int|long|None) – padding_idx needs to be in the interval [-weight.shape[0], weight.shape[0]). If \(padding\_idx < 0\), the \(padding\_idx\) will automatically be converted to \(weight.shape[0] + padding\_idx\) . It will output all-zero padding data whenever lookup encounters \(padding\_idx\) in id. And the padding data will not be updated while training. If set None, it makes no effect to output. Default: None.

  • name (str|None) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

Returns

Embedding Tensor mapped by x. The data type is the same as weight.

Return type

Tensor

Examples

import numpy as np
import paddle
import paddle.nn as nn

x0 = np.arange(3, 6).reshape((3, 1)).astype(np.int64)
w0 = np.full(shape=(10, 3), fill_value=2).astype(np.float32)

# x.data = [[3], [4], [5]]
# x.shape = [3, 1]
x = paddle.to_tensor(x0, stop_gradient=False)

# w.data = [[2. 2. 2.] ... [2. 2. 2.]]
# w.shape = [10, 3]
w = paddle.to_tensor(w0, stop_gradient=False)

# emb.data = [[[2., 2., 2.]], [[2., 2., 2.]], [[2., 2., 2.]]]
# emb.shape = [3, 1, 3]
emb = nn.functional.embedding(
        x=x, weight=w, sparse=True, name="embedding")