title: How to access pytorch model parameters by index and then update that specific layer?
body: <p>If I have a network of 5 layers, I'd like to update the first layer only.</p>
  <p>Currently, I'm doing something like this. I managed to get the weights of the first layer, change them and return the weights (of the first layer only). However, I can't combine and update the weights back with the original myModel.</p>
  <pre><code>for parameter in myModel.parameters():
      layers=[x.data for x in myModel.parameters()]  
      print(layers[0])  # get the first layer
      ... # code to change up the weights of the first layer.
      # now how do I update the first layer of myModel?
  
  ---
  
  </code></pre>
  <p>Class definition of model:</p>
  <pre><code>class Actor(nn.Module):
  
   def __init__(self, state_dim=s_dim, action_dim=1, action_lim=a_max):
  
      super(Actor, self).__init__()
  
      self.state_dim = state_dim
      self.action_dim = action_dim
      self.action_lim = torch.Tensor([action_lim])
      self.fitness = 0
      self.fc1 = nn.Linear(state_dim,256)
      self.bn1 = nn.BatchNorm1d(1)
      self.fc2 = nn.Linear(256,128)
      #self.fc2.weight.data = fanin_init(self.fc2.weight.data.size())
      self.bn2 = nn.BatchNorm1d(1)
      self.fc3 = nn.Linear(128,64)
      self.bn3 = nn.BatchNorm1d(1)
      self.fc4 = nn.Linear(64,action_dim) 
  
   def forward(self, state):
  
      x = self.fc1(state)
      x = F.relu(x)
      x = self.fc2(x)
      x = F.relu(x)
      x = self.fc3(x)
      x = F.relu(x)
      action = T.tanh(self.fc4(x))
  
      action = action * self.action_lim
  
      return action
  </code></pre>
  <p>This problem occurs when I'm working on crossover operation of genetic algorithm. The idea is to get two actors(genes) and crossover the weights of the first layers. And then update the original two actors with two updated first layers(offsprings). So trying to get an updated actor model with the weights of the first layers changed.</p>
  <pre><code>        for name, target_param in gene1.named_parameters():
              layers1=[x.data for x in gene1.parameters()]
              layers2=[x.data for x in gene2.parameters()]
              y1 = layers1[0]
              y2 = layers2[0]
              try: num_cross_overs = random.randint(0, int(layers1[0].shape[0] * 0.3))  # Number of Crossovers
              except: num_cross_overs = 1 
              for i in range(num_cross_overs):
                  receiver_choice = random.random()  # Choose which gene to receive the perturbation, returns random floating number between 0 and 1.
                  if receiver_choice &lt; 0.5:   # W1 chosen to receive the perturbation.
                      ind_cr = random.randint(0, y1.shape[0]-1)   # Only need to crossover the output weights of the layer. Choose a crossover point.
                      y1[ind_cr, :] = y2[ind_cr, :]   # Exchange a certain part of the weights, it only swaps a certain index of the list rather than all the index leading up to it. But rmr that there's multiple crossovers.
                  else:
                      ind_cr = random.randint(0, y1.shape[0]-1)  
                      y2[ind_cr, :] = y1[ind_cr, :]
  </code></pre>