https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
LibtorchRLPPOLoss Class Reference

PPO clipped surrogate loss on top of the reusable RL buffer/value-estimation core. More...

#include <LibtorchRLPPOLoss.h>

Public Member Functions

 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.
 

Static Private Member Functions

static torch::Tensor reduceActionDimension (const torch::Tensor &tensor)
 Collapse multi-action log-probabilities or entropies into one column tensor.
 

Private Attributes

const Real _clip_parameter
 PPO clipping width used in the actor surrogate objective.
 
const Real _entropy_coeff
 Weight applied to the entropy bonus inside the actor loss.
 

Detailed Description

PPO clipped surrogate loss on top of the reusable RL buffer/value-estimation core.

Definition at line 34 of file LibtorchRLPPOLoss.h.

Constructor & Destructor Documentation

◆ LibtorchRLPPOLoss()

LibtorchRLPPOLoss::LibtorchRLPPOLoss ( Real  clip_parameter,
Real  entropy_coeff 
)

Build the PPO loss helper.

Parameters
clip_parameterPPO clipping width.
entropy_coeffWeight applied to the entropy bonus.

Definition at line 14 of file LibtorchRLPPOLoss.C.

15 : _clip_parameter(clip_parameter), _entropy_coeff(entropy_coeff)
16{
17}
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.

Member Function Documentation

◆ compute()

LibtorchRLPPOLossOutput LibtorchRLPPOLoss::compute ( Moose::LibtorchActorNeuralNet &  policy_network,
Moose::LibtorchArtificialNeuralNet &  value_network,
const LibtorchRLMiniBatch &  batch 
) const

Compute actor, critic, and entropy terms for one mini-batch.

Parameters
policy_networkActor network used for the policy term.
value_networkCritic network used for the value term.
batchMini-batch pulled from the on-policy trajectory buffer.

Definition at line 20 of file LibtorchRLPPOLoss.C.

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}
static torch::Tensor reduceActionDimension(const torch::Tensor &tensor)
Collapse multi-action log-probabilities or entropies into one column tensor.
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
auto exp(const T &)
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.

Referenced by LibtorchDRLControlTrainer::trainController().

◆ reduceActionDimension()

torch::Tensor LibtorchRLPPOLoss::reduceActionDimension ( const torch::Tensor &  tensor)
staticprivate

Collapse multi-action log-probabilities or entropies into one column tensor.

Parameters
tensorAction-wise tensor to reduce.

Definition at line 46 of file LibtorchRLPPOLoss.C.

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}

Referenced by compute().

Member Data Documentation

◆ _clip_parameter

const Real LibtorchRLPPOLoss::_clip_parameter
private

PPO clipping width used in the actor surrogate objective.

Definition at line 62 of file LibtorchRLPPOLoss.h.

Referenced by compute().

◆ _entropy_coeff

const Real LibtorchRLPPOLoss::_entropy_coeff
private

Weight applied to the entropy bonus inside the actor loss.

Definition at line 64 of file LibtorchRLPPOLoss.h.

Referenced by compute().


The documentation for this class was generated from the following files: