https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchRLPPOLoss.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#ifdef MOOSE_LIBTORCH_ENABLED
11
12#include "LibtorchRLPPOLoss.h"
13
14LibtorchRLPPOLoss::LibtorchRLPPOLoss(const Real clip_parameter, const Real entropy_coeff)
15 : _clip_parameter(clip_parameter), _entropy_coeff(entropy_coeff)
16{
17}
18
22 const LibtorchRLMiniBatch & batch) const
23{
24 auto observations = batch.observations;
25 policy_network.evaluate(observations, false);
26
27 const auto current_log_probability =
28 reduceActionDimension(policy_network.logProbability(batch.actions));
29 const auto previous_log_probability = reduceActionDimension(batch.old_log_probabilities);
30 const auto entropy = reduceActionDimension(policy_network.entropy());
31
32 const auto ratio = (current_log_probability - previous_log_probability).exp();
33 const auto surr1 = ratio * batch.advantages;
34 const auto surr2 =
35 torch::clamp(ratio, 1.0 - _clip_parameter, 1.0 + _clip_parameter) * batch.advantages;
36
38 output.actor_loss = -(torch::min(surr1, surr2) + _entropy_coeff * entropy).mean();
39 output.critic_loss =
40 torch::mse_loss(value_network.forward(batch.observations), batch.value_targets);
41 output.entropy = entropy.mean();
42 return output;
43}
44
45torch::Tensor
46LibtorchRLPPOLoss::reduceActionDimension(const torch::Tensor & tensor)
47{
48 if (!tensor.defined())
49 return tensor;
50
51 if (tensor.dim() == 1)
52 return tensor.unsqueeze(1);
53
54 if (tensor.size(-1) == 1)
55 return tensor;
56
57 return tensor.sum(-1, true);
58}
59
60#endif
static torch::Tensor reduceActionDimension(const torch::Tensor &tensor)
Collapse multi-action log-probabilities or entropies into one column tensor.
const Real _entropy_coeff
Weight applied to the entropy bonus inside the actor loss.
const Real _clip_parameter
PPO clipping width used in the actor surrogate objective.
LibtorchRLPPOLoss(Real clip_parameter, Real entropy_coeff)
Build the PPO loss helper.
LibtorchRLPPOLossOutput compute(Moose::LibtorchActorNeuralNet &policy_network, Moose::LibtorchArtificialNeuralNet &value_network, const LibtorchRLMiniBatch &batch) const
Compute actor, critic, and entropy terms for one mini-batch.
Feed-forward actor network coupled to a Gaussian or Beta action distribution.
torch::Tensor logProbability(const torch::Tensor &other)
Evaluate the log-probability of an action under the current actor state.
virtual torch::Tensor evaluate(torch::Tensor &input, bool sampled, c10::optional< at::Generator > generator=c10::nullopt)
Evaluate the actor and either sample from it or use its deterministic action.
torch::Tensor entropy()
Compute the entropy of the current action distribution.
virtual torch::Tensor forward(const torch::Tensor &x) override
Tensor data for one PPO mini-batch.
torch::Tensor actions
Action rows that match the sampled observations.
torch::Tensor value_targets
Critic targets aligned with the sampled observations.
torch::Tensor old_log_probabilities
Behavior-policy log probabilities for the sampled actions.
torch::Tensor advantages
Advantage estimates aligned with the sampled observations.
torch::Tensor observations
Flattened observation rows for the mini-batch.
Actor, critic, and entropy terms produced by one PPO loss evaluation.
torch::Tensor actor_loss
Clipped actor loss for the current mini-batch.
torch::Tensor critic_loss
Critic regression loss for the current mini-batch.
torch::Tensor entropy
Mean action-distribution entropy for the current mini-batch.