Paddle 用dygraph做loss.backward()的时候出错!

yjghlzjz  于 2021-11-30  发布在  Java
关注(0)|答案(3)|浏览(378)

先贴下代码
optimizer -->

def get_ner_loss(label_acts,ner_acts,ner_actprobs,ner_rewards,ner_final_rewards,pretraining=False,vocab=None):
	seq_len = len(label_acts)
	ner_tot_reward = [0. for i in range(seq_len)]
	grads = to_variable(np.zeros((1,),dtype=np.float32))
	j = 0
	for i in range(seq_len):
		if label_acts[i] > 0:
			ner_tot_reward[i] = sum(ner_rewards[j]) / seq_len + ner_final_rewards[j]  #
			for k in range(seq_len)[::-1]:
				to_grad = - layers.log(ner_actprobs[j][k]) ---> 这是算cross_entropy, ner_actprobs[j][k] 指的是预测的实体的最大概率,这样就不用算完再reduce_mean了
				if not pretraining:
					to_grad *= to_variable(np.array(ner_tot_reward[i]))

				cur_ner_act = ner_acts[j][k]
				cur_ner_str = vocab.ner_itos.get(cur_ner_act)

				concern_labels = ['Tri_B', 'Tri_I', 'Arg_tri_B', 'Arg_entity_B', 'Arg_O_B',
								  'Arg_tri_I', 'Arg_entity_I', 'Arg_O_I', 'NA_L', 'NA_R']
				concern_weights = {'Tri_B': 1.1, 'Tri_I':1.0, 'Arg_tri_B':1.1, 'Arg_entity_B':1.1, 'Arg_O_B':1.1,
								  'Arg_tri_I':1.0, 'Arg_entity_I':1.0, 'Arg_O_I':1.0, 'NA_L':0.3, 'NA_R':0.3}
				noconcern_labels = ['O','O_tri_I','O_entity_I','O_tri_B','O_entity_B']
				noconcern_weights = {'O':0.3,'O_tri_I':0.4,'O_entity_I':0.4,'O_tri_B':0.4,'O_entity_B':0.4}
				flag = False
				for label in concern_labels:
					if cur_ner_str == label:
						to_grad *= concern_weights[label]
						flag = True
						break
				if not flag:
					for label in noconcern_labels:
						if cur_ner_str == label:
							to_grad *= noconcern_weights[label]
							break
				grads = grads + to_grad
			j += 1
	return ner_tot_reward, grads

报错,在本机上:

Traceback (most recent call last):
  File "E:/BioNLP[HRL]/paddleversion/main.py", line 68, in <module>
    a.backward()
  File "<decorator-gen-75>", line 2, in backward
  File "C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages\paddle\fluid\wrapped_decorator.py", line 25, in __impl__
    return wrapped_func(*args,**kwargs)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages\paddle\fluid\framework.py", line 216, in __impl__
    return func(*args,**kwargs)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python37\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):
--------------------------------------------
Windows not support stack backtrace yet.

----------------------
Error Message Summary:
----------------------
Error: `num_col_dims` must be between (0, rank_of_tensor). at (D:\1.8.1\paddle\paddle/fluid/framework/eigen.h:82)
-------------------------------------------------------------------------------------------------

在服务器上:

2020-05-30 09:12:48,354-INFO: Load data start ...
W0530 09:12:50.825495   743 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0530 09:12:50.829921   743 device_context.cc:245] device: 0, cuDNN Version: 7.3.
Traceback (most recent call last):
  File "main.py", line 66, in <module>
    gold_labels=gold_labels, mode=mode, vocab=pp.vocab)
  File "/home/aistudio/optimizer.py", line 101, in sample_optimizer
    gold_labels=gold_labels,mode=mode,ta_bias=ta_bias,ner_bias=ner_bias,vocab=vocab)
  File "/home/aistudio/optimizer.py", line 122, in optimizer
    grads.backward()
  File "</opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/decorator.py:decorator-gen-60>", line 2, in backward
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
    return wrapped_func(*args,**kwargs)
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py", line 207, in __impl__
    return func(*args,**kwargs)
  File "/opt/conda/envs/python35-paddle120-env/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::CUDADeviceContext, float>::Compute(paddle::framework::ExecutionContext const&) const
4   std::_Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CUDAPlace, false, 0ul, paddle::operators::DropoutGradKernel<paddle::platform::CUDADeviceContext, float>, paddle::operators::DropoutGradKernel<paddle::platform::CUDADeviceContext, paddle::platform::float16>, paddle::operators::DropoutGradKernel<paddle::platform::CUDADeviceContext, 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, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > >, std::less<std::string>, std::allocator<std::pair<std::string const, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > > > > > const*, std::map<std::string, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > >, std::less<std::string>, std::allocator<std::pair<std::string const, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > > > > > 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(std::map<std::string, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > >, std::less<std::string>, std::allocator<std::pair<std::string const, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > > > > > const&, std::map<std::string, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > >, std::less<std::string>, std::allocator<std::pair<std::string const, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > > > > > const&)
7   paddle::imperative::Engine::RunOp(paddle::imperative::OpBase*, std::map<std::string, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > >, std::less<std::string>, std::allocator<std::pair<std::string const, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > > > > > const&, std::map<std::string, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > >, std::less<std::string>, std::allocator<std::pair<std::string const, std::vector<std::shared_ptr<paddle::imperative::VarBase>, std::allocator<std::shared_ptr<paddle::imperative::VarBase> > > > > > const&, paddle::platform::Place const&)
8   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)

希望大佬指点一下

9q78igpj

9q78igpj1#

PS: dygraph里面没有封装好的GRU么,类似torch里面的那种,自己写的GRU或者LSTM运行效率有点慢,尤其是需要BiLSTM或者BiGRU的情况,我看model文件夹dygraph里面大部分都是自定义的LSTM而且是单向版本,如果可能,建议在以后的版本里面能给一个封装好的GRU(不是GRUunit)和LSTM,自己由于技术水平有限,自己写的GRU或者LSTM实在是有点辣眼睛,特别是如何处理Paddling的时候。

jecbmhm3

jecbmhm32#

从错误日志来看,是dropout的grad出错了,但是不太合理,方便的话提供一个完整的环境, 我们复现下

gcxthw6b

gcxthw6b3#

哥,你邮箱是多少,我给你发一下我的工程,你给参详参详

相关问题