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

2023-06-15,,

错误实例:

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)的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。