SG++-Doxygen-Documentation
resumingLearningProcess.py

This is an example of how to resume the learning process from a checkpoint without recalculating the last iteration.

Sometimes one needs to resume the learning process using the saved grid and alpha files in order to refine the learner or to use some different learning parameters. And while the definition of the setup with command line arguments may be comfortable for the most common tasks, the default behaviour in this case will result in the recalculating of the last learning iteration. In order to change this behaviour the direct interaction with Learner object is needed.

This example shows how to create the Learner object using your command line arguments as well as how to omit the recomputation of the last iteration resuming the learning.

In order to create a Learner object one should use the LearnerBuilder . You can let the TerminalController to generate the code from the command line arguments.

Let's suppose you learning setup was defined by the following command line arguments

 python $SGPP_PATH/misc/python/classifier.py -v --mode test --data \ /path/to/dataset_train.arff.gz --test /path/to/dataset_test.arff.gz --border \ --level 2 -r 0.0001 -C identity --adapt_points 400 --grid_limit 50000 \ --regression --checkpoint /path/to/checkpoint_file.checkpoint.gz --stats \ /path/to/stats_file.stats.gz --grid /path/to/grid_file.grid.gz --adapt_start 9 \ -L 0.0001 With TerminalController class you now can automatically generate the code to create the Learner object using LearnerBuilder. Just call the TerminalController.py with all the command line arguments as above and an additional argument –generate  python$SGPP_PATH/datadriven/python/controller/TerminalController.py --generate -v --mode \
test --data /path/to/dataset_train.arff.gz --test /path/to/dataset_test.arff.gz \
--border --level 2 -r 0.0001 -C identity --adapt_points 400 --grid_limit 50000 \
--regression --checkpoint /path/to/checkpoint_file.checkpoint.gz --stats \
/path/to/stats_file.stats.gz --grid /path/to/grid_file.grid.gz --adapt_start 9 \
-L 0.0001

This will result in the following output:

 builder = LearnerBuilder()
builder.buildRegressor()
builder.withTrainingDataFromARFFFile('/path/to/dataset_train.arff.gz')
builder.withTestingDataFromARFFFile('/path/to/dataset_test.arff.gz')
builder = builder.withGrid()
builder.fromFile('/path/to/grid_file.grid.gz')
builder.withStartingIterationNumber(9)
builder = builder.withSpecification()
builder.withLambda(0.00010000000000000000)
builder.withIdentityOperator()
builder = builder.withStopPolicy()
builder.withGridSizeLimit(50000)
builder = builder.withCGSolver()
builder.withAccuracy(0.000100)
builder.withImax(500)
builder.withProgressPresenter(InfoToScreenRegressor())
builder.withProgressPresenter(InfoToFile('/path/to/stats_file.stats.gz'))
checkpointController = CheckpointController('checkpoint_file.checkpoint.gz', '/path/to')
builder.withCheckpointController(checkpointController)

You can then use this output to generate the Learner object.

1 # include all necessary classes
4  InfoToScreenRegressor, InfoToFile)

Paste the code as a string into your script:

1 # code automatically generated using TerminalController
2 code = """builder = LearnerBuilder()
3 builder.buildRegressor()
4 builder.withTrainingDataFromARFFFile('/path/to/dataset_train.arff.gz')
5 builder.withTestingDataFromARFFFile('/path/to/dataset_test.arff.gz')
6 builder = builder.withGrid()
7 builder.fromFile('/path/to/grid_file.grid.gz')
8 builder.withStartingIterationNumber(9)
9 builder = builder.withSpecification()
11 builder.withLambda(0.00010000000000000000)
12 builder.withIdentityOperator()
13 builder = builder.withStopPolicy()
14 builder.withGridSizeLimit(50000)
15 builder = builder.withCGSolver()
16 builder.withAccuracy(0.000100)
17 builder.withImax(500)
18 builder.withProgressPresenter(InfoToScreenRegressor())
19 builder.withProgressPresenter(InfoToFile('/path/to/stats_file.stats.gz'))
20 checkpointController = CheckpointController('checkpoint_file.checkpoint.gz', '/path/to')
21 builder.withCheckpointController(checkpointController)"""

Now you can execute the code and obtain the Learner object with

1 # create the Learner object
2 exec(code)
3 learner = builder.andGetResult()

Usually if you want to restart your classifier from the checkpoint and to perform some more learning iterations, you had to repeat last iteration step even so you already have the alpha vector. Depending on the problem size this recomputation can be very expensive. Here is how you can omit it.

Once the Learner object is loaded, e.g., as showed above, update results to recalculate errors per grid points and refine the grid

1 # update the values of errors per grid point
2 dataset = learner.dataContainer
3 trainSubset = dataset.getTrainDataset()
4 testSubset = dataset.getTestDataset()
5 learner.updateResults(learner.alpha, trainSubset, testSubset)
6 # refine the grid
7 learner.refineGrid()
8 # resume the learning process
9 learner.learnDataWithTest()