什么时候应该使用 nn.ModuleList,什么时候应该使用 nn.Sequential? [英] When should I use nn.ModuleList and when should I use nn.Sequential?
问题描述
我是 Pytorch 的新手,我不太明白的一件事是 nn.ModuleList
和 nn.Sequential
的用法.我能知道什么时候应该使用一个而不是另一个吗?谢谢.
I am new to Pytorch and one thing that I don't quite understand is the usage of nn.ModuleList
and nn.Sequential
. Can I know when I should use one over the other? Thanks.
推荐答案
nn.ModuleList
没有 forward
方法,但是 nn.Sequential
确实有一个.因此,您可以在 nn.Sequential
中包装多个模块并在输入上运行它.
nn.ModuleList
does not have a forward
method, but nn.Sequential
does have one. So you can wrap several modules in nn.Sequential
and run it on the input.
nn.ModuleList
只是一个 Python list(尽管它很有用,因为可以通过优化器发现和训练参数).而 nn.Sequential
是一个在输入上顺序运行组件的模块.
nn.ModuleList
is just a Python list (though it's useful since the parameters can be discovered and trained via an optimizer). While nn.Sequential
is a module that sequentially runs the component on the input.
这篇关于什么时候应该使用 nn.ModuleList,什么时候应该使用 nn.Sequential?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!