https://mooseframework.inl.gov
Public Member Functions | Protected Attributes | List of all members
StochasticTools::Standardizer Class Reference

Class for standardizing data (centering and scaling) More...

#include <Standardizer.h>

Public Member Functions

 Standardizer ()=default
 
void set (const Real &n)
 Methods for setting mean and standard deviation directly Sets mean=0, std=1 for n variables. More...
 
void set (const Real &mean, const Real &stdev)
 Sets mean and std for a single variable. More...
 
void set (const Real &mean, const Real &stdev, const Real &n)
 Sets mean and std for a n variables variable. More...
 
void set (const std::vector< Real > &mean, const std::vector< Real > &stdev)
 Sets mean and std directly using provided vectors. More...
 
const torch::TensorgetMean () const
 Get the mean vector. More...
 
const torch::TensorgetStdDev () const
 Get the standard deviation vector. More...
 
void computeSet (const torch::Tensor &input)
 Methods for computing and setting mean and standard deviation. More...
 
void storeHelper (std::ostream &stream, void *context) const
 Helper for dataStore. More...
 
void getStandardized (torch::Tensor &input) const
 Returns the standardized (centered and scaled) of the provided input. More...
 
void getDestandardized (torch::Tensor &input) const
 De-standardizes (de-centered and de-scaled) the assumed standardized input. More...
 
void getDescaled (torch::Tensor &input) const
 De-scales the assumed scaled input. More...
 
void getScaled (torch::Tensor &input) const
 Scales the assumed de-scaled input. More...
 

Protected Attributes

torch::Tensor _mean
 
torch::Tensor _stdev
 

Detailed Description

Class for standardizing data (centering and scaling)

Definition at line 24 of file Standardizer.h.

Constructor & Destructor Documentation

◆ Standardizer()

StochasticTools::Standardizer::Standardizer ( )
default

Member Function Documentation

◆ computeSet()

void StochasticTools::Standardizer::computeSet ( const torch::Tensor input)

Methods for computing and setting mean and standard deviation.

Definition at line 79 of file Standardizer.C.

Referenced by StochasticTools::GaussianProcess::standardizeData(), StochasticTools::GaussianProcess::standardizeParameters(), and TEST().

80 {
81  if (input.dim() != 2)
82  mooseError("Standardizer input must be a rank-2 tensor.");
83  // Compute mean and standard deviation
84  _mean = torch::mean(input, 0, false);
85  _stdev = torch::std(input, 0, 0, false);
86 }
void mooseError(Args &&... args)

◆ getDescaled()

void StochasticTools::Standardizer::getDescaled ( torch::Tensor input) const

De-scales the assumed scaled input.

Definition at line 103 of file Standardizer.C.

Referenced by GaussianProcessSurrogate::evaluate().

104 {
105  checkInputCompatibility(input, _stdev);
106  input.mul_(asFeatureVector(_stdev, input));
107 }

◆ getDestandardized()

void StochasticTools::Standardizer::getDestandardized ( torch::Tensor input) const

De-standardizes (de-centered and de-scaled) the assumed standardized input.

Definition at line 96 of file Standardizer.C.

Referenced by GaussianProcessSurrogate::evaluate().

97 {
98  checkInputCompatibility(input, _mean);
99  input.mul_(asFeatureVector(_stdev, input)).add_(asFeatureVector(_mean, input));
100 }

◆ getMean()

const torch::Tensor& StochasticTools::Standardizer::getMean ( ) const
inline

Get the mean vector.

Definition at line 40 of file Standardizer.h.

Referenced by LibtorchANNSurrogate::evaluate(), and TEST().

40 { return _mean; }

◆ getScaled()

void StochasticTools::Standardizer::getScaled ( torch::Tensor input) const

Scales the assumed de-scaled input.

Definition at line 110 of file Standardizer.C.

111 {
112  checkInputCompatibility(input, _stdev);
113  input.div_(asFeatureVector(_stdev, input));
114 }

◆ getStandardized()

void StochasticTools::Standardizer::getStandardized ( torch::Tensor input) const

Returns the standardized (centered and scaled) of the provided input.

Definition at line 89 of file Standardizer.C.

Referenced by GaussianProcessSurrogate::evaluate(), StochasticTools::GaussianProcess::standardizeData(), and StochasticTools::GaussianProcess::standardizeParameters().

90 {
91  checkInputCompatibility(input, _mean);
92  input.sub_(asFeatureVector(_mean, input)).div_(asFeatureVector(_stdev, input));
93 }

◆ getStdDev()

const torch::Tensor& StochasticTools::Standardizer::getStdDev ( ) const
inline

Get the standard deviation vector.

Definition at line 42 of file Standardizer.h.

Referenced by LibtorchANNSurrogate::evaluate(), and TEST().

42 { return _stdev; }

◆ set() [1/4]

void StochasticTools::Standardizer::set ( const Real n)

Methods for setting mean and standard deviation directly Sets mean=0, std=1 for n variables.

Definition at line 48 of file Standardizer.C.

Referenced by dataLoad(), GaussianProcessTrainer::postTrain(), LibtorchANNTrainer::postTrain(), and ActiveLearningGaussianProcess::reTrain().

49 {
50  _mean = torch::zeros({long(n)}, at::kDouble);
51  _stdev = torch::ones({long(n)}, at::kDouble);
52 }

◆ set() [2/4]

void StochasticTools::Standardizer::set ( const Real mean,
const Real stdev 
)

Sets mean and std for a single variable.

Definition at line 55 of file Standardizer.C.

56 {
57  _mean = torch::full({1}, mean, at::kDouble);
58  _stdev = torch::full({1}, stdev, at::kDouble);
59 }

◆ set() [3/4]

void StochasticTools::Standardizer::set ( const Real mean,
const Real stdev,
const Real n 
)

Sets mean and std for a n variables variable.

Definition at line 62 of file Standardizer.C.

63 {
64  auto options = torch::TensorOptions().dtype(at::kDouble);
65  _mean = torch::full({long(n)}, mean, options);
66  _stdev = torch::full({long(n)}, stdev, options);
67 }

◆ set() [4/4]

void StochasticTools::Standardizer::set ( const std::vector< Real > &  mean,
const std::vector< Real > &  stdev 
)

Sets mean and std directly using provided vectors.

Definition at line 70 of file Standardizer.C.

71 {
72  mooseAssert(mean.size() == stdev.size(),
73  "Provided mean and standard deviation vectors are of differing size.");
74  _mean = LibtorchUtils::vectorToTensorCopy(mean, {long(mean.size())});
75  _stdev = LibtorchUtils::vectorToTensorCopy(stdev, {long(stdev.size())});
76 }
torch::Tensor vectorToTensorCopy(const std::vector< DataType > &vector, c10::IntArrayRef sizes)

◆ storeHelper()

void StochasticTools::Standardizer::storeHelper ( std::ostream &  stream,
void context 
) const

Helper for dataStore.

Definition at line 118 of file Standardizer.C.

Referenced by dataStore().

119 {
120  const auto mean = LibtorchUtils::toCPUContiguous(_mean);
121  const auto stdev = LibtorchUtils::toCPUContiguous(_stdev);
122  auto mean_accessor = mean.accessor<Real, 1>();
123  auto stdev_accessor = stdev.accessor<Real, 1>();
124  unsigned int n = mean.size(0);
125  dataStore(stream, n, context);
126  for (unsigned int ii = 0; ii < n; ++ii)
127  dataStore(stream, mean_accessor[ii], context);
128  for (unsigned int ii = 0; ii < n; ++ii)
129  dataStore(stream, stdev_accessor[ii], context);
130 }
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void dataStore(std::ostream &stream, StochasticTools::Standardizer &standardizer, void *context)
Definition: Standardizer.C:136

Member Data Documentation

◆ _mean

torch::Tensor StochasticTools::Standardizer::_mean
protected

Definition at line 63 of file Standardizer.h.

Referenced by computeSet(), getDestandardized(), getMean(), getStandardized(), set(), and storeHelper().

◆ _stdev

torch::Tensor StochasticTools::Standardizer::_stdev
protected

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