https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
void set (const Real &mean, const Real &stdev)
 Sets mean and std for a single variable.
 
void set (const Real &mean, const Real &stdev, const Real &n)
 Sets mean and std for a n variables variable.
 
void set (const std::vector< Real > &mean, const std::vector< Real > &stdev)
 Sets mean and std directly using provided vectors.
 
const torch::Tensor & getMean () const
 Get the mean vector.
 
const torch::Tensor & getStdDev () const
 Get the standard deviation vector.
 
void computeSet (const torch::Tensor &input)
 Methods for computing and setting mean and standard deviation.
 
void storeHelper (std::ostream &stream, void *context) const
 Helper for dataStore.
 
void getStandardized (torch::Tensor &input) const
 Returns the standardized (centered and scaled) of the provided input.
 
void getDestandardized (torch::Tensor &input) const
 De-standardizes (de-centered and de-scaled) the assumed standardized input.
 
void getDescaled (torch::Tensor &input) const
 De-scales the assumed scaled input.
 
void getScaled (torch::Tensor &input) const
 Scales the assumed de-scaled input.
 

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.

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)

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

◆ getDescaled()

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

De-scales the assumed scaled input.

Definition at line 103 of file Standardizer.C.

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

Referenced by GaussianProcessSurrogate::evaluate().

◆ 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.

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

Referenced by GaussianProcessSurrogate::evaluate().

◆ getMean()

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

Get the mean vector.

Definition at line 40 of file Standardizer.h.

40{ return _mean; }

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

◆ 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.

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

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

◆ getStdDev()

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

Get the standard deviation vector.

Definition at line 42 of file Standardizer.h.

42{ return _stdev; }

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

◆ set() [1/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() [2/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() [3/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.

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

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

◆ 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.

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}
void dataStore(std::ostream &stream, LineSegment &l, void *context)
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

Referenced by dataStore().

Member Data Documentation

◆ _mean

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

◆ _stdev

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

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