2020-11-03

'''
将nn神经网络层链接起来
1:Sequential
    1)、起名字。
    2)、列表。
    3)、有序字典
2:module_list
 
'''
import torch.nn as nn
from collections import OrderedDict
net1 = nn.Sequential()  # 给神经网络层起名字
net1.add_module('conv', nn.Conv2d(3, 3, 3))
net1.add_module('batchnorm', nn.BatchNorm2d(3))
net1.add_module('activation_layer', nn.ReLU())

print(net1)
print(net1.conv)
net2 = nn.Sequential(
    nn.Conv2d(3, 3, 3), nn.BatchNorm2d(3), nn.ReLU()
)
print(net2)
print(net2[0])

net3 = nn.Sequential(
    OrderedDict([('conv', nn.Conv2d(3, 3, 3)),
                 ('batchnorm', nn.BatchNorm2d(3)),
                 ('activation_layer', nn.ReLU()),
                 ])
)
print(net3)


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.module_list = nn.ModuleList(
            [
                nn.Conv2d(3, 3, 3),
                nn.ReLU()
            ])

你可能感兴趣的:(2020-11-03)