动态图中使用 dropout,反向传播失败。
Created by: zylustc
paddle: 1.8.3 简单的测试代码:
import paddle.fluid as fluid
import numpy as np
with fluid.dygraph.guard():
dropout = fluid.dygraph.Dropout(p=0.5)
value = np.arange(14).reshape(14).astype("float64")
a=fluid.dygraph.to_variable(value)
a.stop_gradient=False
linear1 = fluid.Linear(14, 5, dtype="float64")
linear2 = fluid.Linear(5, 1, dtype="float64")
adam = fluid.optimizer.Adam(parameter_list=[linear1.parameters(),linear2.parameters()])
x=linear1(a)
x=dropout(x)
out = linear2(x)
out.backward()
报错:
File "33.py", line 22, in <module>
out.backward()
File "<decorator-gen-76>", line 2, in backward
File "/share/group-soft/anaconda/install/envs/paddle/lib/python3.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
return wrapped_func(*args, **kwargs)
File "/share/group-soft/anaconda/install/envs/paddle/lib/python3.7/site-packages/paddle/fluid/framework.py", line 216, in __impl__
return func(*args, **kwargs)
File "/share/group-soft/anaconda/install/envs/paddle/lib/python3.7/site-packages/paddle/fluid/dygraph/varbase_patch_methods.py", line 116, in backward
self._run_backward(backward_strategy, framework._dygraph_tracer())
paddle.fluid.core_avx.EnforceNotMet:
--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0 std::string paddle::platform::GetTraceBackString<char const*>(char const*&&, char const*, int)
1 paddle::platform::EnforceNotMet::EnforceNotMet(std::__exception_ptr::exception_ptr, char const*, int)
2 paddle::framework::EigenMatrix<unsigned char, 1, long>::Reshape(paddle::framework::Tensor const&, int)
3 paddle::operators::DropoutGradKernel<paddle::platform::CPUDeviceContext, double>::Compute(paddle::framework::ExecutionContext const&) const
4 std::_Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CPUPlace, false, 1ul, paddle::operators::DropoutGradKernel<paddle::platform::CPUDeviceContext, float>, paddle::operators::DropoutGradKernel<paddle::platform::CPUDeviceContext, double> >::operator()(char const*, char const*, int) const::{lambda(paddle::framework::ExecutionContext const&)#1}>::_M_invoke(std::_Any_data const&, paddle::framework::ExecutionContext const&)
5 paddle::imperative::PreparedOp::Run(std::map<std::string, paddle::imperative::SavedVariableWrapperList, std::less<std::string>, std::allocator<std::pair<std::string const, paddle::imperative::SavedVariableWrapperList> > > const&, std::map<std::string, paddle::imperative::SavedVariableWrapperList, std::less<std::string>, std::allocator<std::pair<std::string const, paddle::imperative::SavedVariableWrapperList> > > const&, std::unordered_map<std::string, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator<int> >, std::vector<float, std::allocator<float> >, std::vector<std::string, std::allocator<std::string> >, bool, std::vector<bool, std::allocator<bool> >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocator<paddle::framework::BlockDesc*> >, std::vector<long, std::allocator<long> >, 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_>, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator<int> >, std::vector<float, std::allocator<float> >, std::vector<std::string, std::allocator<std::string> >, bool, std::vector<bool, std::allocator<bool> >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocator<paddle::framework::BlockDesc*> >, std::vector<long, std::allocator<long> >, 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&)
6 paddle::imperative::OpBase::Run(paddle::framework::OperatorBase const&, std::map<std::string, paddle::imperative::SavedVariableWrapperList, std::less<std::string>, std::allocator<std::pair<std::string const, paddle::imperative::SavedVariableWrapperList> > > const&, std::map<std::string, paddle::imperative::SavedVariableWrapperList, std::less<std::string>, std::allocator<std::pair<std::string const, paddle::imperative::SavedVariableWrapperList> > > const&, std::unordered_map<std::string, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator<int> >, std::vector<float, std::allocator<float> >, std::vector<std::string, std::allocator<std::string> >, bool, std::vector<bool, std::allocator<bool> >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocator<paddle::framework::BlockDesc*> >, std::vector<long, std::allocator<long> >, 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_>, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator<int> >, std::vector<float, std::allocator<float> >, std::vector<std::string, std::allocator<std::string> >, bool, std::vector<bool, std::allocator<bool> >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocator<paddle::framework::BlockDesc*> >, std::vector<long, std::allocator<long> >, 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&, paddle::platform::Place const&)
7 paddle::imperative::BasicEngine::Execute()
----------------------
Error Message Summary:
----------------------
Error: `num_col_dims` must be between (0, rank_of_tensor). at (/paddle/paddle/fluid/framework/eigen.h:82)
去掉dropout 就可以正常运行