pytorch构建二层神经网络

import torch
dtype=torch.float
device=torch.device("cpu")
# N是批量大小; D_in是输入维度;
# H是隐藏的维度; D_out是输出维度。
N,D_in,H,D_out=64,1000,100,10
#创建随机输入和输出数据
x=torch.randn(N,D_in,device=device,dtype=dtype)
y=torch.randn(N,D_out,device=device,dtype=dtype)
#随机权重初始化
w1=torch.randn(D_in,H,device=device,dtype=dtype)
w2=torch.randn(H,D_out,device=device,dtype=dtype)

learning_rate=1e-6

for i in range(500):
    h=x.mm(w1)
    h_relu=h.clamp(min=0)#将数据夹紧到区间相当于RELU
    y_pred=h_relu.mm(w2)

    #计算损失
    loss=(y_pred-y).pow(2).sum().item()
    print(i,loss)

    grad_y_pred=2.0*(y_pred-y)
    grad_w2=h_relu.t().mm(grad_y_pred)
    grad_h_relu=grad_y_pred.mm(w2.t())
    grad_h=grad_h_relu.clone()
    grad_h[h<0]=0
    grad_w1=x.t().mm(grad_h)

    w1-=learning_rate*grad_w1
    w2-=learning_rate*grad_w2

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章