pytorch中children(),modules(),named_children(),named_modules(),named_parameters(),parameters()的区别

  children():返回包含直接子模块的迭代器

for module in model.children():
    print(module)
    
GRU(34, 144, num_layers=2, batch_first=True, bidirectional=True)
Sequential(
  (0): Linear(in_features=288, out_features=144, bias=True)
  (1): ReLU()
  (2): Linear(in_features=144, out_features=2, bias=True)
)

 modules():(递归)返回包含所有子模块(直接、间接)的迭代器

for module in model.modules():
    print(module)
    
Model(
  (rnn): GRU(34, 144, num_layers=2, batch_first=True, bidirectional=True)
  (classifier): Sequential(
    (0): Linear(in_features=288, out_features=144, bias=True)
    (1): ReLU()
    (2): Linear(in_features=144, out_features=2, bias=True)
  )
)
GRU(34, 144, num_layers=2, batch_first=True, bidirectional=True)
Sequential(
  (0): Linear(in_features=288, out_features=144, bias=True)
  (1): ReLU()
  (2): Linear(in_features=144, out_features=2, bias=True)
)
Linear(in_features=288, out_features=144, bias=True)
ReLU()
Linear(in_features=144, out_features=2, bias=True)

named_children() :返回包含直接子模块的迭代器,同时产生模块的名称以及模块本身

for name, module in model.named_children():
    print(name, module)
    
rnn GRU(34, 144, num_layers=2, batch_first=True, bidirectional=True)
classifier Sequential(
  (0): Linear(in_features=288, out_features=144, bias=True)
  (1): ReLU()
  (2): Linear(in_features=144, out_features=2, bias=True)
)

 named_modules():返回包含所有子模块(直接、间接)的迭代器,同时产生模块的名称以及模块本身

for name, module in model.named_modules():
    print(name, module)
    
 Model(
  (rnn): GRU(34, 144, num_layers=2, batch_first=True, bidirectional=True)
  (classifier): Sequential(
    (0): Linear(in_features=288, out_features=144, bias=True)
    (1): ReLU()
    (2): Linear(in_features=144, out_features=2, bias=True)
  )
)
rnn GRU(34, 144, num_layers=2, batch_first=True, bidirectional=True)
classifier Sequential(
  (0): Linear(in_features=288, out_features=144, bias=True)
  (1): ReLU()
  (2): Linear(in_features=144, out_features=2, bias=True)
)
classifier.0 Linear(in_features=288, out_features=144, bias=True)
classifier.1 ReLU()
classifier.2 Linear(in_features=144, out_features=2, bias=True)

named_parameters():返回模块参数上的迭代器,产生参数的名称和参数本身

for name, parameter in model.named_parameters():
    print(name, parameter)

rnn.weight_ih_l0 Parameter containing:
[432, 34] float32@cuda:0
tensor([[-0.0785, -0.0164, -0.0400,  ..., -0.0276,  0.0482, -0.0297],
        [ 0.0041,  0.0281,  0.0573,  ..., -0.0196,  0.0507, -0.0302],
        [-0.0349, -0.0134, -0.0212,  ...,  0.0052,  0.0242,  0.0371],
        ...

rnn.weight_hh_l0 Parameter containing:
[432, 144] float32@cuda:0
tensor([[-0.0689,  0.0739,  0.0355,  ...,  0.0283,  0.0801, -0.0530],
        [ 0.0135,  0.0520,  0.0689,  ...,  0.0556,  0.0094,  0.0135],
        [ 0.0598, -0.0322,  0.0021,  ..., -0.0829, -0.0644, -0.0503],
        ...

rnn.bias_ih_l0 Parameter containing:...
rnn.bias_hh_l0 Parameter containing:...
rnn.weight_hh_l0_reverse Parameter containing:...
......

parameters(): 返回模块参数上的迭代器,不包括名称(参考上)

 

你可能感兴趣的:(python,深度学习,自然语言处理,pytorch,神经网络)