如何使用embedding和LodTensor
Created by: yangperasd
paddlepaddle version: 1.1.0 python version: 3.5 非常简单的问题,如何embedding和LodTensor
- embedding
关于embedding,官网已经给出了很多例子,但是以下简单程序产生的错误让人无法明白错在哪里, 希望官方能给予解释。
data = [9,2,1,1,5,4,5,2]
data = np.array(data, dtype='int64')
data = data.reshape(2,4)
with fluid.program_guard(main_program=fluid.Program(), startup_program=fluid.Program()):
exe = fluid.Executor(place=fluid.CPUPlace())
x = fluid.layers.data(shape=[4], name='x',dtype='int64')
word = fluid.layers.embedding(input=x, size=[10,4])
exe.run(fluid.default_startup_program())
result = exe.run(fluid.default_main_program(),
feed={'x':data},
fetch_list=[word],
return_numpy=False)
print(np.array(result[0]))
错误信息:
EnforceNotMet: Enforce failed. Expected ids_dims[ids_rank - 1] == 1, but received ids_dims[ids_rank - 1]:4 != 1:1. The last dimension of the 'Ids' tensor must be 1. at [/paddle/paddle/fluid/operators/lookup_table_op.cc:39] PaddlePaddle Call Stacks: 0 0x7f60d54afbd6p paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int) + 486 1 0x7f60d56eb49ep paddle::operators::LookupTableOp::InferShape(paddle::framework::InferShapeContext*) const + 1470 2 0x7f60d555b3c6p paddle::framework::OpDesc::InferShape(paddle::framework::BlockDesc const&) const + 886 ...
- LodTensor 关于LodTensor,官网解释为变长的Tensor,但是以下程序显示无法将LodTensor传递给一个全连接网络,请问如何操作。
data = [9,2,1,1,5,4,5,2]
data = np.array(data, dtype='float32')
data = fluid.create_lod_tensor(data,[[3,5]],place=fluid.CPUPlace())
with fluid.program_guard(main_program=fluid.Program(), startup_program=fluid.Program()):
exe = fluid.Executor(place=fluid.CPUPlace())
x = fluid.layers.data(shape=[5], name='x',dtype='float32',lod_level=1)
fc_x = fluid.layers.fc(input=x, size=4)
exe.run(fluid.default_startup_program())
result = exe.run(fluid.default_main_program(),
feed={'x':data},
fetch_list=[fc_x],
return_numpy=False)
print(np.array(result[0]))
错误信息:
EnforceNotMet: Enforce failed. Expected x_dims.size() > x_num_col_dims, but received x_dims.size():1 <= x_num_col_dims:1. The input tensor X's rank of MulOp should be larger than x_num_col_dims. at [/paddle/paddle/fluid/operators/mul_op.cc:48] ...
谢谢。