软件:Jupyter notebook
反向传播练习,以下代码
import numpy as np
# 编写乘法层类
class MulLayer:
def __init__(self):
# 编写需要初始化的变量
self.x = None
self.y = None
def forward(self, x, y):
# 编写前向传播过程
self.x = x
self.y = y
out = x * y
return out
def backward(self, dout):
# 编写反向传播过程
dx = dout *self.y
dy = dout *self.x
return dx, dy
# 编写加法层类
class AddLayer:
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
# 前向传播的输入变量
apple = 100
apple_num = 2
orange = 150
orange_num = 3
tax = 1.1
# layer功能层实例化
mul_apple_layer = MulLayer()
mul_orange_layer = MulLayer()
add_apple_orange_layer = AddLayer()
mul_tax_layer = MulLayer()
# forward:写出前向消费金额计算过程
apple_price = mul_apple_layer.forward(apple, apple_num) # (1)
orange_price = mul_orange_layer.forward(orange, orange_num) # (2)
all_price = add_apple_orange_layer.forward(apple_price, orange_price) # (3)
price = mul_tax_layer.forward(all_price, tax) # (4)
# backward:写出反向传播求导的过程
dprice = 1
dall_price, dtax = mul_tax_layer.backward(dprice) # (4)
dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price) # (3)
dorange, dorange_num = mul_orange_layer.backward(dorange_price) # (2)
dapple, dapple_num = mul_apple_layer.backward(dapple_price) # (1)
print("price:", int(price))
print("dApple:", dapple)
print("dApple_num:", int(dapple_num))
print("dOrange:", dorange)
print("dOrange_num:", int(dorange_num))
print("dTax:", dtax)
合在一个编译框里,就可以输出,结果如下
price: 715
dApple: 2.2
dApple_num: 110
dOrange: 3.3000000000000003
dOrange_num: 165
dTax: 650
从apple行之前,分开成两个编译框,分别运行,就会报错如下
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-30ce15d60af2> in <module>()
20 # backward:写出反向传播求导的过程
21 dprice = 1
---> 22 dall_price, dtax = mul_tax_layer.backward(dprice) # (4)
23 dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price) # (3)
24 dorange, dorange_num = mul_orange_layer.backward(dorange_price) # (2)
<ipython-input-4-f8bcb7ea494e> in backward(self, dout)
18 def backward(self, dout):
19 # 编写反向传播过程
---> 20 dx = dout *self.y
21 dy = dout *self.x
22 return dx, dy
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
一模一样的代码,复制粘贴的,有盆友懂吗?为什么会出现这样的问题?