【python】*号用法 如nn.Sequential(*layers)

形参——单个星号代表这个位置接收任意多个非关键字参数,转化成元组方式。
实参——如果*号加在了是实参上,代表的是将输入迭代器拆成一个个元素。

【python】*号用法 如nn.Sequential(*layers)_第1张图片
从nn.Sequential的定义来看,输入要么是orderdict,要么是一系列的模型,遇到list,必须用*号进行转化,否则会报错 TypeError: list is not a Module subclass

# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([           #orderdict按照建造时候的顺序进行存储
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

nn.Sequential()容器的好处,引用方便。

你可能感兴趣的:(Python)