N1H111SM's Miniverse

2020/05/17 Share

Materials

# Mechanism

.parameters() works like this: it walks all members of the class (anything added to self) and does one of three things with each member:

• If the member is a parameter (something registered with register_parameter(...) or of type nn.Parameter), it adds it to the parameters list.
• If the member is of type (or is a subclass of) nn.Module, .parameters() is called recursively.
• Otherwise, it is ignored

In theory, you could add a 4th option to handle lists, but nn.ModuleList was chosen instead.

# Difference

nn.Sequentialnn.ModuleList 有以下两点不同：

• Sequential实现定义了forward函数，可以直接拿来作为Module接受输入输出；而ModuleList只是将一个列表的带参tensor集合在一起，forward函数需要自己再进行手动定义。
• Sequential定义好了不同layer之间的连接顺序；而ModuleList可以在forward函数中更加自由地定义层与层之间的连接关系，甚至可以重复使用实现参数共享。