模型结构:
4层bp模型如下
代码:
使用方法;
python bp_train_use_matrix.py 0.16
# coding: utf-8
import torch
import sys
# 4层神经网络
# x—>h—>m—>y
# y = tanh(tanh(tanh(x@Wx)@Wh)@Wm)
# h=torch.tanh(x@Wx)
# m=torch.tanh(h@Wh)
# y=torch.tanh(m@Wm)
# 模型输入x
x1 = torch.tensor([[0.1,0.2,0.3]])
x2 = torch.tensor([[0.3,0.2,0.1]])
x = torch.cat([x1,x2],0)
print(x)
# 模型期望输出
y_gt_1=torch.tensor([[1,0]])
y_gt_2=torch.tensor([[0,1]])
y_gt = torch.cat([y_gt_1,y_gt_2],0)
Wx = torch.randn(3,4)
print(Wx.shape)
Wh = torch.randn(4,3)
print(Wh.shape)
Wm = torch.randn(3,2)
print(Wm.shape)
def model(x,Wx,Wh,Wm):
h=torch.tanh(x@Wx)
m=torch.tanh(h@Wh)
y=torch.tanh(m@Wm)
print("model:y=%s"%(y))
return h,m,y
def train(x,y_gt,Wx,Wh,Wm,lr):
h,m,y = model(x,Wx,Wh,Wm)
# 求误差
EY=y-y_gt
# 求 DLoss/DWm
EYDY=EY*(1-EY**2)
#DWm=m.t()*EY*(1-EY**2)
DWm = torch.cat([EYDY,EYDY,EYDY],0)*torch.cat([m.t(),m.t()],1)
# 求 DLoss/DWh
EM=EY*(1-EY**2)@Wm.t()
EMDM = EM*(1-EM**2)
DWh = torch.cat([EMDM,EMDM,EMDM,EMDM],0)*torch.cat([h.t(),h.t(),h.t()],1)
# 求 Dloss/DWx
EH=EM*(1-EM**2)@Wh.t()