1.model.parameters()中参数的存储方式
在PyTorch中,模型的参数是通过一个个torch.Tensor
对象存储的。每个torch.Tensor
对象代表了模型中的一个参数,它可以是一个权重矩阵、偏置向量或者模型中的其他可学习参数。
当你调用model.parameters()
时,你会得到一个包含所有这些torch.Tensor
对象的生成器。这些张量(tensors)是多维数组,它们可以有不同的形状和数据类型,具体取决于它们在模型中的作用。
例如一层卷积的权重矩阵如下:
class A(nn.Module):
def __init__(self):
super().__init__()
self.conv_layer = nn.Sequential(
nn.Conv2d(2, 2, 3),
nn.Conv2d(2, 2, 3),
nn.Conv2d(2, 2, 3)
)
def forward(self, x):
x = self.conv_layer(x)
return x
tensor([[[[-0.0867, 0.1540, -0.1894],
[-0.0881, -0.0181, -0.1127],
[-0.1503, -0.0576, -0.1661]],
[[-0.0447, 0.2345, 0.0317],
[-0.0845, 0.1751, 0.1120],
[-0.0362, 0.0467, -0.1501]]],
[[