SG++-Doxygen-Documentation
Generalised Sparse Grids

This example tests generalised sparse grids.

It generates a Friedman1 dataset and then compares the performance of estimators with various grid granularities. The grid granularities are controlled by the parameter $$T$$; the number of grid points is then given by $$G_n^T$$ and the approximation space is given by $$V_n^T$$.

\begin{align} G_n^T &= \bigcup_{\substack{\vert {\mathbf{l}} \vert_1 - T \vert \mathbf{i} \vert_\infty \\ \leq n + d - 1 - T n}} G_{\mathbf{l}},\\ V_n^T &= \bigoplus_{\substack{\vert {\mathbf{l}} \vert_1 - T \vert \mathbf{i} \vert_\infty \\ \leq n + d - 1 - T n}} W_{\mathbf{l}}\nonumber \end{align}

We first import all pysgpp and other utility libraries.

1 import numpy as np
2 import pysgpp as sg; sg.omp_set_num_threads(4)
3 #import pandas as pd
4 import sklearn.datasets as data

This function generates the Friedman1 dataset on demand.

1 def generate_friedman1(seed):
2  (X,y) = data.make_friedman1(n_samples=10000, random_state=seed, noise=1.0)
3  y = sg.DataVector(y)
4  X = sg.DataMatrix(X)
5  return X, y

This function evaluates the performance of a learner with standard settings and different values of T.

1 def evaluate(X_tr, y_tr, X_te, y_te, T):
2  grid = sg.RegularGridConfiguration()
3  grid.dim_ = 10
4  grid.level_ = 4
5  grid.t_ = T
6  grid.type_ = sg.GridType_ModLinear
7
11
12  solv = sg.SLESolverConfiguration()
13  solv.maxIterations_ = 50
14  solv.eps_ = 10e-6
15  solv.threshold_ = 10e-6
16  solv.type_ = sg.SLESolverType_CG
17
18  final_solv = solv
19  final_solv.maxIterations = 200
20
21  regular = sg.RegularizationConfiguration()
22  regular.type_ = sg.RegularizationType_Identity
23  regular.exponentBase_ = 1.0
24  regular.lambda_ = 10e-4

Create the estimator, train it with the training data and then return the error for the testing set.

1  estimator = sg.RegressionLearner(grid, adapt, solv, final_solv,regular)
2  estimator.train(X_tr,y_tr)
3  print(estimator.getGridSize())
4  return estimator.getMSE(X_te,y_te)
5
6 def main():

First generate the training and test data.

1  X_tr, y_tr = generate_friedman1(123456)
2  X_te, y_te = generate_friedman1(345678)

Then we evaluate the testing error for $$T \in \{-0.5, 0, 0.5, 1.0\}$$.

1  Ts = [-0.5, 0, 0.5, 1.0]
2  for T in Ts:
3  mse = evaluate(X_tr, y_tr, X_te, y_te, T)
4  print("The sparse grid with T={:2.1f} achieved a testing RMSE of {:2.4f}.".format(T, np.sqrt(mse)))
5
6 if __name__ == '__main__':
7  main()