问题描述:使用PaddlePaddle编写好网络运行时,报张量类型不正确的错误,我反复检查了自己的网络结构中张量类型相关的定义,并打印了传入数据的类型,类型都相匹配,但运行时就是报张量类似错误,逐步排查后,发现是训练代码段会抛出该错误,不知道如果修改?
报错信息:
EnforceNotMet: Tensor holds the wrong type, it holds l at [/paddle/paddle/fluid/framework/tensor_impl.h:29]
PaddlePaddle Call Stacks:
0 0x7fe0624486b6p paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int) + 486
1 0x7fe0624501c0p float const* paddle::framework::Tensor::data<float>() const + 192
2 0x7fe06278f813p void paddle::operators::ElementwiseComputeEx<paddle::operators::SubFunctor<float>, paddle::platform::CPUDeviceContext, float, float>(paddle::framework::ExecutionContext const&, paddle::framework::Tensor const*, paddle::framework::Tensor const*, int, paddle::operators::SubFunctor<float>, paddle::framework::Tensor*) + 67
3 0x7fe062936e53p paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, float>::Compute(paddle::framework::ExecutionContext const&) const + 323
4 0x7fe062936ed3p std::_Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CPUPlace, false, 0ul, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, float>, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, double>, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, int>, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, long> >::operator()(char const*, char const*) const::{
lambda(paddle::framework::ExecutionContext const&)#1}>::_M_invoke(std::_Any_data const&, paddle::framework::ExecutionContext const&) + 35
5 0x7fe062fc52ecp 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 + 492
6 0x7fe062fc171fp 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&) + 255
7 0x7fe0625094eap paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 298
8 0x7fe062509ee0p paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool) + 128
9 0x7fe06242ff5dp
10 0x7fe06247ade4p pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 2596
11 0x4e9ba7p PyCFunction_Call + 119
12 0x53c6d5p PyEval_EvalFrameEx + 23029
def train_program():
# feature vector of length 13
x = fluid.layers.data(name='x', shape=[11], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
y = fluid.layers.data(name='y', shape=[1], dtype='int64')
loss = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_loss = fluid.layers.mean(loss)
return avg_loss
问题分析:张量类型错误有多种可能,但就提供的代码而言,可能是损失函数定义的类型不满足当前的网络结构,出错代码中使用了square_error_cost,即平方差损失,导致代码报错。
问题解决:
使用交叉熵损失函数代替平方差损失函数
paddle.fluid.layers.cross_entropy(input, label, soft_label=False, ignore_index=-100)