zl程序教程

您现在的位置是:首页 >  后端

当前栏目

pytorch nn.parameter反向传播问题

PyTorch 反向 Parameter 传播 NN 问题
2023-09-14 09:14:34 时间
import torch
class net(torch.nn.Module):
    def __init__(self):
        super(net,self).__init__()
        self.p_1=torch.nn.Linear(1,12)
        self.p_2=torch.nn.Linear(1,12)
        self.p_x=torch.nn.Linear(1,12)
        self.t_mask=torch.nn.Linear(132,12)
    def forward(self,x):

        weight=self.p_1(torch.Tensor([1]))+self.p_2(torch.Tensor([1]))
        for i in range(10):
            x1=self.p_1(torch.Tensor([1]))*((i+1)**self.p_x(torch.Tensor([1])))
            x2=self.p_2(torch.Tensor([1]))*(i+1)
            weight = torch.cat((weight,x1+x2),0)
        x=self.t_mask(weight)
        return x


class net1(torch.nn.Module):
    def __init__(self):
        super(net1,self).__init__()
        self.p_1=torch.nn.Parameter(torch.Tensor(12))
        self.p_2=torch.nn.Parameter(torch.Tensor(12))
        self.p_x=torch.nn.Parameter(torch.Tensor(12))

    def forward(self,x):

        self.t_mask=self.p_1+self.p_2
        for i in range(10):
            x1=self.p_1*((i+1)**self.p_x)
            x2=self.p_2*(i+1)
            self.t_mask = torch.cat((self.t_mask,x1+x2),0)
        x=self.t_mask.view(1,1,-1,1,1)*x
        return x
if __name__ == '__main__':
    data=torch.rand([1])
    nt=net1()
    lf=torch.nn.BCELoss()
    op=torch.optim.Adam(nt.parameters(),lr=0.1)
    for i in range(10):
        out_data=nt(data)
        loss=lf(torch.rand(out_data.shape),torch.rand(out_data.shape))
        print(loss)
        loss.requires_grad = True

        op.zero_grad()
        loss.backward()
        op.step()