torch访问nn.Sequential()的每一层

1.访问中间层

import torch
import torch.nn as nn
model = nn.Sequential(
            nn.Linear(10,10),
            nn.ReLU(),
            nn.Linear(10,10),
            nn.ReLU(),
            nn.Linear(10,10),
            nn.ReLU(),
            nn.Linear(10,10),
            nn.ReLU(),
        )
for i,layer in enumerate(list(model)):
        print(i,layer)
    
#输出:
0 Linear(in_features=10, out_features=10, bias=True)
1 ReLU()
2 Linear(in_features=10, out_features=10, bias=True)
3 ReLU()
4 Linear(in_features=10, out_features=10, bias=True)
5 ReLU()
6 Linear(in_features=10, out_features=10, bias=True)
7 ReLU()

 

posted @ 2021-07-09 10:38  lypbendlf  阅读(1305)  评论(0)    收藏  举报