pytorch中model.modules()和model.children()的区别

model.modules()和model.children()均为迭代器,model.modules()会遍历model中所有的子层,而model.children()仅会遍历当前层。

# model.modules()类似于 [[1, 2], 3],其遍历结果为:
[[1, 2], 3], [1, 2], 1, 2, 3
 
# model.children()类似于 [[1, 2], 3],其遍历结果为:
[1, 2], 3

用model.children()进行初始化参数时,可能会漏掉部分,用model.modules()会遍历所有层

参考链接:https://discuss.pytorch.org/t/module-children-vs-module-modules/4551

原文地址:https://www.cnblogs.com/tingtin/p/13544596.html