2023.1.16
前两篇文章都在讲述理论,今天实现代码操作:关于加法节点,乘法节点的内容在这篇文章。
https://blog.youkuaiyun.com/m0_72675651/article/details/128695488
在以后的学习中,将把构建神经网络的“层”实现为一个类。这里的“层”是指神经网络中功能的单位。
这样写感觉到可以让代码变得美观一点,而更容易找出错误并修改
class Addyer: # 加法节点
def __init__(self):
pass
def forward(self, x, y):
out = x + y
return out
def backward(self, dout):
dx = dout * 1
dy = dout * 1
return dx, dy
class Mullyer: # 乘法节点
def __init__(self): # __init__() 中会初始化实例变量
self.x = None
self.y = None
def forward(self, x, y):
self.x = y
self.y = x
out = x * y
return out
def backward(self, dout):
dx = dout * self.x
dy = dout * self.y
return dx, dy
现在用代码实现了,加法层和乘法层,所以我们用代码解决这个问题:
利用反向传播法,求解,篮球、足球分别对应付金额上涨1元的影响是多少?
代码实现:
class Addyer: # 加法节点
def __init__(self):
pass
def forward(self, x, y):
out = x + y
return out
def backward(self, dout):
dx = dout * 1
dy = dout * 1
return dx, dy
class Mullyer: # 乘法节点
def __init__(self): # __init__() 中会初始化实例变量
self.x = None
self.y = None
def forward(self, x, y):
self.x = y
self.y = x
out = x * y
return out
def backward(self, dout):
dx = dout * self.x
dy = dout * self.y
return dx, dy
discout = 0.9
basketball = 100 # b
football = 200 # f
basketball_num = 2
footbal_num = 3
# 求解问题时 因为乘法层的反向传播需要正向传播的参数
m = Mullyer()
m1 = Mullyer()
m2 = Mullyer()
a = Addyer()
b_price = m2.forward(basketball, basketball_num)
f_price = m1.forward(football, footbal_num)
b_f_price = a.forward(b_price, f_price)
final_price = m.forward(discout, b_f_price)
print(final_price) # 720
# 求解 应付金额上涨1元 delta=1
# 篮球得影响
delta = 1
ddiscount, db_f_price = m.backward(delta)
dbaskbetball1, dfootball1 = a.backward(db_f_price)
dbaskbetball, dbaskbetball_num = m2.backward(dbaskbetball1)
dfootball, dfootball_num = m1.backward(dfootball1)
print(db_f_price) # 800
print(dfootball1) # 0.9
print(dbaskbetball, dbaskbetball_num) # 1.8 90.0
print(dfootball, dfootball_num) # 2.7 180.0