关于机器学习:MindSpore网络自定义反向报错TypeError-The-params-of-function-bprop-of

53次阅读

共计 2153 个字符,预计需要花费 6 分钟才能阅读完成。

  1. 报错形容 1.1 零碎环境 Hardware Environment(Ascend/GPU/CPU): GPUSoftware Environment:MindSpore version (source or binary): 1.7.0Python version (e.g., Python 3.7.5): 3.7.5OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04.4 LTSGCC/Compiler version (if compiled from source): 7.5.01.2 根本信息 1.2.1 源码 import mindspore as ms
    import mindspore.nn as nn
    from mindspore.common.tensor import Tensor
    from mindspore.ops import composite as C

grad_all = C.GradOperation(get_all=True)

class MulAdd(nn.Cell):

def construct(self, x, y):
    return 2 * x + y

def bprop(self, x, y, out):
    return 2 * x, 2 * y

mul_add = MulAdd()
x = Tensor(1, dtype=ms.int32)
y = Tensor(2, dtype=ms.int32)
output = grad_all(mul_add)(x, y)1.2.2 报错 TypeError: The params of function ‘bprop’ of Primitive or Cell requires the forward inputs as well as the ‘out’ and ‘dout’Traceback (most recent call last):
File “test_grad.py”, line 20, in <module>

output = grad_all(mul_add)(x, y)

File “/home/liangzhibo/mindspore/build/package/mindspore/common/api.py”, line 522, in staging_specialize

out = _MindsporeFunctionExecutor(func, hash_obj, input_signature, process_obj)(*args)

File “/home/liangzhibo/mindspore/build/package/mindspore/common/api.py”, line 93, in wrapper

results = fn(*arg, **kwargs)

File “/home/liangzhibo/mindspore/build/package/mindspore/common/api.py”, line 353, in call

phase = self.compile(args_list, self.fn.__name__)

File “/home/liangzhibo/mindspore/build/package/mindspore/common/api.py”, line 321, in compile

is_compile = self._graph_executor.compile(self.fn, compile_args, phase, True)

TypeError: The params of function ‘bprop’ of Primitive or Cell requires the forward inputs as well as the ‘out’ and ‘dout’.
In file test_grad.py(13)

def bprop(self, x, y, out):
^

  • The Traceback of Net Construct Code:

In file test_grad.py(13)

def bprop(self, x, y, out):
^

  • C++ Call Stack: (For framework developers)

    mindspore/ccsrc/frontend/optimizer/ad/kprim.cc:651 BuildOutput

  • 起因剖析与解决办法在这个用例中,咱们应用了 Cell 的自定义反向规定。而报错信息也提醒了咱们是自定义规定的输出,即 def bprop(self, x, y, out): 这句话存在谬误。在自定义 Cell 的反向规定 bprop 时,须要承受三类输出,别离是 Cell 的正向输出(在本用例中为 x,y),Cell 的正向输入(在本用例中为 out),以及输出网络反向的累加梯度(dout)。本用例中正式因为短少了 dout 输出,因而运行失败。因而咱们只须要将代码更改为:def bprop(self, x, y, out, dout):
    return 2 x, 2 y 程序即可失常运行。下图示意了三类输出别离的意义,dout 为反向图前一个节点输入的梯度,bprop 函数须要此输出来对计算的梯度进行继承与应用。

    另外,bprop 的三类输出是构图的时候须要应用的,因而即便某些输出在 bprop 函数中没有被应用,也是须要传入 bprop 中的。3. 参考文档 https://www.mindspore.cn/tuto…

正文完
 0