Created by: Aurelius84
append_backward API输入类型检查(仅涉及python端)
Variable
1. loss参数必须为fluid.backward.append_backward(loss=avg_loss.name) # 若传入string类型
- 优化前只有一个assert语句相关报错
File "backward.py", line 1245, in append_backward
assert isinstance(loss, framework.Variable)
AssertionError
- 优化后,会提示当前转入的类型
TypeError: The type of 'loss' in fluid.backward.append_backward must be <class 'paddle.fluid.framework.Variable'>, but received <class 'str'>.
list[Variable|str]
2. parameter_list参数为param_names = [
param.name
for param in fluid.default_main_program().block(0).all_parameters()
]
param_names[0] = np.random.random([10])
fluid.backward.append_backward(loss=avg_loss, parameter_list=self.param_names)
- 优化前,若parameter_list的元素不是Varibale或str,报如下错:
TypeError: The type of parameter_list's member must be paddle.fluid.Variable or str, but received <class 'numpy.ndarray'>.
- 优化后,增加了具体元素的位置信息
TypeError: The type of 'parameter_list[0]' in fluid.backward.append_backward must be (<class 'paddle.fluid.framework.Variable'>, (<class 'str'>,)), but received <class 'numpy.ndarray'>.
3. callbacks参数必须为list
def callback(block, context):
return
fluid.backward.append_backward(loss=avg_loss, callbacks=callback)
- 优化前,仅会输出assert信息
_append_backward_ops_
assert (isinstance(callbacks, list))
AssertionError
- 优化后,按照规范输出报错信息
TypeError: The type of 'callbacks' in fluid.backward.append_backward must be <class 'list'>, but received <class 'function'>.
- 移除了对
fluid.gradients
的no_grad_set的强制set类型检查
之前存在no_grad_set传入list、tuple的情况,已在内部进行兼容处理,并已经给出了完备的报错信息提示,故此处移除对
fluid.gradients
的no_grad_set的强制set类型检查