静态图实现对抗训练(FGM),梯度运算过程出现[operator < elementwise_add > error]错误,交换加法算符位置可解决
Created by: xwen99
- 问题:
- 静态图实现对抗训练(FGM),梯度运算过程出现[operator < elementwise_add > error]错误
- 交换加法算符位置可解决
- 版本、环境信息:
- PaddlePaddle版本:1.8.1
- PaddleHub版本:1.7.1
- GPU:v100
- 系统环境:AI Studio 环境,Python 3.7
- 训练信息
- 单机单卡
- 复现信息:
- 相关代码:
def _scale_l2(x, norm_length):
# shape(x) = (batch, num_timesteps, d)
# Divide x by max(abs(x)) for a numerically stable L2 norm.
# 2norm(x) = a * 2norm(x/a)
# Scale over the full sequence, dims (1, 2)
alpha = fluid.layers.reduce_max(fluid.layers.abs(x), dim=[1, 2], keep_dim=True) + 1e-12
l2_norm = alpha * fluid.layers.sqrt(
fluid.layers.reduce_sum(fluid.layers.pow(x / alpha, 2), dim=[1, 2], keep_dim=True) + 1e-6)
x_unit = x / l2_norm
return norm_length * x_unit
def adversarial_loss(self, loss):
grad = fluid.backward.append_backward(loss, [self.feature.name])[0][1]
perturb = _scale_l2(grad, 1.0)
adv_loss = self.cl_loss_from_embedding(self.feature + perturb) # 报错在这里
# 如果交换位置,变成 perturb + self.feature 就不报错
return adv_loss
对抗损失 adv_loss 在 build_env 时被加到正常的损失上:
def _build_env(self):
"""
building the program and strategy for specific running phase.
"""
if self.env.is_inititalized:
return
self._build_env_start_event()
self.env.is_inititalized = True
self.env.main_program = clone_program(
self._base_main_program, for_test=False)
self.env.startup_program = fluid.Program()
with fluid.program_guard(self.env.main_program,
self._base_startup_program):
with fluid.unique_name.guard(self.env.UNG):
self.env.outputs = self._build_net()
if self.is_train_phase or self.is_test_phase:
self.env.labels = self._add_label()
self.env.loss = self._add_loss()
self.env.metrics = self._add_metrics()
if self.is_predict_phase or self.is_test_phase:
self.env.main_program = clone_program(
self.env.main_program, for_test=True)
hub.common.paddle_helper.set_op_attr(
self.env.main_program, is_test=True)
if self.is_train_phase:
with fluid.program_guard(self.env.main_program,
self._base_startup_program):
with fluid.unique_name.guard(self.env.UNG):
self.env.adv_loss = self.adversarial_loss(self.loss)
self.env.loss += self.env.adv_loss # 这里
...
- 问题描述:
- 报错信息:
Traceback (most recent call last):
File "reading_comprehension.py", line 104, in <module>
reading_comprehension_task.finetune_and_eval()
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 868, in finetune_and_eval
return self.finetune(do_eval=True)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 883, in finetune
self.init_if_necessary()
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 367, in init_if_necessary
if not self.load_checkpoint():
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 834, in load_checkpoint
main_program=self.main_program)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 508, in main_program
self._build_env()
File "/home/aistudio/work/DuReader-Robust-With-Paddlehub/my_reading_comprehension_task.py", line 563, in _build_env
self.env.adv_loss = self.adversarial_loss(self.loss)
File "/home/aistudio/work/DuReader-Robust-With-Paddlehub/my_reading_comprehension_task.py", line 520, in adversarial_loss
adv_loss = self.cl_loss_from_embedding(self.feature + perturb)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py", line 242, in __impl__
attrs={'axis': axis})
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2610, in append_op
attrs=kwargs.get("attrs", None))
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py", line 1971, in __init__
self.desc.infer_shape(self.block.desc)
paddle.fluid.core_avx.EnforceNotMet:
--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0 std::string paddle::platform::GetTraceBackString<std::string const&>(std::string const&, char const*, int)
1 paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const*, int)
2 paddle::operators::ElementwiseOp::InferShape(paddle::framework::InferShapeContext*) const
3 paddle::framework::OpDesc::InferShape(paddle::framework::BlockDesc const&) const
------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py", line 2610, in append_op
attrs=kwargs.get("attrs", None))
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py", line 242, in __impl__
attrs={'axis': axis})
File "/home/aistudio/work/DuReader-Robust-With-Paddlehub/my_reading_comprehension_task.py", line 520, in adversarial_loss
adv_loss = self.cl_loss_from_embedding(self.feature + perturb)
File "/home/aistudio/work/DuReader-Robust-With-Paddlehub/my_reading_comprehension_task.py", line 563, in _build_env
self.env.adv_loss = self.adversarial_loss(self.loss)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 508, in main_program
self._build_env()
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 834, in load_checkpoint
main_program=self.main_program)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 367, in init_if_necessary
if not self.load_checkpoint():
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 883, in finetune
self.init_if_necessary()
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddlehub/finetune/task/base_task.py", line 868, in finetune_and_eval
return self.finetune(do_eval=True)
File "reading_comprehension.py", line 104, in <module>
reading_comprehension_task.finetune_and_eval()
----------------------
Error Message Summary:
----------------------
NotFoundError: No Input(Y) found for ElementwiseOp operator.
[Hint: Expected ctx->HasInput("Y") == true, but received ctx->HasInput("Y"):0 != true:1.] at (/paddle/paddle/fluid/operators/elementwise/elementwise_op.h:42)
[operator < elementwise_add > error]