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

Samples shuffled mini-batches from a flattened on-policy trajectory batch. More...

#include <LibtorchRLMiniBatchSampler.h>

Public Member Functions

std::vector< LibtorchRLMiniBatch > sample (const LibtorchRLTrajectoryBuffer::TensorBatch &batch, unsigned int batch_size, bool standardize_advantage, c10::optional< at::Generator > generator=c10::nullopt) const
 Shuffle a flattened rollout batch into PPO-sized chunks.
 

Static Private Member Functions

static void validateBatch (const LibtorchRLTrajectoryBuffer::TensorBatch &batch)
 Sanity-check that the flattened rollout tensors all line up.
 
static LibtorchRLMiniBatch makeMiniBatch (const LibtorchRLTrajectoryBuffer::TensorBatch &batch, const torch::Tensor &indices, bool standardize_advantage)
 Slice one shuffled mini-batch out of the flattened rollout tensors.
 

Detailed Description

Samples shuffled mini-batches from a flattened on-policy trajectory batch.

Definition at line 46 of file LibtorchRLMiniBatchSampler.h.

Member Function Documentation

◆ makeMiniBatch()

LibtorchRLMiniBatch LibtorchRLMiniBatchSampler::makeMiniBatch ( const LibtorchRLTrajectoryBuffer::TensorBatch &  batch,
const torch::Tensor &  indices,
bool  standardize_advantage 
)
staticprivate

Slice one shuffled mini-batch out of the flattened rollout tensors.

Parameters
batchFlattened rollout tensors.
indicesRow indices assigned to this mini-batch.
standardize_advantageWhether to normalize the advantages in this slice.

Definition at line 71 of file LibtorchRLMiniBatchSampler.C.

74{
75 LibtorchRLMiniBatch mini_batch;
76
77 mini_batch.observations = batch.observations.index({indices});
78 mini_batch.actions = batch.actions.index({indices});
79 mini_batch.old_log_probabilities = batch.log_probabilities.index({indices});
80 mini_batch.value_targets = batch.value_targets.index({indices});
81 mini_batch.advantages = batch.advantages.index({indices});
82
83 if (standardize_advantage)
84 {
85 const auto std = mini_batch.advantages.std(false);
86 mini_batch.advantages = (mini_batch.advantages - mini_batch.advantages.mean()) / (std + 1e-10);
87 }
88
89 return mini_batch;
90}
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.
torch::Tensor actions
Flattened action matrix.
torch::Tensor value_targets
Flattened critic targets.
torch::Tensor observations
Flattened observation matrix.
torch::Tensor advantages
Flattened advantages.
torch::Tensor log_probabilities
Flattened action log-probabilities.

Referenced by sample().

◆ sample()

std::vector< LibtorchRLMiniBatch > LibtorchRLMiniBatchSampler::sample ( const LibtorchRLTrajectoryBuffer::TensorBatch &  batch,
unsigned int  batch_size,
bool  standardize_advantage,
c10::optional< at::Generator >  generator = c10::nullopt 
) const

Shuffle a flattened rollout batch into PPO-sized chunks.

Parameters
batchFlattened rollout tensors ready for PPO updates.
batch_sizePreferred number of rows per mini-batch.
standardize_advantageWhether to normalize the advantages inside each mini-batch.
generatorOptional random-number generator used for the row permutation.

Definition at line 20 of file LibtorchRLMiniBatchSampler.C.

24{
25 std::vector<LibtorchRLMiniBatch> mini_batches;
26
27 if (!batch.size())
28 return mini_batches;
29
30 validateBatch(batch);
31
32 const auto effective_batch_size = std::max<unsigned int>(1, batch_size);
33 auto permutation =
34 at::randperm(batch.size(), generator, torch::TensorOptions().dtype(torch::kLong));
35
36 for (std::int64_t batch_begin = 0; batch_begin < batch.size();
37 batch_begin += effective_batch_size)
38 {
39 const auto batch_end = std::min<std::int64_t>(
40 batch.size(), batch_begin + static_cast<std::int64_t>(effective_batch_size));
41 const auto indices = permutation.narrow(0, batch_begin, batch_end - batch_begin);
42 mini_batches.push_back(makeMiniBatch(batch, indices, standardize_advantage));
43 }
44
45 return mini_batches;
46}
static void validateBatch(const LibtorchRLTrajectoryBuffer::TensorBatch &batch)
Sanity-check that the flattened rollout tensors all line up.
static LibtorchRLMiniBatch makeMiniBatch(const LibtorchRLTrajectoryBuffer::TensorBatch &batch, const torch::Tensor &indices, bool standardize_advantage)
Slice one shuffled mini-batch out of the flattened rollout tensors.
std::int64_t size() const
Return the number of transitions represented by the batch.

Referenced by LibtorchDRLControlTrainer::trainController().

◆ validateBatch()

void LibtorchRLMiniBatchSampler::validateBatch ( const LibtorchRLTrajectoryBuffer::TensorBatch &  batch)
staticprivate

Sanity-check that the flattened rollout tensors all line up.

Parameters
batchFlattened rollout tensors to validate.

Definition at line 49 of file LibtorchRLMiniBatchSampler.C.

50{
51 if (!batch.actions.defined() || !batch.log_probabilities.defined() ||
52 !batch.value_targets.defined() || !batch.advantages.defined())
53 mooseError("RL tensor batches must define observations, actions, log probabilities, value "
54 "targets, and advantages before mini-batch sampling.");
55
56 const auto batch_size = batch.size();
57 const auto validate_rows = [batch_size](const torch::Tensor & tensor, std::string_view name)
58 {
59 if (!tensor.defined() || tensor.size(0) != batch_size)
61 "RL tensor batch field ", name, " must have the same number of rows as observations.");
62 };
63
64 validate_rows(batch.actions, "actions");
65 validate_rows(batch.log_probabilities, "log_probabilities");
66 validate_rows(batch.value_targets, "value_targets");
67 validate_rows(batch.advantages, "advantages");
68}
void mooseError(Args &&... args)
const std::string name
Definition Setup.h:21

Referenced by sample().


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