Fluid中使用Inferencer出现错误

原创
2019/03/08 11:24
阅读数 113
  • 问题描述:使用Fluid版本的PaddlePaddle编写一个简单的结构时,训练没有问题,但在进行Inferencer预测操作时,报DataType of Paddle Op mul must be the same错误,我检查了自己的数据预测操作,确认了数据类型与数据结构都与Inferencer预测网络中输入层定义的数据类型与结构一致,但依旧报错

  • 报错输出:

Traceback (most recent call last):
  File "/Users/jizhi/Desktop/Paddle/Paddlecode/code1.py", line 119, in <module>
    results = inferencer.infer({'mm': test_x})
  File "/Users/jizhi/anaconda3/envs/paddle/lib/python3.5/site-packages/paddle/fluid/contrib/inferencer.py", line 104, in infer
    return_numpy=return_numpy)
  File "/Users/jizhi/anaconda3/envs/paddle/lib/python3.5/site-packages/paddle/fluid/executor.py", line 470, in run
    self.executor.run(program.desc, scope, 0, True, True)
paddle.fluid.core.EnforceNotMet: DataType of Paddle Op mul must be the same. Get mm(5) != fc_0.w_0(6) at [/Users/paddle/minqiyang/Paddle/paddle/fluid/framework/operator.cc:847]
PaddlePaddle Call Stacks: 
0          0x10e2eaa68p paddle::platform::EnforceNotMet::EnforceNotMet(std::exception_ptr, char const*, int) + 760
1          0x10f114a10p paddle::framework::OperatorWithKernel::IndicateDataType(paddle::framework::ExecutionContext const&) const + 864
2          0x10f114aacp paddle::framework::OperatorWithKernel::GetExpectedKernelType(paddle::framework::ExecutionContext const&) const + 44
3          0x10f113099p paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const + 265
4          0x10f10f141p paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) + 577
5          0x10e3b83a6p paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 390
6          0x10e3b7dd3p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 163
7          0x10e31e837p void pybind11::cpp_function::initialize<paddle::pybind::pybind11_init()::$_64, void, paddle::framework::Executor&, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(paddle::pybind::pybind11_init()::$_64&&, void (*)(paddle::framework::Executor&, paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::'lambda'(pybind11::detail::function_call&)::__invoke(pybind11::detail::function_call&) + 135
8          0x10e2f53aap pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 5786
9          0x10141659fp PyCFunction_Call + 127
10         0x1014e17e7p PyEval_EvalFrameEx + 33207
11         0x1014d7fafp _PyEval_EvalCodeWithName + 335
12         0x1014de2a7p PyEval_EvalFrameEx + 19575
13         0x1014d7fafp _PyEval_EvalCodeWithName + 335
14         0x1014de2a7p PyEval_EvalFrameEx + 19575
15         0x1014d7fafp _PyEval_EvalCodeWithName + 335
16         0x10152a758p PyRun_FileExFlags + 248
17         0x101529eeep PyRun_SimpleFileExFlags + 382
18         0x10154ed86p Py_Main + 3622
19         0x101390861p main + 497
20      0x7fff5dffe015p start + 1
21                 0x2p
  • 问题复现:
def train_program():
    y = fluid.layers.data(name='y', shape=[1], dtype='float64')
    x = fluid.layers.data(name='x', shape=[13], dtype='float64')

    y_predict = fluid.layers.fc(input=x, size=1, act=None)
    # 平均损失
    loss = fluid.layers.square_error_cost(input=y_predict, label=y)
    avg_loss = fluid.layers.mean(loss)
    return avg_loss

trainer = Trainer(
    train_func=train_program,
    place=place,
    optimizer_func=optimizer_program
)

def inference_program():
    mm = fluid.layers.data(name='mm', shape=[13], dtype='float32')
    y_predict = fluid.layers.fc(input=mm, size=1, act=None)
    return y_predict

inferencer = Inferencer(
    infer_func = inference_program, param_path = params_dirname, place=place
)

batch_size = 10
test_reader = paddle.batch(paddle.dataset.uci_housing.test(), batch_size=batch_size)
test_data = next(test_reader()) 
test_x = numpy.array([data[0] for data in test_data]).astype('float32')
test_y = numpy.array([data[1] for data in test_data]).astype('float32')
  • 问题分析:
    但从报错输出DataType of Paddle Op mul must be the same来看,就是类型输出问题,通过问题描述中的内容来看,预测网络的结构应该是没有问题的,那么就是报数据类型问题,那么很大的可能就是训练模型的数据类型与输入的数据类型不匹配,因为预测网络要读入训练网络训练后的模型文件,所有运行网络的类型对预测时的数据类型要求也要一致。

  • 解决方法:

将训练时,使用的数据类型与预测时输入的数据类型也对应上,就可以解决该报错,让程序正常运行。

def train_program():
    y = fluid.layers.data(name='y', shape=[1], dtype='float32')
    x = fluid.layers.data(name='x', shape=[13], dtype='float32')

    y_predict = fluid.layers.fc(input=x, size=1, act=None)
    # 平均损失
    loss = fluid.layers.square_error_cost(input=y_predict, label=y)
    avg_loss = fluid.layers.mean(loss)
    return avg_loss

trainer = Trainer(
    train_func=train_program,
    place=place,
    optimizer_func=optimizer_program
)

def inference_program():
    mm = fluid.layers.data(name='mm', shape=[13], dtype='float32')
    y_predict = fluid.layers.fc(input=mm, size=1, act=None)
    return y_predict

inferencer = Inferencer(
    infer_func = inference_program, param_path = params_dirname, place=place
)

batch_size = 10
test_reader = paddle.batch(paddle.dataset.uci_housing.test(), batch_size=batch_size)
test_data = next(test_reader()) 
test_x = numpy.array([data[0] for data in test_data]).astype('float32')
test_y = numpy.array([data[1] for data in test_data]).astype('float32')



本文分享 CSDN - 飞桨PaddlePaddle。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部