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 dataStore (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 80 of file Standardizer.C.

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

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

◆ dataStore()

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

Helper for dataStore.

Definition at line 119 of file Standardizer.C.

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

Referenced by dataStore(), and dataStore().

◆ getDescaled()

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

De-scales the assumed scaled input.

Definition at line 104 of file Standardizer.C.

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

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 97 of file Standardizer.C.

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

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 111 of file Standardizer.C.

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

◆ getStandardized()

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

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

Definition at line 90 of file Standardizer.C.

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

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 56 of file Standardizer.C.

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

◆ 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 63 of file Standardizer.C.

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

◆ 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 49 of file Standardizer.C.

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

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 71 of file Standardizer.C.

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

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: