【学习笔记】Paramater.size()

size()指定的是parameter的维度。

例子

def test_parameter():
    import torch.nn as nn
    net = nn.Sequential(nn.Linear(4, 3), nn.ReLU(), nn.Linear(3, 1))
    weight_0 = list(net[0].parameters())[0]
    print(weight_0.size())
    print(weight_0.size(0))
    print(weight_0.size(1))

输出结果如下

torch.Size([3, 4])
3
4

weight_0的shape如下
可以看到weight_0的shape是torch.Size([3,4]),也就是weight_0.size()的输出结果,weight_0.size(0)指的就是weight_0的第一个维度3,同理weight_0.size(1)==4.
【学习笔记】Paramater.size()_第1张图片

pytorch中的register_parameter()和parameter()的区别参考
https://blog.csdn.net/xinjieyuan/article/details/106951116?spm=1001.2014.3001.5506

你可能感兴趣的:(笔记,学习,深度学习,人工智能)