If I want to add a fully connected layer after pooling in the Resnet, how can use setattr and getattr instead of this:
self.layer1 = nn.Linear(512, 512)
self.layer2 = nn.Linear(512, num_classes)

self.layer3 = nn.Linear(512, 512)
self.layer4 = nn.Linear(512, num_classes)


out = out.view(out.size(0), -1)
out1 = self.layer1(out)
out1 = F.relu(out1)
out1 = self.layer2(out1)

out2 = self.layer3(out)
out2 = F.relu(out2)
out2 = self.layer4(out2)

this one shows error,
self.num_ftrs = num_filters   
       for i in range(self.num_ftrs):
           setattr(self, "fc%d" % i, nn.Linear(512, 512))
           setattr(self, "fc%d" % i, nn.Linear(512, num_classes))


out = out.view(out.size(0), -1)
outputs = []      
for i in range(self.num_ftrs):
       out = getattr(self, "fc%d" % i)(out)
       out = F.relu(out)
       outputs.append(getattr(self, "fc%d" % i)(out))