TVP

# MindSpore反向传播配置关键字参数

DechinPhy

920

## 技术背景

import mindspore as ms
from mindspore import nn, Tensor, value_and_grad
from mindspore import numpy as msnp

class Net(nn.Cell):
def bprop(self, x, y=1, out, dout):
return msnp.cos(x) + y
def construct(self, x, y=1):
return msnp.sin(x) + y

x = Tensor([3.14], ms.float32)
net = Net()
print (net(x, y=1), value_and_grad(net)(x, y=0))

  File "test_rand.py", line 53
def bprop(self, x, y=1, out, dout):
^
SyntaxError: non-default argument follows default argument

import mindspore as ms
from mindspore import nn, Tensor, value_and_grad
from mindspore import numpy as msnp

class Net(nn.Cell):
def bprop(self, x, out, dout, y=1):
return msnp.cos(x) + y
def construct(self, x, y=1):
return msnp.sin(x) + y

x = Tensor([3.14], ms.float32)
net = Net()
print (net(x, y=1), value_and_grad(net)(x, y=0))

[1.0015925] (Tensor(shape=[1], dtype=Float32, value= [ 1.59254798e-03]), Tensor(shape=[1], dtype=Float32, value= [ 1.25169754e-06]))

，所以在construct函数计算函数值时，得到的结果应该是

，那么这里面

，但是在这里输入的

，而导数的计算结果却是

。这就说明，在MindSpore的自定义反向传播函数中，并不支持传入关键字参数。

## 解决方案

import mindspore as ms
from mindspore import nn, Tensor, value_and_grad
from mindspore import numpy as msnp

class Net(nn.Cell):
def bprop(self, x, y, out, dout):
return msnp.cos(x) + y if y is not None else msnp.cos(x)
def construct(self, x, y=1):
return msnp.sin(x) + y

x = Tensor([3.14], ms.float32)
net = Net()
print (net(x, y=1), value_and_grad(net)(x, y=0))

[1.0015925] (Tensor(shape=[1], dtype=Float32, value= [ 1.59254798e-03]), Tensor(shape=[1], dtype=Float32, value= [-9.99998748e-01]))

import mindspore as ms
from mindspore import nn, Tensor, value_and_grad
from mindspore import numpy as msnp

class Net(nn.Cell):
def bprop(self, x, w, y, out, dout):
return w*msnp.cos(x) + y if y is not None else msnp.cos(x)
def construct(self, x, w=1, y=1):
return msnp.sin(x) + y

x = Tensor([3.14], ms.float32)
net = Net()
print (net(x, y=1), value_and_grad(net)(x, y=0, w=2))

[1.0015925] (Tensor(shape=[1], dtype=Float32, value= [ 1.59254798e-03]), Tensor(shape=[1], dtype=Float32, value= [ 2.00000000e+00]))

import mindspore as ms
from mindspore import nn, Tensor, value_and_grad
from mindspore import numpy as msnp

class Net(nn.Cell):
def bprop(self, x, w, y, out, dout):
return w*msnp.cos(x) + y if y is not None else w*msnp.cos(x)
def construct(self, x, w=1, y=1):
return msnp.sin(x) + y if y is not None else msnp.sin(x)

x = Tensor([3.14], ms.float32)
net = Net()
print (net(x, y=1), value_and_grad(net)(x, w=2, y=None))

[1.0015925] (Tensor(shape=[1], dtype=Float32, value= [ 1.59254798e-03]), Tensor(shape=[1], dtype=Float32, value= [-1.99999750e+00]))

。但是就目前来说，只有这一个方法可以允许我们向bprop函数传递关键字参数。

## 参考链接

1. https://cloud.tencent.com/developer/article/2415926

0 条评论

LV.

• 技术背景
• 解决方案
• 总结概要
• 版权声明
• 参考链接