ptrblck:

nn.Sequential


Thank you for your reply, @ptrblck.
I tried
self.layer5 = nn.Sequential(nn.Linear(512, 512),
                          nn.ReLU(),
                          nn.Linear(512, num_classes))
        
self.num_ftrs = 2   
for i in range(self.num_ftrs):
       setattr(self, "fc%d" % i, self.layer5)

and in the forward
clf_outputs = []      
for i in range(self.num_ftrs):
      clf_outputs.append(getattr(self, "fc%d" % i)(out))

but the output clf_outputs[0] and clf_outputs[1] were the same. The output is different if I change it to
self.layer5 = nn.Sequential(nn.Linear(512, 512),
                    nn.ReLU(),
                    nn.Linear(512, num_classes))

self.layer6 = nn.Sequential(nn.Linear(512, 512),
                    nn.ReLU(),
                    nn.Linear(512, num_classes))

and in forward
clf_outputs1 = self.layer5(out)
clf_outputs2 = self.layer6(out)
clf_outputs = [clf_outputs1, clf_outputs2]

But I want the first approach getting work, Is there any ways that I can keep using setattr, getattr and getting different outputs?