nn.Parameter

torch.nn.Parameter是继承自torch.Tensor的子类,其主要作用是作为nn.Module中的可训练参数使用。它与torch.Tensor的区别就是nn.Parameter会自动被认为是module的可训练参数,即加入到parameter()这个迭代器中去;而module中非nn.Parameter()的普通tensor是不在parameter中的。

没有使用nn.Parameter

import torch
from torch import nn
 
class MyModule(nn.Module):
    def __init__(self, input_size, output_size):
        super(MyModule, self).__init__()
        self.test = torch.rand(input_size, output_size)
        self.linear = nn.Linear(input_size, output_size)
    def forward(self, x):
        return self.linear(x)
 
model = MyModule(4, 2)
print(list(model.named_parameters()))

nn.Parameter_第1张图片
使用了nn.Paramter

import torch
from torch import nn

class MyModule(nn.Module):
def init(self, input_size, output_size):
super(MyModule, self).init()
self.test = nn.Parameter(torch.rand(input_size, output_size))
self.linear = nn.Linear(input_size, output_size)
def forward(self, x):
return self.linear(x)

model = MyModule(4, 2)
print(list(model.named_parameters()))

nn.Parameter_第2张图片

使用register_parameter()注册

import torch
from torch import nn
 
class MyModule(nn.Module):
    def __init__(self, input_size, output_size):
        super(MyModule, self).__init__()
        self.linear = nn.Linear(input_size, output_size)
    def forward(self, x):
        return self.linear(x)
 
model = MyModule(4, 2)
my_test = nn.Parameter(torch.rand(4, 2))
model.register_parameter('test',my_test)
print(list(model.named_parameters()))

nn.Parameter_第3张图片

你可能感兴趣的:(pytorch,深度学习,pytorch,python)