如何在模型中引入可学习参数(Pytorch)

2023-05-18编程技术162663

错误实例:

def init(self):
self.w1 = torch.nn.Parameter(torch.FloatTensor(1),requires_grad=True).cuda()
self.w2 = torch.nn.Parameter(torch.FloatTensor(1),requires_grad=True).cuda()
self.w1.data.fill_(0.3)
self.w2.data.fill_(0.3)
def forward(self, x):
out = self.w1 * out1 + self.w2 * out2
out = self.fc(out)

正确实例:

def init(self):
self.w1 = torch.nn.Parameter(torch.FloatTensor(1),requires_grad=True)
self.w2 = torch.nn.Parameter(torch.FloatTensor(1),requires_grad=True)
self.w1.data.fill_(0.3)
self.w2.data.fill_(0.3)
def forward(self, x):
out = self.w1 * out1 + self.w2 * out2
out = self.fc(out)

如何在模型中引入可学习参数(Pytorch)的相关教程结束。

本文地址:https://www.ufcn.cn/tutorials/2650907.html

如非特殊说明,本站内容均来自于网友自主分享,概不代表本站观点,如有任何问题我们都将在收到反馈后的第一时间进行处理!