Paddle
Paddle copied to clipboard
embedding,reduce_mean之后,怎样再处理与lodtensor concate
def embedding_layer(input):
return fluid.layers.embedding(
input=input,
is_sparse=True,
is_distributed=False,
size=[10, 2],
param_attr=fluid.ParamAttr(name="SparseFeatFactors", initializer=fluid.initializer.Uniform()))
x1 = fluid.layers.data(name="x1", shape=[1], lod_level=1, dtype='int64')
'''
name: "x1"
type {
type: LOD_TENSOR
lod_tensor {
tensor {
data_type: INT64
dims: -1
dims: 1
}
lod_level: 1
}
}
例子:[[1]
[2]]
'''
sparse_embed_seq = embedding_layer(x1)
'''
name: "embedding_0.tmp_0"
type {
type: LOD_TENSOR
lod_tensor {
tensor {
data_type: FP32
dims: -1
dims: 2
}
lod_level: 1
}
}
persistable: false
例子:[[ 0.75940454 -0.7810694 ]
[ 0.9981985 0.03987873]]
'''
se3 = fluid.layers.reduce_mean(sparse_embed_seq, dim=0, keep_dim=True)
'''
name: "reduce_mean_0.tmp_0"
type {
type: LOD_TENSOR
lod_tensor {
tensor {
data_type: FP32
dims: 1
dims: 2
}
}
}
例子:[[ 0.8788015 -0.37059534]]
'''
dense_input = fluid.layers.data(name="dense_input", shape=[1], dtype='float32')
'''
name: "dense_input"
type {
type: LOD_TENSOR
lod_tensor {
tensor {
data_type: FP32
dims: -1
dims: 1
}
lod_level: 0
}
}
例子:[[0.2]]
'''
#----------------下面报错,因为第一个dim不一样------------------
Traceback (most recent call last):
File "test_emb_lod.py", line 45, in
concated = fluid.layers.concat([se3, dense_input], axis=1)
File "/home/work/anaconda2/lib/python2.7/site-packages/paddle/fluid/layers/tensor.py", line 192, in concat
attrs={'axis': axis})
File "/home/work/anaconda2/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 50, in append_op
return self.main_program.current_block().append_op(args, kwargs)
File "/home/work/anaconda2/lib/python2.7/site-packages/paddle/fluid/framework.py", line 1207, in append_op
op = Operator(block=self, desc=op_desc, args, kwargs)
File "/home/work/anaconda2/lib/python2.7/site-packages/paddle/fluid/framework.py", line 656, in init
self.desc.infer_shape(self.block.desc)
paddle.fluid.core.EnforceNotMet: Enforce failed. Expected out_dims[j] == ins[i][j], but received out_dims[j]:1 != ins[i][j]:-1.
Input tensors should have the same elements except the specify axis. at [/paddle/paddle/fluid/operators/concat_op.cc:52]
PaddlePaddle Call Stacks:
0 0x7fd018c33ab6p paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const, int) + 486
1 0x7fd0194d0e9ep paddle::operators::ConcatOp::InferShape(paddle::framework::InferShapeContext) const + 1502
2 0x7fd018cdefd6p paddle::framework::OpDesc::InferShape(paddle::framework::BlockDesc const&) const + 886
3 0x7fd018c8b7d5p void pybind11::cpp_function::initialize<pybind11::cpp_function::initialize<void, paddle::framework::OpDesc, paddle::framework::BlockDesc const&, pybind11::name, pybind11::is_method, pybind11::sibling>(void (paddle::framework::OpDesc::)(paddle::framework::BlockDesc const&) const, pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::OpDesc const, paddle::framework::BlockDesc const&)#1}, void, paddle::framework::OpDesc const, paddle::framework::BlockDesc const&, pybind11::name, pybind11::is_method, pybind11::sibling>(pybind11::cpp_function::initialize<void, paddle::framework::OpDesc, paddle::framework::BlockDesc const&, pybind11::name, pybind11::is_method, pybind11::sibling>(void (paddle::framework::OpDesc::)(paddle::framework::BlockDesc const&) const, pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(paddle::framework::OpDesc const*, paddle::framework::BlockDesc const&)#1}&&, void ()(paddle::framework::OpDesc const, paddle::framework::BlockDesc const&), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::{lambda(pybind11::detail::function_call&)#3}::_FUN(pybind11::detail::function_call) + 213
4 0x7fd018c68cb4p pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 2596
5 0x7fd03e37beecp PyEval_EvalFrameEx + 33468
6 0x7fd03e37d4e9p PyEval_EvalCodeEx + 2025
7 0x7fd03e3060c7p
8 0x7fd03e2e1773p PyObject_Call + 67
9 0x7fd03e2f050dp
10 0x7fd03e2e1773p PyObject_Call + 67
11 0x7fd03e33a254p
12 0x7fd03e336b0bp
13 0x7fd03e2e1773p PyObject_Call + 67
14 0x7fd03e3764d0p PyEval_EvalFrameEx + 10400
15 0x7fd03e37d4e9p PyEval_EvalCodeEx + 2025
16 0x7fd03e3060c7p
17 0x7fd03e2e1773p PyObject_Call + 67
18 0x7fd03e3764d0p PyEval_EvalFrameEx + 10400
19 0x7fd03e37d4e9p PyEval_EvalCodeEx + 2025
20 0x7fd03e37a482p PyEval_EvalFrameEx + 26706
21 0x7fd03e37d4e9p PyEval_EvalCodeEx + 2025
22 0x7fd03e37a482p PyEval_EvalFrameEx + 26706
23 0x7fd03e37d4e9p PyEval_EvalCodeEx + 2025
24 0x7fd03e37d70ap PyEval_EvalCode + 26
25 0x7fd03e39693dp
26 0x7fd03e397ab8p PyRun_FileExFlags + 120
27 0x7fd03e398cd8p PyRun_SimpleFileExFlags + 232
28 0x7fd03e3aad3cp Py_Main + 2988
29 0x7fd03d5c9445p __libc_start_main + 245
30 0x564d58a1c87fp
我想得到[[ 0.8788015 -0.37059534 0.2]] 是要把se3再转成序列再concate,还是怎样操作???
代码片段是这几行,上面加的注释是例子
def embedding_layer(input):
return fluid.layers.embedding(
input=input,
is_sparse=True,
is_distributed=False,
size=[10, 2],
param_attr=fluid.ParamAttr(name="SparseFeatFactors", initializer=fluid.initializer.Uniform()))
x1 = fluid.layers.data(name="x1", shape=[1], lod_level=1, dtype='int64')
sparse_embed_seq = embedding_layer(x1)
se3 = fluid.layers.reduce_mean(sparse_embed_seq, dim=0, keep_dim=True)
dense_input = fluid.layers.data(name="dense_input", shape=[1], dtype='float32')