def make_estimator(lambda_reg, prior): grid = sg.RegularGridConfiguration() grid.dim_ = 4 grid.level_ = 5 grid.type_ = sg.GridType_ModLinear adapt = sg.AdaptivityConfiguration() adapt.numRefinements_ = 5 adapt.noPoints_ = 3 solv = sg.SLESolverConfiguration() solv.maxIterations_ = 50 solv.eps_ = 10e-6 solv.threshold_ = 10e-6 solv.type_ = sg.SLESolverType_CG final_solv = solv final_solv.maxIterations = 200 regular = sg.RegularizationConfiguration() regular.type_ = sg.RegularizationType_Diagonal regular.exponentBase_ = prior regular.lambda_ = lambda_reg estimator = sg.RegressionLearner(grid, adapt, solv, final_solv, regular) return estimator
def evaluate(X_tr, y_tr, X_te, y_te, interactions=None): grid = sg.RegularGridConfiguration() grid.dim_ = 64 grid.level_ = 2 grid.type_ = sg.GridType_ModLinear adapt = sg.AdaptivityConfiguration() adapt.numRefinements_ = 0 adapt.noPoints_ = 0 solv = sg.SLESolverConfiguration() solv.maxIterations_ = 50 solv.eps_ = 10e-6 solv.threshold_ = 10e-6 solv.type_ = sg.SLESolverType_CG final_solv = solv final_solv.maxIterations = 200 regular = sg.RegularizationConfiguration() regular.type_ = sg.RegularizationType_Identity regular.exponentBase_ = 1.0 regular.lambda_ = 0.1 X_tr = sg.DataMatrix(X_tr) y_tr = sg.DataVector(y_tr) X_te = sg.DataMatrix(X_te) y_te = sg.DataVector(y_te) if interactions is None: estimator = sg.ClassificationLearner(grid, adapt, solv, final_solv,regular) else: estimator = sg.ClassificationLearner(grid, adapt, solv, final_solv,regular, interactions) estimator.train(X_tr,y_tr) return estimator.getAccuracy(X_te,y_te)
def evaluate(X_tr, y_tr, X_te, y_te, T): grid = sg.RegularGridConfiguration() grid.dim_ = 10 grid.level_ = 4 grid.t_ = T grid.type_ = sg.GridType_ModLinear adapt = sg.AdaptivityConfiguration() adapt.numRefinements_ = 5 adapt.noPoints_ = 3 solv = sg.SLESolverConfiguration() solv.maxIterations_ = 50 solv.eps_ = 1e-5 solv.threshold_ = 1e-5 solv.type_ = sg.SLESolverType_CG final_solv = solv final_solv.maxIterations = 200 regular = sg.RegularizationConfiguration() regular.type_ = sg.RegularizationType_Identity regular.exponentBase_ = 1.0 regular.lambda_ = 1e-3 ## Create the estimator, train it with the training data and then return the error ## for the testing set. estimator = sg.RegressionLearner(grid, adapt, solv, final_solv, regular) estimator.train(X_tr, y_tr) print(estimator.getGridSize()) return estimator.getMSE(X_te, y_te)
def make_estimator(penalty, l1_ratio, lambda_reg): grid = sg.RegularGridConfiguration() grid.dim_ = 10 grid.level_ = 2 grid.type_ = sg.GridType_ModLinear adapt = sg.AdaptivityConfiguration() adapt.numRefinements_ = 0 adapt.noPoints_ = 0 solv = sg.SLESolverConfiguration() solv.maxIterations_ = 500 solv.threshold_ = 10e-10 solv.type_ = sg.SLESolverType_FISTA final_solv = solv regular = sg.RegularizationConfiguration() regular.type_ = penalty regular.exponentBase_ = 1.0 regular.lambda_ = lambda_reg regular.l1_ratio_ = l1_ratio estimator = sg.RegressionLearner(grid, adapt, solv, final_solv,regular) return estimator
def main(): # Generate data print("generate dataset... ", end=' ') data_tr,_ = generate_friedman1(123456) print("Done") print("generated a friedman1 dataset (10D) with 2000 samples") # Config grid print("create grid config... ", end=' ') grid = sg.RegularGridConfiguration() grid.dim_ = 10 grid.level_ = 3 grid.type_ = sg.GridType_Linear print("Done") # Config adaptivity print("create adaptive refinement config... ", end=' ') adapt = sg.AdaptivityConfiguration() adapt.numRefinements_ = 0 adapt.noPoints_ = 10 print("Done") # Config solver print("create solver config... ", end=' ') solv = sg.SLESolverConfiguration() solv.maxIterations_ = 1000 solv.eps_ = 1e-14 solv.threshold_ = 1e-14 solv.type_ = sg.SLESolverType_CG print("Done") # Config regularization print("create regularization config... ", end=' ') regular = sg.RegularizationConfiguration() regular.regType_ = sg.RegularizationType_Laplace print("Done") # Config cross validation for learner print("create learner config... ", end=' ') #crossValid = sg.CrossvalidationConfiguration() crossValid = sg.CrossvalidationConfiguration() crossValid.enable_ = False crossValid.kfold_ = 3 crossValid.lambda_ = 3.16228e-06 crossValid.lambdaStart_ = 1e-1 crossValid.lambdaEnd_ = 1e-10 crossValid.lambdaSteps_ = 3 crossValid.logScale_ = True crossValid.shuffle_ = True crossValid.seed_ = 1234567 crossValid.silent_ = False print("Done") # # Create the learner with the given configuration # print("create the learner... ") learner = sg.LearnerSGDE(grid, adapt, solv, regular, crossValid) learner.initialize(data_tr) # Train the learner print("start training... ") learner.train() print("done training") # # Estimate the probability density function (pdf) via a Gaussian kernel # density estimation (KDE) and print the corresponding values # kde = sg.KernelDensityEstimator(data_tr) x = sg.DataVector(learner.getDim()) x.setAll(0.5) print("-----------------------------------------------") print(learner.getSurpluses().getSize(), " -> ", learner.getSurpluses().sum()) print("pdf_SGDE(x) = ", learner.pdf(x), " ~ ", kde.pdf(x), " = pdf_KDE(x)") print("mean_SGDE = ", learner.mean(), " ~ ", kde.mean(), " = mean_KDE") print("var_SGDE = ", learner.variance(), " ~ ", kde.variance(), " = var_KDE") # Print the covariances C = sg.DataMatrix(grid.dim_, grid.dim_) print("----------------------- Cov_SGDE -----------------------") learner.cov(C) print(C) print("----------------------- Cov_KDE -----------------------") kde.cov(C) print(C) # # Apply the inverse Rosenblatt transformatio to a matrix of random points. To # do this, first generate the random points uniformly, then initialize an # inverse Rosenblatt transformation operation and apply it to the points. # Finally print the calculated values # print("-----------------------------------------------") opInvRos = sg.createOperationInverseRosenblattTransformation(learner.getGrid()) points = sg.DataMatrix(randu_mat(12, grid.dim_)) print(points) pointsCdf = sg.DataMatrix(points.getNrows(), points.getNcols()) opInvRos.doTransformation(learner.getSurpluses(), points, pointsCdf) # # To check whether the results are correct perform a Rosenform transformation on # the data that has been created by the inverse Rosenblatt transformation above # and print the calculated values # points.setAll(0.0) opRos = sg.createOperationRosenblattTransformation(learner.getGrid()) opRos.doTransformation(learner.getSurpluses(), pointsCdf, points) print("-----------------------------------------------") print(pointsCdf) print("-----------------------------------------------") print(points)