9 #ifdef MOOSE_LIBTORCH_ENABLED 21 params.
addClassDescription(
"Covariance function for multioutput Gaussian Processes based on the " 22 "Linear Model of Coregionalization (LMC).");
24 "num_latent_funcs", 1.,
"The number of latent functions for the expansion of the outputs.");
32 _num_expansion_terms(getParam<unsigned
int>(
"num_latent_funcs"))
37 generator_latent.
seed(0, 1980);
42 const std::string a_coeff_name =
"acoeff_" + std::to_string(exp_i);
45 acoeff_values[out_i] = 3.0 * generator_latent.
rand(0) + 1.0;
53 const std::string lambda_name =
"lambda_" + std::to_string(exp_i);
56 lambda_values[out_i] = 3.0 * generator_latent.
rand(0) + 1.0;
64 const torch::Tensor &
x,
65 const torch::Tensor & xp,
66 const bool is_self_covariance)
const 68 const auto options =
x.options().dtype(at::kDouble);
70 torch::Tensor K_params = torch::zeros({
x.sizes()[0], xp.sizes()[0]}, options);
73 torch::Tensor K_working;
80 K_working = torch::kron(
B, K_params);
87 const torch::Tensor &
x,
88 const std::string & hyper_param_name,
89 unsigned int ind)
const 93 if (
name().length() + 1 > hyper_param_name.length())
97 const std::string name_without_prefix = hyper_param_name.substr(
name().length() + 1);
102 const std::string acoeff_prefix =
"acoeff_";
103 const std::string lambda_prefix =
"lambda_";
106 const auto options =
x.options().dtype(at::kDouble);
108 torch::Tensor K_params = torch::zeros({
x.sizes()[0],
x.sizes()[0]}, options);
110 if (name_without_prefix.find(acoeff_prefix) != std::string::npos)
113 const int number = std::stoi(name_without_prefix.substr(acoeff_prefix.length()));
117 else if (name_without_prefix.find(lambda_prefix) != std::string::npos)
120 const int number = std::stoi(name_without_prefix.substr(lambda_prefix.length()));
124 dKdhp = torch::kron(dBdhp, K_params);
130 const auto options =
x.options().dtype(at::kDouble);
133 torch::Tensor dKdhp_sub = torch::zeros({
x.sizes()[0],
x.sizes()[0]}, options);
139 found = dependent_covar->computedKdhyper(dKdhp_sub,
x, hyper_param_name, ind);
142 mooseError(
"Hyperparameter ", hyper_param_name,
"not found!");
151 dKdhp = torch::kron(
B, dKdhp_sub);
162 const auto & a_coeffs = *
_a_coeffs[exp_i];
163 const auto & lambda_coeffs = *
_lambdas[exp_i];
164 Bmat = torch::outer(a_coeffs, a_coeffs) + torch::diag(lambda_coeffs);
169 const unsigned int exp_i,
170 const unsigned int index)
const 172 const auto & a_coeffs = *
_a_coeffs[exp_i];
173 mooseAssert(cast_int<int64_t>(index) < a_coeffs.numel(),
"Incorrect LMC coefficient index.");
174 auto basis = torch::zeros_like(a_coeffs);
175 const auto index_tensor =
176 torch::tensor({cast_int<int64_t>(index)},
177 torch::TensorOptions().dtype(torch::kLong).device(a_coeffs.device()));
178 basis.index_fill_(0, index_tensor, 1.0);
179 grad = torch::outer(basis, a_coeffs) + torch::outer(a_coeffs, basis);
184 const unsigned int exp_i,
185 const unsigned int index)
const 187 mooseAssert(index <
_num_outputs,
"Incorrect LMC lambda index.");
188 auto basis = torch::zeros_like(*
_lambdas[exp_i]);
189 const auto index_tensor =
190 torch::tensor({cast_int<int64_t>(index)},
191 torch::TensorOptions().dtype(torch::kLong).device(
_lambdas[exp_i]->device()));
192 basis.index_fill_(0, index_tensor, 1.0);
193 grad = torch::diag(basis);
std::unordered_set< std::string > _tunable_hp
list of tunable hyper-parameters
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 unsigned int _num_expansion_terms
The number of expansion terms in the output ovariance matrix.
static const std::string K
void seed(std::size_t i, unsigned int seed)
LMC(const InputParameters ¶meters)
std::vector< CovarianceFunctionBase * > _covariance_functions
Vector of pointers to the dependent covariance functions.
Base class for covariance functions that are used in Gaussian Processes.
registerMooseObject("StochasticToolsApp", LMC)
static InputParameters validParams()
static InputParameters validParams()
std::vector< const torch::Tensor * > _lambdas
const std::string & name() const
const unsigned int _num_outputs
The number of outputs this covariance function is used to describe.
const std::vector< double > x
std::string grad(const std::string &var)
void computeAGradient(torch::Tensor &grad, const unsigned int exp_i, const unsigned int index) const
Computes the gradient of $B$ with respect to the entries in $a_i$ in the following expression: $B = ...
void computeBMatrix(torch::Tensor &Bmat, const unsigned int exp_i) const
Computes the covariance matrix for the outputs (using the latent coefficients) We use a $B = a_i a_i...
void computeLambdaGradient(torch::Tensor &grad, const unsigned int exp_i, const unsigned int index) const
Computes the gradient of $B$ with respect to the entries in $lambda_i$ in the following expression: $...
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 sets of points in the parameter space.
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
torch::Tensor & addVectorRealHyperParameter(const std::string &name, const std::vector< Real > &value, const bool is_tunable)
Register a vector hyperparameter to this covariance function.
std::vector< const torch::Tensor * > _a_coeffs
The vectors in the $B = a_i a_i^T + diag(lambda_i)$ expansion.
Covariance function for multi-output Gaussian Processes based on the linear model of coregionalizatio...
void ErrorVector unsigned int