推广方式有哪几种,重庆电子商务seo,微信小程序+网站开发,给网站底部做友情链接上一篇关于反向传播的代码仅支持单变量的梯度计算#xff0c;下面我们将扩展代码使其支持多个输入/输出。增加了对多输入函数#xff08;如 Add#xff09;#xff0c;以实现的计算。
1.关于前向传播可变长参数的改进-修改Function类 修改方法#xff1a; Function用于对…上一篇关于反向传播的代码仅支持单变量的梯度计算下面我们将扩展代码使其支持多个输入/输出。增加了对多输入函数如 Add以实现的计算。
1.关于前向传播可变长参数的改进-修改Function类 修改方法 Function用于对输入输出做规定帮助实现右图的效果接受inputs 返回outputs 2.关于反向传播可变长参数的改进 修改函数类的反向传播 修改Variable类的反向传播
改进前 获取y.creator,获取输入creator.inputs根据y.grads计算x.grads:creator.backward(y.grads)
2.3两步的解包和打包操作 最后修改square方法 完整代码
import numpy as npclass Variable:def __init__(self, data):if data is not None:if not isinstance(data, np.ndarray):raise TypeError({} is not supported.format(type(data)))self.data dataself.grad Noneself.creator Nonedef set_creator(self, func):self.creator funcdef backward(self):if self.grad is None:self.grad np.ones_like(self.data)funcs [self.creator]while funcs:f funcs.pop()gys [output.grad for output in f.outputs] # 获取所有输出的梯度gxs f.backward(*gys) # 调用 backward 方法if not isinstance(gxs, tuple): # 确保 gxs 是元组gxs (gxs,)for x, gx in zip(f.inputs, gxs): # 为每个输入分配梯度x.grad gxif x.creator is not None:funcs.append(x.creator)class Function:def __call__(self, *inputs):xs [x.data for x in inputs] # 提取输入数据ys self.forward(*xs) # 前向传播解包if not isinstance(ys, tuple): # 确保 ys 是元组ys (ys,)outputs [Variable(as_array(y)) for y in ys] # 创建输出变量for output in outputs:output.set_creator(self)self.inputs inputs # 保存输入self.outputs outputs # 保存输出return outputs if len(outputs) 1 else outputs[0] # 根据输出数量返回def forward(self, *xs):raise NotImplementedError()def backward(self, *gys):raise NotImplementedError()# 实现具体的函数类
class Square(Function):def forward(self, x):return x ** 2def backward(self, gy):x self.inputs[0].data # 从 inputs 中获取数据gx 2 * x * gyreturn gxclass Add(Function):def forward(self, x0, x1):y x0 x1return ydef backward(self, gy):return gy, gy # 对两个输入返回相同的梯度# 定义便捷函数
def square(x):return Square()(x)def add(x0, x1):return Add()(x0, x1)# 定义 as_array 函数
def as_array(x):if np.isscalar(x):return np.array(x)return x# 测试代码
x Variable(np.array(2.0))
y Variable(np.array(3.0))
z add(square(x), square(y))
z.backward()
print(z.data) # 输出结果: 13.0 (2^2 3^2 4 9 13)
print(x.grad) # 输出梯度: 4.0 (dz/dx 2 * 2 4)
print(y.grad) # 输出梯度: 6.0 (dz/dy 2 * 3 6)
运行结果 文章转载自: http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.wbns.cn.gov.cn.wbns.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.bctr.cn.gov.cn.bctr.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.bpmtq.cn.gov.cn.bpmtq.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.rykx.cn.gov.cn.rykx.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn