当前位置:网站首页>NN in pytorch Modulelist and nn.sequential

NN in pytorch Modulelist and nn.sequential

2022-07-23 09:40:00 Mick..

nn.ModuleList() and nn.Sequential() Can be used to build neural networks .nn.ModuleList() Function is used to store various modules , The front and back modules are not related .

class net(nn.Module):
    def __init__(self):
        super(net6, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(3)])
 
    def forward(self, x):
        for layer in self.linears:
            x = layer(x)
        return x
 
net = net()
print(net)
# net(
#   (linears): ModuleList(
#     (0): Linear(in_features=10, out_features=10, bias=True)
#     (1): Linear(in_features=10, out_features=10, bias=True)
#     (2): Linear(in_features=10, out_features=10, bias=True)
#   )
# )

* You can unpack the list

class net(nn.Module):
    def __init__(self):
        super(net7, self).__init__()
        self.linear_list = [nn.Linear(10, 10) for i in range(3)]
        self.linears = nn.Sequential(*self.linear_list)  ###  * You can unpack the list 
 
    def forward(self, x):
        self.x = self.linears(x)
        return x
 
net = net()
print(net)
# net(
#   (linears): Sequential(
#     (0): Linear(in_features=10, out_features=10, bias=True)
#     (1): Linear(in_features=10, out_features=10, bias=True)
#     (2): Linear(in_features=10, out_features=10, bias=True)
#   )
# )

原网站

版权声明
本文为[Mick..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230127482410.html