Pytorch-nn.Module-modules()

测试代码:

import torch.nn as nn
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(10, 20, 4)
        self.conv2 = nn.Conv2d(20, 40, 4)

model = Model()

for m in model.modules():
    print(m)

 

结果:

Model(
  (conv1): Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
  (conv2): Conv2d(20, 40, kernel_size=(4, 4), stride=(1, 1))
)
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
Conv2d(20, 40, kernel_size=(4, 4), stride=(1, 1))

结论:

 modules()返回一个包含 当前模型 所有模块的迭代器。

    原文作者:pytorch
    原文地址: https://www.cnblogs.com/leebxo/p/10096686.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞