nn.ModuleList()与nn.Sequential()

本文是对其他博客的补充,建议同时参考其他博客

Pytorch系列1: torch.nn.Sequential()讲解_xddwz的博客-CSDN博客_torch.nn.sequential

Pytorch使用 nn.ModuleList() 和nn.Sequential()编写神经网络模型_假装很坏的谦谦君的博客-CSDN博客

nn.ModuleList()构建的组件没有顺序,而nn.Sequential()构建的有顺序。

nn.ModuleList()有个好处是 在forward时,中间层可以多个输入。

nn.Sequential()直接默认从上到下运行。其实也可以在中间层改变输入,像字典那样取key值,但显然没有for直接循环简单。

nn.Modulelist()
...
def __init__(self, ...):
    self.op_num = 3
    self._op_trans = nn.ModuleList()
    self._op_trans.append(ISPMixedOp(C_out, C_out, width_mult_list=width_mult_list))
    for i in range(self.op_num-1):
        self._op_trans.append(ISPMixedOp(C_out, C_out, width_mult_list=width_mult_list))

def forward(self, x):
    output = self._op_trans[0](x, alphas, (ratio_fusion, ratio_out))
    for i in range(self.op_num-1):
        output = self._op_trans[i](output, alphas, (ratio_out, ratio_out))
    return output
...


nn.Sequential()
...
def __init__(self, ...):
    self.op_num = 3
    self._op_trans = nn.Sequential(OrderedDict([(str(i), ISPMixedOp(C_out, C_out, op_idx, stride=1, shift=shift)) for i in range(self.op_num)]))

def forward(self, x):
    output = self.op_trans(x)
...

你可能感兴趣的:(pytorch,python,深度学习)