def calculate_gradient(self, batch_info, device, model, rollout):
rollout = rollout.to_transitions()<EOL>dones = rollout.batch_tensor('<STR_LIT>')<EOL>rewards = rollout.batch_tensor('<STR_LIT>')<EOL>observations_next = rollout.batch_tensor('<STR_LIT>')<EOL>actions = rollout.batch_tensor('<STR_LIT>')<EOL>observations = rollout.batch_tensor('<STR_LIT>')<EOL>with torch.no_grad():<EOL><INDENT>target_next_value = self.target_model.value(observations_next)<EOL>target_value = rewards + (<NUM_LIT:1.0> - dones) * self.discount_factor * target_next_value<EOL><DEDENT>model_value = model.value(observations, actions)<EOL>value_loss = F.mse_loss(model_value, target_value)<EOL>value_loss.backward()<EOL>model_action = model.action(observations)<EOL>model_action_value = model.value(observations, model_action)<EOL>policy_loss = -model_action_value.mean()<EOL>model_action_grad = torch.autograd.grad(policy_loss, model_action)[<NUM_LIT:0>]<EOL>model_action.backward(gradient=model_action_grad)<EOL>return {<EOL>'<STR_LIT>': policy_loss.item(),<EOL>'<STR_LIT>': value_loss.item(),<EOL>}<EOL>
Calculate loss of the supplied rollout