前向传播
损失函数 def loss(o,t):
diff=(o-t)**2
return diff.mean()??为什么求均值
梯度下降db dw: db=dL/dtp * dtp/db 链式求导
更新w=w-Adw b=b-Adw
def loss(o,t):
diff=(o-t)**2
return diff.mean()
def dloss(o,t):
ddiff=2*(0-t)/o.size(0)
return ddiff
def dmodel_dw(i,w,d):
return i
def dmodel_db(i,w,b):
return 1
def grad(i,o,t,w,b):
dloss_do=ddiff(o,t)
dw=dloss_do * dmodel_dw
db=dloss_do * dmodel_db
return torch.stack([dloss_dw.sum(),dloss_db.sum()])
每个tensor张量都存在grad属性:parames=torch.tensor([1.0,0.0],requires_grad=True)
此时parames将自动追溯所有由parames运算得到的张量,并对所有得到的张量进行求导,将得到的导数值自动填充到parames的grad属性中
no_grad:此模块下的张量不进行梯度更新,即requires_grad=Flase
可视化: