SG++-Doxygen-Documentation
learnerSGDETest.py

This example can be found under datadriven/examples/learnerSGDETest.py.

1 import sklearn.datasets as data
2 import numpy as np
3 import matplotlib.pyplot as plt
4 from pysgpp.extensions.datadriven.uq.plot.plot1d import plotSG1d
5 from pysgpp.extensions.datadriven.uq.plot.plot2d import plotSG2d
6 from pysgpp.extensions.datadriven.learner import Types
7 import pysgpp as sg
8 
9 
10 ## This function generates the Friedman1 dataset on demand
11 def generate_friedman1(seed):
12  (X,y) = data.make_friedman1(n_samples=2000, random_state=seed, noise=1.0)
13 
14  # transform values to DataMatrix/DataVector types
15  X = sg.DataMatrix(X)
16  y = sg.DataVector(y)
17 
18  return (X,y)
19 
20 
21 ## This function defines a vector of uniformly random numbers
22 def randu_vec(size):
23  return np.random.rand(size)
24 
25 
26 ## This function defines a matrix of uniformly random numbers
27 def randu_mat(nrows, ncols):
28  return np.random.rand(nrows, ncols)
29 
30 
31 ## ~~~ Main ~~~ ##
32 def main():
33  # Generate data
34  print("generate dataset... ", end=' ')
35  data_tr,_ = generate_friedman1(123456)
36  print("Done")
37  print("generated a friedman1 dataset (10D) with 2000 samples")
38 
39  # Config grid
40  print("create grid config... ", end=' ')
41  grid = sg.RegularGridConfiguration()
42  grid.dim_ = 10
43  grid.level_ = 3
44  grid.type_ = sg.GridType_Linear
45  print("Done")
46 
47  # Config adaptivity
48  print("create adaptive refinement config... ", end=' ')
49  adapt = sg.AdaptivityConfiguration()
50  adapt.numRefinements_ = 0
51  adapt.noPoints_ = 10
52  print("Done")
53 
54  # Config solver
55  print("create solver config... ", end=' ')
56  solv = sg.SLESolverConfiguration()
57  solv.maxIterations_ = 1000
58  solv.eps_ = 1e-14
59  solv.threshold_ = 1e-14
60  solv.type_ = sg.SLESolverType_CG
61  print("Done")
62 
63  # Config regularization
64  print("create regularization config... ", end=' ')
65  regular = sg.RegularizationConfiguration()
66  regular.regType_ = sg.RegularizationType_Laplace
67  print("Done")
68 
69  # Config cross validation for learner
70  print("create learner config... ", end=' ')
71  #crossValid = sg.CrossvalidationConfiguration()
72  crossValid = sg.CrossvalidationConfiguration()
73  crossValid.enable_ = False
74  crossValid.kfold_ = 3
75  crossValid.lambda_ = 3.16228e-06
76  crossValid.lambdaStart_ = 1e-1
77  crossValid.lambdaEnd_ = 1e-10
78  crossValid.lambdaSteps_ = 3
79  crossValid.logScale_ = True
80  crossValid.shuffle_ = True
81  crossValid.seed_ = 1234567
82  crossValid.silent_ = False
83  print("Done")
84 
85  #
86  # Create the learner with the given configuration
87  #
88  print("create the learner... ")
89  learner = sg.LearnerSGDE(grid, adapt, solv, regular, crossValid)
90  learner.initialize(data_tr)
91 
92  # Train the learner
93  print("start training... ")
94  learner.train()
95  print("done training")
96 
97  #
98  # Estimate the probability density function (pdf) via a Gaussian kernel
99  # density estimation (KDE) and print the corresponding values
100  #
101  kde = sg.KernelDensityEstimator(data_tr)
102  x = sg.DataVector(learner.getDim())
103  x.setAll(0.5)
104 
105  print("-----------------------------------------------")
106  print(learner.getSurpluses().getSize(), " -> ", learner.getSurpluses().sum())
107  print("pdf_SGDE(x) = ", learner.pdf(x), " ~ ", kde.pdf(x), " = pdf_KDE(x)")
108  print("mean_SGDE = ", learner.mean(), " ~ ", kde.mean(), " = mean_KDE")
109  print("var_SGDE = ", learner.variance(), " ~ ", kde.variance(), " = var_KDE")
110 
111  # Print the covariances
112  C = sg.DataMatrix(grid.dim_, grid.dim_)
113  print("----------------------- Cov_SGDE -----------------------")
114  learner.cov(C)
115  print(C)
116  print("----------------------- Cov_KDE -----------------------")
117  kde.cov(C)
118  print(C)
119 
120  #
121  # Apply the inverse Rosenblatt transformatio to a matrix of random points. To
122  # do this, first generate the random points uniformly, then initialize an
123  # inverse Rosenblatt transformation operation and apply it to the points.
124  # Finally print the calculated values
125  #
126  print("-----------------------------------------------")
127  opInvRos = sg.createOperationInverseRosenblattTransformation(learner.getGrid())
128  points = sg.DataMatrix(randu_mat(12, grid.dim_))
129  print(points)
130 
131  pointsCdf = sg.DataMatrix(points.getNrows(), points.getNcols())
132  opInvRos.doTransformation(learner.getSurpluses(), points, pointsCdf)
133 
134  #
135  # To check whether the results are correct perform a Rosenform transformation on
136  # the data that has been created by the inverse Rosenblatt transformation above
137  # and print the calculated values
138  #
139  points.setAll(0.0)
140  opRos = sg.createOperationRosenblattTransformation(learner.getGrid())
141  opRos.doTransformation(learner.getSurpluses(), pointsCdf, points)
142 
143  print("-----------------------------------------------")
144  print(pointsCdf)
145  print("-----------------------------------------------")
146  print(points)
147 
148 
149 if __name__ == '__main__':
150  main()
151