Skip to content

Commit

Permalink
Adding regularization to polynomial chaos #14835
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmprince committed Jul 6, 2023
1 parent 100ec5e commit a931e26
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ The other technique is using ordinary least-squares (OLS) regression, like in [P
2.874495194734781, 2.2416013478899863, 1.3479621256735075,
1.1574930187341494, 0.6939367594300845, 0.5817680465237121,
0.417989501880757]},
{'name': 'OLS with Regularization (penalty = 1)',
'type': 'scatter',
'x': [16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192],
'y': [4.91600612112163, 3.94911986807379, 3.4242180431008484,
2.7094356794174708, 1.6871974555746947, 1.3923862418279123,
1.1994658425158664, 1.0696251119444582, 0.9079419204733286,
0.8692326803241464]},
{'name': 'Integration',
'type': 'scatter',
'x': [16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class PolynomialChaosTrainer : public SurrogateTrainer
/// The method in which to perform the regression (0=integration, 1=OLS)
unsigned int _rtype;

/// The penalty parameter for Ridge regularization
const Real & _ridge_penalty;

/// QuadratureSampler pointer, necessary for applying quadrature weights
QuadratureSampler * _quad_sampler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PolynomialChaosTrainer::validParams()
"regression_type",
rtype,
"The type of regression to perform for finding polynomial coefficents.");
params.addParam<Real>("penalty", 0.0, "Ridge regularization penalty factor for OLS regression.");

params.suppressParameter<MooseEnum>("response_type");
return params;
Expand All @@ -42,6 +43,7 @@ PolynomialChaosTrainer::PolynomialChaosTrainer(const InputParameters & parameter
_coeff(declareModelData<std::vector<Real>>("_coeff")),
_poly(declareModelData<std::vector<std::unique_ptr<const PolynomialQuadrature::Polynomial>>>(
"_poly")),
_ridge_penalty(getParam<Real>("penalty")),
_quad_sampler(dynamic_cast<QuadratureSampler *>(&_sampler))
{
// Check if number of distributions is correct
Expand All @@ -60,6 +62,9 @@ PolynomialChaosTrainer::PolynomialChaosTrainer(const InputParameters & parameter
paramError("sampler",
"QuadratureSampler must use all Sampler columns for training, and cannot be"
" used with other Reporters - otherwise, quadrature integration does not work.");
if (_rtype == 0 && _ridge_penalty != 0.0)
paramError("penalty",
"Ridge regularization penalty is only relevant if 'regression_type = ols'.");

// Make polynomials
for (const auto & nm : getParam<std::vector<DistributionName>>("distributions"))
Expand Down Expand Up @@ -137,6 +142,10 @@ PolynomialChaosTrainer::train()
_calculators[c]->updateCalculator(basis[i]);
}
_r_sum += (*_rval);

if (_ridge_penalty != 0.0)
for (const auto i : make_range(_ncoeff))
_matrix(i, i) += _ridge_penalty;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
{
"app_name": "main",
"current_time": "Thu Jul 6 10:11:22 2023",
"executable": "/Users/prinzm/projects/moose/modules/stochastic_tools/test/tests/surrogates/poly_chaos/../../../../stochastic_tools-opt",
"executable_time": "Thu Jul 6 09:49:33 2023",
"libmesh_version": "",
"moose_version": "git commit ab3f6df228 on 2023-06-29",
"petsc_version": "3.16.6",
"reporters": {
"stats": {
"type": "PolynomialChaosReporter",
"values": {
"model_ols": {
"type": "PolynomialChaos const*"
},
"model_pet_ols": {
"type": "PolynomialChaos const*"
}
}
}
},
"slepc_version": "3.16.2",
"time_steps": [
{
"stats": {
"model_ols": {
"coeff": [
0.31457843096156746,
0.24083396773631338,
0.11465842944128411,
0.01083631744383195,
0.1465065015893934,
0.12290301951321941,
-0.021546628651270217,
0.14667654541057476,
0.04475259380145579,
0.12576761523291627,
-0.029275332699672744,
0.019753953198892223,
0.1354131783232185,
0.1401677393969815,
0.03197555723360114
],
"ncoeff": 15,
"ndim": 4,
"order": 3,
"poly": [
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
},
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
},
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
},
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
}
],
"tuple": [
[
0,
0,
0,
0
],
[
1,
0,
0,
0
],
[
0,
1,
0,
0
],
[
0,
0,
1,
0
],
[
0,
0,
0,
1
],
[
2,
0,
0,
0
],
[
1,
1,
0,
0
],
[
1,
0,
1,
0
],
[
1,
0,
0,
1
],
[
0,
2,
0,
0
],
[
0,
1,
1,
0
],
[
0,
1,
0,
1
],
[
0,
0,
2,
0
],
[
0,
0,
1,
1
],
[
0,
0,
0,
2
]
]
},
"model_pet_ols": {
"coeff": [
0.3218385578019423,
0.2523296083301419,
0.11341238833558517,
0.02235142493877989,
0.14994948618158732,
0.120664974464419,
-0.01845253763362933,
0.14683361051452376,
0.0461526418095108,
0.12086802434400433,
-0.026512987214038376,
0.028543394112937215,
0.13384136168248167,
0.1424221168710075,
0.029900826025355872
],
"ncoeff": 15,
"ndim": 4,
"order": 3,
"poly": [
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
},
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
},
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
},
{
"lower_bound": 0.0,
"type": "Legendre",
"upper_bound": 1.0
}
],
"tuple": [
[
0,
0,
0,
0
],
[
1,
0,
0,
0
],
[
0,
1,
0,
0
],
[
0,
0,
1,
0
],
[
0,
0,
0,
1
],
[
2,
0,
0,
0
],
[
1,
1,
0,
0
],
[
1,
0,
1,
0
],
[
1,
0,
0,
1
],
[
0,
2,
0,
0
],
[
0,
1,
1,
0
],
[
0,
1,
0,
1
],
[
0,
0,
2,
0
],
[
0,
0,
1,
1
],
[
0,
0,
0,
2
]
]
}
},
"time": 1.0,
"time_step": 1
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
jsondiff = ols_test_out.json
detail = 'MonteCarlo sampler with Uniform distribution using least-squares regression, '
[]
[monte_carlo_ridge]
type = JSONDiff
input = ols_test.i
cli_args = 'Trainers/train_ols/penalty=1.0 Trainers/train_pet_ols/penalty=1.0 Outputs/file_base=ridge_test_out'
jsondiff = ridge_test_out.json
detail = 'MonteCarlo sampler with Uniform distribution using Ridge regression, '
[]
[gauss_legendre_integration]
type = CSVDiff
input = main_2d_quad.i
Expand Down

0 comments on commit a931e26

Please sign in to comment.