10#ifdef MOOSE_LIBTORCH_ENABLED
14#include <torch/torch.h>
40 unsigned int num_inputs,
41 unsigned int num_outputs,
42 torch::DeviceType device_type = torch::kCPU,
43 torch::ScalarType scalar_type = torch::kDouble,
44 const std::vector<Real> & output_scaling_factors = {});
50 virtual void initialize(c10::optional<at::Generator> generator = c10::nullopt) = 0;
56 virtual void reset(
const torch::Tensor & input) = 0;
62 virtual torch::Tensor
sample(c10::optional<at::Generator> generator = c10::nullopt)
const = 0;
73 virtual torch::Tensor
logProbability(
const torch::Tensor & action)
const = 0;
78 virtual torch::Tensor
entropy()
const = 0;
100 torch::Tensor
prepareAction(
const torch::Tensor & action)
const;
141 unsigned int num_inputs,
142 unsigned int num_outputs,
143 torch::DeviceType device_type = torch::kCPU,
144 torch::ScalarType scalar_type = torch::kDouble,
145 bool build_on_construct =
true,
146 const std::vector<Real> & output_scaling_factors = {},
147 bool state_independent_std =
true);
153 virtual void initialize(c10::optional<at::Generator> generator = c10::nullopt)
override;
159 virtual void reset(
const torch::Tensor & input)
override;
165 virtual torch::Tensor
166 sample(c10::optional<at::Generator> generator = c10::nullopt)
const override;
177 virtual torch::Tensor
logProbability(
const torch::Tensor & action)
const override;
182 virtual torch::Tensor
entropy()
const override;
187 virtual bool isBounded()
const override {
return false; }
270 unsigned int num_inputs,
271 unsigned int num_outputs,
272 const std::vector<Real> & minimum_values,
273 const std::vector<Real> & maximum_values,
274 torch::DeviceType device_type = torch::kCPU,
275 torch::ScalarType scalar_type = torch::kDouble,
276 bool build_on_construct =
true,
277 const std::vector<Real> & output_scaling_factors = {});
283 virtual void initialize(c10::optional<at::Generator> generator = c10::nullopt)
override;
289 virtual void reset(
const torch::Tensor & input)
override;
295 virtual torch::Tensor
296 sample(c10::optional<at::Generator> generator = c10::nullopt)
const override;
307 virtual torch::Tensor
logProbability(
const torch::Tensor & action)
const override;
312 virtual torch::Tensor
entropy()
const override;
317 virtual bool isBounded()
const override {
return true; }
Reusable continuous-action distribution interface for actor policies.
const torch::DeviceType _device_type
Torch device used by this distribution.
const std::string _name
Module name used for registration and serialization.
torch::Tensor _action_scale_tensor
Registered libtorch buffer holding the per-action scaling factors.
torch::Tensor prepareAction(const torch::Tensor &action) const
Convert actions to the configured device and scalar type.
virtual torch::Tensor sample(c10::optional< at::Generator > generator=c10::nullopt) const =0
Draw a stochastic action sample in physical units.
void synchronizeScalingFactorsFromBuffer()
Sync cached scaling metadata from the registered buffers after loading state.
virtual torch::Tensor logProbability(const torch::Tensor &action) const =0
Evaluate the log-probability of an action under the current distribution.
std::vector< Real > _output_scaling_factors
Cached per-action scaling factors applied in physical units.
torch::Tensor prepareFeatures(const torch::Tensor &input) const
Convert actor features to the configured device and scalar type.
const torch::Tensor & actionScaleTensor() const
Return the registered tensor that stores per-action scaling factors.
virtual torch::Tensor deterministicAction() const =0
Return the deterministic action used for evaluation.
virtual void initialize(c10::optional< at::Generator > generator=c10::nullopt)=0
Initialize the trainable distribution parameters.
const unsigned int _num_outputs
Number of action dimensions produced by this distribution.
virtual void reset(const torch::Tensor &input)=0
Refresh cached distribution parameters from the latest actor features.
virtual torch::Tensor entropy() const =0
Compute the entropy of the current 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.
virtual bool isBounded() const =0
Tell callers whether the distribution enforces explicit action bounds.
Beta action distribution for bounded action spaces.
virtual void reset(const torch::Tensor &input) override
Refresh the Beta distribution parameters from the latest actor features.
torch::Tensor _min_tensor
Tensor form of the lower action bounds.
torch::nn::Linear _beta_module
Linear head that produces the Beta beta parameters.
torch::nn::Linear _alpha_module
Linear head that produces the Beta alpha parameters.
const torch::nn::Linear & alphaModule() const
Return the Beta alpha head.
const std::vector< Real > _maximum_values
Upper action bounds in physical units.
torch::Tensor _max_tensor
Tensor form of the upper action bounds.
virtual void initialize(c10::optional< at::Generator > generator=c10::nullopt) override
Initialize the trainable Beta distribution parameters.
const std::vector< Real > _minimum_values
Lower action bounds in physical units.
torch::Tensor _alpha_tensor
Cached Beta alpha parameters from the latest reset.
torch::nn::Linear & alphaModule()
Return the Beta alpha head.
const torch::nn::Linear & betaModule() const
Return the Beta beta head.
torch::Tensor _mean
Cached normalized Beta mean from the latest reset.
torch::Tensor _log_norm
Cached log normalization factor for the latest Beta distribution state.
torch::Tensor _beta_tensor
Cached Beta beta parameters from the latest reset.
virtual bool isBounded() const override
Return true because the Beta distribution enforces action bounds.
const torch::Tensor & alphaTensor() const
Return the cached Beta alpha tensor.
torch::Tensor _alpha_beta_tensor
Cached sum of the alpha and beta parameters.
torch::nn::Linear & betaModule()
Return the Beta beta head.
void constructDistribution()
Build and register the Beta distribution heads.
const torch::Tensor & betaTensor() const
Return the cached Beta beta tensor.
virtual torch::Tensor deterministicAction() const override
Return the Beta mean action used for deterministic evaluation.
virtual torch::Tensor entropy() const override
Compute the entropy of the current Beta distribution.
virtual torch::Tensor sample(c10::optional< at::Generator > generator=c10::nullopt) const override
Draw a stochastic Beta action sample in physical units.
virtual torch::Tensor logProbability(const torch::Tensor &action) const override
Evaluate the log-probability of an action under the current Beta distribution.
Gaussian action distribution for unbounded action spaces.
void constructDistribution()
Build and register the Gaussian distribution heads.
torch::Tensor _std_tensor
Cached Gaussian action standard deviation from the latest reset.
torch::nn::Linear _std_module
Linear head that produces state-dependent Gaussian log-std inputs.
torch::nn::Linear & meanModule()
Return the Gaussian mean head.
const torch::nn::Linear & stdModule() const
Return the state-dependent Gaussian standard-deviation head.
torch::nn::Linear & stdModule()
Return the state-dependent Gaussian standard-deviation head.
torch::Tensor _mean
Cached Gaussian action mean from the latest reset.
bool stateIndependentStd() const
Return whether the Gaussian standard deviation ignores the current actor features.
virtual torch::Tensor entropy() const override
Compute the entropy of the current Gaussian distribution.
torch::nn::Linear _mean_module
Linear head that produces the Gaussian action mean.
virtual torch::Tensor deterministicAction() const override
Return the Gaussian mean action used for deterministic evaluation.
virtual torch::Tensor sample(c10::optional< at::Generator > generator=c10::nullopt) const override
Draw a stochastic Gaussian action sample in physical units.
const torch::Tensor & logStd() const
Return the state-independent Gaussian log-standard-deviation parameter.
torch::Tensor _log_std_tensor
Cached Gaussian action log standard deviation from the latest reset.
torch::Tensor _log_std_parameter
Trainable state-independent Gaussian log-std.
virtual void initialize(c10::optional< at::Generator > generator=c10::nullopt) override
Initialize the trainable Gaussian distribution parameters.
const torch::Tensor & stdTensor() const
Return the cached Gaussian standard-deviation tensor.
virtual torch::Tensor logProbability(const torch::Tensor &action) const override
Evaluate the log-probability of an action under the current Gaussian distribution.
virtual void reset(const torch::Tensor &input) override
Refresh the Gaussian distribution parameters from the latest actor features.
const torch::nn::Linear & meanModule() const
Return the Gaussian mean head.
virtual bool isBounded() const override
Return false because the Gaussian distribution is unbounded.
const bool _state_independent_std
Whether the Gaussian std ignores the current actor features.
torch::Tensor & logStd()
Return the state-independent Gaussian log-standard-deviation parameter.