- 知识点一:self.features = nn.Sequential(OrderedDict([])这里问什么要在nn.Sequential()中加入python中的一个有序字典呢?
看一下**nn.Sequential()**的源码,发现原来使用OrderedDict是nn.Sequential()的两种用法中的其中一种而已,该方法可以实现对与每个模块的key值进行重新命名。如过不使用OrderedDict的话,会按照加入到nn.Sequential()中每个模块的索引(如0,1,2,3…)等等来作为每一个模块的key(键)。自然通过两种方式生成的模型中的每个模块单独进行索引时使用的键也就不一样了。
参考连接
class Sequential(Module):
r"""A sequential container.
Modules will be added to it in the order they are passed in the constructor.
Alternatively, an ordered dict of modules can also be passed in.
To make it easier to understand, here is a small example::
# Example of using Sequential
model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1,20,5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20,64,5)),
('relu2', nn.ReLU())
]))
"""
@overload
def __init__(self, *args: Module) -> None:
...
@overload
def __init__(self, arg: 'OrderedDict[str, Module]') -> None:
...
def __init__(self, *args: Any):
super(Sequential, self).__init__()
if len(args) == 1 and isinstance(args[0], OrderedDict):
for key, module in args[0].items():
self.add_module(key, module)
else:
for idx, module in enumerate(args):
self.add_module(str(idx), module)



