title: Can't replace classifier on Densenet121 in pytorch
body: <p>I am trying to do some transfer learning using this github DenseNet121 model (<a href="https://github.com/gaetandi/cheXpert.git" rel="nofollow noreferrer">https://github.com/gaetandi/cheXpert.git</a>). I'm running into issues resizing the classification layer from 14 to 2 outputs. </p>
  
  <p>Relevant part of the github code is:</p>
  
  <pre><code>class DenseNet121(nn.Module):
      """Model modified.
      The architecture of our model is the same as standard DenseNet121
      except the classifier layer which has an additional sigmoid function.
      """
      def __init__(self, out_size):
          super(DenseNet121, self).__init__()
          self.densenet121 = torchvision.models.densenet121(pretrained=True)
          num_ftrs = self.densenet121.classifier.in_features
          self.densenet121.classifier = nn.Sequential(
              nn.Linear(num_ftrs, out_size),
              nn.Sigmoid()
          )
  def forward(self, x):
      x = self.densenet121(x)
      return x
  </code></pre>
  
  <p>I load and init with:</p>
  
  <pre><code># initialize and load the model
  model = DenseNet121(nnClassCount).cuda()
  model = torch.nn.DataParallel(model).cuda()
  modeldict = torch.load("model_ones_3epoch_densenet.tar")
  model.load_state_dict(modeldict['state_dict'])
  </code></pre>
  
  <p>It looks like DenseNet doesn't split layers up into children so <code>model = nn.Sequential(*list(modelRes.children())[:-1])</code> won't work. </p>
  
  <p><code>model.classifier = nn.Linear(1024, 2)</code> seems to work on default DenseNets, but with the modified classifier (additional sigmoid function) here it ends up just adding an additional classifier layer without replacing the original.</p>
  
  <p>I've tried </p>
  
  <pre><code>model.classifier = nn.Sequential(
      nn.Linear(1024, dset_classes_number), 
      nn.Sigmoid()
  )
  </code></pre>
  
  <p>But am having the same added instead of replaced classifier issue:</p>
  
  <pre><code>...
        )
        (classifier): Sequential(
          (0): Linear(in_features=1024, out_features=14, bias=True)
          (1): Sigmoid()
        )
      )
    )
    (classifier): Sequential(
      (0): Linear(in_features=1024, out_features=2, bias=True)
      (1): Sigmoid()
    )
  )
  </code></pre>