Release: | 1.4 |
---|---|
Date: | October 13, 2010 |
This section gives an overview of Bolt’s API. Bolt itself is designed around a number of simple data structures for representing models and input examples. The functionality of Bolt is structured in a number of core modules. Each module provides an abstraction of common functionality. For example there exist modules to represent models, train models, load data sets, or evaluate models.
Contents
implemented via numpy arrays (numpy.array).
implemented via numpy record arrays (numpy.recarray).
Each bolt.model.Model is parametrized by a weight vector which is represented as a 1D numpy.array, allowing efficient random access.
An instance (aka a feature vector), on the other hand, is represented as a s sparse vector via a numpy record array. The data type of the record array is data type bolt.sparsedtype which is a tuple (uint32,float64). The advantage of record arrays is that they are mapped directly to C struct arrays.
The bolt.model module contains classes which represent parametric models supported by Bolt.
Currently, the following models are supported:
bolt.model.LinearModel: a linear model for binary classification and regression. bolt.model.GeneralizedLinearModel: a linear model for multi-class classification.
Bases: object
A generalized linear model of the form .
Create a generalized linear model for classification problems with k classes.
Parameters: |
|
---|
Predicts class of each instances in instances. Optionally, gives confidence score to each prediction if confidence is True. This method yields GeneralizedLinearModel.__call__() for each instance in instances.
Parameters: |
|
---|---|
Returns: | a generator over the class assignments and optionally a confidence score. |
The probability distribution of class assignment.
Transforms the confidence scores into a probability via a logit function
.
Returns: | a k-dimensional probability vector. |
---|
Bases: object
A linear model of the form .
Create a linear model with an
m-dimensional vector and b = 0.
Parameters: |
|
---|
Evaluates for each
instance x in instances.
Optionally, gives confidence score to each prediction if confidence is True.
This method yields LinearModel.__call__() for each instance in instances.
Parameters: |
|
---|---|
Returns: | a generator over the class assignments and optionally a confidence score. |
The trainer package contains concrete Trainer classes which are used to train a Model on a Dataset.
Bases: object
A One-versus-All trainer for multi-class models.
It trains one binary classifier for each class c that predicts the class or all-other classes.
Parameter: | trainer – A concrete Trainer implementation which is used to train k LinearModel classifiers that try to predict one class versus all others. |
---|
Train the glm using k binary LinearModel classifiers by applying the One-versus-All multi-class strategy.
Parameters: |
|
---|
The bolt.trainer.sgd module is the core of bolt. Its an extension module written in cython containing efficient implementations of Stochastic Gradient Descent and PEGASOS.
The module contains two Trainer classes:
- SGD: A plain stochastic gradient descent implementation, supporting various LossFunction and different penalties, including L1, L2, and Elastic-net penalty.
- PEGASOS: Similar to SGD, however, after each update it projects the current weight vector onto the L2 ball of radius 1/sqrt(lambda). Currently, only supports hinge loss and L2 penalty.
The module contains a number of concrete LossFunction implementations that can be plugged into the SGD trainer. Bolt provides LossFunctions for Classification and Regression.
The following Classification loss functions are supported:
- ModifiedHuber: A quadratical smoothed version of the hinge loss.
- Hinge: The loss function employed by the Support Vector Machine classifier.
- Log: The loss function of Logistic Regression.
The following Regression loss functions are supported:
- SquaredError: Standard squared error loss function.
- Huber: Huber robust regression loss.
The module also contains a number of utility function:
- predict(): computes the dot product between a sparse and a dense feature vector.
Bases: bolt.trainer.sgd.LossFunction
Base class for loss functions for classification.
Bases: bolt.trainer.sgd.Classification
SVM classification loss for binary classification tasks with y in {-1,1}.
Bases: bolt.trainer.sgd.Classification
Logistic regression loss for binary classification tasks with y in {-1,1}.
Bases: object
Base class for convex loss functions
Evaluate the derivative of the loss function.
Parameters: |
|
---|---|
Returns: | double |
Evaluate the loss function.
Parameters: |
|
---|---|
Returns: | double |
Bases: bolt.trainer.sgd.Classification
Modified Huber loss function for binary classification tasks with y in {-1,1}. Its equivalent to quadratically smoothed SVM with gamma = 2.
See T. Zhang ‘Solving Large Scale Linear Prediction Problems Using Stochastic Gradient Descent’, ICML‘04.
Bases: object
Primal estimated sub-gradient solver for svm [Shwartz2007].
Train model on the dataset using PEGASOS.
Parameters: |
|
---|
Bases: bolt.trainer.sgd.LossFunction
Base class for loss functions for regression.
Bases: object
Plain stochastic gradient descent solver. The solver supports various LossFunction and different penalties (L1, L2, and Elastic-Net).
Train model on the dataset using SGD.
Parameters: |
|
---|
Computes x*w + b efficiently.
Parameters: |
|
---|---|
Returns: | A double representing x*w + b. |
Bases: object
Averaged Perceptron learning algorithm.
Train model on the dataset using SGD.
Parameters: |
|
---|
Bases: object
Stochastic gradient descent solver for maxent (aka multinomial logistic regression). The solver supports various penalties (L1, L2, and Elastic-Net).
Train model on the dataset using SGD.
Parameters: |
|
---|
The bolt.io module provides Dataset specifications and routines for convenient input/output.
The module provides the following classes:
bolt.io.MemoryDataset : an in-memory dataset.
Bases: bolt.io.Dataset
A Dataset wrapper which binarizes the class labels.
Most methods are deligated to the wrapped Dataset. Only BinaryDataset.__iter__() and BinaryDataset.labels() are wrapped.
Creates a binary class wrapper for dataset.
Parameters: |
|
---|
Bases: object
Dataset interface.
Bases: bolt.io.Dataset
An in-memory dataset. The instances and labels are stored as two parallel arrays. Access to the parallel arrays is via an indexing array which allows convenient shuffeling.
Parameters: |
|
---|
Loads the dataset from fname.
For binary format the extension of fname has to be .npy, otherwise SVM^light format is assumed. For gzipped files thefilename must end with .gz.
Parameters: |
|
---|---|
Returns: | The MemoryDataset |
Examples:
SVM^light format:>>> ds = bolt.io.MemoryDataset.load('train.txt')Gzipped SVM^light format:
>>> ds = bolt.io.MemoryDataset.load('train.txt.gz')Binary format:>>> ds = bolt.io.MemoryDataset.load('train.npy')
Merge a sequence of Dataset objects.
Parameter: | dsets – A list of MemoryDataset |
---|---|
Returns: | A MemoryDataset containing the merged examples. |
Samples nexamples examples from the dataset.
Parameters: |
|
---|---|
Returns: | A MemoryDataset containing the nexamples examples. |
Split the Dataset into nfolds new Dataset objects. The split is done according to the index array. If MemoryDataset.n % nfolds is not 0 the remaining examples are discarded.
Parameter: | nfolds (integer) – The number of folds |
---|---|
Returns: | An array of nfolds MemoryDataset objects; one for each fold |
Store Dataset in binary form. Uses numpy.save for serialization.
Parameter: | fname – The filename |
---|
Convert numpy arrays of bolt.io.densetype to sparse arrays of bolt.io.sparsetype.
Example:
>>> x = np.array([1,0,0,0,0.2], dtype = bolt.io.densedtype)
>>> bolt.dense2sparse(x)
array([(0L, 1.0), (4L, 0.2)],
dtype=bolt.io.sparsedtype)
The data type of sparse vectors.
Example:
>>> x = np.array([(0,1),(4,0.2)], dtype = sparsedtype)
The eval module contains various routines for model evaluation.
The following evaluation metrics are currently supported:
errorrate(): the error rate of the binary classifier.
rmse(): the root mean squared error of a regressor.
cost(): the cost of a model w.r.t. a given loss function.
The cost of the loss function.
Parameters: |
|
---|---|
Returns: | sum([loss.(model(x),y) for x,y in ds]) |
Report the error of the model on the test examples. If the loss function of the model is bolt.trainer.sgd.Classification then errorrate() is computes, else rmse() is computed if loss function inherits from bolt.trainer.sgd.Regression.
Parameters: |
|
---|---|
Returns: | Either errorrate() or rmse(); depending on the loss function. |
Compute the misclassification rate of the model. Assumes that labels are coded as 1 or -1.
zero/one loss: if p*y > 0 then 0 else 1
Parameters: |
|
---|---|
Returns: | (100.0 / n) * sum( p*y > 0 ? 0 : 1 for p,y in ds). |
Compute the root mean squared error of the model.
Parameters: |
|
---|---|
Returns: | sum([(model(x)-y)**2.0 for x,y in ds]). |
[Shwartz2007] | Shwartz, S. S., Singer, Y., and Srebro, N., Pegasos: Primal estimated sub-gradient solver for svm. In Proceedings of ICML ‘07. |
[Tsuruoka2009] | (1, 2) Tsuruoka, Y., Tsujii, J., and Ananiadou, S., Stochastic gradient descent training for l1-regularized log-linear models with cumulative penalty. In Proceedings of the AFNLP/ACL ‘09. |
[Zhang2004] | (1, 2) Zhang, T., Solving large scale linear prediction problems using stochastic gradient descent algorithms. In Proceedings of ICML ‘04. |
[Zou2005] | (1, 2) Zou, H., and Hastie, T., Regularization and variable selection via the elastic net. Journal of the Royal Statistical Society Series B, 67 (2), 301-320. |
[Freund1998] | Freund, Y., and Schapire, R. E., Large margin classification using the perceptron algorithm. In Machine Learning, 37, 277-296. |