https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SquaredExponentialCovariance.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9#ifdef MOOSE_LIBTORCH_ENABLED
10
12#include <cmath>
13
15
18{
20 params.addClassDescription("Squared Exponential covariance function.");
21 params.addRequiredParam<std::vector<Real>>("length_factor",
22 "Length factors to use for Covariance Kernel");
23 params.addRequiredParam<Real>("signal_variance",
24 "Signal Variance ($\\sigma_f^2$) to use for kernel calculation.");
25 params.addParam<Real>(
26 "noise_variance", 0.0, "Noise Variance ($\\sigma_n^2$) to use for kernel calculation.");
27 return params;
28}
29
31 : CovarianceFunctionBase(parameters),
32 _length_factor(addVectorRealHyperParameter(
33 "length_factor", getParam<std::vector<Real>>("length_factor"), true)),
34 _sigma_f_squared(
35 addRealHyperParameter("signal_variance", getParam<Real>("signal_variance"), true)),
36 _sigma_n_squared(
37 addRealHyperParameter("noise_variance", getParam<Real>("noise_variance"), true))
38{
39}
40
41void
43 const torch::Tensor & x,
44 const torch::Tensor & xp,
45 const bool is_self_covariance) const
46{
47 if ((unsigned)x.sizes()[1] != _length_factor.numel())
48 mooseError("length_factor size does not match dimension of trainer input.");
49
51 K, x, xp, _length_factor, _sigma_f_squared, _sigma_n_squared, is_self_covariance);
52}
53
54void
56 const torch::Tensor & x,
57 const torch::Tensor & xp,
58 const torch::Tensor & length_factor,
59 const torch::Tensor & sigma_f_squared,
60 const torch::Tensor & sigma_n_squared,
61 const bool is_self_covariance)
62{
63 mooseAssert(x.sizes()[1] == xp.sizes()[1],
64 "Number of parameters do not match in covariance kernel calculation");
65
66 const auto l_factor = length_factor.unsqueeze(0);
67 K = torch::cdist(torch::div(x, l_factor), torch::div(xp, l_factor), 2.0);
68 K.pow_(2).mul_(-0.5).exp_().mul_(sigma_f_squared);
69 if (is_self_covariance)
70 K.diagonal().add_(sigma_n_squared);
71}
72
73bool
75 const torch::Tensor & x,
76 const std::string & hyper_param_name,
77 unsigned int ind) const
78{
79 if (name().length() + 1 > hyper_param_name.length())
80 return false;
81
82 const std::string name_without_prefix = hyper_param_name.substr(name().length() + 1);
83
84 if (name_without_prefix == "noise_variance")
85 {
86 const auto options = x.options().dtype(at::kDouble);
88 x,
89 x,
91 torch::tensor(0.0, options),
92 torch::tensor(1.0, options),
93 true);
94 return true;
95 }
96
97 if (name_without_prefix == "signal_variance")
98 {
99 const auto options = x.options().dtype(at::kDouble);
101 x,
102 x,
104 torch::tensor(1.0, options),
105 torch::tensor(0.0, options),
106 false);
107 return true;
108 }
109
110 if (name_without_prefix == "length_factor")
111 {
113 return true;
114 }
115
116 return false;
117}
118
119void
121 const torch::Tensor & x,
122 const torch::Tensor & length_factor,
123 const torch::Tensor & sigma_f_squared,
124 const int ind)
125{
126 mooseAssert(ind < x.sizes()[1], "Incorrect length factor index");
127
128 const auto l_factor = length_factor.unsqueeze(0);
129 K = torch::cdist(torch::div(x, l_factor), torch::div(x, l_factor), 2.0);
130 K.pow_(2).mul_(-0.5).exp_().mul_(sigma_f_squared);
131 const auto coordinate = x.select(1, ind);
132 const auto coordinate_distance_squared =
133 torch::pow(coordinate.unsqueeze(1) - coordinate.unsqueeze(0), 2);
134 const auto length_factor_ind = length_factor.select(0, ind);
135
136 K.mul_(coordinate_distance_squared).div_(torch::pow(length_factor_ind, 3));
137}
138
139#endif
const std::vector< double > x
registerMooseObject("StochasticToolsApp", SquaredExponentialCovariance)
Base class for covariance functions that are used in Gaussian Processes.
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
const std::string & name() const
void mooseError(Args &&... args) const
bool computedKdhyper(torch::Tensor &dKdhp, const torch::Tensor &x, const std::string &hyper_param_name, unsigned int ind) const override
Redirect dK/dhp for hyperparameter "hp".
const torch::Tensor & _sigma_f_squared
signal variance (\sigma_f^2)
const torch::Tensor & _length_factor
lengh factor (\ell) for the kernel, in vector form for multiple parameters
const torch::Tensor & _sigma_n_squared
noise variance (\sigma_n^2)
static void SquaredExponentialFunction(torch::Tensor &K, const torch::Tensor &x, const torch::Tensor &xp, const torch::Tensor &length_factor, const torch::Tensor &sigma_f_squared, const torch::Tensor &sigma_n_squared, const bool is_self_covariance)
static void computedKdlf(torch::Tensor &K, const torch::Tensor &x, const torch::Tensor &length_factor, const torch::Tensor &sigma_f_squared, const int ind)
Computes dK/dlf for individual length factors.
void computeCovarianceMatrix(torch::Tensor &K, const torch::Tensor &x, const torch::Tensor &xp, const bool is_self_covariance) const override
Generates the Covariance Matrix given two points in the parameter space.
SquaredExponentialCovariance(const InputParameters &parameters)