svmunit – LIBSVM Support Vector Machine Unit

class pybrain.structure.modules.svmunit.SVMUnit(indim=0, outdim=0, model=None)

This unit represents an Support Vector Machine and is implemented through the LIBSVM Python interface. It functions somewhat like a Model or a Network, but combining it with other PyBrain Models is currently discouraged. Its main function is to compare against feed-forward network classifications. You cannot get or set model parameters, but you can load and save the entire model in LIBSVM format. Sequential data and backward passes are not supported. See the corresponding example code for usage.

__init__(indim=0, outdim=0, model=None)

Initializes as empty module.

If model is given, initialize using this LIBSVM model instead. indim and outdim are for compatibility only, and ignored.

forwardPass(values=False)

Produce the output from the current input vector, or process a dataset.

If values is False or ‘class’, output is set to the number of the predicted class. If True or ‘raw’, produces decision values instead. These are stored in a dictionary for multi-class SVM. If prob, class probabilities are produced. This works only if probability option was set for SVM training.

activateOnDataset(dataset, values=False)

Run the module’s forward pass on the given dataset unconditionally and return the output as a list.

Parameter:dataset – A non-sequential supervised data set.
Key values:Passed trough to forwardPass() method.
getNbClasses()
return number of classes the current model uses
reset()
Reset input and output buffers
setModel(model)
Set the SVM model.
loadModel(filename)
Read the SVM model description from a file
saveModel(filename)
Save the SVM model description from a file

svmtrainer – LIBSVM Support Vector Machine Trainer

class pybrain.supervised.trainers.svmtrainer.SVMTrainer(svmunit, dataset, modelfile=None, plot=False)

A class performing supervised learning of a DataSet by an SVM unit. See the remarks on SVMUnit above. This whole class is a bit of a hack, and provided mostly for convenience of comparisons.

__init__(svmunit, dataset, modelfile=None, plot=False)

Initialize data and unit to be trained, and load the model, if provided.

The passed svmunit has to be an object of class SVMUnit that is going to be trained on the ClassificationDataSet object dataset. Compared to FNN training we do not use a test data set, instead 5-fold cross-validation is performed if needed.

If modelfile is provided, this model is loaded instead of training. If plot is True, a grid search is performed and the resulting pattern is plotted.

train(search=False, **kwargs)

Train the SVM on the dataset. For RBF kernels (the default), an optional meta-parameter search can be performed.

Key search:optional name of grid search class to use for RBF kernels: ‘GridSearch’ or ‘GridSearchDOE’
Key log2g:base 2 log of the RBF width parameter
Key log2c:base 2 log of the slack parameter
Key searchlog:filename into which to dump the search log
Key others:...are passed through to the grid search and/or libsvm
setParams(**kwargs)

Set parameters for SVM training. Apart from the ones below, you can use all parameters defined for the LIBSVM svm_model class, see their documentation.

Key searchlog:Save a list of coordinates and the achieved CV accuracy to this file.
load(filename)
no training at all - just load the SVM model from a file
save(filename)
save the trained SVM
class pybrain.supervised.trainers.svmtrainer.GridSearch(problem, targets, cmin, cmax, cstep=None, crossval=5, plotflag=False, maxdepth=8, searchlog='gridsearch_results.txt', **params)

Helper class used by SVMTrainer to perform an exhaustive grid search, and plot the resulting accuracy surface, if desired. Adapted from the LIBSVM python toolkit.

__init__(problem, targets, cmin, cmax, cstep=None, crossval=5, plotflag=False, maxdepth=8, searchlog='gridsearch_results.txt', **params)

Set up (log) grid search over the two RBF kernel parameters C and gamma.

Parameters:
  • problem – the LIBSVM svm_problem to be optimized, ie. the input and target data
  • targets – unfortunately, the targets used in the problem definition have to be given again here
  • cmin – lower left corner of the log2C/log2gamma window to search
  • cmax – upper right corner of the log2C/log2gamma window to search
Key cstep:

step width for log2C and log2gamma (ignored for DOE search)

Key crossval:

split dataset into this many parts for cross-validation

Key plotflag:

if True, plot the error surface contour (regular) or search pattern (DOE)

Key maxdepth:

maximum window bisection depth (DOE only)

Key searchlog:

Save a list of coordinates and the achieved CV accuracy to this file

Key others:

...are passed through to the cross_validation method of LIBSVM

search()
iterate successive parameter grid refinement and evaluation; adapted from LIBSVM grid search tool
setParams(**kwargs)
set parameters for SVM training
class pybrain.supervised.trainers.svmtrainer.GridSearchDOE(problem, targets, cmin, cmax, cstep=None, crossval=5, plotflag=False, maxdepth=8, searchlog='gridsearch_results.txt', **params)

Same as GridSearch, but implements a design-of-experiments based search pattern, as described by C. Staelin, http://www.hpl.hp.com/techreports/2002/HPL-2002-354R1.pdf

search(cmin=None, cmax=None)
iterate parameter grid refinement and evaluation recursively