Get key of nn.Sequential made with OrderedDict

In the case like image above, can I extract the name(I mean original key of OrderedDict) of the Sequential module?

It seems easy but not easy…

Oh, I founded nn.ModuleDict
https://pytorch.org/docs/stable/generated/torch.nn.ModuleDict.html
I’d better to use this than Sequential with OrderedDict…

Please share ideas
Thank you

Hi, yes.
ModuleDict can be used with Sequential and then queried using layer names, like so -

model_dict = nn.ModuleDict({
                'fc1': nn.Linear(786, 120),
                'first_fc1_relu': nn.ReLU(),
                'fc2': nn.Linear(120, 10)
        })

model = nn.Sequential(model_dict)
print(model)
print(model[0]['fc1'] )

gives

Sequential(
  (0): ModuleDict(
    (fc1): Linear(in_features=786, out_features=120, bias=True)
    (first_fc1_relu): ReLU()
    (fc2): Linear(in_features=120, out_features=10, bias=True)
  )
)

Linear(in_features=786, out_features=120, bias=True)

With OrderedDict, you can access the specific layers by their names like this -

model_dict = OrderedDict()
model_dict['fc1'] = nn.Linear(786, 120)
print(model_dict)
model = nn.Sequential(model_dict)
print(model)
print(model.fc1)

gives

Sequential(
  (fc1): Linear(in_features=786, out_features=120, bias=True)
)

Linear(in_features=786, out_features=120, bias=True)

In both cases, the layers’ parameters are registered properly.

1 Like