I am trying to get a dictionary output with key as the layer name and value as the parent layer name.
For example.
class Model(nn.Module):
def __init__(self, instance_size=15, X_size=5, Uncern_size=5):
super(MyModel, self).__init__()
self.embedding_instance = nn.Linear(instance_size, 8)
self.embedding_X = nn.Linear(X_size, 3)
self.embedding_uncern = nn.Linear(Uncern_size, 3)
self.fc1 = nn.Linear(14, 5)
self.fc2 = nn.Linear(5, 1)
def forward(self, instance, X, uncern):
instance_embed = torch.relu(self.embedding_instance(instance))
X_embed = torch.relu(self.embedding_X(X))
uncern_embed = torch.relu(self.embedding_uncern(uncern))
concatenated = torch.cat((instance_embed, X_embed, uncern_embed), dim=1)
output = torch.relu(self.fc1(concatenated))
output = self.fc2(output)
return output
model =Model()
The function I am trying to write should return the child-parent (key-value) relation as follows.
{ input_layers => [“instance”, “X”, “Uncern”],
hidden_layers =>{“embedding_inst” => [“instance”],
“enbedding_X” => [“X”],
“embedding_Uncern” => [“Uncern”],
“fc1” => [“embedding_inst”, “embedding_X”, “embedding_Uncern”]},
output_layers => {“fc2” => [“fc1”])}
}
I am trying to use the model.fc1.named_children() method, but it returns a empty list. I was hoping to get the layer name “fc2” as an output so that I could write a function that produced the desired dict. Is there a way I could do it?