使用fluid.layers.split训练代码报错
测试代码如下
def test_metric_loss():
place = fluid.CPUPlace()
exe = fluid.Executor(place)
main_program = fluid.default_main_program()
x = fluid.layers.data(name='x', shape=[10], dtype='float32')
feature = fluid.layers.fc(input=x, size=20, act=None)
batch_size = 6
samples_each_class = 2
pos, neg = fluid.layers.split(feature, num_or_sections=[1, 20 - 1], dim=1)
if 1:
loss = fluid.layers.reduce_mean(pos)
print('loss', loss.shape)
else:
posmax = fluid.layers.reduce_max(pos)
negmin = fluid.layers.reduce_max(neg)
loss = posmax - negmin
print('posmax', posmax.shape, 'negmin', negmin.shape, 'loss', loss.shape)
exe.run(fluid.default_startup_program())
optimizer = fluid.optimizer.SGD(learning_rate=0.1)
opts = optimizer.minimize(loss)
batch_data = np.random.rand(batch_size,10).astype(np.float32)
output = exe.run(main_program,feed={'x': batch_data},fetch_list=[loss])
print(output)
```
报错信息
('loss', (1L,))
Traceback (most recent call last):
File "debugfluid.py", line 441, in <module>
func(*sys.argv[2:])
File "debugfluid.py", line 409, in test_metric_loss
opts = optimizer.minimize(loss)
File "/home/users/liuguoyi01/easy-paddle-run/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/optimizer.py", line 498, in minimize
no_grad_set=no_grad_set)
File "/home/users/liuguoyi01/easy-paddle-run/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/optimizer.py", line 403, in backward
no_grad_set, callbacks)
File "/home/users/liuguoyi01/easy-paddle-run/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/backward.py", line 518, in append_backward
_append_backward_vars_(root_block, fwd_op_num, grad_to_var, grad_info_map)
File "/home/users/liuguoyi01/easy-paddle-run/paddle-release/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/backward.py", line 354, in _append_backward_vars_
op_desc.infer_shape(block.desc)
paddle.fluid.core.EnforceNotMet: Cannot find variable split_0.tmp_1@GRAD at [/paddle/paddle/fluid/framework/op_desc.cc:193]
PaddlePaddle Call Stacks:
0 0x7f1fb60f3640p void paddle::platform::EnforceNotMet::Init<char const*>(char const*, char const*, int) + 352
1 0x7f1fb60f39b9p paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int) + 137
2 0x7f1fb62548a7p paddle::framework::CompileTimeInferShapeContext::GetDim(std::string const&) const + 1575
3 0x7f1fb62592fep paddle::framework::CompileTimeInferShapeContext::GetInputsDim(std::string const&) const + 142
4 0x7f1fb6533b48p paddle::operators::ConcatOp::InferShape(paddle::framework::InferShapeContext*) const + 264
5 0x7f1fb624e06ep paddle::framework::OpDesc::InferShape(paddle::framework::BlockDesc const&) const + 862
6 0x7f1fb61a497cp
7 0x7f1fb612672ep
8 0x7f20040ee55fp PyEval_EvalFrameEx + 29855
9 0x7f20040f086dp PyEval_EvalCodeEx + 2061
10 0x7f20040ed9fcp PyEval_EvalFrameEx + 26940
11 0x7f20040f086dp PyEval_EvalCodeEx + 2061
12 0x7f20040ed9fcp PyEval_EvalFrameEx + 26940
13 0x7f20040f086dp PyEval_EvalCodeEx + 2061
14 0x7f20040ed9fcp PyEval_EvalFrameEx + 26940
15 0x7f20040f086dp PyEval_EvalCodeEx + 2061
16 0x7f20040ed9fcp PyEval_EvalFrameEx + 26940
17 0x7f20040f086dp PyEval_EvalCodeEx + 2061
18 0x7f2004066f40p
19 0x7f2004034f13p PyObject_Call + 67
20 0x7f20040ea657p PyEval_EvalFrameEx + 13719
21 0x7f20040f086dp PyEval_EvalCodeEx + 2061
22 0x7f20040f09a2p PyEval_EvalCode + 50
23 0x7f2004119782p PyRun_FileExFlags + 146
24 0x7f200411aaf9p PyRun_SimpleFileExFlags + 217
25 0x7f200413082dp Py_Main + 3149
26 0x7f200332dbd5p __libc_start_main + 245
27 0x4007a1p
import paddle.fluid as fluid
import numpy as np
def test_metric_loss():
place = fluid.CPUPlace()
exe = fluid.Executor(place)
main_program = fluid.default_main_program()
x = fluid.layers.data(name='x', shape=[10], dtype='float32')
feature = fluid.layers.fc(input=x, size=20, act=None)
batch_size = 6
samples_each_class = 2
pos, neg = fluid.layers.split(feature, num_or_sections=[1, 20 - 1], dim=1)
if 1:
pos.stop_gradient = True
neg.stop_gradient = True
loss = fluid.layers.reduce_mean(pos)
print('loss', loss.shape)
else:
posmax = fluid.layers.reduce_max(pos)
negmin = fluid.layers.reduce_max(neg)
loss = posmax - negmin
print('posmax', posmax.shape, 'negmin', negmin.shape, 'loss', loss.shape)
exe.run(fluid.default_startup_program())
optimizer = fluid.optimizer.SGD(learning_rate=0.1)
opts = optimizer.minimize(loss)
batch_data = np.random.rand(batch_size,10).astype(np.float32)
output = exe.run(main_program,feed={'x': batch_data},fetch_list=[loss])
print(output)
test_metric_loss()
为啥要加 pos.stop_gradient = True
我们再check下
通过paddle 同学 ,问题已经定位, 目前paddle 不参与损失的叶子节点,需要手工增加stop_gradient属性。 希望paddle 报错信息增加相关的说明, 从原始报错信息基本得不到有用的反馈。
另外一个问题(else里面代码)是由于 exe.run(fluid.default_startup_program()),应该写在 minimize后面, 这块虽然定位了问题, 但报错信息也是没法帮助定位错误, 希望paddle改进
经验证,最新的dev版本(2019-7-9) 可以在非损失的叶子节点自动添加stop-gradient. 下面代码在注释掉 neg.stop_gradient = True 可以正常跑
def test_metric_loss():
place = fluid.CPUPlace()
exe = fluid.Executor(place)
main_program = fluid.default_main_program()
x = fluid.layers.data(name='x', shape=[10], dtype='float32')
feature = fluid.layers.fc(name='fc', input=x, size=20, act=None)
batch_size = 6
samples_each_class = 2
pos, neg = fluid.layers.split(feature, num_or_sections=[1, 20 - 1], dim=1)
#neg.stop_gradient = True
loss = fluid.layers.reduce_mean(pos)
optimizer = fluid.optimizer.SGD(learning_rate=0.1)
opts = optimizer.minimize(loss)
exe.run(fluid.default_startup_program())
batch_data = np.random.rand(batch_size,10).astype(np.float32)
output = exe.run(main_program,feed={'x': batch_data},fetch_list=[loss.name, 'fc.w_0@GRAD'])
print(output)
感谢paddle 修复这个问题
另外issue中提到的 关于 default_startup_program的报错信息,仍然让人看不懂,目前没有修复。我单开一个issue吧