Created by: songyouwei
Currently, dygraph Embedding
layer uses the outdated lookup_table
op, which forces the last dimension of the input to be 1
. But fluid.embedding
uses lookup_table_v2
op.
This PR changes the underlying op to lookup_table_v2
, the shape of output Tensor is generated by appending an emb_size
dimension to the last dimension of the input Tensor shape.
Example:
before
import paddle.fluid as fluid
import numpy as np
dict_size = 100
emb_dim = 32
with fluid.dygraph.guard():
x_data = np.arange(12).reshape(4, 3).astype('int64')
x = fluid.dygraph.to_variable(x_data)
x = fluid.layers.unsqueeze(input=x, axes=[-1]) # need unsqueeze the last dim
emb = fluid.dygraph.Embedding(
size=[dict_size, emb_dim],
param_attr='emb.w',
is_sparse=False)
out = emb(x)
out.shape
after
import paddle.fluid as fluid
import numpy as np
dict_size = 100
emb_dim = 32
with fluid.dygraph.guard():
x_data = np.arange(12).reshape(4, 3).astype('int64')
x = fluid.dygraph.to_variable(x_data)
emb = fluid.dygraph.Embedding(
size=[dict_size, emb_dim],
param_attr='emb.w',
is_sparse=False)
out = emb(x)
out.shape