模型定义optimizer 时报错,cos_sim_op.cc 显示有维数错误
Created by: maurise-yq-hu
-
版本、环境信息: 1)PaddlePaddle版本:1.5 2)CPU: 3)GPU:None 4)系统环境:centos 6.3, python 2.7.13
-
训练信息 1)单机,3cpu
-
复现信息: 逐步执行时,发现是这一步出错:optimizer.minimize(train_loss)。
-
问题描述:请详细描述您的问题,同步贴出报错信息、日志、可复现的代码片段 报错信息:
Traceback (most recent call last):
File "mf_model_test.py", line 93, in <module>
optimizer.minimize(train_loss)
File "</home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/decorator.pyc:decorator-gen-20>", line 2, in minimize
File "/home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
return wrapped_func(*args, **kwargs)
File "/home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/paddle/fluid/dygraph/base.py", line 87, in __impl__
return func(*args, **kwargs)
File "/home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/paddle/fluid/optimizer.py", line 594, in minimize
no_grad_set=no_grad_set)
File "/home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/paddle/fluid/optimizer.py", line 493, in backward
no_grad_set, callbacks)
File "/home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/paddle/fluid/backward.py", line 578, in append_backward
_append_backward_vars_(root_block, fwd_op_num, grad_to_var, grad_info_map)
File "/home/paddle/tools/paddle_release_home/python/lib/python2.7/site-packages/paddle/fluid/backward.py", line 392, in _append_backward_vars_
op_desc.infer_shape(block.desc)
paddle.fluid.core_avx.EnforceNotMet: Enforce failed. Expected out_grad_dims == target_xnorm_dims, but received out_grad_dims:1 != target_xnorm_dims:-1, 1.
Shape of Input(Out@Grad) must be [X.Dim(0), 1]. at [/paddle/paddle/fluid/operators/cos_sim_op.cc:150]
PaddlePaddle Call Stacks:
0 0x7f7526dfc808p void paddle::platform::EnforceNotMet::Init<std::string>(std::string, char const*, int) + 360
1 0x7f7526dfcb57p paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const*, int) + 87
2 0x7f75270c62f2p paddle::operators::CosSimOpGrad::InferShape(paddle::framework::InferShapeContext*) const + 4594
3 0x7f7526f62dfep paddle::framework::OpDesc::InferShape(paddle::framework::BlockDesc const&) const + 862
4 0x7f7526ea12dcp
5 0x7f7526e2b9b6p
6 0x7f755e82fbb8p PyEval_EvalFrameEx + 25016
7 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
8 0x7f755e830345p PyEval_EvalFrameEx + 26949
9 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
10 0x7f755e830345p PyEval_EvalFrameEx + 26949
11 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
12 0x7f755e830345p PyEval_EvalFrameEx + 26949
13 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
14 0x7f755e7a9f85p
15 0x7f755e777df3p PyObject_Call + 67
16 0x7f755e82cf97p PyEval_EvalFrameEx + 13719
17 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
18 0x7f755e7a9f85p
19 0x7f755e777df3p PyObject_Call + 67
20 0x7f755e82cf97p PyEval_EvalFrameEx + 13719
21 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
22 0x7f755e830345p PyEval_EvalFrameEx + 26949
23 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
24 0x7f755e830345p PyEval_EvalFrameEx + 26949
25 0x7f755e8330bdp PyEval_EvalCodeEx + 2061
26 0x7f755e8331f2p PyEval_EvalCode + 50
27 0x7f755e85bf42p PyRun_FileExFlags + 146
28 0x7f755e85d2d9p PyRun_SimpleFileExFlags + 217
29 0x7f755e87300dp Py_Main + 3149
30 0x7f755da70bd5p __libc_start_main + 245
31 0x4007a1p
代码:
class TmpCl():
def set_network(self):
self.dim["usr_dct_size"] = 7
self.dim["news_dct_size"] = 4
# input data
self.dat_lr["usr"] = fluid.layers.data(name='usr_in', shape=[1], dtype='int64')
self.dat_lr["news"] = fluid.layers.data(name='news_in', shape=[1], dtype='int64')
self.dat_lr["label"] = fluid.layers.data(name='label_in', shape=[1], dtype='int64')
# embedding layers
self.lr["usr_1"] = fluid.layers.embedding(input=self.dat_lr["usr"],
size=[self.dim["usr_dct_size"], 32],
is_sparse=False)
self.lr["news_1"] = fluid.layers.embedding(input=self.dat_lr["news"],
size=[self.dim["news_dct_size"], 32],
is_sparse=False)
self.lr["usr_2"] = fluid.layers.embedding(input=self.dat_lr["usr"],
size=[self.dim["usr_dct_size"], 1],
is_sparse=False)
self.lr["news_2"] = fluid.layers.embedding(input=self.dat_lr["news"],
size=[self.dim["news_dct_size"], 1],
is_sparse=False)
self.lr["mf_inner_prod"] = fluid.layers.cos_sim(self.lr["usr_1"], self.lr["news_1"])
self.lr["sim_sum"] = fluid.layers.sum([self.lr["mf_inner_prod"], self.lr["usr_2"], self.lr["news_2"]])
self.lr["prediction"] = fluid.layers.sigmoid(self.lr["sim_sum"])
network = TmpCl()
train_prog = fluid.Program()
train_startup = fluid.Program()
with fluid.program_guard(train_prog, train_startup):
# Use fluid.unique_name.guard() to share parameters with test network
with fluid.unique_name.guard():
_ = network.set_network()
train_loss = fluid.layers.square_error_cost(input=network.lr["prediction"], label=network.dat_lr["label"])
optimizer = fluid.optimizer.SGD(learning_rate=0.001)
optimizer.minimize(train_loss)
逐步执行时确认过,cos_sim层的两个输入维数是相同的
-> self.lr["mf_inner_prod"] = fluid.layers.cos_sim(self.lr["usr_1"], self.lr["news_1"])
(Pdb) self.lr["usr_1"].shape
(-1L, 32L)
(Pdb) self.lr["news_1"].shape
(-1L, 32L)
(Pdb) self.lr["usr_1"].dtype
VarType.FP32
(Pdb) self.lr["news_1"].dtype
VarType.FP32