【翻译】class torch.nn.Sequential(*args)

参考链接: class torch.nn.Sequential(*args)

在这里插入图片描述

原文及翻译:

Sequential   Sequential部分
class torch.nn.Sequential(*args)
类型 torch.nn.Sequential(*args)
    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
    # 使用Sequential的例子
    model = nn.Sequential(
              nn.Conv2d(1,20,5),
              nn.ReLU(),
              nn.Conv2d(20,64,5),
              nn.ReLU()
            )

    # Example of using Sequential with OrderedDict
    # Sequential使用有序字典的例子
    model = nn.Sequential(OrderedDict([
              ('conv1', nn.Conv2d(1,20,5)),
              ('relu1', nn.ReLU()),
              ('conv2', nn.Conv2d(20,64,5)),
              ('relu2', nn.ReLU())
            ]))

代码实验:

Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0

(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000026E386DD330>
>>> import torch.nn as nn
>>> from collections import OrderedDict
>>>
>>> # Example of using Sequential
>>>
>>> model = nn.Sequential(
...           nn.Conv2d(1,20,5),
...           nn.ReLU(),
...           nn.Conv2d(20,64,5),
...           nn.ReLU()
...         )
>>> model
Sequential(
  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (1): ReLU()
  (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (3): ReLU()
)
>>> model[0]
Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
>>> model[1]
ReLU()
>>> model[2]
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
>>> model[3]
ReLU()
>>> model[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda3\envs\ssd4pytorch1_2_0\lib\site-packages\torch\nn\modules\container.py", line 68, in __getitem__
    return self._get_item_by_idx(self._modules.values(), idx)
  File "D:\Anaconda3\envs\ssd4pytorch1_2_0\lib\site-packages\torch\nn\modules\container.py", line 60, in _get_item_by_idx
    raise IndexError('index {} is out of range'.format(idx))
IndexError: index 4 is out of range
>>> model[-1]
ReLU()
>>> model[-2]
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
>>> model[-4]
Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
>>> model[-5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda3\envs\ssd4pytorch1_2_0\lib\site-packages\torch\nn\modules\container.py", line 68, in __getitem__
    return self._get_item_by_idx(self._modules.values(), idx)
  File "D:\Anaconda3\envs\ssd4pytorch1_2_0\lib\site-packages\torch\nn\modules\container.py", line 60, in _get_item_by_idx
    raise IndexError('index {} is out of range'.format(idx))
IndexError: index -5 is out of range
>>>
>>> len(model)
4
>>>
>>> for m in model:
...     print(m)
...
Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
ReLU()
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
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())
...         ]))
>>> model
Sequential(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (relu1): ReLU()
  (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (relu2): ReLU()
)
>>> model[0]
Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
>>> for m in model:
...     print(m)
...
Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
ReLU()
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
ReLU()
>>>
>>> model['conv1']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda3\envs\ssd4pytorch1_2_0\lib\site-packages\torch\nn\modules\container.py", line 68, in __getitem__
    return self._get_item_by_idx(self._modules.values(), idx)
  File "D:\Anaconda3\envs\ssd4pytorch1_2_0\lib\site-packages\torch\nn\modules\container.py", line 58, in _get_item_by_idx
    idx = operator.index(idx)
TypeError: 'str' object cannot be interpreted as an integer
>>> model.conv1
Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
>>> model.relu1
ReLU()
>>> model.conv2
Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
>>> model.relu2
ReLU()
>>>
>>>

猜你喜欢

转载自blog.csdn.net/m0_46653437/article/details/112760230