I’m implementing a version of DDPG, and trying to calculate the policy loss.
# TODO: Should find grad for states, policy_actions but treat q_model params as constant
policy_loss = -self.q_model(states, policy_actions).mean()

I can do this by looping through q_model params, storing current values, setting requires_grad to False, calculating policy_loss, then restoring after.
orig_requires_grads = [p.requires_grad for p in self.q_model.parameters()]
for p in self.q_model.parameters(): p.requires_grad = False
policy_loss = -self.q_model(states, policy_actions).mean()
for p, rg in zip(self.q_model.parameters(), orig_requires_grads): p.requires_grad = rg

This feels clumsy. Is there a better way to temporarily disable a model like this? I looked for something like self.q_model.detach()(states, policy_actions).mean() but was surprised to find it doesn’t exist. Obviously I could make a helper.
I plan on moving this model over to TorchScript at some point, and I’m not sure whether this is going to work out there?
There are some old threads that indicate that manual looping is the best way, but I want to check in case any developments have happened here.

  
      github.com/pytorch/pytorch
  
  
    
  
	  
  

  
    
      [Feature request] detach (fix?) a nn.module 13
    

    
      
        opened 
        
          
        
        Sep 7, 2017
      
      

        
          closed 
        
          
        
        Mar 12, 2020
      
        

      
        
          
          ruotianluo
        
      
    
  



  Suppose, we have a siamese network.
out1 = net(in1)
out2 = net(in2)
However, we want the gradient of parameters in net only affected by...



    triage review


  
  
    
    
  
  



  
    
    
    Detach the loaded model 
  
  
    Is there a correct way to detach a loaded model, when we don’t need to compute its gradients ? 
For the moment, I’m doing that: 
model = torch.load('mymodel.pth')
for variable in model.parameters():
    variable.detach_()

Here, I am lucky because the model contains Variable parameters, but it could contain sub-models…