Sorry for my vague description. Actually I asked two seperated problem, which may mislead you.

The background of the first question is  fitler pruning, specifically SOFT Filter Pruning, for which I would like to write code as shown bellow.  As we can see, before each trainging epoch, I would manipulate the weight and pass the new_state_dict to the model. The first question is that is my code OK according to your  preliminary judgment? Would this cause any latent bugs such as AutoGradient  problem?

for loop in training_epochs:
        optimizer.zero_grad()
        loss.backward()
        
        # manipulate conva.weight of state_dict, then load the new_state_dict
        new_state_dict = model.state_dict() 
        # an manipulated example. The key here is assign a from_numpy to the weight
        new_state_dict['conva.weight'] = torch.from_numpy(np.zeros(new_state_dict['conva.weight'].shape )
        model.load_state_dict(new_state_dict)

        optimizer.step()


The second question is seperate from the former one. Said that state_dict['conva.weight'].shape==[16,3,3,3], i.e., there are 16 filters. Now I want to update only the last 8 filters in the training period while  keeping the first 8 filters the same and unchagned, and my method is to make the gradient of the first 8 filters zero in every epoch as follows.
Is the code OK? (I am afraid of latent bugs)

for loop in training_epochs:
        optimizer.zero_grad()
        loss.backward()
        
        for item in model.named_parameters():
             if item[0]=='conva.weight':
                     grad_mask = np.ones(item.grad.shape)
                     grad_mask[8:,:,:,:] = 0
                     item.grad.data = grad_mask
        optimizer.step()