Is this correct?
Invert gradient function for actions defined in the interval [-1,1]:
def invert_gradient(grads, action):

    pdiff_max = torch.div(-action+1.0,2.0)
    pdiff_min = torch.div(action+1.0,2.0)

    zeros_grad    = torch.zeros_like(grads)
    grad_inverter = torch.zeros_like(grads)

    grad_inverter = torch.where(torch.gt(grads, zeros_grad), torch.mul(grads,pdiff_max), torch.mul(grads,pdiff_min))

    return grad_inverter

Outside training loop:
params_grads = {}
for name, param in actor.named_parameters():
    params_grads[name] = torch.zeros_like(param.data)

Inside training loop to update actor network parameters:
actor_optimizer.zero_grad()
action_Q = actor(state_batch)
policy_output = -critic(state_batch,action_Q)

for i in range(policy_output.size()[0]):
      policy_output[i].backward(retain_graph=True)
      for name, param in actor.named_parameters():
            params_grads[name] += invert_gradient(param.grad.data, action_Q.detach()[i])

     for name, param in actor.named_parameters():
          param.grad.data = params_grads[name]/batch_size
          params_grads[name] = torch.zeros_like(param.data)

actor_optimizer.step()