nn.Parameter()

class torch.nn.Parameter()

Parameter是Variable的子类,当Parameter赋值给Module属性的时候,他会自动被添加到Module的参数列表中。


示例:分别使用nn.Linear和nn.Parameter实现下图所示的全连接层。
nn.Parameter()_第1张图片

1、通过nn.Linear实现

# 定义模型结构
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        # dense_1
        self.dense_1 = nn.Linear(5, 4, bias=True)
        # dense_2
        self.dense_2 = nn.Linear(4, 1, bias=True)
                
    def forward(self, input_data):
        out = torch.tanh(self.dense_1(input_data))
        out = torch.sigmoid(self.dense_2(out))
        return out
model = Model()

# 查看模型参数
for name, param in model.named_parameters():
    print(name)
    print(type(param.data))
    print(param.shape)

# 输出
dense_1.weight
<class 'torch.Tensor'>
torch.Size([4, 5])
dense_1.bias
<class 'torch.Tensor'>
torch.Size([4])

dense_2.weight
<class 'torch.Tensor'>
torch.Size([1, 4])
dense_2.bias
<class 'torch.Tensor'>
torch.Size([1])

2、通过nn.Parameter实现

# 定义模型结构
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()      
        # dense_1  
        self.w_1 = nn.Parameter(torch.randn(5, 4))
        self.b_1 = nn.Parameter(torch.randn(4))
        # dense_2
        self.w_2 = nn.Parameter(torch.randn(4, 1))
        self.b_2 = nn.Parameter(torch.randn(1))
    def forward(self, input_data):
        out = torch.tanh(torch.mm(input_data, self.w_1) + self.b_1)
        out = torch.sigmoid(torch.mm(out, self.w_2) + self.b_2)
        return out
model = Model()

# 查看模型参数
for name, param in model.named_parameters():
    print(name)
    print(type(param.data))
    print(param.shape)
    
# 输出
w_1
<class 'torch.Tensor'>
torch.Size([5, 4])
b_1
<class 'torch.Tensor'>
torch.Size([4])

w_2
<class 'torch.Tensor'>
torch.Size([4, 1])
b_2
<class 'torch.Tensor'>
torch.Size([1])

你可能感兴趣的:(PyTorch学习笔记,数据挖掘,pytorch,tensorflow)