maxkferg / matlab-pmml

Gaussian Process Model PMML Adapter for MATLAB
Other
1 stars 1 forks source link

Gaussian Process Regression PMML Support for Matlab

Save and load a trained Gaussian Process regression (GPR) model to/from PMML. This package exposes the pmml.GaussianProcess class which is used to represent a trained GPR model. The model hyperparameters can be optimized using the GPML package, or directly on any GaussianProcess object. GaussianProcess objects can be used to generate scores for new x values, regardless of whether they were initialized from a PMML file, or a trained GPML model.

Creating GaussianProcess objects

pmml.GaussianProcess(hyperparameters, infFunc, meanFunc, covFunc, likFunc, xTrain, yTrain)

Create a new GaussianProcess object from either a PMML file or input parameters.

Where:

The covariance kernel function can be any valid function name, as specified in the PMML standard e.g. ['RadialBasisKernel','ARDSquaredExponentialKernel','AbsoluteExponentialKernel','GeneralizedExponentialKernel']

The hyp parameter should take the same form as used by the GPML package.

pmml.GaussianProcess(filename)

Create a new GaussianProcess object from an existing PMML file. This method of creating GaussianProcess objects is used to load trained models from PMML.

Where:

Object methods

Once a GaussianProcess object has been created it can be used to score new x values or it can be saved to a PMML file. For this section, assume that p is a valid GaussianProcess object.

p.optimize()

Optimize the the hyperparameters of this model, for the training values kernel type passed at initialization.

p.score(xNew)

Return scores for the new x values. xNew should be an m x n matrix of values where each row represents a test point. The method will return an m x 1 column vector of y values (scores).

p.toPMML(filename)

Return the trained GPR model as valid PMML. If the optional filename parameter is provided, the PMML will be saved to file.

Example

    % Define valid function inputs matching the documentation example
    % The hyperparameters are defined in the same way that gpml returns them
    % This make the PMML package easier to use with gpml, but requires the
    % PMML package to make conversions internally
    sn = 0.1051;
    lambda1=1.5164;
    lambda2=59.3113;
    gamma=sqrt(2.4890);
    hyp.lik = log(sn);
    hyp.mean = [];
    hyp.cov = log([lambda1; lambda2; gamma]);

    meanfunc = 'MeanZero';
    covfunc = 'ARDSquaredExponentialKernel';
    likfunc = 'Gaussian';
    inffunc = 'Exact';
    xTrain = [1,3; 2,6];
    yTrain = [1; 2];

    % Create a GPR model
    p = pmml.GaussianProcess(hyp, inffunc, meanfunc, covfunc, likfunc, xTrain, yTrain);

    % Optimize the hyperparameters
    p.optimize();

    % Access the hyperparameters
    p.hyp

    % Score some new values
    p.score([1,4])

    % Save the pmml model
    p.toPMML('output.pmml');

The GPR model and training points are now saved in the PMML format. The model can be loaded and used to score some new values.

    % Load the GPR model from file
    p = pmml.GaussianProcess('output.pmml');

    % Score the new values
    p.score([1,4])

    % Score multiple trianing points
    p.score([1,4; 2,3; 0,3])

GPML Compatibility

This package is designed to work flawlessly with the GPML package. GPML objects can easily be converted to PMML by adding it's hyperparameters to the pmml.GaussianProcess class.

% Use the GPML package to train a GP model
hyp = minimize(self.hyp, @gp, n, infer, meanZero, ARDSquaredExponentialKernel, likGauss, x, y);

% Save that model to PMML
meanFunc = 'MeanZero';
infFunc = 'Exact';
covFunc = 'ARDSquaredExponentialKernel';
likFunc = 'Gaussian';
p = pmml.GaussianProcess(hyp, inffunc, meanfunc, covfunc, likfunc, xTrain, yTrain);
p.toPMML(filename);

MATLAB-PMML can also handle the training automatically. Internally it uses GPML, so the following code is equivaluent (and preferred) to the example above:

meanFunc = 'MeanZero';
infFunc = 'Exact';
covFunc = 'ARDSquaredExponentialKernel';
likFunc = 'Gaussian';
% Define the model
p = pmml.GaussianProcess(hyp, inffunc, meanfunc, covfunc, likfunc, xTrain, yTrain);
% Train the model
hyp = p.optimize()
% Save the model
p.toPMML(filename);

TODO

Contributors