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

Gaussian action distribution for unbounded action spaces. More...

#include <LibtorchActionDistribution.h>

Inheritance diagram for Moose::LibtorchGaussianActionDistribution:
[legend]

Public Member Functions

 LibtorchGaussianActionDistribution (const std::string &name, unsigned int num_inputs, unsigned int num_outputs, torch::DeviceType device_type=torch::kCPU, torch::ScalarType scalar_type=torch::kDouble, bool build_on_construct=true, const std::vector< Real > &output_scaling_factors={}, bool state_independent_std=true)
 Build the Gaussian action distribution used for unbounded controls.
 
virtual void initialize (c10::optional< at::Generator > generator=c10::nullopt) override
 Initialize the trainable Gaussian distribution parameters.
 
virtual void reset (const torch::Tensor &input) override
 Refresh the Gaussian distribution parameters from the latest actor features.
 
virtual torch::Tensor sample (c10::optional< at::Generator > generator=c10::nullopt) const override
 Draw a stochastic Gaussian action sample in physical units.
 
virtual torch::Tensor deterministicAction () const override
 Return the Gaussian mean action used for deterministic evaluation.
 
virtual torch::Tensor logProbability (const torch::Tensor &action) const override
 Evaluate the log-probability of an action under the current Gaussian distribution.
 
virtual torch::Tensor entropy () const override
 Compute the entropy of the current Gaussian distribution.
 
virtual bool isBounded () const override
 Return false because the Gaussian distribution is unbounded.
 
bool stateIndependentStd () const
 Return whether the Gaussian standard deviation ignores the current actor features.
 
torch::nn::Linear & meanModule ()
 Return the Gaussian mean head.
 
const torch::nn::Linear & meanModule () const
 Return the Gaussian mean head.
 
torch::nn::Linear & stdModule ()
 Return the state-dependent Gaussian standard-deviation head.
 
const torch::nn::Linear & stdModule () const
 Return the state-dependent Gaussian standard-deviation head.
 
torch::Tensor & logStd ()
 Return the state-independent Gaussian log-standard-deviation parameter.
 
const torch::Tensor & logStd () const
 Return the state-independent Gaussian log-standard-deviation parameter.
 
const torch::Tensor & stdTensor () const
 Return the cached Gaussian standard-deviation tensor.
 
void synchronizeScalingFactorsFromBuffer ()
 Sync cached scaling metadata from the registered buffers after loading state.
 

Protected Member Functions

torch::Tensor prepareFeatures (const torch::Tensor &input) const
 Convert actor features to the configured device and scalar type.
 
torch::Tensor prepareAction (const torch::Tensor &action) const
 Convert actions to the configured device and scalar type.
 
const torch::Tensor & actionScaleTensor () const
 Return the registered tensor that stores per-action scaling factors.
 

Protected Attributes

const std::string _name
 Module name used for registration and serialization.
 
const unsigned int _num_inputs
 Number of actor features feeding this distribution.
 
const unsigned int _num_outputs
 Number of action dimensions produced by this distribution.
 
const torch::DeviceType _device_type
 Torch device used by this distribution.
 
const torch::ScalarType _data_type
 Torch scalar type used by this distribution.
 
std::vector< Real > _output_scaling_factors
 Cached per-action scaling factors applied in physical units.
 
torch::Tensor _action_scale_tensor
 Registered libtorch buffer holding the per-action scaling factors.
 

Private Member Functions

void constructDistribution ()
 Build and register the Gaussian distribution heads.
 

Private Attributes

const bool _state_independent_std
 Whether the Gaussian std ignores the current actor features.
 
torch::nn::Linear _mean_module {nullptr}
 Linear head that produces the Gaussian action mean.
 
torch::nn::Linear _std_module {nullptr}
 Linear head that produces state-dependent Gaussian log-std inputs.
 
torch::Tensor _log_std_parameter
 Trainable state-independent Gaussian log-std.
 
torch::Tensor _mean
 Cached Gaussian action mean from the latest reset.
 
torch::Tensor _std_tensor
 Cached Gaussian action standard deviation from the latest reset.
 
torch::Tensor _log_std_tensor
 Cached Gaussian action log standard deviation from the latest reset.
 

Detailed Description

Gaussian action distribution for unbounded action spaces.

Definition at line 126 of file LibtorchActionDistribution.h.

Constructor & Destructor Documentation

◆ LibtorchGaussianActionDistribution()

Moose::LibtorchGaussianActionDistribution::LibtorchGaussianActionDistribution ( const std::string &  name,
unsigned int  num_inputs,
unsigned int  num_outputs,
torch::DeviceType  device_type = torch::kCPU,
torch::ScalarType  scalar_type = torch::kDouble,
bool  build_on_construct = true,
const std::vector< Real > &  output_scaling_factors = {},
bool  state_independent_std = true 
)

Build the Gaussian action distribution used for unbounded controls.

Parameters
nameModule name used for registration and serialization.
num_inputsNumber of actor features feeding the distribution.
num_outputsNumber of action dimensions produced by the distribution.
device_typeTorch device used by the module.
scalar_typeTorch scalar type used by the module.
build_on_constructWhether to build the torch modules right away.
output_scaling_factorsOptional per-action scaling applied in physical units.
state_independent_stdWhether the learned std should ignore the current state.

Definition at line 102 of file LibtorchActionDistribution.C.

112 name, num_inputs, num_outputs, device_type, data_type, output_scaling_factors),
113 _state_independent_std(state_independent_std)
114{
115 if (build_on_construct)
117}
const std::string name
Definition Setup.h:21
LibtorchActionDistribution(const std::string &name, unsigned int num_inputs, unsigned int num_outputs, torch::DeviceType device_type=torch::kCPU, torch::ScalarType scalar_type=torch::kDouble, const std::vector< Real > &output_scaling_factors={})
Build an action-distribution module for an actor network.
void constructDistribution()
Build and register the Gaussian distribution heads.
const bool _state_independent_std
Whether the Gaussian std ignores the current actor features.

Member Function Documentation

◆ actionScaleTensor()

const torch::Tensor & Moose::LibtorchActionDistribution::actionScaleTensor ( ) const
inlineprotectedinherited

◆ constructDistribution()

void Moose::LibtorchGaussianActionDistribution::constructDistribution ( )
private

Build and register the Gaussian distribution heads.

Definition at line 120 of file LibtorchActionDistribution.C.

121{
122 _mean_module = register_module(
123 "mean", torch::nn::Linear(torch::nn::LinearOptions(_num_inputs, _num_outputs).bias(true)));
125
127 _log_std_parameter = register_parameter(
128 "log_std",
129 torch::zeros({_num_outputs},
130 torch::TensorOptions().dtype(_data_type).device(_device_type)));
131 else
132 {
133 _std_module = register_module(
134 "std", torch::nn::Linear(torch::nn::LinearOptions(_num_inputs, _num_outputs).bias(true)));
136 }
137}
const torch::DeviceType _device_type
Torch device used by this distribution.
const unsigned int _num_outputs
Number of action dimensions produced by this distribution.
const unsigned int _num_inputs
Number of actor features feeding this distribution.
const torch::ScalarType _data_type
Torch scalar type used by this distribution.
torch::nn::Linear _std_module
Linear head that produces state-dependent Gaussian log-std inputs.
torch::nn::Linear _mean_module
Linear head that produces the Gaussian action mean.
torch::Tensor _log_std_parameter
Trainable state-independent Gaussian log-std.

Referenced by LibtorchGaussianActionDistribution().

◆ deterministicAction()

torch::Tensor Moose::LibtorchGaussianActionDistribution::deterministicAction ( ) const
overridevirtual

Return the Gaussian mean action used for deterministic evaluation.

Implements Moose::LibtorchActionDistribution.

Definition at line 182 of file LibtorchActionDistribution.C.

183{
184 return _mean * actionScaleTensor();
185}
const torch::Tensor & actionScaleTensor() const
Return the registered tensor that stores per-action scaling factors.
torch::Tensor _mean
Cached Gaussian action mean from the latest reset.

◆ entropy()

torch::Tensor Moose::LibtorchGaussianActionDistribution::entropy ( ) const
overridevirtual

Compute the entropy of the current Gaussian distribution.

Implements Moose::LibtorchActionDistribution.

Definition at line 201 of file LibtorchActionDistribution.C.

202{
203 const auto log_action_scale = torch::log(torch::abs(actionScaleTensor()));
204 constexpr Real pi = 3.14159265358979323846;
205 return 0.5 * std::log(2.0 * pi) + _log_std_tensor + 0.5 + log_action_scale;
206}
torch::Tensor _log_std_tensor
Cached Gaussian action log standard deviation from the latest reset.
const Real pi
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

◆ initialize()

void Moose::LibtorchGaussianActionDistribution::initialize ( c10::optional< at::Generator >  generator = c10::nullopt)
overridevirtual

Initialize the trainable Gaussian distribution parameters.

Parameters
generatorOptional random-number generator used for reproducible initialization.

Implements Moose::LibtorchActionDistribution.

Definition at line 140 of file LibtorchActionDistribution.C.

141{
142 Moose::orthogonalInitializeTensor(_mean_module->weight, 1.0, generator);
143 torch::nn::init::zeros_(_mean_module->bias);
144
146 {
147 torch::nn::init::zeros_(_log_std_parameter);
148 return;
149 }
150
151 Moose::orthogonalInitializeTensor(_std_module->weight, 1.0, generator);
152 torch::nn::init::zeros_(_std_module->bias);
153}
void orthogonalInitializeTensor(torch::Tensor &tensor, Real gain=1.0, c10::optional< at::Generator > generator=c10::nullopt)

◆ isBounded()

virtual bool Moose::LibtorchGaussianActionDistribution::isBounded ( ) const
inlineoverridevirtual

Return false because the Gaussian distribution is unbounded.

Implements Moose::LibtorchActionDistribution.

Definition at line 187 of file LibtorchActionDistribution.h.

187{ return false; }

◆ logProbability()

torch::Tensor Moose::LibtorchGaussianActionDistribution::logProbability ( const torch::Tensor &  action) const
overridevirtual

Evaluate the log-probability of an action under the current Gaussian distribution.

Parameters
actionAction tensor in physical units.

Implements Moose::LibtorchActionDistribution.

Definition at line 188 of file LibtorchActionDistribution.C.

189{
190 const auto scaled_action = prepareAction(action);
191 const auto log_action_scale = torch::log(torch::abs(actionScaleTensor()));
192 const auto unscaled_action = scaled_action / actionScaleTensor();
193
194 constexpr Real pi = 3.14159265358979323846;
195 const torch::Tensor var = _std_tensor * _std_tensor;
196 return -((unscaled_action - _mean) * (unscaled_action - _mean)) / (2.0 * var) - _log_std_tensor -
197 0.5 * std::log(2.0 * pi) - log_action_scale;
198}
torch::Tensor prepareAction(const torch::Tensor &action) const
Convert actions to the configured device and scalar type.
torch::Tensor _std_tensor
Cached Gaussian action standard deviation from the latest reset.

◆ logStd() [1/2]

torch::Tensor & Moose::LibtorchGaussianActionDistribution::logStd ( )
inline

Return the state-independent Gaussian log-standard-deviation parameter.

Definition at line 217 of file LibtorchActionDistribution.h.

217{ return _log_std_parameter; }

◆ logStd() [2/2]

const torch::Tensor & Moose::LibtorchGaussianActionDistribution::logStd ( ) const
inline

Return the state-independent Gaussian log-standard-deviation parameter.

Definition at line 222 of file LibtorchActionDistribution.h.

222{ return _log_std_parameter; }

◆ meanModule() [1/2]

torch::nn::Linear & Moose::LibtorchGaussianActionDistribution::meanModule ( )
inline

Return the Gaussian mean head.

Definition at line 197 of file LibtorchActionDistribution.h.

197{ return _mean_module; }

◆ meanModule() [2/2]

const torch::nn::Linear & Moose::LibtorchGaussianActionDistribution::meanModule ( ) const
inline

Return the Gaussian mean head.

Definition at line 202 of file LibtorchActionDistribution.h.

202{ return _mean_module; }

◆ prepareAction()

torch::Tensor Moose::LibtorchActionDistribution::prepareAction ( const torch::Tensor &  action) const
protectedinherited

Convert actions to the configured device and scalar type.

Parameters
actionRaw action tensor.

Definition at line 92 of file LibtorchActionDistribution.C.

93{
94 auto scaled_action = action;
95 if (_data_type != scaled_action.scalar_type())
96 scaled_action = scaled_action.to(_data_type);
97 if (_device_type != scaled_action.device().type())
98 scaled_action = scaled_action.to(_device_type);
99 return scaled_action;
100}

Referenced by logProbability(), and Moose::LibtorchBetaActionDistribution::logProbability().

◆ prepareFeatures()

torch::Tensor Moose::LibtorchActionDistribution::prepareFeatures ( const torch::Tensor &  input) const
protectedinherited

Convert actor features to the configured device and scalar type.

Parameters
inputRaw actor feature tensor.

Definition at line 81 of file LibtorchActionDistribution.C.

82{
83 auto features = input;
84 if (_data_type != features.scalar_type())
85 features = features.to(_data_type);
86 if (_device_type != features.device().type())
87 features = features.to(_device_type);
88 return features;
89}

Referenced by reset(), and Moose::LibtorchBetaActionDistribution::reset().

◆ reset()

void Moose::LibtorchGaussianActionDistribution::reset ( const torch::Tensor &  input)
overridevirtual

Refresh the Gaussian distribution parameters from the latest actor features.

Parameters
inputFeature tensor coming from the actor body.

Implements Moose::LibtorchActionDistribution.

Definition at line 156 of file LibtorchActionDistribution.C.

157{
158 const auto features = prepareFeatures(input);
159 _mean = _mean_module->forward(features);
160
162 {
163 if (_mean.dim() <= 1)
165 else
166 _log_std_tensor = _log_std_parameter.view({1, -1}).expand(_mean.sizes());
167 }
168 else
169 _log_std_tensor = _std_module->forward(features);
170
171 _log_std_tensor = torch::clamp(_log_std_tensor, std::log(1e-12), -std::log(1e-12));
172 _std_tensor = torch::exp(_log_std_tensor);
173}
torch::Tensor prepareFeatures(const torch::Tensor &input) const
Convert actor features to the configured device and scalar type.

◆ sample()

torch::Tensor Moose::LibtorchGaussianActionDistribution::sample ( c10::optional< at::Generator >  generator = c10::nullopt) const
overridevirtual

Draw a stochastic Gaussian action sample in physical units.

Parameters
generatorOptional random-number generator used for sampling.

Implements Moose::LibtorchActionDistribution.

Definition at line 176 of file LibtorchActionDistribution.C.

177{
178 return at::normal(_mean, _std_tensor, generator) * actionScaleTensor();
179}

◆ stateIndependentStd()

bool Moose::LibtorchGaussianActionDistribution::stateIndependentStd ( ) const
inline

Return whether the Gaussian standard deviation ignores the current actor features.

Definition at line 192 of file LibtorchActionDistribution.h.

192{ return _state_independent_std; }

◆ stdModule() [1/2]

torch::nn::Linear & Moose::LibtorchGaussianActionDistribution::stdModule ( )
inline

Return the state-dependent Gaussian standard-deviation head.

Definition at line 207 of file LibtorchActionDistribution.h.

207{ return _std_module; }

◆ stdModule() [2/2]

const torch::nn::Linear & Moose::LibtorchGaussianActionDistribution::stdModule ( ) const
inline

Return the state-dependent Gaussian standard-deviation head.

Definition at line 212 of file LibtorchActionDistribution.h.

212{ return _std_module; }

◆ stdTensor()

const torch::Tensor & Moose::LibtorchGaussianActionDistribution::stdTensor ( ) const
inline

Return the cached Gaussian standard-deviation tensor.

Definition at line 227 of file LibtorchActionDistribution.h.

227{ return _std_tensor; }

◆ synchronizeScalingFactorsFromBuffer()

void Moose::LibtorchActionDistribution::synchronizeScalingFactorsFromBuffer ( )
inherited

Sync cached scaling metadata from the registered buffers after loading state.

Definition at line 73 of file LibtorchActionDistribution.C.

74{
75 auto action_scale =
76 _action_scale_tensor.detach().reshape({-1}).to(torch::kCPU).to(torch::kDouble);
78}
std::vector< Real > _output_scaling_factors
Cached per-action scaling factors applied in physical units.
void tensorToVector(torch::Tensor &tensor, std::vector< DataType > &vector)

Referenced by Moose::loadLibtorchActorNeuralNetState().

Member Data Documentation

◆ _action_scale_tensor

torch::Tensor Moose::LibtorchActionDistribution::_action_scale_tensor
protectedinherited

◆ _data_type

const torch::ScalarType Moose::LibtorchActionDistribution::_data_type
protectedinherited

◆ _device_type

const torch::DeviceType Moose::LibtorchActionDistribution::_device_type
protectedinherited

◆ _log_std_parameter

torch::Tensor Moose::LibtorchGaussianActionDistribution::_log_std_parameter
private

Trainable state-independent Gaussian log-std.

Definition at line 242 of file LibtorchActionDistribution.h.

Referenced by constructDistribution(), initialize(), logStd(), logStd(), and reset().

◆ _log_std_tensor

torch::Tensor Moose::LibtorchGaussianActionDistribution::_log_std_tensor
private

Cached Gaussian action log standard deviation from the latest reset.

Definition at line 248 of file LibtorchActionDistribution.h.

Referenced by entropy(), logProbability(), and reset().

◆ _mean

torch::Tensor Moose::LibtorchGaussianActionDistribution::_mean
private

Cached Gaussian action mean from the latest reset.

Definition at line 244 of file LibtorchActionDistribution.h.

Referenced by deterministicAction(), logProbability(), reset(), and sample().

◆ _mean_module

torch::nn::Linear Moose::LibtorchGaussianActionDistribution::_mean_module {nullptr}
private

Linear head that produces the Gaussian action mean.

Definition at line 238 of file LibtorchActionDistribution.h.

238{nullptr};

Referenced by constructDistribution(), initialize(), meanModule(), meanModule(), and reset().

◆ _name

const std::string Moose::LibtorchActionDistribution::_name
protectedinherited

Module name used for registration and serialization.

Definition at line 107 of file LibtorchActionDistribution.h.

◆ _num_inputs

const unsigned int Moose::LibtorchActionDistribution::_num_inputs
protectedinherited

Number of actor features feeding this distribution.

Definition at line 109 of file LibtorchActionDistribution.h.

Referenced by constructDistribution(), and Moose::LibtorchBetaActionDistribution::constructDistribution().

◆ _num_outputs

const unsigned int Moose::LibtorchActionDistribution::_num_outputs
protectedinherited

◆ _output_scaling_factors

std::vector<Real> Moose::LibtorchActionDistribution::_output_scaling_factors
protectedinherited

Cached per-action scaling factors applied in physical units.

Definition at line 117 of file LibtorchActionDistribution.h.

Referenced by Moose::LibtorchActionDistribution::LibtorchActionDistribution(), and Moose::LibtorchActionDistribution::synchronizeScalingFactorsFromBuffer().

◆ _state_independent_std

const bool Moose::LibtorchGaussianActionDistribution::_state_independent_std
private

Whether the Gaussian std ignores the current actor features.

Definition at line 236 of file LibtorchActionDistribution.h.

Referenced by constructDistribution(), initialize(), reset(), and stateIndependentStd().

◆ _std_module

torch::nn::Linear Moose::LibtorchGaussianActionDistribution::_std_module {nullptr}
private

Linear head that produces state-dependent Gaussian log-std inputs.

Definition at line 240 of file LibtorchActionDistribution.h.

240{nullptr};

Referenced by constructDistribution(), initialize(), reset(), stdModule(), and stdModule().

◆ _std_tensor

torch::Tensor Moose::LibtorchGaussianActionDistribution::_std_tensor
private

Cached Gaussian action standard deviation from the latest reset.

Definition at line 246 of file LibtorchActionDistribution.h.

Referenced by logProbability(), reset(), sample(), and stdTensor().


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