pax_global_header00006660000000000000000000000064115471450120014512gustar00rootroot0000000000000052 comment=41a5c7aa48231bc57ea8ff6798b6e418aaeca9a0 liblinear-1.8+dfsg/000077500000000000000000000000001154714501200142425ustar00rootroot00000000000000liblinear-1.8+dfsg/COPYRIGHT000066400000000000000000000027161154714501200155430ustar00rootroot00000000000000 Copyright (c) 2007-2011 The LIBLINEAR Project. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither name of copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. liblinear-1.8+dfsg/Makefile000066400000000000000000000011431154714501200157010ustar00rootroot00000000000000CXX ?= g++ CC ?= gcc CFLAGS = -Wall -Wconversion -O3 -fPIC LIBS = SHVER = 1 all: train predict lib: linear.o tron.o $(CXX) -shared -dynamiclib linear.o tron.o -o liblinear.so.$(SHVER) train: tron.o linear.o train.c $(CXX) $(CFLAGS) -o train train.c tron.o linear.o $(LIBS) predict: tron.o linear.o predict.c $(CXX) $(CFLAGS) -o predict predict.c tron.o linear.o $(LIBS) tron.o: tron.cpp tron.h $(CXX) $(CFLAGS) -c -o tron.o tron.cpp linear.o: linear.cpp linear.h $(CXX) $(CFLAGS) -c -o linear.o linear.cpp clean: cd matlab; make clean rm -f *~ tron.o linear.o train predict liblinear.so.$(SHVER) liblinear-1.8+dfsg/Makefile.win000066400000000000000000000016101154714501200164740ustar00rootroot00000000000000#You must ensure nmake.exe, cl.exe, link.exe are in system path. #VCVARS32.bat #Under dosbox prompt #nmake -f Makefile.win ########################################## CXXC = cl.exe CFLAGS = -nologo -O2 -EHsc -I. -D __WIN32__ -D _CRT_SECURE_NO_DEPRECATE TARGET = windows all: $(TARGET)\train.exe $(TARGET)\predict.exe $(TARGET)\train.exe: tron.obj linear.obj train.c blas\*.c $(CXX) $(CFLAGS) -Fe$(TARGET)\train.exe tron.obj linear.obj train.c blas\*.c $(TARGET)\predict.exe: tron.obj linear.obj predict.c blas\*.c $(CXX) $(CFLAGS) -Fe$(TARGET)\predict.exe tron.obj linear.obj predict.c blas\*.c linear.obj: linear.cpp linear.h $(CXX) $(CFLAGS) -c linear.cpp tron.obj: tron.cpp tron.h $(CXX) $(CFLAGS) -c tron.cpp lib: linear.cpp linear.h linear.def tron.obj $(CXX) $(CFLAGS) -LD linear.cpp tron.obj blas\*.c -Fe$(TARGET)\liblinear -link -DEF:linear.def clean: -erase /Q *.obj $(TARGET)\. liblinear-1.8+dfsg/README000066400000000000000000000405411154714501200151260ustar00rootroot00000000000000LIBLINEAR is a simple package for solving large-scale regularized linear classification. It currently supports L2-regularized logistic regression/L2-loss support vector classification/L1-loss support vector classification, and L1-regularized L2-loss support vector classification/ logistic regression. This document explains the usage of LIBLINEAR. To get started, please read the ``Quick Start'' section first. For developers, please check the ``Library Usage'' section to learn how to integrate LIBLINEAR in your software. Table of Contents ================= - When to use LIBLINEAR but not LIBSVM - Quick Start - Installation - `train' Usage - `predict' Usage - Examples - Library Usage - Building Windows Binaries - Additional Information - MATLAB/OCTAVE interface - PYTHON interface When to use LIBLINEAR but not LIBSVM ==================================== There are some large data for which with/without nonlinear mappings gives similar performances. Without using kernels, one can efficiently train a much larger set via a linear classifier. These data usually have a large number of features. Document classification is an example. Warning: While generally liblinear is very fast, its default solver may be slow under certain situations (e.g., data not scaled or C is large). See Appendix B of our SVM guide about how to handle such cases. http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf Warning: If you are a beginner and your data sets are not large, you should consider LIBSVM first. LIBSVM page: http://www.csie.ntu.edu.tw/~cjlin/libsvm Quick Start =========== See the section ``Installation'' for installing LIBLINEAR. After installation, there are programs `train' and `predict' for training and testing, respectively. About the data format, please check the README file of LIBSVM. Note that feature index must start from 1 (but not 0). A sample classification data included in this package is `heart_scale'. Type `train heart_scale', and the program will read the training data and output the model file `heart_scale.model'. If you have a test set called heart_scale.t, then type `predict heart_scale.t heart_scale.model output' to see the prediction accuracy. The `output' file contains the predicted class labels. For more information about `train' and `predict', see the sections `train' Usage and `predict' Usage. To obtain good performances, sometimes one needs to scale the data. Please check the program `svm-scale' of LIBSVM. For large and sparse data, use `-l 0' to keep the sparsity. Installation ============ On Unix systems, type `make' to build the `train' and `predict' programs. Run them without arguments to show the usages. On other systems, consult `Makefile' to build them (e.g., see 'Building Windows binaries' in this file) or use the pre-built binaries (Windows binaries are in the directory `windows'). This software uses some level-1 BLAS subroutines. The needed functions are included in this package. If a BLAS library is available on your machine, you may use it by modifying the Makefile: Unmark the following line #LIBS ?= -lblas and mark LIBS ?= blas/blas.a `train' Usage ============= Usage: train [options] training_set_file [model_file] options: -s type : set type of solver (default 1) 0 -- L2-regularized logistic regression (primal) 1 -- L2-regularized L2-loss support vector classification (dual) 2 -- L2-regularized L2-loss support vector classification (primal) 3 -- L2-regularized L1-loss support vector classification (dual) 4 -- multi-class support vector classification by Crammer and Singer 5 -- L1-regularized L2-loss support vector classification 6 -- L1-regularized logistic regression 7 -- L2-regularized logistic regression (dual) -c cost : set the parameter C (default 1) -e epsilon : set tolerance of termination criterion -s 0 and 2 |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2, where f is the primal function and pos/neg are # of positive/negative data (default 0.01) -s 1, 3, 4 and 7 Dual maximal violation <= eps; similar to libsvm (default 0.1) -s 5 and 6 |f'(w)|_inf <= eps*min(pos,neg)/l*|f'(w0)|_inf, where f is the primal function (default 0.01) -B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1) -wi weight: weights adjust the parameter C of different classes (see README for details) -v n: n-fold cross validation mode -q : quiet mode (no outputs) Option -v randomly splits the data into n parts and calculates cross validation accuracy on them. Formulations: For L2-regularized logistic regression (-s 0), we solve min_w w^Tw/2 + C \sum log(1 + exp(-y_i w^Tx_i)) For L2-regularized L2-loss SVC dual (-s 1), we solve min_alpha 0.5(alpha^T (Q + I/2/C) alpha) - e^T alpha s.t. 0 <= alpha_i, For L2-regularized L2-loss SVC (-s 2), we solve min_w w^Tw/2 + C \sum max(0, 1- y_i w^Tx_i)^2 For L2-regularized L1-loss SVC dual (-s 3), we solve min_alpha 0.5(alpha^T Q alpha) - e^T alpha s.t. 0 <= alpha_i <= C, For L1-regularized L2-loss SVC (-s 5), we solve min_w \sum |w_j| + C \sum max(0, 1- y_i w^Tx_i)^2 For L1-regularized logistic regression (-s 6), we solve min_w \sum |w_j| + C \sum log(1 + exp(-y_i w^Tx_i)) where Q is a matrix with Q_ij = y_i y_j x_i^T x_j. For L2-regularized logistic regression (-s 7), we solve min_alpha 0.5(alpha^T Q alpha) + \sum alpha_i*log(alpha_i) + \sum (C-alpha_i)*log(C-alpha_i) - a constant s.t. 0 <= alpha_i <= C, If bias >= 0, w becomes [w; w_{n+1}] and x becomes [x; bias]. The primal-dual relationship implies that -s 1 and -s 2 give the same model, and -s 0 and -s 7 give the same. We implement 1-vs-the rest multi-class strategy. In training i vs. non_i, their C parameters are (weight from -wi)*C and C, respectively. If there are only two classes, we train only one model. Thus weight1*C vs. weight2*C is used. See examples below. We also implement multi-class SVM by Crammer and Singer (-s 4): min_{w_m, \xi_i} 0.5 \sum_m ||w_m||^2 + C \sum_i \xi_i s.t. w^T_{y_i} x_i - w^T_m x_i >= \e^m_i - \xi_i \forall m,i where e^m_i = 0 if y_i = m, e^m_i = 1 if y_i != m, Here we solve the dual problem: min_{\alpha} 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i s.t. \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i where w_m(\alpha) = \sum_i \alpha^m_i x_i, and C^m_i = C if m = y_i, C^m_i = 0 if m != y_i. `predict' Usage =============== Usage: predict [options] test_file model_file output_file options: -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0) Examples ======== > train data_file Train linear SVM with L2-loss function. > train -s 0 data_file Train a logistic regression model. > train -v 5 -e 0.001 data_file Do five-fold cross-validation using L2-loss svm. Use a smaller stopping tolerance 0.001 than the default 0.1 if you want more accurate solutions. > train -c 10 -w1 2 -w2 5 -w3 2 four_class_data_file Train four classifiers: positive negative Cp Cn class 1 class 2,3,4. 20 10 class 2 class 1,3,4. 50 10 class 3 class 1,2,4. 20 10 class 4 class 1,2,3. 10 10 > train -c 10 -w3 1 -w2 5 two_class_data_file If there are only two classes, we train ONE model. The C values for the two classes are 10 and 50. > predict -b 1 test_file data_file.model output_file Output probability estimates (for logistic regression only). Library Usage ============= - Function: model* train(const struct problem *prob, const struct parameter *param); This function constructs and returns a linear classification model according to the given training data and parameters. struct problem describes the problem: struct problem { int l, n; int *y; struct feature_node **x; double bias; }; where `l' is the number of training data. If bias >= 0, we assume that one additional feature is added to the end of each data instance. `n' is the number of feature (including the bias feature if bias >= 0). `y' is an array containing the target values. And `x' is an array of pointers, each of which points to a sparse representation (array of feature_node) of one training vector. For example, if we have the following training data: LABEL ATTR1 ATTR2 ATTR3 ATTR4 ATTR5 ----- ----- ----- ----- ----- ----- 1 0 0.1 0.2 0 0 2 0 0.1 0.3 -1.2 0 1 0.4 0 0 0 0 2 0 0.1 0 1.4 0.5 3 -0.1 -0.2 0.1 1.1 0.1 and bias = 1, then the components of problem are: l = 5 n = 6 y -> 1 2 1 2 3 x -> [ ] -> (2,0.1) (3,0.2) (6,1) (-1,?) [ ] -> (2,0.1) (3,0.3) (4,-1.2) (6,1) (-1,?) [ ] -> (1,0.4) (6,1) (-1,?) [ ] -> (2,0.1) (4,1.4) (5,0.5) (6,1) (-1,?) [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (6,1) (-1,?) struct parameter describes the parameters of a linear classification model: struct parameter { int solver_type; /* these are for training only */ double eps; /* stopping criteria */ double C; int nr_weight; int *weight_label; double* weight; }; solver_type can be one of L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL. L2R_LR L2-regularized logistic regression (primal) L2R_L2LOSS_SVC_DUAL L2-regularized L2-loss support vector classification (dual) L2R_L2LOSS_SVC L2-regularized L2-loss support vector classification (primal) L2R_L1LOSS_SVC_DUAL L2-regularized L1-loss support vector classification (dual) MCSVM_CS multi-class support vector classification by Crammer and Singer L1R_L2LOSS_SVC L1-regularized L2-loss support vector classification L1R_LR L1-regularized logistic regression L2R_LR_DUAL L2-regularized logistic regression (dual) C is the cost of constraints violation. eps is the stopping criterion. nr_weight, weight_label, and weight are used to change the penalty for some classes (If the weight for a class is not changed, it is set to 1). This is useful for training classifier using unbalanced input data or with asymmetric misclassification cost. nr_weight is the number of elements in the array weight_label and weight. Each weight[i] corresponds to weight_label[i], meaning that the penalty of class weight_label[i] is scaled by a factor of weight[i]. If you do not want to change penalty for any of the classes, just set nr_weight to 0. *NOTE* To avoid wrong parameters, check_parameter() should be called before train(). struct model stores the model obtained from the training procedure: struct model { struct parameter param; int nr_class; /* number of classes */ int nr_feature; double *w; int *label; /* label of each class */ double bias; }; param describes the parameters used to obtain the model. nr_class and nr_feature are the number of classes and features, respectively. The nr_feature*nr_class array w gives feature weights. We use one against the rest for multi-class classification, so each feature index corresponds to nr_class weight values. Weights are organized in the following way +------------------+------------------+------------+ | nr_class weights | nr_class weights | ... | for 1st feature | for 2nd feature | +------------------+------------------+------------+ If bias >= 0, x becomes [x; bias]. The number of features is increased by one, so w is a (nr_feature+1)*nr_class array. The value of bias is stored in the variable bias. The array label stores class labels. - Function: void cross_validation(const problem *prob, const parameter *param, int nr_fold, int *target); This function conducts cross validation. Data are separated to nr_fold folds. Under given parameters, sequentially each fold is validated using the model from training the remaining. Predicted labels in the validation process are stored in the array called target. The format of prob is same as that for train(). - Function: int predict(const model *model_, const feature_node *x); This functions classifies a test vector using the given model. The predicted label is returned. - Function: int predict_values(const struct model *model_, const struct feature_node *x, double* dec_values); This function gives nr_w decision values in the array dec_values. nr_w is 1 if there are two classes except multi-class svm by Crammer and Singer (-s 4), and is the number of classes otherwise. We implement one-vs-the rest multi-class strategy (-s 0,1,2,3) and multi-class svm by Crammer and Singer (-s 4) for multi-class SVM. The class with the highest decision value is returned. - Function: int predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates); This function gives nr_class probability estimates in the array prob_estimates. nr_class can be obtained from the function get_nr_class. The class with the highest probability is returned. Currently, we support only the probability outputs of logistic regression. - Function: int get_nr_feature(const model *model_); The function gives the number of attributes of the model. - Function: int get_nr_class(const model *model_); The function gives the number of classes of the model. - Function: void get_labels(const model *model_, int* label); This function outputs the name of labels into an array called label. - Function: const char *check_parameter(const struct problem *prob, const struct parameter *param); This function checks whether the parameters are within the feasible range of the problem. This function should be called before calling train() and cross_validation(). It returns NULL if the parameters are feasible, otherwise an error message is returned. - Function: int save_model(const char *model_file_name, const struct model *model_); This function saves a model to a file; returns 0 on success, or -1 if an error occurs. - Function: struct model *load_model(const char *model_file_name); This function returns a pointer to the model read from the file, or a null pointer if the model could not be loaded. - Function: void free_model_content(struct model *model_ptr); This function frees the memory used by the entries in a model structure. - Function: void free_and_destroy_model(struct model **model_ptr_ptr); This function frees the memory used by a model and destroys the model structure. - Function: void destroy_param(struct parameter *param); This function frees the memory used by a parameter set. - Function: void set_print_string_function(void (*print_func)(const char *)); Users can specify their output format by a function. Use set_print_string_function(NULL); for default printing to stdout. Building Windows Binaries ========================= Windows binaries are in the directory `windows'. To build them via Visual C++, use the following steps: 1. Open a dos command box and change to liblinear directory. If environment variables of VC++ have not been set, type "C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat" You may have to modify the above command according which version of VC++ or where it is installed. 2. Type nmake -f Makefile.win clean all MATLAB/OCTAVE Interface ======================= Please check the file README in the directory `matlab'. PYTHON Interface ================ Please check the file README in the directory `python'. Additional Information ====================== If you find LIBLINEAR helpful, please cite it as R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R. Wang, and C.-J. Lin. LIBLINEAR: A Library for Large Linear Classification, Journal of Machine Learning Research 9(2008), 1871-1874. Software available at http://www.csie.ntu.edu.tw/~cjlin/liblinear For any questions and comments, please send your email to cjlin@csie.ntu.edu.tw liblinear-1.8+dfsg/heart_scale000066400000000000000000000660261154714501200164510ustar00rootroot00000000000000+1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 -1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1 +1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1 -1 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1 -1 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1 -1 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1 +1 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5 +1 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1 +1 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1 +1 1:0.416667 2:-1 3:1 4:0.0566038 5:0.283105 6:-1 7:1 8:0.267176 9:-1 10:0.290323 12:1 13:1 -1 1:0.25 2:1 3:1 4:-0.226415 5:-0.506849 6:-1 7:-1 8:0.374046 9:-1 10:-0.83871 12:-1 13:1 -1 2:1 3:1 4:-0.0943396 5:-0.543379 6:-1 7:1 8:-0.389313 9:1 10:-1 11:-1 12:-1 13:1 -1 1:-0.375 2:1 3:0.333333 4:-0.132075 5:-0.502283 6:-1 7:1 8:0.664122 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.333333 2:1 3:-1 4:-0.245283 5:-0.506849 6:-1 7:-1 8:0.129771 9:-1 10:-0.16129 12:0.333333 13:-1 -1 1:0.166667 2:-1 3:1 4:-0.358491 5:-0.191781 6:-1 7:1 8:0.343511 9:-1 10:-1 11:-1 12:-0.333333 13:-1 -1 1:0.75 2:-1 3:1 4:-0.660377 5:-0.894977 6:-1 7:-1 8:-0.175573 9:-1 10:-0.483871 12:-1 13:-1 +1 1:-0.291667 2:1 3:1 4:-0.132075 5:-0.155251 6:-1 7:-1 8:-0.251908 9:1 10:-0.419355 12:0.333333 13:1 +1 2:1 3:1 4:-0.132075 5:-0.648402 6:1 7:1 8:0.282443 9:1 11:1 12:-1 13:1 -1 1:0.458333 2:1 3:-1 4:-0.698113 5:-0.611872 6:-1 7:1 8:0.114504 9:1 10:-0.419355 12:-1 13:-1 -1 1:-0.541667 2:1 3:-1 4:-0.132075 5:-0.666667 6:-1 7:-1 8:0.633588 9:1 10:-0.548387 11:-1 12:-1 13:1 +1 1:0.583333 2:1 3:1 4:-0.509434 5:-0.52968 6:-1 7:1 8:-0.114504 9:1 10:-0.16129 12:0.333333 13:1 -1 1:-0.208333 2:1 3:-0.333333 4:-0.320755 5:-0.456621 6:-1 7:1 8:0.664122 9:-1 10:-0.935484 12:-1 13:-1 -1 1:-0.416667 2:1 3:1 4:-0.603774 5:-0.191781 6:-1 7:-1 8:0.679389 9:-1 10:-0.612903 12:-1 13:-1 -1 1:-0.25 2:1 3:1 4:-0.660377 5:-0.643836 6:-1 7:-1 8:0.0992366 9:-1 10:-0.967742 11:-1 12:-1 13:-1 -1 1:0.0416667 2:-1 3:-0.333333 4:-0.283019 5:-0.260274 6:1 7:1 8:0.343511 9:1 10:-1 11:-1 12:-0.333333 13:-1 -1 1:-0.208333 2:-1 3:0.333333 4:-0.320755 5:-0.319635 6:-1 7:-1 8:0.0381679 9:-1 10:-0.935484 11:-1 12:-1 13:-1 -1 1:-0.291667 2:-1 3:1 4:-0.169811 5:-0.465753 6:-1 7:1 8:0.236641 9:1 10:-1 12:-1 13:-1 -1 1:-0.0833333 2:-1 3:0.333333 4:-0.509434 5:-0.228311 6:-1 7:1 8:0.312977 9:-1 10:-0.806452 11:-1 12:-1 13:-1 +1 1:0.208333 2:1 3:0.333333 4:-0.660377 5:-0.525114 6:-1 7:1 8:0.435115 9:-1 10:-0.193548 12:-0.333333 13:1 -1 1:0.75 2:-1 3:0.333333 4:-0.698113 5:-0.365297 6:1 7:1 8:-0.0992366 9:-1 10:-1 11:-1 12:-0.333333 13:-1 +1 1:0.166667 2:1 3:0.333333 4:-0.358491 5:-0.52968 6:-1 7:1 8:0.206107 9:-1 10:-0.870968 12:-0.333333 13:1 -1 1:0.541667 2:1 3:1 4:0.245283 5:-0.534247 6:-1 7:1 8:0.0229008 9:-1 10:-0.258065 11:-1 12:-1 13:0.5 -1 1:-0.666667 2:-1 3:0.333333 4:-0.509434 5:-0.593607 6:-1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.25 2:1 3:1 4:0.433962 5:-0.086758 6:-1 7:1 8:0.0534351 9:1 10:0.0967742 11:1 12:-1 13:1 +1 1:-0.125 2:1 3:1 4:-0.0566038 5:-0.6621 6:-1 7:1 8:-0.160305 9:1 10:-0.709677 12:-1 13:1 +1 1:-0.208333 2:1 3:1 4:-0.320755 5:-0.406393 6:1 7:1 8:0.206107 9:1 10:-1 11:-1 12:0.333333 13:1 +1 1:0.333333 2:1 3:1 4:-0.132075 5:-0.630137 6:-1 7:1 8:0.0229008 9:1 10:-0.387097 11:-1 12:-0.333333 13:1 +1 1:0.25 2:1 3:-1 4:0.245283 5:-0.328767 6:-1 7:1 8:-0.175573 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.458333 2:1 3:0.333333 4:-0.320755 5:-0.753425 6:-1 7:-1 8:0.206107 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.208333 2:1 3:1 4:-0.471698 5:-0.561644 6:-1 7:1 8:0.755725 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.541667 2:1 3:1 4:0.0943396 5:-0.557078 6:-1 7:-1 8:0.679389 9:-1 10:-1 11:-1 12:-1 13:1 -1 1:0.375 2:-1 3:1 4:-0.433962 5:-0.621005 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.375 2:1 3:0.333333 4:-0.320755 5:-0.511416 6:-1 7:-1 8:0.648855 9:1 10:-0.870968 11:-1 12:-1 13:-1 -1 1:-0.291667 2:1 3:-0.333333 4:-0.867925 5:-0.675799 6:1 7:-1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:1 +1 1:0.25 2:1 3:0.333333 4:-0.396226 5:-0.579909 6:1 7:-1 8:-0.0381679 9:-1 10:-0.290323 12:-0.333333 13:0.5 -1 1:0.208333 2:1 3:0.333333 4:-0.132075 5:-0.611872 6:1 7:1 8:0.435115 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.166667 2:1 3:0.333333 4:-0.54717 5:-0.894977 6:-1 7:1 8:-0.160305 9:-1 10:-0.741935 11:-1 12:1 13:-1 +1 1:-0.375 2:1 3:1 4:-0.698113 5:-0.675799 6:-1 7:1 8:0.618321 9:-1 10:-1 11:-1 12:-0.333333 13:-1 +1 1:0.541667 2:1 3:-0.333333 4:0.245283 5:-0.452055 6:-1 7:-1 8:-0.251908 9:1 10:-1 12:1 13:0.5 +1 1:0.5 2:-1 3:1 4:0.0566038 5:-0.547945 6:-1 7:1 8:-0.343511 9:-1 10:-0.677419 12:1 13:1 +1 1:-0.458333 2:1 3:1 4:-0.207547 5:-0.136986 6:-1 7:-1 8:-0.175573 9:1 10:-0.419355 12:-1 13:0.5 -1 1:-0.0416667 2:1 3:-0.333333 4:-0.358491 5:-0.639269 6:1 7:-1 8:0.725191 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.5 2:-1 3:0.333333 4:-0.132075 5:0.328767 6:1 7:1 8:0.312977 9:-1 10:-0.741935 11:-1 12:-0.333333 13:-1 -1 1:0.416667 2:-1 3:-0.333333 4:-0.132075 5:-0.684932 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:0.333333 13:-1 -1 1:-0.333333 2:-1 3:-0.333333 4:-0.320755 5:-0.506849 6:-1 7:1 8:0.587786 9:-1 10:-0.806452 12:-1 13:-1 -1 1:-0.5 2:-1 3:-0.333333 4:-0.792453 5:-0.671233 6:-1 7:-1 8:0.480916 9:-1 10:-1 11:-1 12:-0.333333 13:-1 +1 1:0.333333 2:1 3:1 4:-0.169811 5:-0.817352 6:-1 7:1 8:-0.175573 9:1 10:0.16129 12:-0.333333 13:-1 -1 1:0.291667 2:-1 3:0.333333 4:-0.509434 5:-0.762557 6:1 7:-1 8:-0.618321 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.25 2:-1 3:1 4:0.509434 5:-0.438356 6:-1 7:-1 8:0.0992366 9:1 10:-1 12:-1 13:-1 +1 1:0.375 2:1 3:-0.333333 4:-0.509434 5:-0.292237 6:-1 7:1 8:-0.51145 9:-1 10:-0.548387 12:-0.333333 13:1 -1 1:0.166667 2:1 3:0.333333 4:0.0566038 5:-1 6:1 7:-1 8:0.557252 9:-1 10:-0.935484 11:-1 12:-0.333333 13:1 +1 1:-0.0833333 2:-1 3:1 4:-0.320755 5:-0.182648 6:-1 7:-1 8:0.0839695 9:1 10:-0.612903 12:-1 13:1 -1 1:-0.375 2:1 3:0.333333 4:-0.509434 5:-0.543379 6:-1 7:-1 8:0.496183 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.291667 2:-1 3:-1 4:0.0566038 5:-0.479452 6:-1 7:-1 8:0.526718 9:-1 10:-0.709677 11:-1 12:-1 13:-1 -1 1:0.416667 2:1 3:-1 4:-0.0377358 5:-0.511416 6:1 7:1 8:0.206107 9:-1 10:-0.258065 11:1 12:-1 13:0.5 +1 1:0.166667 2:1 3:1 4:0.0566038 5:-0.315068 6:-1 7:1 8:-0.374046 9:1 10:-0.806452 12:-0.333333 13:0.5 -1 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.383562 6:-1 7:1 8:0.755725 9:1 10:-1 11:-1 12:-1 13:-1 +1 1:0.208333 2:-1 3:-0.333333 4:-0.207547 5:-0.118721 6:1 7:1 8:0.236641 9:-1 10:-1 11:-1 12:0.333333 13:-1 -1 1:-0.375 2:-1 3:0.333333 4:-0.54717 5:-0.47032 6:-1 7:-1 8:0.19084 9:-1 10:-0.903226 12:-0.333333 13:-1 +1 1:-0.25 2:1 3:0.333333 4:-0.735849 5:-0.465753 6:-1 7:-1 8:0.236641 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.333333 2:1 3:1 4:-0.509434 5:-0.388128 6:-1 7:-1 8:0.0534351 9:1 10:0.16129 12:-0.333333 13:1 -1 1:0.166667 2:-1 3:1 4:-0.509434 5:0.0410959 6:-1 7:-1 8:0.40458 9:1 10:-0.806452 11:-1 12:-1 13:-1 -1 1:0.708333 2:1 3:-0.333333 4:0.169811 5:-0.456621 6:-1 7:1 8:0.0992366 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.958333 2:-1 3:0.333333 4:-0.132075 5:-0.675799 6:-1 8:-0.312977 9:-1 10:-0.645161 12:-1 13:-1 -1 1:0.583333 2:-1 3:1 4:-0.773585 5:-0.557078 6:-1 7:-1 8:0.0839695 9:-1 10:-0.903226 11:-1 12:0.333333 13:-1 +1 1:-0.333333 2:1 3:1 4:-0.0943396 5:-0.164384 6:-1 7:1 8:0.160305 9:1 10:-1 12:1 13:1 -1 1:-0.333333 2:1 3:1 4:-0.811321 5:-0.625571 6:-1 7:1 8:0.175573 9:1 10:-0.0322581 12:-1 13:-1 -1 1:-0.583333 2:-1 3:0.333333 4:-1 5:-0.666667 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.458333 2:-1 3:0.333333 4:-0.509434 5:-0.621005 6:-1 7:-1 8:0.557252 9:-1 10:-1 12:-1 13:-1 -1 1:0.125 2:1 3:-0.333333 4:-0.509434 5:-0.497717 6:-1 7:-1 8:0.633588 9:-1 10:-0.741935 11:-1 12:-1 13:-1 +1 1:0.208333 2:1 3:1 4:-0.0188679 5:-0.579909 6:-1 7:-1 8:-0.480916 9:-1 10:-0.354839 12:-0.333333 13:1 +1 1:-0.75 2:1 3:1 4:-0.509434 5:-0.671233 6:-1 7:-1 8:-0.0992366 9:1 10:-0.483871 12:-1 13:1 +1 1:0.208333 2:1 3:1 4:0.0566038 5:-0.342466 6:-1 7:1 8:-0.389313 9:1 10:-0.741935 11:-1 12:-1 13:1 -1 1:-0.5 2:1 3:0.333333 4:-0.320755 5:-0.598174 6:-1 7:1 8:0.480916 9:-1 10:-0.354839 12:-1 13:-1 -1 1:0.166667 2:1 3:1 4:-0.698113 5:-0.657534 6:-1 7:-1 8:-0.160305 9:1 10:-0.516129 12:-1 13:0.5 -1 1:-0.458333 2:1 3:-1 4:0.0188679 5:-0.461187 6:-1 7:1 8:0.633588 9:-1 10:-0.741935 11:-1 12:0.333333 13:-1 -1 1:0.375 2:1 3:-0.333333 4:-0.358491 5:-0.625571 6:1 7:1 8:0.0534351 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.25 2:1 3:-1 4:0.584906 5:-0.342466 6:-1 7:1 8:0.129771 9:-1 10:0.354839 11:1 12:-1 13:1 -1 1:-0.5 2:-1 3:-0.333333 4:-0.396226 5:-0.178082 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.125 2:1 3:1 4:0.0566038 5:-0.465753 6:-1 7:1 8:-0.129771 9:-1 10:-0.16129 12:-1 13:1 -1 1:0.25 2:1 3:-0.333333 4:-0.132075 5:-0.56621 6:-1 7:-1 8:0.419847 9:1 10:-1 11:-1 12:-1 13:-1 +1 1:0.333333 2:-1 3:1 4:-0.320755 5:-0.0684932 6:-1 7:1 8:0.496183 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.0416667 2:1 3:1 4:-0.433962 5:-0.360731 6:-1 7:1 8:-0.419847 9:1 10:-0.290323 12:-0.333333 13:1 +1 1:0.0416667 2:1 3:1 4:-0.698113 5:-0.634703 6:-1 7:1 8:-0.435115 9:1 10:-1 12:-0.333333 13:-1 +1 1:-0.0416667 2:1 3:1 4:-0.415094 5:-0.607306 6:-1 7:-1 8:0.480916 9:-1 10:-0.677419 11:-1 12:0.333333 13:1 +1 1:-0.25 2:1 3:1 4:-0.698113 5:-0.319635 6:-1 7:1 8:-0.282443 9:1 10:-0.677419 12:-0.333333 13:-1 -1 1:0.541667 2:1 3:1 4:-0.509434 5:-0.196347 6:-1 7:1 8:0.221374 9:-1 10:-0.870968 12:-1 13:-1 +1 1:0.208333 2:1 3:1 4:-0.886792 5:-0.506849 6:-1 7:-1 8:0.29771 9:-1 10:-0.967742 11:-1 12:-0.333333 13:1 -1 1:0.458333 2:-1 3:0.333333 4:-0.132075 5:-0.146119 6:-1 7:-1 8:-0.0534351 9:-1 10:-0.935484 11:-1 12:-1 13:1 -1 1:-0.125 2:-1 3:-0.333333 4:-0.509434 5:-0.461187 6:-1 7:-1 8:0.389313 9:-1 10:-0.645161 11:-1 12:-1 13:-1 -1 1:-0.375 2:-1 3:0.333333 4:-0.735849 5:-0.931507 6:-1 7:-1 8:0.587786 9:-1 10:-0.806452 12:-1 13:-1 +1 1:0.583333 2:1 3:1 4:-0.509434 5:-0.493151 6:-1 7:-1 8:-1 9:-1 10:-0.677419 12:-1 13:-1 -1 1:-0.166667 2:-1 3:1 4:-0.320755 5:-0.347032 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.166667 2:1 3:1 4:0.339623 5:-0.255708 6:1 7:1 8:-0.19084 9:-1 10:-0.677419 12:1 13:1 +1 1:0.416667 2:1 3:1 4:-0.320755 5:-0.415525 6:-1 7:1 8:0.160305 9:-1 10:-0.548387 12:-0.333333 13:1 +1 1:-0.208333 2:1 3:1 4:-0.433962 5:-0.324201 6:-1 7:1 8:0.450382 9:-1 10:-0.83871 12:-1 13:1 -1 1:-0.0833333 2:1 3:0.333333 4:-0.886792 5:-0.561644 6:-1 7:-1 8:0.0992366 9:1 10:-0.612903 12:-1 13:-1 +1 1:0.291667 2:-1 3:1 4:0.0566038 5:-0.39726 6:-1 7:1 8:0.312977 9:-1 10:-0.16129 12:0.333333 13:1 +1 1:0.25 2:1 3:1 4:-0.132075 5:-0.767123 6:-1 7:-1 8:0.389313 9:1 10:-1 11:-1 12:-0.333333 13:1 -1 1:-0.333333 2:-1 3:-0.333333 4:-0.660377 5:-0.844749 6:-1 7:-1 8:0.0229008 9:-1 10:-1 12:-1 13:-1 +1 1:0.0833333 2:-1 3:1 4:0.622642 5:-0.0821918 6:-1 8:-0.29771 9:1 10:0.0967742 12:-1 13:-1 -1 1:-0.5 2:1 3:-0.333333 4:-0.698113 5:-0.502283 6:-1 7:-1 8:0.251908 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.291667 2:-1 3:1 4:0.207547 5:-0.182648 6:-1 7:1 8:0.374046 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.0416667 2:-1 3:0.333333 4:-0.226415 5:-0.187215 6:1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.458333 2:1 3:-0.333333 4:-0.509434 5:-0.228311 6:-1 7:-1 8:0.389313 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.166667 2:-1 3:-0.333333 4:-0.245283 5:-0.3379 6:-1 7:-1 8:0.389313 9:-1 10:-1 12:-1 13:-1 +1 1:-0.291667 2:1 3:1 4:-0.509434 5:-0.438356 6:-1 7:1 8:0.114504 9:-1 10:-0.741935 11:-1 12:-1 13:1 +1 1:0.125 2:-1 3:1 4:1 5:-0.260274 6:1 7:1 8:-0.0534351 9:1 10:0.290323 11:1 12:0.333333 13:1 -1 1:0.541667 2:-1 3:-1 4:0.0566038 5:-0.543379 6:-1 7:-1 8:-0.343511 9:-1 10:-0.16129 11:1 12:-1 13:-1 +1 1:0.125 2:1 3:1 4:-0.320755 5:-0.283105 6:1 7:1 8:-0.51145 9:1 10:-0.483871 11:1 12:-1 13:1 +1 1:-0.166667 2:1 3:0.333333 4:-0.509434 5:-0.716895 6:-1 7:-1 8:0.0381679 9:-1 10:-0.354839 12:1 13:1 +1 1:0.0416667 2:1 3:1 4:-0.471698 5:-0.269406 6:-1 7:1 8:-0.312977 9:1 10:0.0322581 12:0.333333 13:-1 +1 1:0.166667 2:1 3:1 4:0.0943396 5:-0.324201 6:-1 7:-1 8:-0.740458 9:1 10:-0.612903 12:-0.333333 13:1 -1 1:0.5 2:-1 3:0.333333 4:0.245283 5:0.0684932 6:-1 7:1 8:0.221374 9:-1 10:-0.741935 11:-1 12:-1 13:-1 -1 1:0.0416667 2:1 3:0.333333 4:-0.415094 5:-0.328767 6:-1 7:1 8:0.236641 9:-1 10:-0.83871 11:1 12:-0.333333 13:-1 -1 1:0.0416667 2:-1 3:0.333333 4:0.245283 5:-0.657534 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-0.333333 13:-1 +1 1:0.375 2:1 3:1 4:-0.509434 5:-0.356164 6:-1 7:-1 8:-0.572519 9:1 10:-0.419355 12:0.333333 13:1 -1 1:-0.0416667 2:-1 3:0.333333 4:-0.207547 5:-0.680365 6:-1 7:1 8:0.496183 9:-1 10:-0.967742 12:-1 13:-1 -1 1:-0.0416667 2:1 3:-0.333333 4:-0.245283 5:-0.657534 6:-1 7:-1 8:0.328244 9:-1 10:-0.741935 11:-1 12:-0.333333 13:-1 +1 1:0.291667 2:1 3:1 4:-0.566038 5:-0.525114 6:1 7:-1 8:0.358779 9:1 10:-0.548387 11:-1 12:0.333333 13:1 +1 1:0.416667 2:-1 3:1 4:-0.735849 5:-0.347032 6:-1 7:-1 8:0.496183 9:1 10:-0.419355 12:0.333333 13:-1 +1 1:0.541667 2:1 3:1 4:-0.660377 5:-0.607306 6:-1 7:1 8:-0.0687023 9:1 10:-0.967742 11:-1 12:-0.333333 13:-1 -1 1:-0.458333 2:1 3:1 4:-0.132075 5:-0.543379 6:-1 7:-1 8:0.633588 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.458333 2:1 3:1 4:-0.509434 5:-0.452055 6:-1 7:1 8:-0.618321 9:1 10:-0.290323 11:1 12:-0.333333 13:-1 -1 1:0.0416667 2:1 3:0.333333 4:0.0566038 5:-0.515982 6:-1 7:1 8:0.435115 9:-1 10:-0.483871 11:-1 12:-1 13:1 -1 1:-0.291667 2:-1 3:0.333333 4:-0.0943396 5:-0.767123 6:-1 7:1 8:0.358779 9:1 10:-0.548387 11:1 12:-1 13:-1 -1 1:0.583333 2:-1 3:0.333333 4:0.0943396 5:-0.310502 6:-1 7:-1 8:0.541985 9:-1 10:-1 11:-1 12:-0.333333 13:-1 +1 1:0.125 2:1 3:1 4:-0.415094 5:-0.438356 6:1 7:1 8:0.114504 9:1 10:-0.612903 12:-0.333333 13:-1 -1 1:-0.791667 2:-1 3:-0.333333 4:-0.54717 5:-0.616438 6:-1 7:-1 8:0.847328 9:-1 10:-0.774194 11:-1 12:-1 13:-1 -1 1:0.166667 2:1 3:1 4:-0.283019 5:-0.630137 6:-1 7:-1 8:0.480916 9:1 10:-1 11:-1 12:-1 13:1 +1 1:0.458333 2:1 3:1 4:-0.0377358 5:-0.607306 6:-1 7:1 8:-0.0687023 9:-1 10:-0.354839 12:0.333333 13:0.5 -1 1:0.25 2:1 3:1 4:-0.169811 5:-0.3379 6:-1 7:1 8:0.694656 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.125 2:1 3:0.333333 4:-0.132075 5:-0.511416 6:-1 7:-1 8:0.40458 9:-1 10:-0.806452 12:-0.333333 13:1 -1 1:-0.0833333 2:1 3:-1 4:-0.415094 5:-0.60274 6:-1 7:1 8:-0.175573 9:1 10:-0.548387 11:-1 12:-0.333333 13:-1 +1 1:0.0416667 2:1 3:-0.333333 4:0.849057 5:-0.283105 6:-1 7:1 8:0.89313 9:-1 10:-1 11:-1 12:-0.333333 13:1 +1 2:1 3:1 4:-0.45283 5:-0.287671 6:-1 7:-1 8:-0.633588 9:1 10:-0.354839 12:0.333333 13:1 +1 1:-0.0416667 2:1 3:1 4:-0.660377 5:-0.525114 6:-1 7:-1 8:0.358779 9:-1 10:-1 11:-1 12:-0.333333 13:-1 +1 1:-0.541667 2:1 3:1 4:-0.698113 5:-0.812785 6:-1 7:1 8:-0.343511 9:1 10:-0.354839 12:-1 13:1 +1 1:0.208333 2:1 3:0.333333 4:-0.283019 5:-0.552511 6:-1 7:1 8:0.557252 9:-1 10:0.0322581 11:-1 12:0.333333 13:1 -1 1:-0.5 2:-1 3:0.333333 4:-0.660377 5:-0.351598 6:-1 7:1 8:0.541985 9:1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.5 2:1 3:0.333333 4:-0.660377 5:-0.43379 6:-1 7:-1 8:0.648855 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.125 2:-1 3:0.333333 4:-0.509434 5:-0.575342 6:-1 7:-1 8:0.328244 9:-1 10:-0.483871 12:-1 13:-1 -1 1:0.0416667 2:-1 3:0.333333 4:-0.735849 5:-0.356164 6:-1 7:1 8:0.465649 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.458333 2:-1 3:1 4:-0.320755 5:-0.191781 6:-1 7:-1 8:-0.221374 9:-1 10:-0.354839 12:0.333333 13:-1 -1 1:-0.0833333 2:-1 3:0.333333 4:-0.320755 5:-0.406393 6:-1 7:1 8:0.19084 9:-1 10:-0.83871 11:-1 12:-1 13:-1 -1 1:-0.291667 2:-1 3:-0.333333 4:-0.792453 5:-0.643836 6:-1 7:-1 8:0.541985 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.0833333 2:1 3:1 4:-0.132075 5:-0.584475 6:-1 7:-1 8:-0.389313 9:1 10:0.806452 11:1 12:-1 13:1 -1 1:-0.333333 2:1 3:-0.333333 4:-0.358491 5:-0.16895 6:-1 7:1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:0.125 2:1 3:-1 4:-0.509434 5:-0.694064 6:-1 7:1 8:0.389313 9:-1 10:-0.387097 12:-1 13:1 +1 1:0.541667 2:-1 3:1 4:0.584906 5:-0.534247 6:1 7:-1 8:0.435115 9:1 10:-0.677419 12:0.333333 13:1 +1 1:-0.625 2:1 3:-1 4:-0.509434 5:-0.520548 6:-1 7:-1 8:0.694656 9:1 10:0.225806 12:-1 13:1 +1 1:0.375 2:-1 3:1 4:0.0566038 5:-0.461187 6:-1 7:-1 8:0.267176 9:1 10:-0.548387 12:-1 13:-1 -1 1:0.0833333 2:1 3:-0.333333 4:-0.320755 5:-0.378995 6:-1 7:-1 8:0.282443 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.208333 2:1 3:1 4:-0.358491 5:-0.392694 6:-1 7:1 8:-0.0992366 9:1 10:-0.0322581 12:0.333333 13:1 -1 1:-0.416667 2:1 3:1 4:-0.698113 5:-0.611872 6:-1 7:-1 8:0.374046 9:-1 10:-1 11:-1 12:-1 13:1 -1 1:0.458333 2:-1 3:1 4:0.622642 5:-0.0913242 6:-1 7:-1 8:0.267176 9:1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.125 2:-1 3:1 4:-0.698113 5:-0.415525 6:-1 7:1 8:0.343511 9:-1 10:-1 11:-1 12:-1 13:-1 -1 2:1 3:0.333333 4:-0.320755 5:-0.675799 6:1 7:1 8:0.236641 9:-1 10:-0.612903 11:1 12:-1 13:-1 -1 1:-0.333333 2:-1 3:1 4:-0.169811 5:-0.497717 6:-1 7:1 8:0.236641 9:1 10:-0.935484 12:-1 13:-1 +1 1:0.5 2:1 3:-1 4:-0.169811 5:-0.287671 6:1 7:1 8:0.572519 9:-1 10:-0.548387 12:-0.333333 13:-1 -1 1:0.666667 2:1 3:-1 4:0.245283 5:-0.506849 6:1 7:1 8:-0.0839695 9:-1 10:-0.967742 12:-0.333333 13:-1 +1 1:0.666667 2:1 3:0.333333 4:-0.132075 5:-0.415525 6:-1 7:1 8:0.145038 9:-1 10:-0.354839 12:1 13:1 +1 1:0.583333 2:1 3:1 4:-0.886792 5:-0.210046 6:-1 7:1 8:-0.175573 9:1 10:-0.709677 12:0.333333 13:-1 -1 1:0.625 2:-1 3:0.333333 4:-0.509434 5:-0.611872 6:-1 7:1 8:-0.328244 9:-1 10:-0.516129 12:-1 13:-1 -1 1:-0.791667 2:1 3:-1 4:-0.54717 5:-0.744292 6:-1 7:1 8:0.572519 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.375 2:-1 3:1 4:-0.169811 5:-0.232877 6:1 7:-1 8:-0.465649 9:-1 10:-0.387097 12:1 13:-1 +1 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.214612 6:-1 7:-1 8:-0.221374 9:1 10:0.354839 12:1 13:1 +1 1:-0.291667 2:1 3:0.333333 4:0.0566038 5:-0.520548 6:-1 7:-1 8:0.160305 9:-1 10:0.16129 12:-1 13:-1 +1 1:0.583333 2:1 3:1 4:-0.415094 5:-0.415525 6:1 7:-1 8:0.40458 9:-1 10:-0.935484 12:0.333333 13:1 -1 1:-0.125 2:1 3:0.333333 4:-0.339623 5:-0.680365 6:-1 7:-1 8:0.40458 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.458333 2:1 3:0.333333 4:-0.509434 5:-0.479452 6:1 7:-1 8:0.877863 9:-1 10:-0.741935 11:1 12:-1 13:1 +1 1:0.125 2:-1 3:1 4:-0.245283 5:0.292237 6:-1 7:1 8:0.206107 9:1 10:-0.387097 12:0.333333 13:1 +1 1:-0.5 2:1 3:1 4:-0.698113 5:-0.789954 6:-1 7:1 8:0.328244 9:-1 10:-1 11:-1 12:-1 13:1 -1 1:-0.458333 2:-1 3:1 4:-0.849057 5:-0.365297 6:-1 7:1 8:-0.221374 9:-1 10:-0.806452 12:-1 13:-1 -1 2:1 3:0.333333 4:-0.320755 5:-0.452055 6:1 7:1 8:0.557252 9:-1 10:-1 11:-1 12:1 13:-1 -1 1:-0.416667 2:1 3:0.333333 4:-0.320755 5:-0.136986 6:-1 7:-1 8:0.389313 9:-1 10:-0.387097 11:-1 12:-0.333333 13:-1 +1 1:0.125 2:1 3:1 4:-0.283019 5:-0.73516 6:-1 7:1 8:-0.480916 9:1 10:-0.322581 12:-0.333333 13:0.5 -1 1:-0.0416667 2:1 3:1 4:-0.735849 5:-0.511416 6:1 7:-1 8:0.160305 9:-1 10:-0.967742 11:-1 12:1 13:1 -1 1:0.375 2:-1 3:1 4:-0.132075 5:0.223744 6:-1 7:1 8:0.312977 9:-1 10:-0.612903 12:-1 13:-1 +1 1:0.708333 2:1 3:0.333333 4:0.245283 5:-0.347032 6:-1 7:-1 8:-0.374046 9:1 10:-0.0645161 12:-0.333333 13:1 -1 1:0.0416667 2:1 3:1 4:-0.132075 5:-0.484018 6:-1 7:-1 8:0.358779 9:-1 10:-0.612903 11:-1 12:-1 13:-1 +1 1:0.708333 2:1 3:1 4:-0.0377358 5:-0.780822 6:-1 7:-1 8:-0.175573 9:1 10:-0.16129 11:1 12:-1 13:1 -1 1:0.0416667 2:1 3:-0.333333 4:-0.735849 5:-0.164384 6:-1 7:-1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:1 +1 1:-0.75 2:1 3:1 4:-0.396226 5:-0.287671 6:-1 7:1 8:0.29771 9:1 10:-1 11:-1 12:-1 13:1 -1 1:-0.208333 2:1 3:0.333333 4:-0.433962 5:-0.410959 6:1 7:-1 8:0.587786 9:-1 10:-1 11:-1 12:0.333333 13:-1 -1 1:0.0833333 2:-1 3:-0.333333 4:-0.226415 5:-0.43379 6:-1 7:1 8:0.374046 9:-1 10:-0.548387 12:-1 13:-1 -1 1:0.208333 2:-1 3:1 4:-0.886792 5:-0.442922 6:-1 7:1 8:-0.221374 9:-1 10:-0.677419 12:-1 13:-1 -1 1:0.0416667 2:-1 3:0.333333 4:-0.698113 5:-0.598174 6:-1 7:-1 8:0.328244 9:-1 10:-0.483871 12:-1 13:-1 -1 1:0.666667 2:-1 3:-1 4:-0.132075 5:-0.484018 6:-1 7:-1 8:0.221374 9:-1 10:-0.419355 11:-1 12:0.333333 13:-1 +1 1:1 2:1 3:1 4:-0.415094 5:-0.187215 6:-1 7:1 8:0.389313 9:1 10:-1 11:-1 12:1 13:-1 -1 1:0.625 2:1 3:0.333333 4:-0.54717 5:-0.310502 6:-1 7:-1 8:0.221374 9:-1 10:-0.677419 11:-1 12:-0.333333 13:1 +1 1:0.208333 2:1 3:1 4:-0.415094 5:-0.205479 6:-1 7:1 8:0.526718 9:-1 10:-1 11:-1 12:0.333333 13:1 +1 1:0.291667 2:1 3:1 4:-0.415094 5:-0.39726 6:-1 7:1 8:0.0687023 9:1 10:-0.0967742 12:-0.333333 13:1 +1 1:-0.0833333 2:1 3:1 4:-0.132075 5:-0.210046 6:-1 7:-1 8:0.557252 9:1 10:-0.483871 11:-1 12:-1 13:1 +1 1:0.0833333 2:1 3:1 4:0.245283 5:-0.255708 6:-1 7:1 8:0.129771 9:1 10:-0.741935 12:-0.333333 13:1 -1 1:-0.0416667 2:1 3:-1 4:0.0943396 5:-0.214612 6:1 7:-1 8:0.633588 9:-1 10:-0.612903 12:-1 13:1 -1 1:0.291667 2:-1 3:0.333333 4:-0.849057 5:-0.123288 6:-1 7:-1 8:0.358779 9:-1 10:-1 11:-1 12:-0.333333 13:-1 -1 1:0.208333 2:1 3:0.333333 4:-0.792453 5:-0.479452 6:-1 7:1 8:0.267176 9:1 10:-0.806452 12:-1 13:1 +1 1:0.458333 2:1 3:0.333333 4:-0.415094 5:-0.164384 6:-1 7:-1 8:-0.0839695 9:1 10:-0.419355 12:-1 13:1 -1 1:-0.666667 2:1 3:0.333333 4:-0.320755 5:-0.43379 6:-1 7:-1 8:0.770992 9:-1 10:0.129032 11:1 12:-1 13:-1 +1 1:0.25 2:1 3:-1 4:0.433962 5:-0.260274 6:-1 7:1 8:0.343511 9:-1 10:-0.935484 12:-1 13:1 -1 1:-0.0833333 2:1 3:0.333333 4:-0.415094 5:-0.456621 6:1 7:1 8:0.450382 9:-1 10:-0.225806 12:-1 13:-1 -1 1:-0.416667 2:-1 3:0.333333 4:-0.471698 5:-0.60274 6:-1 7:-1 8:0.435115 9:-1 10:-0.935484 12:-1 13:-1 +1 1:0.208333 2:1 3:1 4:-0.358491 5:-0.589041 6:-1 7:1 8:-0.0839695 9:1 10:-0.290323 12:1 13:1 -1 1:-1 2:1 3:-0.333333 4:-0.320755 5:-0.643836 6:-1 7:1 8:1 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.5 2:-1 3:-0.333333 4:-0.320755 5:-0.643836 6:-1 7:1 8:0.541985 9:-1 10:-0.548387 11:-1 12:-1 13:-1 -1 1:0.416667 2:-1 3:0.333333 4:-0.226415 5:-0.424658 6:-1 7:1 8:0.541985 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.0833333 2:1 3:0.333333 4:-1 5:-0.538813 6:-1 7:-1 8:0.267176 9:1 10:-1 11:-1 12:-0.333333 13:1 -1 1:0.0416667 2:1 3:0.333333 4:-0.509434 5:-0.39726 6:-1 7:1 8:0.160305 9:-1 10:-0.870968 12:-1 13:1 -1 1:-0.375 2:1 3:-0.333333 4:-0.509434 5:-0.570776 6:-1 7:-1 8:0.51145 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.0416667 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:-1 8:-0.160305 9:1 10:-0.0967742 12:-0.333333 13:1 +1 1:0.5 2:1 3:1 4:-0.226415 5:-0.415525 6:-1 7:1 8:-0.145038 9:-1 10:-0.0967742 12:-0.333333 13:1 -1 1:0.166667 2:1 3:0.333333 4:0.0566038 5:-0.808219 6:-1 7:-1 8:0.572519 9:-1 10:-0.483871 11:-1 12:-1 13:-1 +1 1:0.416667 2:1 3:1 4:-0.320755 5:-0.0684932 6:1 7:1 8:-0.0687023 9:1 10:-0.419355 11:-1 12:1 13:1 -1 1:-0.75 2:-1 3:1 4:-0.169811 5:-0.739726 6:-1 7:-1 8:0.694656 9:-1 10:-0.548387 11:-1 12:-1 13:-1 -1 1:-0.5 2:1 3:-0.333333 4:-0.226415 5:-0.648402 6:-1 7:-1 8:-0.0687023 9:-1 10:-1 12:-1 13:0.5 +1 1:0.375 2:-1 3:0.333333 4:-0.320755 5:-0.374429 6:-1 7:-1 8:-0.603053 9:-1 10:-0.612903 12:-0.333333 13:1 +1 1:-0.416667 2:-1 3:1 4:-0.283019 5:-0.0182648 6:1 7:1 8:-0.00763359 9:1 10:-0.0322581 12:-1 13:1 -1 1:0.208333 2:-1 3:-1 4:0.0566038 5:-0.283105 6:1 7:1 8:0.389313 9:-1 10:-0.677419 11:-1 12:-1 13:-1 -1 1:-0.0416667 2:1 3:-1 4:-0.54717 5:-0.726027 6:-1 7:1 8:0.816794 9:-1 10:-1 12:-1 13:0.5 +1 1:0.333333 2:-1 3:1 4:-0.0377358 5:-0.173516 6:-1 7:1 8:0.145038 9:1 10:-0.677419 12:-1 13:1 +1 1:-0.583333 2:1 3:1 4:-0.54717 5:-0.575342 6:-1 7:-1 8:0.0534351 9:-1 10:-0.612903 12:-1 13:1 -1 1:-0.333333 2:1 3:1 4:-0.603774 5:-0.388128 6:-1 7:1 8:0.740458 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.0416667 2:1 3:1 4:-0.358491 5:-0.410959 6:-1 7:-1 8:0.374046 9:1 10:-1 11:-1 12:-0.333333 13:1 -1 1:0.375 2:1 3:0.333333 4:-0.320755 5:-0.520548 6:-1 7:-1 8:0.145038 9:-1 10:-0.419355 12:1 13:1 +1 1:0.375 2:-1 3:1 4:0.245283 5:-0.826484 6:-1 7:1 8:0.129771 9:-1 10:1 11:1 12:1 13:1 -1 2:-1 3:1 4:-0.169811 5:-0.506849 6:-1 7:1 8:0.358779 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.416667 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:1 8:-0.251908 9:1 10:-0.193548 12:-1 13:1 -1 1:-0.25 2:1 3:0.333333 4:-0.169811 5:-0.401826 6:-1 7:1 8:0.29771 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.0416667 2:1 3:-0.333333 4:-0.509434 5:-0.0913242 6:-1 7:-1 8:0.541985 9:-1 10:-0.935484 11:-1 12:-1 13:-1 +1 1:0.625 2:1 3:0.333333 4:0.622642 5:-0.324201 6:1 7:1 8:0.206107 9:1 10:-0.483871 12:-1 13:1 -1 1:-0.583333 2:1 3:0.333333 4:-0.132075 5:-0.109589 6:-1 7:1 8:0.694656 9:-1 10:-1 11:-1 12:-1 13:-1 -1 2:-1 3:1 4:-0.320755 5:-0.369863 6:-1 7:1 8:0.0992366 9:-1 10:-0.870968 12:-1 13:-1 +1 1:0.375 2:-1 3:1 4:-0.132075 5:-0.351598 6:-1 7:1 8:0.358779 9:-1 10:0.16129 11:1 12:0.333333 13:-1 -1 1:-0.0833333 2:-1 3:0.333333 4:-0.132075 5:-0.16895 6:-1 7:1 8:0.0839695 9:-1 10:-0.516129 11:-1 12:-0.333333 13:-1 +1 1:0.291667 2:1 3:1 4:-0.320755 5:-0.420091 6:-1 7:-1 8:0.114504 9:1 10:-0.548387 11:-1 12:-0.333333 13:1 +1 1:0.5 2:1 3:1 4:-0.698113 5:-0.442922 6:-1 7:1 8:0.328244 9:-1 10:-0.806452 11:-1 12:0.333333 13:0.5 -1 1:0.5 2:-1 3:0.333333 4:0.150943 5:-0.347032 6:-1 7:-1 8:0.175573 9:-1 10:-0.741935 11:-1 12:-1 13:-1 +1 1:0.291667 2:1 3:0.333333 4:-0.132075 5:-0.730594 6:-1 7:1 8:0.282443 9:-1 10:-0.0322581 12:-1 13:-1 +1 1:0.291667 2:1 3:1 4:-0.0377358 5:-0.287671 6:-1 7:1 8:0.0839695 9:1 10:-0.0967742 12:0.333333 13:1 +1 1:0.0416667 2:1 3:1 4:-0.509434 5:-0.716895 6:-1 7:-1 8:-0.358779 9:-1 10:-0.548387 12:-0.333333 13:1 -1 1:-0.375 2:1 3:-0.333333 4:-0.320755 5:-0.575342 6:-1 7:1 8:0.78626 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:-0.375 2:1 3:1 4:-0.660377 5:-0.251142 6:-1 7:1 8:0.251908 9:-1 10:-1 11:-1 12:-0.333333 13:-1 -1 1:-0.0833333 2:1 3:0.333333 4:-0.698113 5:-0.776256 6:-1 7:-1 8:-0.206107 9:-1 10:-0.806452 11:-1 12:-1 13:-1 -1 1:0.25 2:1 3:0.333333 4:0.0566038 5:-0.607306 6:1 7:-1 8:0.312977 9:-1 10:-0.483871 11:-1 12:-1 13:-1 -1 1:0.75 2:-1 3:-0.333333 4:0.245283 5:-0.196347 6:-1 7:-1 8:0.389313 9:-1 10:-0.870968 11:-1 12:0.333333 13:-1 -1 1:0.333333 2:1 3:0.333333 4:0.0566038 5:-0.465753 6:1 7:-1 8:0.00763359 9:1 10:-0.677419 12:-1 13:-1 +1 1:0.0833333 2:1 3:1 4:-0.283019 5:0.0365297 6:-1 7:-1 8:-0.0687023 9:1 10:-0.612903 12:-0.333333 13:1 +1 1:0.458333 2:1 3:0.333333 4:-0.132075 5:-0.0456621 6:-1 7:-1 8:0.328244 9:-1 10:-1 11:-1 12:-1 13:-1 -1 1:-0.416667 2:1 3:1 4:0.0566038 5:-0.447489 6:-1 7:-1 8:0.526718 9:-1 10:-0.516129 11:-1 12:-1 13:-1 -1 1:0.208333 2:-1 3:0.333333 4:-0.509434 5:-0.0228311 6:-1 7:-1 8:0.541985 9:-1 10:-1 11:-1 12:-1 13:-1 +1 1:0.291667 2:1 3:1 4:-0.320755 5:-0.634703 6:-1 7:1 8:-0.0687023 9:1 10:-0.225806 12:0.333333 13:1 +1 1:0.208333 2:1 3:-0.333333 4:-0.509434 5:-0.278539 6:-1 7:1 8:0.358779 9:-1 10:-0.419355 12:-1 13:-1 -1 1:-0.166667 2:1 3:-0.333333 4:-0.320755 5:-0.360731 6:-1 7:-1 8:0.526718 9:-1 10:-0.806452 11:-1 12:-1 13:-1 +1 1:-0.208333 2:1 3:-0.333333 4:-0.698113 5:-0.52968 6:-1 7:-1 8:0.480916 9:-1 10:-0.677419 11:1 12:-1 13:1 -1 1:-0.0416667 2:1 3:0.333333 4:0.471698 5:-0.666667 6:1 7:-1 8:0.389313 9:-1 10:-0.83871 11:-1 12:-1 13:1 -1 1:-0.375 2:1 3:-0.333333 4:-0.509434 5:-0.374429 6:-1 7:-1 8:0.557252 9:-1 10:-1 11:-1 12:-1 13:1 -1 1:0.125 2:-1 3:-0.333333 4:-0.132075 5:-0.232877 6:-1 7:1 8:0.251908 9:-1 10:-0.580645 12:-1 13:-1 -1 1:0.166667 2:1 3:1 4:-0.132075 5:-0.69863 6:-1 7:-1 8:0.175573 9:-1 10:-0.870968 12:-1 13:0.5 +1 1:0.583333 2:1 3:1 4:0.245283 5:-0.269406 6:-1 7:1 8:-0.435115 9:1 10:-0.516129 12:1 13:-1 liblinear-1.8+dfsg/linear.cpp000066400000000000000000001335221154714501200162260ustar00rootroot00000000000000#include #include #include #include #include #include "linear.h" #include "tron.h" typedef signed char schar; template static inline void swap(T& x, T& y) { T t=x; x=y; y=t; } #ifndef min template static inline T min(T x,T y) { return (x static inline T max(T x,T y) { return (x>y)?x:y; } #endif template static inline void clone(T*& dst, S* src, int n) { dst = new T[n]; memcpy((void *)dst,(void *)src,sizeof(T)*n); } #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL static void print_string_stdout(const char *s) { fputs(s,stdout); fflush(stdout); } static void (*liblinear_print_string) (const char *) = &print_string_stdout; #if 1 static void info(const char *fmt,...) { char buf[BUFSIZ]; va_list ap; va_start(ap,fmt); vsprintf(buf,fmt,ap); va_end(ap); (*liblinear_print_string)(buf); } #else static void info(const char *fmt,...) {} #endif class l2r_lr_fun : public function { public: l2r_lr_fun(const problem *prob, double Cp, double Cn); ~l2r_lr_fun(); double fun(double *w); void grad(double *w, double *g); void Hv(double *s, double *Hs); int get_nr_variable(void); private: void Xv(double *v, double *Xv); void XTv(double *v, double *XTv); double *C; double *z; double *D; const problem *prob; }; l2r_lr_fun::l2r_lr_fun(const problem *prob, double Cp, double Cn) { int i; int l=prob->l; int *y=prob->y; this->prob = prob; z = new double[l]; D = new double[l]; C = new double[l]; for (i=0; iy; int l=prob->l; int w_size=get_nr_variable(); Xv(w, z); for(i=0;i= 0) f += C[i]*log(1 + exp(-yz)); else f += C[i]*(-yz+log(1 + exp(yz))); } f = 2*f; for(i=0;iy; int l=prob->l; int w_size=get_nr_variable(); for(i=0;in; } void l2r_lr_fun::Hv(double *s, double *Hs) { int i; int l=prob->l; int w_size=get_nr_variable(); double *wa = new double[l]; Xv(s, wa); for(i=0;il; feature_node **x=prob->x; for(i=0;iindex!=-1) { Xv[i]+=v[s->index-1]*s->value; s++; } } } void l2r_lr_fun::XTv(double *v, double *XTv) { int i; int l=prob->l; int w_size=get_nr_variable(); feature_node **x=prob->x; for(i=0;iindex!=-1) { XTv[s->index-1]+=v[i]*s->value; s++; } } } class l2r_l2_svc_fun : public function { public: l2r_l2_svc_fun(const problem *prob, double Cp, double Cn); ~l2r_l2_svc_fun(); double fun(double *w); void grad(double *w, double *g); void Hv(double *s, double *Hs); int get_nr_variable(void); private: void Xv(double *v, double *Xv); void subXv(double *v, double *Xv); void subXTv(double *v, double *XTv); double *C; double *z; double *D; int *I; int sizeI; const problem *prob; }; l2r_l2_svc_fun::l2r_l2_svc_fun(const problem *prob, double Cp, double Cn) { int i; int l=prob->l; int *y=prob->y; this->prob = prob; z = new double[l]; D = new double[l]; C = new double[l]; I = new int[l]; for (i=0; iy; int l=prob->l; int w_size=get_nr_variable(); Xv(w, z); for(i=0;i 0) f += C[i]*d*d; } f = 2*f; for(i=0;iy; int l=prob->l; int w_size=get_nr_variable(); sizeI = 0; for (i=0;in; } void l2r_l2_svc_fun::Hv(double *s, double *Hs) { int i; int l=prob->l; int w_size=get_nr_variable(); double *wa = new double[l]; subXv(s, wa); for(i=0;il; feature_node **x=prob->x; for(i=0;iindex!=-1) { Xv[i]+=v[s->index-1]*s->value; s++; } } } void l2r_l2_svc_fun::subXv(double *v, double *Xv) { int i; feature_node **x=prob->x; for(i=0;iindex!=-1) { Xv[i]+=v[s->index-1]*s->value; s++; } } } void l2r_l2_svc_fun::subXTv(double *v, double *XTv) { int i; int w_size=get_nr_variable(); feature_node **x=prob->x; for(i=0;iindex!=-1) { XTv[s->index-1]+=v[i]*s->value; s++; } } } // A coordinate descent algorithm for // multi-class support vector machines by Crammer and Singer // // min_{\alpha} 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i // s.t. \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i // // where e^m_i = 0 if y_i = m, // e^m_i = 1 if y_i != m, // C^m_i = C if m = y_i, // C^m_i = 0 if m != y_i, // and w_m(\alpha) = \sum_i \alpha^m_i x_i // // Given: // x, y, C // eps is the stopping tolerance // // solution will be put in w // // See Appendix of LIBLINEAR paper, Fan et al. (2008) #define GETI(i) (prob->y[i]) // To support weights for instances, use GETI(i) (i) class Solver_MCSVM_CS { public: Solver_MCSVM_CS(const problem *prob, int nr_class, double *C, double eps=0.1, int max_iter=100000); ~Solver_MCSVM_CS(); void Solve(double *w); private: void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new); bool be_shrunk(int i, int m, int yi, double alpha_i, double minG); double *B, *C, *G; int w_size, l; int nr_class; int max_iter; double eps; const problem *prob; }; Solver_MCSVM_CS::Solver_MCSVM_CS(const problem *prob, int nr_class, double *weighted_C, double eps, int max_iter) { this->w_size = prob->n; this->l = prob->l; this->nr_class = nr_class; this->eps = eps; this->max_iter = max_iter; this->prob = prob; this->B = new double[nr_class]; this->G = new double[nr_class]; this->C = weighted_C; } Solver_MCSVM_CS::~Solver_MCSVM_CS() { delete[] B; delete[] G; } int compare_double(const void *a, const void *b) { if(*(double *)a > *(double *)b) return -1; if(*(double *)a < *(double *)b) return 1; return 0; } void Solver_MCSVM_CS::solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new) { int r; double *D; clone(D, B, active_i); if(yi < active_i) D[yi] += A_i*C_yi; qsort(D, active_i, sizeof(double), compare_double); double beta = D[0] - A_i*C_yi; for(r=1;rx[i]; QD[i] = 0; while(xi->index != -1) { QD[i] += (xi->value)*(xi->value); xi++; } active_size_i[i] = nr_class; y_index[i] = prob->y[i]; index[i] = i; } while(iter < max_iter) { double stopping = -INF; for(i=0;i 0) { for(m=0;mx[i]; while(xi->index!= -1) { double *w_i = &w[(xi->index-1)*nr_class]; for(m=0;mvalue); xi++; } double minG = INF; double maxG = -INF; for(m=0;m maxG) maxG = G[m]; } if(y_index[i] < active_size_i[i]) if(alpha_i[prob->y[i]] < C[GETI(i)] && G[y_index[i]] < minG) minG = G[y_index[i]]; for(m=0;mm) { if(!be_shrunk(i, active_size_i[i], y_index[i], alpha_i[alpha_index_i[active_size_i[i]]], minG)) { swap(alpha_index_i[m], alpha_index_i[active_size_i[i]]); swap(G[m], G[active_size_i[i]]); if(y_index[i] == active_size_i[i]) y_index[i] = m; else if(y_index[i] == m) y_index[i] = active_size_i[i]; break; } active_size_i[i]--; } } } if(active_size_i[i] <= 1) { active_size--; swap(index[s], index[active_size]); s--; continue; } if(maxG-minG <= 1e-12) continue; else stopping = max(maxG - minG, stopping); for(m=0;m= 1e-12) { d_ind[nz_d] = alpha_index_i[m]; d_val[nz_d] = d; nz_d++; } } xi = prob->x[i]; while(xi->index != -1) { double *w_i = &w[(xi->index-1)*nr_class]; for(m=0;mvalue; xi++; } } } iter++; if(iter % 10 == 0) { info("."); } if(stopping < eps_shrink) { if(stopping < eps && start_from_all == true) break; else { active_size = l; for(i=0;i= max_iter) info("\nWARNING: reaching max number of iterations\n"); // calculate objective value double v = 0; int nSV = 0; for(i=0;i 0) nSV++; } for(i=0;iy[i]]; info("Objective value = %lf\n",v); info("nSV = %d\n",nSV); delete [] alpha; delete [] alpha_new; delete [] index; delete [] QD; delete [] d_ind; delete [] d_val; delete [] alpha_index; delete [] y_index; delete [] active_size_i; } // A coordinate descent algorithm for // L1-loss and L2-loss SVM dual problems // // min_\alpha 0.5(\alpha^T (Q + D)\alpha) - e^T \alpha, // s.t. 0 <= alpha_i <= upper_bound_i, // // where Qij = yi yj xi^T xj and // D is a diagonal matrix // // In L1-SVM case: // upper_bound_i = Cp if y_i = 1 // upper_bound_i = Cn if y_i = -1 // D_ii = 0 // In L2-SVM case: // upper_bound_i = INF // D_ii = 1/(2*Cp) if y_i = 1 // D_ii = 1/(2*Cn) if y_i = -1 // // Given: // x, y, Cp, Cn // eps is the stopping tolerance // // solution will be put in w // // See Algorithm 3 of Hsieh et al., ICML 2008 #undef GETI #define GETI(i) (y[i]+1) // To support weights for instances, use GETI(i) (i) static void solve_l2r_l1l2_svc( const problem *prob, double *w, double eps, double Cp, double Cn, int solver_type) { int l = prob->l; int w_size = prob->n; int i, s, iter = 0; double C, d, G; double *QD = new double[l]; int max_iter = 1000; int *index = new int[l]; double *alpha = new double[l]; schar *y = new schar[l]; int active_size = l; // PG: projected gradient, for shrinking and stopping double PG; double PGmax_old = INF; double PGmin_old = -INF; double PGmax_new, PGmin_new; // default solver_type: L2R_L2LOSS_SVC_DUAL double diag[3] = {0.5/Cn, 0, 0.5/Cp}; double upper_bound[3] = {INF, 0, INF}; if(solver_type == L2R_L1LOSS_SVC_DUAL) { diag[0] = 0; diag[2] = 0; upper_bound[0] = Cn; upper_bound[2] = Cp; } for(i=0; iy[i] > 0) { y[i] = +1; } else { y[i] = -1; } QD[i] = diag[GETI(i)]; feature_node *xi = prob->x[i]; while (xi->index != -1) { QD[i] += (xi->value)*(xi->value); xi++; } index[i] = i; } while (iter < max_iter) { PGmax_new = -INF; PGmin_new = INF; for (i=0; ix[i]; while(xi->index!= -1) { G += w[xi->index-1]*(xi->value); xi++; } G = G*yi-1; C = upper_bound[GETI(i)]; G += alpha[i]*diag[GETI(i)]; PG = 0; if (alpha[i] == 0) { if (G > PGmax_old) { active_size--; swap(index[s], index[active_size]); s--; continue; } else if (G < 0) PG = G; } else if (alpha[i] == C) { if (G < PGmin_old) { active_size--; swap(index[s], index[active_size]); s--; continue; } else if (G > 0) PG = G; } else PG = G; PGmax_new = max(PGmax_new, PG); PGmin_new = min(PGmin_new, PG); if(fabs(PG) > 1.0e-12) { double alpha_old = alpha[i]; alpha[i] = min(max(alpha[i] - G/QD[i], 0.0), C); d = (alpha[i] - alpha_old)*yi; xi = prob->x[i]; while (xi->index != -1) { w[xi->index-1] += d*xi->value; xi++; } } } iter++; if(iter % 10 == 0) info("."); if(PGmax_new - PGmin_new <= eps) { if(active_size == l) break; else { active_size = l; info("*"); PGmax_old = INF; PGmin_old = -INF; continue; } } PGmax_old = PGmax_new; PGmin_old = PGmin_new; if (PGmax_old <= 0) PGmax_old = INF; if (PGmin_old >= 0) PGmin_old = -INF; } info("\noptimization finished, #iter = %d\n",iter); if (iter >= max_iter) info("\nWARNING: reaching max number of iterations\nUsing -s 2 may be faster (also see FAQ)\n\n"); // calculate objective value double v = 0; int nSV = 0; for(i=0; i 0) ++nSV; } info("Objective value = %lf\n",v/2); info("nSV = %d\n",nSV); delete [] QD; delete [] alpha; delete [] y; delete [] index; } // A coordinate descent algorithm for // the dual of L2-regularized logistic regression problems // // min_\alpha 0.5(\alpha^T Q \alpha) + \sum \alpha_i log (\alpha_i) + (upper_bound_i - alpha_i) log (upper_bound_i - alpha_i) , // s.t. 0 <= alpha_i <= upper_bound_i, // // where Qij = yi yj xi^T xj and // upper_bound_i = Cp if y_i = 1 // upper_bound_i = Cn if y_i = -1 // // Given: // x, y, Cp, Cn // eps is the stopping tolerance // // solution will be put in w // // See Algorithm 5 of Yu et al., MLJ 2010 #undef GETI #define GETI(i) (y[i]+1) // To support weights for instances, use GETI(i) (i) void solve_l2r_lr_dual(const problem *prob, double *w, double eps, double Cp, double Cn) { int l = prob->l; int w_size = prob->n; int i, s, iter = 0; double *xTx = new double[l]; int max_iter = 1000; int *index = new int[l]; double *alpha = new double[2*l]; // store alpha and C - alpha schar *y = new schar[l]; int max_inner_iter = 100; // for inner Newton double innereps = 1e-2; double innereps_min = min(1e-8, eps); double upper_bound[3] = {Cn, 0, Cp}; for(i=0; iy[i] > 0) { y[i] = +1; } else { y[i] = -1; } alpha[2*i] = min(0.001*upper_bound[GETI(i)], 1e-8); alpha[2*i+1] = upper_bound[GETI(i)] - alpha[2*i]; xTx[i] = 0; feature_node *xi = prob->x[i]; while (xi->index != -1) { xTx[i] += (xi->value)*(xi->value); w[xi->index-1] += y[i]*alpha[2*i]*xi->value; xi++; } index[i] = i; } while (iter < max_iter) { for (i=0; ix[i]; while (xi->index != -1) { ywTx += w[xi->index-1]*xi->value; xi++; } ywTx *= y[i]; double a = xisq, b = ywTx; // Decide to minimize g_1(z) or g_2(z) int ind1 = 2*i, ind2 = 2*i+1, sign = 1; if(0.5*a*(alpha[ind2]-alpha[ind1])+b < 0) { ind1 = 2*i+1; ind2 = 2*i; sign = -1; } // g_t(z) = z*log(z) + (C-z)*log(C-z) + 0.5a(z-alpha_old)^2 + sign*b(z-alpha_old) double alpha_old = alpha[ind1]; double z = alpha_old; if(C - z < 0.5 * C) z = 0.1*z; double gp = a*(z-alpha_old)+sign*b+log(z/(C-z)); Gmax = max(Gmax, fabs(gp)); // Newton method on the sub-problem const double eta = 0.1; // xi in the paper int inner_iter = 0; while (inner_iter <= max_inner_iter) { if(fabs(gp) < innereps) break; double gpp = a + C/(C-z)/z; double tmpz = z - gp/gpp; if(tmpz <= 0) z *= eta; else // tmpz in (0, C) z = tmpz; gp = a*(z-alpha_old)+sign*b+log(z/(C-z)); newton_iter++; inner_iter++; } if(inner_iter > 0) // update w { alpha[ind1] = z; alpha[ind2] = C-z; xi = prob->x[i]; while (xi->index != -1) { w[xi->index-1] += sign*(z-alpha_old)*yi*xi->value; xi++; } } } iter++; if(iter % 10 == 0) info("."); if(Gmax < eps) break; if(newton_iter <= l/10) innereps = max(innereps_min, 0.1*innereps); } info("\noptimization finished, #iter = %d\n",iter); if (iter >= max_iter) info("\nWARNING: reaching max number of iterations\nUsing -s 0 may be faster (also see FAQ)\n\n"); // calculate objective value double v = 0; for(i=0; il; int w_size = prob_col->n; int j, s, iter = 0; int max_iter = 1000; int active_size = w_size; int max_num_linesearch = 20; double sigma = 0.01; double d, G_loss, G, H; double Gmax_old = INF; double Gmax_new, Gnorm1_new; double Gnorm1_init; double d_old, d_diff; double loss_old, loss_new; double appxcond, cond; int *index = new int[w_size]; schar *y = new schar[l]; double *b = new double[l]; // b = 1-ywTx double *xj_sq = new double[w_size]; feature_node *x; double C[3] = {Cn,0,Cp}; for(j=0; jy[j] > 0) y[j] = 1; else y[j] = -1; } for(j=0; jx[j]; while(x->index != -1) { int ind = x->index-1; double val = x->value; x->value *= y[ind]; // x->value stores yi*xij xj_sq[j] += C[GETI(ind)]*val*val; x++; } } while(iter < max_iter) { Gmax_new = 0; Gnorm1_new = 0; for(j=0; jx[j]; while(x->index != -1) { int ind = x->index-1; if(b[ind] > 0) { double val = x->value; double tmp = C[GETI(ind)]*val; G_loss -= tmp*b[ind]; H += tmp*val; } x++; } G_loss *= 2; G = G_loss; H *= 2; H = max(H, 1e-12); double Gp = G+1; double Gn = G-1; double violation = 0; if(w[j] == 0) { if(Gp < 0) violation = -Gp; else if(Gn > 0) violation = Gn; else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) { active_size--; swap(index[s], index[active_size]); s--; continue; } } else if(w[j] > 0) violation = fabs(Gp); else violation = fabs(Gn); Gmax_new = max(Gmax_new, violation); Gnorm1_new += violation; // obtain Newton direction d if(Gp <= H*w[j]) d = -Gp/H; else if(Gn >= H*w[j]) d = -Gn/H; else d = -w[j]; if(fabs(d) < 1.0e-12) continue; double delta = fabs(w[j]+d)-fabs(w[j]) + G*d; d_old = 0; int num_linesearch; for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) { d_diff = d_old - d; cond = fabs(w[j]+d)-fabs(w[j]) - sigma*delta; appxcond = xj_sq[j]*d*d + G_loss*d + cond; if(appxcond <= 0) { x = prob_col->x[j]; while(x->index != -1) { b[x->index-1] += d_diff*x->value; x++; } break; } if(num_linesearch == 0) { loss_old = 0; loss_new = 0; x = prob_col->x[j]; while(x->index != -1) { int ind = x->index-1; if(b[ind] > 0) loss_old += C[GETI(ind)]*b[ind]*b[ind]; double b_new = b[ind] + d_diff*x->value; b[ind] = b_new; if(b_new > 0) loss_new += C[GETI(ind)]*b_new*b_new; x++; } } else { loss_new = 0; x = prob_col->x[j]; while(x->index != -1) { int ind = x->index-1; double b_new = b[ind] + d_diff*x->value; b[ind] = b_new; if(b_new > 0) loss_new += C[GETI(ind)]*b_new*b_new; x++; } } cond = cond + loss_new - loss_old; if(cond <= 0) break; else { d_old = d; d *= 0.5; delta *= 0.5; } } w[j] += d; // recompute b[] if line search takes too many steps if(num_linesearch >= max_num_linesearch) { info("#"); for(int i=0; ix[i]; while(x->index != -1) { b[x->index-1] -= w[i]*x->value; x++; } } } } if(iter == 0) Gnorm1_init = Gnorm1_new; iter++; if(iter % 10 == 0) info("."); if(Gnorm1_new <= eps*Gnorm1_init) { if(active_size == w_size) break; else { active_size = w_size; info("*"); Gmax_old = INF; continue; } } Gmax_old = Gmax_new; } info("\noptimization finished, #iter = %d\n", iter); if(iter >= max_iter) info("\nWARNING: reaching max number of iterations\n"); // calculate objective value double v = 0; int nnz = 0; for(j=0; jx[j]; while(x->index != -1) { x->value *= prob_col->y[x->index-1]; // restore x->value x++; } if(w[j] != 0) { v += fabs(w[j]); nnz++; } } for(j=0; j 0) v += C[GETI(j)]*b[j]*b[j]; info("Objective value = %lf\n", v); info("#nonzeros/#features = %d/%d\n", nnz, w_size); delete [] index; delete [] y; delete [] b; delete [] xj_sq; } // A coordinate descent algorithm for // L1-regularized logistic regression problems // // min_w \sum |wj| + C \sum log(1+exp(-yi w^T xi)), // // Given: // x, y, Cp, Cn // eps is the stopping tolerance // // solution will be put in w // // See Yuan et al. (2011) and appendix of LIBLINEAR paper, Fan et al. (2008) #undef GETI #define GETI(i) (y[i]+1) // To support weights for instances, use GETI(i) (i) static void solve_l1r_lr( const problem *prob_col, double *w, double eps, double Cp, double Cn) { int l = prob_col->l; int w_size = prob_col->n; int j, s, newton_iter=0, iter=0; int max_newton_iter = 100; int max_iter = 1000; int max_num_linesearch = 20; int active_size; int QP_active_size; double nu = 1e-12; double inner_eps = 1; double sigma = 0.01; double w_norm=0, w_norm_new; double z, G, H; double Gnorm1_init; double Gmax_old = INF; double Gmax_new, Gnorm1_new; double QP_Gmax_old = INF; double QP_Gmax_new, QP_Gnorm1_new; double delta, negsum_xTd, cond; int *index = new int[w_size]; schar *y = new schar[l]; double *Hdiag = new double[w_size]; double *Grad = new double[w_size]; double *wpd = new double[w_size]; double *xjneg_sum = new double[w_size]; double *xTd = new double[l]; double *exp_wTx = new double[l]; double *exp_wTx_new = new double[l]; double *tau = new double[l]; double *D = new double[l]; feature_node *x; double C[3] = {Cn,0,Cp}; for(j=0; jy[j] > 0) y[j] = 1; else y[j] = -1; // assume initial w is 0 exp_wTx[j] = 1; tau[j] = C[GETI(j)]*0.5; D[j] = C[GETI(j)]*0.25; } for(j=0; jx[j]; while(x->index != -1) { int ind = x->index-1; if(y[ind] == -1) xjneg_sum[j] += C[GETI(ind)]*x->value; x++; } } while(newton_iter < max_newton_iter) { Gmax_new = 0; Gnorm1_new = 0; active_size = w_size; for(s=0; sx[j]; while(x->index != -1) { int ind = x->index-1; Hdiag[j] += x->value*x->value*D[ind]; tmp += x->value*tau[ind]; x++; } Grad[j] = -tmp + xjneg_sum[j]; double Gp = Grad[j]+1; double Gn = Grad[j]-1; double violation = 0; if(w[j] == 0) { if(Gp < 0) violation = -Gp; else if(Gn > 0) violation = Gn; //outer-level shrinking else if(Gp>Gmax_old/l && Gn<-Gmax_old/l) { active_size--; swap(index[s], index[active_size]); s--; continue; } } else if(w[j] > 0) violation = fabs(Gp); else violation = fabs(Gn); Gmax_new = max(Gmax_new, violation); Gnorm1_new += violation; } if(newton_iter == 0) Gnorm1_init = Gnorm1_new; if(Gnorm1_new <= eps*Gnorm1_init) break; iter = 0; QP_Gmax_old = INF; QP_active_size = active_size; for(int i=0; ix[j]; G = Grad[j] + (wpd[j]-w[j])*nu; while(x->index != -1) { int ind = x->index-1; G += x->value*D[ind]*xTd[ind]; x++; } double Gp = G+1; double Gn = G-1; double violation = 0; if(wpd[j] == 0) { if(Gp < 0) violation = -Gp; else if(Gn > 0) violation = Gn; //inner-level shrinking else if(Gp>QP_Gmax_old/l && Gn<-QP_Gmax_old/l) { QP_active_size--; swap(index[s], index[QP_active_size]); s--; continue; } } else if(wpd[j] > 0) violation = fabs(Gp); else violation = fabs(Gn); QP_Gmax_new = max(QP_Gmax_new, violation); QP_Gnorm1_new += violation; // obtain solution of one-variable problem if(Gp <= H*wpd[j]) z = -Gp/H; else if(Gn >= H*wpd[j]) z = -Gn/H; else z = -wpd[j]; if(fabs(z) < 1.0e-12) continue; z = min(max(z,-10.0),10.0); wpd[j] += z; x = prob_col->x[j]; while(x->index != -1) { int ind = x->index-1; xTd[ind] += x->value*z; x++; } } iter++; if(QP_Gnorm1_new <= inner_eps*Gnorm1_init) { //inner stopping if(QP_active_size == active_size) break; //active set reactivation else { QP_active_size = active_size; QP_Gmax_old = INF; continue; } } QP_Gmax_old = QP_Gmax_new; } if(iter >= max_iter) info("WARNING: reaching max number of inner iterations\n"); delta = 0; w_norm_new = 0; for(j=0; j= max_num_linesearch) { for(int i=0; ix[i]; while(x->index != -1) { exp_wTx[x->index-1] += w[i]*x->value; x++; } } for(int i=0; i= max_newton_iter) info("WARNING: reaching max number of iterations\n"); // calculate objective value double v = 0; int nnz = 0; for(j=0; jl; int n = prob->n; int nnz = 0; int *col_ptr = new int[n+1]; feature_node *x_space; prob_col->l = l; prob_col->n = n; prob_col->y = new int[l]; prob_col->x = new feature_node*[n]; for(i=0; iy[i] = prob->y[i]; for(i=0; ix[i]; while(x->index != -1) { nnz++; col_ptr[x->index]++; x++; } } for(i=1; ix[i] = &x_space[col_ptr[i]]; for(i=0; ix[i]; while(x->index != -1) { int ind = x->index-1; x_space[col_ptr[ind]].index = i+1; // starts from 1 x_space[col_ptr[ind]].value = x->value; col_ptr[ind]++; x++; } } for(i=0; il; int max_nr_class = 16; int nr_class = 0; int *label = Malloc(int,max_nr_class); int *count = Malloc(int,max_nr_class); int *data_label = Malloc(int,l); int i; for(i=0;iy[i]; int j; for(j=0;jeps; int pos = 0; int neg = 0; for(int i=0;il;i++) if(prob->y[i]==+1) pos++; neg = prob->l - pos; function *fun_obj=NULL; switch(param->solver_type) { case L2R_LR: { fun_obj=new l2r_lr_fun(prob, Cp, Cn); TRON tron_obj(fun_obj, eps*min(pos,neg)/prob->l); tron_obj.set_print_string(liblinear_print_string); tron_obj.tron(w); delete fun_obj; break; } case L2R_L2LOSS_SVC: { fun_obj=new l2r_l2_svc_fun(prob, Cp, Cn); TRON tron_obj(fun_obj, eps*min(pos,neg)/prob->l); tron_obj.set_print_string(liblinear_print_string); tron_obj.tron(w); delete fun_obj; break; } case L2R_L2LOSS_SVC_DUAL: solve_l2r_l1l2_svc(prob, w, eps, Cp, Cn, L2R_L2LOSS_SVC_DUAL); break; case L2R_L1LOSS_SVC_DUAL: solve_l2r_l1l2_svc(prob, w, eps, Cp, Cn, L2R_L1LOSS_SVC_DUAL); break; case L1R_L2LOSS_SVC: { problem prob_col; feature_node *x_space = NULL; transpose(prob, &x_space ,&prob_col); solve_l1r_l2_svc(&prob_col, w, eps*min(pos,neg)/prob->l, Cp, Cn); delete [] prob_col.y; delete [] prob_col.x; delete [] x_space; break; } case L1R_LR: { problem prob_col; feature_node *x_space = NULL; transpose(prob, &x_space ,&prob_col); solve_l1r_lr(&prob_col, w, eps*min(pos,neg)/prob->l, Cp, Cn); delete [] prob_col.y; delete [] prob_col.x; delete [] x_space; break; } case L2R_LR_DUAL: solve_l2r_lr_dual(prob, w, eps, Cp, Cn); break; default: fprintf(stderr, "Error: unknown solver_type\n"); break; } } // // Interface functions // model* train(const problem *prob, const parameter *param) { int i,j; int l = prob->l; int n = prob->n; int w_size = prob->n; model *model_ = Malloc(model,1); if(prob->bias>=0) model_->nr_feature=n-1; else model_->nr_feature=n; model_->param = *param; model_->bias = prob->bias; int nr_class; int *label = NULL; int *start = NULL; int *count = NULL; int *perm = Malloc(int,l); // group training data of the same class group_classes(prob,&nr_class,&label,&start,&count,perm); model_->nr_class=nr_class; model_->label = Malloc(int,nr_class); for(i=0;ilabel[i] = label[i]; // calculate weighted C double *weighted_C = Malloc(double, nr_class); for(i=0;iC; for(i=0;inr_weight;i++) { for(j=0;jweight_label[i] == label[j]) break; if(j == nr_class) fprintf(stderr,"WARNING: class label %d specified in weight is not found\n", param->weight_label[i]); else weighted_C[j] *= param->weight[i]; } // constructing the subproblem feature_node **x = Malloc(feature_node *,l); for(i=0;ix[perm[i]]; int k; problem sub_prob; sub_prob.l = l; sub_prob.n = n; sub_prob.x = Malloc(feature_node *,sub_prob.l); sub_prob.y = Malloc(int,sub_prob.l); for(k=0; ksolver_type == MCSVM_CS) { model_->w=Malloc(double, n*nr_class); for(i=0;ieps); Solver.Solve(model_->w); } else { if(nr_class == 2) { model_->w=Malloc(double, w_size); int e0 = start[0]+count[0]; k=0; for(; kw[0], weighted_C[0], weighted_C[1]); } else { model_->w=Malloc(double, w_size*nr_class); double *w=Malloc(double, w_size); for(i=0;iC); for(int j=0;jw[j*nr_class+i] = w[j]; } free(w); } } free(x); free(label); free(start); free(count); free(perm); free(sub_prob.x); free(sub_prob.y); free(weighted_C); return model_; } void cross_validation(const problem *prob, const parameter *param, int nr_fold, int *target) { int i; int *fold_start = Malloc(int,nr_fold+1); int l = prob->l; int *perm = Malloc(int,l); for(i=0;ibias; subprob.n = prob->n; subprob.l = l-(end-begin); subprob.x = Malloc(struct feature_node*,subprob.l); subprob.y = Malloc(int,subprob.l); k=0; for(j=0;jx[perm[j]]; subprob.y[k] = prob->y[perm[j]]; ++k; } for(j=end;jx[perm[j]]; subprob.y[k] = prob->y[perm[j]]; ++k; } struct model *submodel = train(&subprob,param); for(j=begin;jx[perm[j]]); free_and_destroy_model(&submodel); free(subprob.x); free(subprob.y); } free(fold_start); free(perm); } int predict_values(const struct model *model_, const struct feature_node *x, double *dec_values) { int idx; int n; if(model_->bias>=0) n=model_->nr_feature+1; else n=model_->nr_feature; double *w=model_->w; int nr_class=model_->nr_class; int i; int nr_w; if(nr_class==2 && model_->param.solver_type != MCSVM_CS) nr_w = 1; else nr_w = nr_class; const feature_node *lx=x; for(i=0;iindex)!=-1; lx++) { // the dimension of testing data may exceed that of training if(idx<=n) for(i=0;ivalue; } if(nr_class==2) return (dec_values[0]>0)?model_->label[0]:model_->label[1]; else { int dec_max_idx = 0; for(i=1;i dec_values[dec_max_idx]) dec_max_idx = i; } return model_->label[dec_max_idx]; } } int predict(const model *model_, const feature_node *x) { double *dec_values = Malloc(double, model_->nr_class); int label=predict_values(model_, x, dec_values); free(dec_values); return label; } int predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates) { if(check_probability_model(model_)) { int i; int nr_class=model_->nr_class; int nr_w; if(nr_class==2) nr_w = 1; else nr_w = nr_class; int label=predict_values(model_, x, prob_estimates); for(i=0;inr_feature; int n; const parameter& param = model_->param; if(model_->bias>=0) n=nr_feature+1; else n=nr_feature; int w_size = n; FILE *fp = fopen(model_file_name,"w"); if(fp==NULL) return -1; int nr_w; if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) nr_w=1; else nr_w=model_->nr_class; fprintf(fp, "solver_type %s\n", solver_type_table[param.solver_type]); fprintf(fp, "nr_class %d\n", model_->nr_class); fprintf(fp, "label"); for(i=0; inr_class; i++) fprintf(fp, " %d", model_->label[i]); fprintf(fp, "\n"); fprintf(fp, "nr_feature %d\n", nr_feature); fprintf(fp, "bias %.16g\n", model_->bias); fprintf(fp, "w\n"); for(i=0; iw[i*nr_w+j]); fprintf(fp, "\n"); } if (ferror(fp) != 0 || fclose(fp) != 0) return -1; else return 0; } struct model *load_model(const char *model_file_name) { FILE *fp = fopen(model_file_name,"r"); if(fp==NULL) return NULL; int i; int nr_feature; int n; int nr_class; double bias; model *model_ = Malloc(model,1); parameter& param = model_->param; model_->label = NULL; char cmd[81]; while(1) { fscanf(fp,"%80s",cmd); if(strcmp(cmd,"solver_type")==0) { fscanf(fp,"%80s",cmd); int i; for(i=0;solver_type_table[i];i++) { if(strcmp(solver_type_table[i],cmd)==0) { param.solver_type=i; break; } } if(solver_type_table[i] == NULL) { fprintf(stderr,"unknown solver type.\n"); free(model_->label); free(model_); return NULL; } } else if(strcmp(cmd,"nr_class")==0) { fscanf(fp,"%d",&nr_class); model_->nr_class=nr_class; } else if(strcmp(cmd,"nr_feature")==0) { fscanf(fp,"%d",&nr_feature); model_->nr_feature=nr_feature; } else if(strcmp(cmd,"bias")==0) { fscanf(fp,"%lf",&bias); model_->bias=bias; } else if(strcmp(cmd,"w")==0) { break; } else if(strcmp(cmd,"label")==0) { int nr_class = model_->nr_class; model_->label = Malloc(int,nr_class); for(int i=0;ilabel[i]); } else { fprintf(stderr,"unknown text in model file: [%s]\n",cmd); free(model_); return NULL; } } nr_feature=model_->nr_feature; if(model_->bias>=0) n=nr_feature+1; else n=nr_feature; int w_size = n; int nr_w; if(nr_class==2 && param.solver_type != MCSVM_CS) nr_w = 1; else nr_w = nr_class; model_->w=Malloc(double, w_size*nr_w); for(i=0; iw[i*nr_w+j]); fscanf(fp, "\n"); } if (ferror(fp) != 0 || fclose(fp) != 0) return NULL; return model_; } int get_nr_feature(const model *model_) { return model_->nr_feature; } int get_nr_class(const model *model_) { return model_->nr_class; } void get_labels(const model *model_, int* label) { if (model_->label != NULL) for(int i=0;inr_class;i++) label[i] = model_->label[i]; } void free_model_content(struct model *model_ptr) { if(model_ptr->w != NULL) free(model_ptr->w); if(model_ptr->label != NULL) free(model_ptr->label); } void free_and_destroy_model(struct model **model_ptr_ptr) { struct model *model_ptr = *model_ptr_ptr; if(model_ptr != NULL) { free_model_content(model_ptr); free(model_ptr); } } void destroy_param(parameter* param) { if(param->weight_label != NULL) free(param->weight_label); if(param->weight != NULL) free(param->weight); } const char *check_parameter(const problem *prob, const parameter *param) { if(param->eps <= 0) return "eps <= 0"; if(param->C <= 0) return "C <= 0"; if(param->solver_type != L2R_LR && param->solver_type != L2R_L2LOSS_SVC_DUAL && param->solver_type != L2R_L2LOSS_SVC && param->solver_type != L2R_L1LOSS_SVC_DUAL && param->solver_type != MCSVM_CS && param->solver_type != L1R_L2LOSS_SVC && param->solver_type != L1R_LR && param->solver_type != L2R_LR_DUAL) return "unknown solver type"; return NULL; } int check_probability_model(const struct model *model_) { return (model_->param.solver_type==L2R_LR || model_->param.solver_type==L2R_LR_DUAL || model_->param.solver_type==L1R_LR); } void set_print_string_function(void (*print_func)(const char*)) { if (print_func == NULL) liblinear_print_string = &print_string_stdout; else liblinear_print_string = print_func; } liblinear-1.8+dfsg/linear.def000066400000000000000000000005321154714501200161740ustar00rootroot00000000000000LIBRARY liblinear EXPORTS train @1 cross_validation @2 save_model @3 load_model @4 get_nr_feature @5 get_nr_class @6 get_labels @7 predict_values @8 predict @9 predict_probability @10 free_and_destroy_model @11 free_model_content @12 destroy_param @13 check_parameter @14 check_probability_model @15 set_print_string_function @16 liblinear-1.8+dfsg/linear.h000066400000000000000000000035771154714501200157010ustar00rootroot00000000000000#ifndef _LIBLINEAR_H #define _LIBLINEAR_H #ifdef __cplusplus extern "C" { #endif struct feature_node { int index; double value; }; struct problem { int l, n; int *y; struct feature_node **x; double bias; /* < 0 if no bias term */ }; enum { L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL }; /* solver_type */ struct parameter { int solver_type; /* these are for training only */ double eps; /* stopping criteria */ double C; int nr_weight; int *weight_label; double* weight; }; struct model { struct parameter param; int nr_class; /* number of classes */ int nr_feature; double *w; int *label; /* label of each class */ double bias; }; struct model* train(const struct problem *prob, const struct parameter *param); void cross_validation(const struct problem *prob, const struct parameter *param, int nr_fold, int *target); int predict_values(const struct model *model_, const struct feature_node *x, double* dec_values); int predict(const struct model *model_, const struct feature_node *x); int predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates); int save_model(const char *model_file_name, const struct model *model_); struct model *load_model(const char *model_file_name); int get_nr_feature(const struct model *model_); int get_nr_class(const struct model *model_); void get_labels(const struct model *model_, int* label); void free_model_content(struct model *model_ptr); void free_and_destroy_model(struct model **model_ptr_ptr); void destroy_param(struct parameter *param); const char *check_parameter(const struct problem *prob, const struct parameter *param); int check_probability_model(const struct model *model); void set_print_string_function(void (*print_func) (const char*)); #ifdef __cplusplus } #endif #endif /* _LIBLINEAR_H */ liblinear-1.8+dfsg/matlab/000077500000000000000000000000001154714501200155025ustar00rootroot00000000000000liblinear-1.8+dfsg/matlab/Makefile000066400000000000000000000027621154714501200171510ustar00rootroot00000000000000# This Makefile is used under Linux MATLABDIR ?= /usr/local/matlab CXX ?= g++ #CXX = g++-3.3 CC ?= gcc CFLAGS = -Wall -Wconversion -O3 -fPIC -I$(MATLABDIR)/extern/include -I.. MEX = $(MATLABDIR)/bin/mex MEX_OPTION = CC\#$(CXX) CXX\#$(CXX) CFLAGS\#"$(CFLAGS)" CXXFLAGS\#"$(CFLAGS)" # comment the following line if you use MATLAB on a 32-bit computer MEX_OPTION += -largeArrayDims MEX_EXT = $(shell $(MATLABDIR)/bin/mexext) OCTAVEDIR ?= /usr/include/octave OCTAVE_MEX = env CC=$(CXX) mkoctfile OCTAVE_MEX_OPTION = --mex OCTAVE_MEX_EXT = mex OCTAVE_CFLAGS = -Wall -O3 -fPIC -I$(OCTAVEDIR) -I.. all: matlab matlab: binary octave: @make MEX="$(OCTAVE_MEX)" MEX_OPTION="$(OCTAVE_MEX_OPTION)" \ MEX_EXT="$(OCTAVE_MEX_EXT)" CFLAGS="$(OCTAVE_CFLAGS)" \ binary binary: train.$(MEX_EXT) predict.$(MEX_EXT) libsvmread.$(MEX_EXT) libsvmwrite.$(MEX_EXT) train.$(MEX_EXT): train.c ../linear.h ../tron.o ../linear.o linear_model_matlab.o $(MEX) $(MEX_OPTION) train.c ../tron.o ../linear.o linear_model_matlab.o predict.$(MEX_EXT): predict.c ../linear.h ../tron.o ../linear.o linear_model_matlab.o $(MEX) $(MEX_OPTION) predict.c ../tron.o ../linear.o linear_model_matlab.o libsvmread.$(MEX_EXT): libsvmread.c $(MEX) $(MEX_OPTION) libsvmread.c libsvmwrite.$(MEX_EXT): libsvmwrite.c $(MEX) $(MEX_OPTION) libsvmwrite.c linear_model_matlab.o: linear_model_matlab.c ../linear.h $(CXX) $(CFLAGS) -c linear_model_matlab.c ../linear.o: cd ..; make linear.o ../tron.o: cd ..; make tron.o clean: rm -f *~ *.o *.mex* *.obj liblinear-1.8+dfsg/matlab/README000066400000000000000000000152521154714501200163670ustar00rootroot00000000000000-------------------------------------------- --- MATLAB/OCTAVE interface of LIBLINEAR --- -------------------------------------------- Table of Contents ================= - Introduction - Installation - Usage - Returned Model Structure - Other Utilities - Examples - Additional Information Introduction ============ This tool provides a simple interface to LIBLINEAR, a library for large-scale regularized linear classification (http://www.csie.ntu.edu.tw/~cjlin/liblinear). It is very easy to use as the usage and the way of specifying parameters are the same as that of LIBLINEAR. Installation ============ On Unix systems, we recommend using GNU g++ as your compiler and type 'make' to build 'train.mexglx' and 'predict.mexglx'. Note that we assume your MATLAB is installed in '/usr/local/matlab', if not, please change MATLABDIR in Makefile. Example: linux> make To use Octave, type 'make octave': Example: linux> make octave On Windows systems, pre-built 'train.mexw64' and 'predict.mexw64' are included in this package (in ..\windows), so no need to conduct installation unless you run 32 bit windows. If you have modified the sources and would like to re-build the package, type 'mex -setup' in MATLAB to choose a compiler for mex first. Then type 'make' to start the installation. Example: matlab> mex -setup (ps: MATLAB will show the following messages to setup default compiler.) Please choose your compiler for building external interface (MEX) files: Would you like mex to locate installed compilers [y]/n? y Select a compiler: [1] Microsoft Visual C/C++ 2005 in C:\Program Files\Microsoft Visual Studio 8 [0] None Compiler: 1 Please verify your choices: Compiler: Microsoft Visual C/C++ 2005 Location: C:\Program Files\Microsoft Visual Studio 8 Are these correct?([y]/n): y matlab> make For list of supported/compatible compilers for MATLAB, please check the following page: http://www.mathworks.com/support/compilers/current_release/ Usage ===== matlab> model = train(training_label_vector, training_instance_matrix [,'liblinear_options', 'col']); -training_label_vector: An m by 1 vector of training labels. (type must be double) -training_instance_matrix: An m by n matrix of m training instances with n features. It must be a sparse matrix. (type must be double) -liblinear_options: A string of training options in the same format as that of LIBLINEAR. -col: if 'col' is set, each column of training_instance_matrix is a data instance. Otherwise each row is a data instance. matlab> [predicted_label, accuracy, decision_values/prob_estimates] = predict(testing_label_vector, testing_instance_matrix, model [, 'liblinear_options', 'col']); -testing_label_vector: An m by 1 vector of prediction labels. If labels of test data are unknown, simply use any random values. (type must be double) -testing_instance_matrix: An m by n matrix of m testing instances with n features. It must be a sparse matrix. (type must be double) -model: The output of train. -liblinear_options: A string of testing options in the same format as that of LIBLINEAR. -col: if 'col' is set, each column of testing_instance_matrix is a data instance. Otherwise each row is a data instance. Returned Model Structure ======================== The 'train' function returns a model which can be used for future prediction. It is a structure and is organized as [Parameters, nr_class, nr_feature, bias, Label, w]: -Parameters: Parameters -nr_class: number of classes -nr_feature: number of features in training data (without including the bias term) -bias: If >= 0, we assume one additional feature is added to the end of each data instance. -Label: label of each class -w: a nr_w-by-n matrix for the weights, where n is nr_feature or nr_feature+1 depending on the existence of the bias term. nr_w is 1 if nr_class=2 and -s is not 4 (i.e., not multi-class svm by Crammer and Singer). It is nr_class otherwise. If the '-v' option is specified, cross validation is conducted and the returned model is just a scalar: cross-validation accuracy. Result of Prediction ==================== The function 'predict' has three outputs. The first one, predicted_label, is a vector of predicted labels. The second output is a scalar meaning accuracy. The third is a matrix containing decision values or probability estimates (if '-b 1' is specified). If k is the number of classes and k' is the number of classifiers (k'=1 if k=2, otherwise k'=k), for decision values, each row includes results of k' binary linear classifiers. For probabilities, each row contains k values indicating the probability that the testing instance is in each class. Note that the order of classes here is the same as 'Label' field in the model structure. Other Utilities =============== A matlab function libsvmread reads files in LIBSVM format: [label_vector, instance_matrix] = libsvmread('data.txt'); Two outputs are labels and instances, which can then be used as inputs of svmtrain or svmpredict. A matlab function libsvmwrite writes Matlab matrix to a file in LIBSVM format: libsvmwrite('data.txt', label_vector, instance_matrix] The instance_matrix must be a sparse matrix. (type must be double) For windows, `libsvmread.mexw64' and `libsvmwrite.mexw64' are ready in the directory `..\windows'. These codes are prepared by Rong-En Fan and Kai-Wei Chang from National Taiwan University. Examples ======== Train and test on the provided data heart_scale: matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); matlab> model = train(heart_scale_label, heart_scale_inst, '-c 1'); matlab> [predict_label, accuracy, dec_values] = predict(heart_scale_label, heart_scale_inst, model); % test the training data Note that for testing, you can put anything in the testing_label_vector. For probability estimates, you need '-b 1' for training and testing: matlab> [predict_label, accuracy, prob_estimates] = predict(heart_scale_label, heart_scale_inst, model, '-b 1'); Additional Information ====================== Please cite LIBLINEAR as follows R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R. Wang, and C.-J. Lin. LIBLINEAR: A Library for Large Linear Classification, Journal of Machine Learning Research 9(2008), 1871-1874.Software available at http://www.csie.ntu.edu.tw/~cjlin/liblinear For any question, please contact Chih-Jen Lin . liblinear-1.8+dfsg/matlab/libsvmread.c000066400000000000000000000076241154714501200200070ustar00rootroot00000000000000#include #include #include #include #include #include "mex.h" #if MX_API_VER < 0x07030000 typedef int mwIndex; #endif #ifndef max #define max(x,y) (((x)>(y))?(x):(y)) #endif #ifndef min #define min(x,y) (((x)<(y))?(x):(y)) #endif void exit_with_help() { mexPrintf( "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n" ); } static void fake_answer(mxArray *plhs[]) { plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL); } static char *line; static int max_line_len; static char* readline(FILE *input) { int len; if(fgets(line,max_line_len,input) == NULL) return NULL; while(strrchr(line,'\n') == NULL) { max_line_len *= 2; line = (char *) realloc(line, max_line_len); len = (int) strlen(line); if(fgets(line+len,max_line_len-len,input) == NULL) break; } return line; } // read in a problem (in libsvm format) void read_problem(const char *filename, mxArray *plhs[]) { int max_index, min_index, inst_max_index, i; long elements, k; FILE *fp = fopen(filename,"r"); int l = 0; char *endptr; mwIndex *ir, *jc; double *labels, *samples; if(fp == NULL) { mexPrintf("can't open input file %s\n",filename); fake_answer(plhs); return; } max_line_len = 1024; line = (char *) malloc(max_line_len*sizeof(char)); max_index = 0; min_index = 1; // our index starts from 1 elements = 0; while(readline(fp) != NULL) { char *idx, *val; // features int index = 0; inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has start from 0 strtok(line," \t"); // label while (1) { idx = strtok(NULL,":"); // index:value val = strtok(NULL," \t"); if(val == NULL) break; errno = 0; index = (int) strtol(idx,&endptr,10); if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index) { mexPrintf("Wrong input format at line %d\n",l+1); fake_answer(plhs); return; } else inst_max_index = index; min_index = min(min_index, index); elements++; } max_index = max(max_index, inst_max_index); l++; } rewind(fp); // y plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); // x^T if (min_index <= 0) plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); else plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); labels = mxGetPr(plhs[0]); samples = mxGetPr(plhs[1]); ir = mxGetIr(plhs[1]); jc = mxGetJc(plhs[1]); k=0; for(i=0;i start from 0 errno = 0; samples[k] = strtod(val,&endptr); if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) { mexPrintf("Wrong input format at line %d\n",i+1); fake_answer(plhs); return; } ++k; } } jc[l] = k; fclose(fp); free(line); { mxArray *rhs[1], *lhs[1]; rhs[0] = plhs[1]; if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) { mexPrintf("Error: cannot transpose problem\n"); fake_answer(plhs); return; } plhs[1] = lhs[0]; } } void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { if(nrhs == 1) { char filename[256]; mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); if(filename == NULL) { mexPrintf("Error: filename is NULL\n"); return; } read_problem(filename, plhs); } else { exit_with_help(); fake_answer(plhs); return; } } liblinear-1.8+dfsg/matlab/libsvmwrite.c000066400000000000000000000041131154714501200202140ustar00rootroot00000000000000#include #include #include #include "mex.h" #if MX_API_VER < 0x07030000 typedef int mwIndex; #endif void exit_with_help() { mexPrintf( "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n" ); } void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat) { FILE *fp = fopen(filename,"w"); int i, k, low, high, l; mwIndex *ir, *jc; int label_vector_row_num; double *samples, *labels; mxArray *instance_mat_col; // instance sparse matrix in column format if(fp ==NULL) { mexPrintf("can't open output file %s\n",filename); return; } // transpose instance matrix { mxArray *prhs[1], *plhs[1]; prhs[0] = mxDuplicateArray(instance_mat); if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) { mexPrintf("Error: cannot transpose instance matrix\n"); return; } instance_mat_col = plhs[0]; mxDestroyArray(prhs[0]); } // the number of instance l = (int) mxGetN(instance_mat_col); label_vector_row_num = (int) mxGetM(label_vec); if(label_vector_row_num!=l) { mexPrintf("Length of label vector does not match # of instances.\n"); return; } // each column is one instance labels = mxGetPr(label_vec); samples = mxGetPr(instance_mat_col); ir = mxGetIr(instance_mat_col); jc = mxGetJc(instance_mat_col); for(i=0;i #include #include "linear.h" #include "mex.h" #if MX_API_VER < 0x07030000 typedef int mwIndex; #endif #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define NUM_OF_RETURN_FIELD 6 static const char *field_names[] = { "Parameters", "nr_class", "nr_feature", "bias", "Label", "w", }; const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_) { int i; int nr_w; double *ptr; mxArray *return_model, **rhs; int out_id = 0; int n, w_size; rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD); // Parameters // for now, only solver_type is needed rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(rhs[out_id]); ptr[0] = model_->param.solver_type; out_id++; // nr_class rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(rhs[out_id]); ptr[0] = model_->nr_class; out_id++; if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) nr_w=1; else nr_w=model_->nr_class; // nr_feature rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(rhs[out_id]); ptr[0] = model_->nr_feature; out_id++; // bias rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(rhs[out_id]); ptr[0] = model_->bias; out_id++; if(model_->bias>=0) n=model_->nr_feature+1; else n=model_->nr_feature; w_size = n; // Label if(model_->label) { rhs[out_id] = mxCreateDoubleMatrix(model_->nr_class, 1, mxREAL); ptr = mxGetPr(rhs[out_id]); for(i = 0; i < model_->nr_class; i++) ptr[i] = model_->label[i]; } else rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); out_id++; // w rhs[out_id] = mxCreateDoubleMatrix(nr_w, w_size, mxREAL); ptr = mxGetPr(rhs[out_id]); for(i = 0; i < w_size*nr_w; i++) ptr[i]=model_->w[i]; out_id++; /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */ return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names); /* Fill struct matrix with input arguments */ for(i = 0; i < NUM_OF_RETURN_FIELD; i++) mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i])); /* return */ plhs[0] = return_model; mxFree(rhs); return NULL; } const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct) { int i, num_of_fields; int nr_w; double *ptr; int id = 0; int n, w_size; mxArray **rhs; num_of_fields = mxGetNumberOfFields(matlab_struct); rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields); for(i=0;inr_class=0; nr_w=0; model_->nr_feature=0; model_->w=NULL; model_->label=NULL; // Parameters ptr = mxGetPr(rhs[id]); model_->param.solver_type = (int)ptr[0]; id++; // nr_class ptr = mxGetPr(rhs[id]); model_->nr_class = (int)ptr[0]; id++; if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) nr_w=1; else nr_w=model_->nr_class; // nr_feature ptr = mxGetPr(rhs[id]); model_->nr_feature = (int)ptr[0]; id++; // bias ptr = mxGetPr(rhs[id]); model_->bias = (int)ptr[0]; id++; if(model_->bias>=0) n=model_->nr_feature+1; else n=model_->nr_feature; w_size = n; ptr = mxGetPr(rhs[id]); model_->label=Malloc(int, model_->nr_class); for(i=0; inr_class; i++) model_->label[i]=(int)ptr[i]; id++; ptr = mxGetPr(rhs[id]); model_->w=Malloc(double, w_size*nr_w); for(i = 0; i < w_size*nr_w; i++) model_->w[i]=ptr[i]; id++; mxFree(rhs); return NULL; } liblinear-1.8+dfsg/matlab/linear_model_matlab.h000066400000000000000000000002461154714501200216270ustar00rootroot00000000000000const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_); const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct); liblinear-1.8+dfsg/matlab/make.m000066400000000000000000000007621154714501200166020ustar00rootroot00000000000000% This make.m is used under Windows mex -O -largeArrayDims -c ..\blas\*.c -outdir ..\blas mex -O -largeArrayDims -c ..\linear.cpp mex -O -largeArrayDims -c ..\tron.cpp mex -O -largeArrayDims -c linear_model_matlab.c -I..\ mex -O -largeArrayDims train.c -I..\ tron.obj linear.obj linear_model_matlab.obj ..\blas\*.obj mex -O -largeArrayDims predict.c -I..\ tron.obj linear.obj linear_model_matlab.obj ..\blas\*.obj mex -O -largeArrayDims libsvmread.c mex -O -largeArrayDims libsvmwrite.c liblinear-1.8+dfsg/matlab/predict.c000066400000000000000000000155021154714501200173030ustar00rootroot00000000000000#include #include #include #include "linear.h" #include "mex.h" #include "linear_model_matlab.h" #if MX_API_VER < 0x07030000 typedef int mwIndex; #endif #define CMD_LEN 2048 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) int col_format_flag; void read_sparse_instance(const mxArray *prhs, int index, struct feature_node *x, int feature_number, double bias) { int i, j, low, high; mwIndex *ir, *jc; double *samples; ir = mxGetIr(prhs); jc = mxGetJc(prhs); samples = mxGetPr(prhs); // each column is one instance j = 0; low = (int) jc[index], high = (int) jc[index+1]; for(i=low; i=0) { x[j].index = feature_number+1; x[j].value = bias; j++; } x[j].index = -1; } static void fake_answer(mxArray *plhs[]) { plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL); plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL); } void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag) { int label_vector_row_num, label_vector_col_num; int feature_number, testing_instance_number; int instance_index; double *ptr_instance, *ptr_label, *ptr_predict_label; double *ptr_prob_estimates, *ptr_dec_values, *ptr; struct feature_node *x; mxArray *pplhs[1]; // instance sparse matrix in row format int correct = 0; int total = 0; int nr_class=get_nr_class(model_); int nr_w; double *prob_estimates=NULL; if(nr_class==2 && model_->param.solver_type!=MCSVM_CS) nr_w=1; else nr_w=nr_class; // prhs[1] = testing instance matrix feature_number = get_nr_feature(model_); testing_instance_number = (int) mxGetM(prhs[1]); if(col_format_flag) { feature_number = (int) mxGetM(prhs[1]); testing_instance_number = (int) mxGetN(prhs[1]); } label_vector_row_num = (int) mxGetM(prhs[0]); label_vector_col_num = (int) mxGetN(prhs[0]); if(label_vector_row_num!=testing_instance_number) { mexPrintf("Length of label vector does not match # of instances.\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1) { mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } ptr_instance = mxGetPr(prhs[1]); ptr_label = mxGetPr(prhs[0]); // transpose instance matrix if(mxIsSparse(prhs[1])) { if(col_format_flag) { pplhs[0] = (mxArray *)prhs[1]; } else { mxArray *pprhs[1]; pprhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) { mexPrintf("Error: cannot transpose testing instance matrix\n"); fake_answer(plhs); return; } } } else mexPrintf("Testing_instance_matrix must be sparse\n"); prob_estimates = Malloc(double, nr_class); plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); if(predict_probability_flag) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); else plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL); ptr_predict_label = mxGetPr(plhs[0]); ptr_prob_estimates = mxGetPr(plhs[2]); ptr_dec_values = mxGetPr(plhs[2]); x = Malloc(struct feature_node, feature_number+2); for(instance_index=0;instance_indexbias); if(predict_probability_flag) { v = predict_probability(model_, x, prob_estimates); ptr_predict_label[instance_index] = v; for(i=0;i 5 || nrhs < 3) { exit_with_help(); fake_answer(plhs); return; } if(nrhs == 5) { mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1); if(strcmp(cmd, "col") == 0) { col_format_flag = 1; } } if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(mxIsStruct(prhs[2])) { const char *error_msg; // parse options if(nrhs>=4) { int i, argc = 1; char *argv[CMD_LEN/2]; // put options in argv[] mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); if((argv[argc] = strtok(cmd, " ")) != NULL) while((argv[++argc] = strtok(NULL, " ")) != NULL) ; for(i=1;i=argc) { exit_with_help(); fake_answer(plhs); return; } switch(argv[i-1][1]) { case 'b': prob_estimate_flag = atoi(argv[i]); break; default: mexPrintf("unknown option\n"); exit_with_help(); fake_answer(plhs); return; } } } model_ = Malloc(struct model, 1); error_msg = matlab_matrix_to_model(model_, prhs[2]); if(error_msg) { mexPrintf("Error: can't read model: %s\n", error_msg); free_and_destroy_model(&model_); fake_answer(plhs); return; } if(prob_estimate_flag) { if(!check_probability_model(model_)) { mexPrintf("probability output is only supported for logistic regression\n"); prob_estimate_flag=0; } } if(mxIsSparse(prhs[1])) do_predict(plhs, prhs, model_, prob_estimate_flag); else { mexPrintf("Testing_instance_matrix must be sparse\n"); fake_answer(plhs); } // destroy model_ free_and_destroy_model(&model_); } else { mexPrintf("model file should be a struct array\n"); fake_answer(plhs); } return; } liblinear-1.8+dfsg/matlab/run.m000066400000000000000000000001301154714501200164560ustar00rootroot00000000000000[y,xt] = libsvmread('../heart_scale'); model=train(y, xt) [l,a]=predict(y, xt, model); liblinear-1.8+dfsg/matlab/train.c000066400000000000000000000210771154714501200167720ustar00rootroot00000000000000#include #include #include #include #include #include "linear.h" #include "mex.h" #include "linear_model_matlab.h" #if MX_API_VER < 0x07030000 typedef int mwIndex; #endif #define CMD_LEN 2048 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL void print_null(const char *s) {} void print_string_matlab(const char *s) {mexPrintf(s);} void exit_with_help() { mexPrintf( "Usage: model = train(training_label_vector, training_instance_matrix, 'liblinear_options', 'col');\n" "liblinear_options:\n" "-s type : set type of solver (default 1)\n" " 0 -- L2-regularized logistic regression (primal)\n" " 1 -- L2-regularized L2-loss support vector classification (dual)\n" " 2 -- L2-regularized L2-loss support vector classification (primal)\n" " 3 -- L2-regularized L1-loss support vector classification (dual)\n" " 4 -- multi-class support vector classification by Crammer and Singer\n" " 5 -- L1-regularized L2-loss support vector classification\n" " 6 -- L1-regularized logistic regression\n" " 7 -- L2-regularized logistic regression (dual)\n" "-c cost : set the parameter C (default 1)\n" "-e epsilon : set tolerance of termination criterion\n" " -s 0 and 2\n" " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n" " where f is the primal function and pos/neg are # of\n" " positive/negative data (default 0.01)\n" " -s 1, 3, 4 and 7\n" " Dual maximal violation <= eps; similar to libsvm (default 0.1)\n" " -s 5 and 6\n" " |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,\n" " where f is the primal function (default 0.01)\n" "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)\n" "-wi weight: weights adjust the parameter C of different classes (see README for details)\n" "-v n: n-fold cross validation mode\n" "-q : quiet mode (no outputs)\n" "col:\n" " if 'col' is setted, training_instance_matrix is parsed in column format, otherwise is in row format\n" ); } // liblinear arguments struct parameter param; // set by parse_command_line struct problem prob; // set by read_problem struct model *model_; struct feature_node *x_space; int cross_validation_flag; int col_format_flag; int nr_fold; double bias; double do_cross_validation() { int i; int total_correct = 0; int *target = Malloc(int,prob.l); double retval = 0.0; cross_validation(&prob,¶m,nr_fold,target); for(i=0;i 2) { mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1); if((argv[argc] = strtok(cmd, " ")) != NULL) while((argv[++argc] = strtok(NULL, " ")) != NULL) ; } // parse options for(i=1;i=argc && argv[i-1][1] != 'q') // since option -q has no parameter return 1; switch(argv[i-1][1]) { case 's': param.solver_type = atoi(argv[i]); break; case 'c': param.C = atof(argv[i]); break; case 'e': param.eps = atof(argv[i]); break; case 'B': bias = atof(argv[i]); break; case 'v': cross_validation_flag = 1; nr_fold = atoi(argv[i]); if(nr_fold < 2) { mexPrintf("n-fold cross validation: n must >= 2\n"); return 1; } break; case 'w': ++param.nr_weight; param.weight_label = (int *) realloc(param.weight_label,sizeof(int)*param.nr_weight); param.weight = (double *) realloc(param.weight,sizeof(double)*param.nr_weight); param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); param.weight[param.nr_weight-1] = atof(argv[i]); break; case 'q': print_func = &print_null; i--; break; default: mexPrintf("unknown option\n"); return 1; } } set_print_string_function(print_func); if(param.eps == INF) { if(param.solver_type == L2R_LR || param.solver_type == L2R_L2LOSS_SVC) param.eps = 0.01; else if(param.solver_type == L2R_L2LOSS_SVC_DUAL || param.solver_type == L2R_L1LOSS_SVC_DUAL || param.solver_type == MCSVM_CS || param.solver_type == L2R_LR_DUAL) param.eps = 0.1; else if(param.solver_type == L1R_L2LOSS_SVC || param.solver_type == L1R_LR) param.eps = 0.01; } return 0; } static void fake_answer(mxArray *plhs[]) { plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); } int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat) { int i, j, k, low, high; mwIndex *ir, *jc; int elements, max_index, num_samples, label_vector_row_num; double *samples, *labels; mxArray *instance_mat_col; // instance sparse matrix in column format prob.x = NULL; prob.y = NULL; x_space = NULL; if(col_format_flag) instance_mat_col = (mxArray *)instance_mat; else { // transpose instance matrix mxArray *prhs[1], *plhs[1]; prhs[0] = mxDuplicateArray(instance_mat); if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) { mexPrintf("Error: cannot transpose training instance matrix\n"); return -1; } instance_mat_col = plhs[0]; mxDestroyArray(prhs[0]); } // the number of instance prob.l = (int) mxGetN(instance_mat_col); label_vector_row_num = (int) mxGetM(label_vec); if(label_vector_row_num!=prob.l) { mexPrintf("Length of label vector does not match # of instances.\n"); return -1; } // each column is one instance labels = mxGetPr(label_vec); samples = mxGetPr(instance_mat_col); ir = mxGetIr(instance_mat_col); jc = mxGetJc(instance_mat_col); num_samples = (int) mxGetNzmax(instance_mat_col); elements = num_samples + prob.l*2; max_index = (int) mxGetM(instance_mat_col); prob.y = Malloc(int, prob.l); prob.x = Malloc(struct feature_node*, prob.l); x_space = Malloc(struct feature_node, elements); prob.bias=bias; j = 0; for(i=0;i=0) { x_space[j].index = max_index+1; x_space[j].value = prob.bias; j++; } x_space[j++].index = -1; } if(prob.bias>=0) prob.n = max_index+1; else prob.n = max_index; return 0; } // Interface function of matlab // now assume prhs[0]: label prhs[1]: features void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { const char *error_msg; // fix random seed to have same results for each run // (for cross validation) srand(1); // Transform the input Matrix to libsvm format if(nrhs > 1 && nrhs < 5) { int err=0; if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { mexPrintf("Error: label vector and instance matrix must be double\n"); fake_answer(plhs); return; } if(parse_command_line(nrhs, prhs, NULL)) { exit_with_help(); destroy_param(¶m); fake_answer(plhs); return; } if(mxIsSparse(prhs[1])) err = read_problem_sparse(prhs[0], prhs[1]); else { mexPrintf("Training_instance_matrix must be sparse\n"); destroy_param(¶m); fake_answer(plhs); return; } // train's original code error_msg = check_parameter(&prob, ¶m); if(err || error_msg) { if (error_msg != NULL) mexPrintf("Error: %s\n", error_msg); destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); fake_answer(plhs); return; } if(cross_validation_flag) { double *ptr; plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(plhs[0]); ptr[0] = do_cross_validation(); } else { const char *error_msg; model_ = train(&prob, ¶m); error_msg = model_to_matlab_structure(plhs, model_); if(error_msg) mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); free_and_destroy_model(&model_); } destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); } else { exit_with_help(); fake_answer(plhs); return; } } liblinear-1.8+dfsg/predict.c000066400000000000000000000107041154714501200160420ustar00rootroot00000000000000#include #include #include #include #include #include "linear.h" struct feature_node *x; int max_nr_attr = 64; struct model* model_; int flag_predict_probability=0; void exit_input_error(int line_num) { fprintf(stderr,"Wrong input format at line %d\n", line_num); exit(1); } static char *line = NULL; static int max_line_len; static char* readline(FILE *input) { int len; if(fgets(line,max_line_len,input) == NULL) return NULL; while(strrchr(line,'\n') == NULL) { max_line_len *= 2; line = (char *) realloc(line,max_line_len); len = (int) strlen(line); if(fgets(line+len,max_line_len-len,input) == NULL) break; } return line; } void do_predict(FILE *input, FILE *output, struct model* model_) { int correct = 0; int total = 0; int nr_class=get_nr_class(model_); double *prob_estimates=NULL; int j, n; int nr_feature=get_nr_feature(model_); if(model_->bias>=0) n=nr_feature+1; else n=nr_feature; if(flag_predict_probability) { int *labels; if(!check_probability_model(model_)) { fprintf(stderr, "probability output is only supported for logistic regression\n"); exit(1); } labels=(int *) malloc(nr_class*sizeof(int)); get_labels(model_,labels); prob_estimates = (double *) malloc(nr_class*sizeof(double)); fprintf(output,"labels"); for(j=0;j=max_nr_attr-2) // need one more for index = -1 { max_nr_attr *= 2; x = (struct feature_node *) realloc(x,max_nr_attr*sizeof(struct feature_node)); } idx = strtok(NULL,":"); val = strtok(NULL," \t"); if(val == NULL) break; errno = 0; x[i].index = (int) strtol(idx,&endptr,10); if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index) exit_input_error(total+1); else inst_max_index = x[i].index; errno = 0; x[i].value = strtod(val,&endptr); if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) exit_input_error(total+1); // feature indices larger than those in training are not used if(x[i].index <= nr_feature) ++i; } if(model_->bias>=0) { x[i].index = n; x[i].value = model_->bias; i++; } x[i].index = -1; if(flag_predict_probability) { int j; predict_label = predict_probability(model_,x,prob_estimates); fprintf(output,"%d",predict_label); for(j=0;jnr_class;j++) fprintf(output," %g",prob_estimates[j]); fprintf(output,"\n"); } else { predict_label = predict(model_,x); fprintf(output,"%d\n",predict_label); } if(predict_label == target_label) ++correct; ++total; } printf("Accuracy = %g%% (%d/%d)\n",(double) correct/total*100,correct,total); if(flag_predict_probability) free(prob_estimates); } void exit_with_help() { printf( "Usage: predict [options] test_file model_file output_file\n" "options:\n" "-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0)\n" ); exit(1); } int main(int argc, char **argv) { FILE *input, *output; int i; // parse options for(i=1;i=argc) exit_with_help(); input = fopen(argv[i],"r"); if(input == NULL) { fprintf(stderr,"can't open input file %s\n",argv[i]); exit(1); } output = fopen(argv[i+2],"w"); if(output == NULL) { fprintf(stderr,"can't open output file %s\n",argv[i+2]); exit(1); } if((model_=load_model(argv[i+1]))==0) { fprintf(stderr,"can't open model file %s\n",argv[i+1]); exit(1); } x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node)); do_predict(input, output, model_); free_and_destroy_model(&model_); free(line); free(x); fclose(input); fclose(output); return 0; } liblinear-1.8+dfsg/python/000077500000000000000000000000001154714501200155635ustar00rootroot00000000000000liblinear-1.8+dfsg/python/Makefile000066400000000000000000000000471154714501200172240ustar00rootroot00000000000000all = lib lib: cd ..; make lib; cd - liblinear-1.8+dfsg/python/README000066400000000000000000000245161154714501200164530ustar00rootroot00000000000000------------------------------------- --- Python interface of LIBLINEAR --- ------------------------------------- Table of Contents ================= - Introduction - Installation - Quick Start - Design Description - Data Structures - Utility Functions - Additional Information Introduction ============ Python (http://www.python.org/) is a programming language suitable for rapid development. This tool provides a simple Python interface to LIBLINEAR, a library for support vector machines (http://www.csie.ntu.edu.tw/~cjlin/liblinear). The interface is very easy to use as the usage is the same as that of LIBLINEAR. The interface is developed with the built-in Python library "ctypes." Installation ============ On Unix systems, type > make The interface needs only LIBLINEAR shared library, which is generated by the above command. We assume that the shared library is on the LIBLINEAR main directory or in the system path. For windows, the shared library liblinear.dll is ready in the directory `..\windows'. You can also copy it to the system directory (e.g., `C:\WINDOWS\system32\' for Windows XP). To regenerate the shared library, please follow the instruction of building windows binaries in LIBLINEAR README. Quick Start =========== There are two levels of usage. The high-level one uses utility functions in liblinearutil.py and the usage is the same as the LIBLINEAR MATLAB interface. >>> from liblinearutil import * # Read data in LIBSVM format >>> y, x = svm_read_problem('../heart_scale') >>> m = train(y[:200], x[:200], '-c 4') >>> p_label, p_acc, p_val = predict(y[200:], x[200:], m) # Construct problem in python format # Dense data >>> y, x = [1,-1], [[1,0,1], [-1,0,-1]] # Sparse data >>> y, x = [1,-1], [{1:1, 3:1}, {1:-1,3:-1}] >>> prob = problem(y, x) >>> param = parameter('-c 4 -B 1') >>> m = train(prob, param) # Other utility functions >>> save_model('heart_scale.model', m) >>> m = load_model('heart_scale.model') >>> p_label, p_acc, p_val = predict(y, x, m, '-b 1') >>> ACC = evaluations(y, p_val) # Getting online help >>> help(train) The low-level use directly calls C interfaces imported by liblinear.py. Note that all arguments and return values are in ctypes format. You need to handle them carefully. >>> from liblinear import * >>> prob = problem([1,-1], [{1:1, 3:1}, {1:-1,3:-1}]) >>> param = parameter('-c 4') >>> m = liblinear.train(prob, param) # m is a ctype pointer to a model # Convet a Python-fromat instance to feature_nodearray, a ctypes structure >>> x0, max_idx = gen_feature_nodearray({1:1, 3:1}) >>> label = liblinear.predict(m, x0) Design Description ================== There are two files liblinear.py and liblinearutil.py, which respectively correspond to low-level and high-level use of the interface. In liblinear.py, we adopt the Python built-in library "ctypes," so that Python can directly access C structures and interface functions defined in linear.h. While advanced users can use structures/functions in liblinear.py, to avoid handling ctypes structures, in liblinearutil.py we provide some easy-to-use functions. The usage is similar to LIBLINEAR MATLAB interface. Data Structures =============== Three data structures derived from linear.h are node, problem, and parameter. They all contain fields with the same names in linear.h. Access these fields carefully because you directly use a C structure instead of a Python object. The following description introduces additional fields and methods. Before using the data structures, execute the following command to load the LIBLINEAR shared library: >>> from liblinear import * - class feature_node: Construct an feature_node. >>> node = feature_node(idx, val) idx: an integer indicates the feature index. val: a float indicates the feature value. - Function: gen_feature_nodearray(xi [,feature_max=None [,issparse=True]]) Generate a feature vector from a Python list/tuple or a dictionary: >>> xi, max_idx = gen_feature_nodearray({1:1, 3:1, 5:-2}) xi: the returned feature_nodearray (a ctypes structure) max_idx: the maximal feature index of xi issparse: if issparse == True, zero feature values are removed. The default value is True for the sparsity. feature_max: if feature_max is assigned, features with indices larger than feature_max are removed. - class problem: Construct an problem instance >>> prob = problem(y, x, [bias=-1]) y: a Python list/tuple of l labels (type must be int/double). x: a Python list/tuple of l data instances. Each element of x must be an instance of list/tuple/dictionary type. bias: if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1) You can alos modify the bias value by >>> prob.set_bias(1) Note that if your x contains sparse data (i.e., dictionary), the internal ctypes data format is still sparse. - class parameter: Construct an parameter instance >>> param = parameter('training_options') If 'training_options' is empty, LIBLINEAR default values are applied. Set param to LIBLINEAR default values. >>> param.set_to_default_values() Parse a string of options. >>> param.parse_options('training_options') Show values of parameters. >>> param.show() - class model: There are two ways to obtain an instance of model: >>> model_ = train(y, x) >>> model_ = load_model('model_file_name') Note that the returned structure of interface functions liblinear.train and liblinear.load_model is a ctypes pointer of model, which is different from the model object returned by train and load_model in liblinearutil.py. We provide a function toPyModel for the conversion: >>> model_ptr = liblinear.train(prob, param) >>> model_ = toPyModel(model_ptr) If you obtain a model in a way other than the above approaches, handle it carefully to avoid memory leak or segmentation fault. Some interface functions to access LIBLINEAR models are wrapped as members of the class model: >>> type = model_.get_type() >>> nr_feature = model_.get_nr_feature() >>> nr_class = model_.get_nr_class() >>> class_labels = model_.get_labels() >>> is_prob_model = model_.is_probability_model() Utility Functions ================= To use utility functions, type >>> from liblinearutil import * The above command loads train() : train an linear model predict() : predict testing data svm_read_problem() : read the data from a LIBSVM-format file. load_model() : load a LIBLINEAR model. save_model() : save model to a file. evaluations() : evaluate prediction results. - Function: train There are three ways to call train() >>> model = train(y, x [, 'training_options']) >>> model = train(prob [, 'training_options']) >>> model = train(prob, param) y: a list/tuple of l training labels (type must be int/double). x: a list/tuple of l training instances. The feature vector of each training instance is an instance of list/tuple or dictionary. training_options: a string in the same form as that for LIBLINEAR command mode. prob: an problem instance generated by calling problem(y, x). param: an parameter instance generated by calling parameter('training_options') model: the returned model instance. See linear.h for details of this structure. If '-v' is specified, cross validation is conducted and the returned model is just a scalar: cross-validation accuracy for classification and mean-squared error for regression. To train the same data many times with different parameters, the second and the third ways should be faster.. Examples: >>> y, x = svm_read_problem('../heart_scale') >>> prob = problem(y, x) >>> param = parameter('-s 3 -c 5 -q') >>> m = train(y, x, '-c 5') >>> m = train(prob, '-w1 5 -c 5') >>> m = train(prob, param) >>> CV_ACC = train(y, x, '-v 3') - Function: predict To predict testing data with a model, use >>> p_labs, p_acc, p_vals = predict(y, x, model [,'predicting_options']) y: a list/tuple of l true labels (type must be int/double). It is used for calculating the accuracy. Use [] if true labels are unavailable. x: a list/tuple of l predicting instances. The feature vector of each predicting instance is an instance of list/tuple or dictionary. predicting_options: a string of predicting options in the same format as that of LIBLINEAR. model: an model instance. p_labels: a list of predicted labels p_acc: testing accuracy p_vals: a list of decision values or probability estimates (if '-b 1' is specified). If k is the number of classes, for decision values, each element includes results of predicting k binary-class SVMs. if k = 2 and solver is not MCSVM_CS, only one decision value is returned. For probabilities, each element contains k values indicating the probability that the testing instance is in each class. Note that the order of classes here is the same as 'model.label' field in the model structure. Example: >>> m = train(y, x, '-c 5') >>> p_labels, p_acc, p_vals = predict(y, x, m) - Functions: svm_read_problem/load_model/save_model See the usage by examples: >>> y, x = svm_read_problem('data.txt') >>> m = load_model('model_file') >>> save_model('model_file', m) - Function: evaluations Calculate some evaluations using the true values (ty) and predicted values (pv): >>> ACC = evaluations(ty, pv) ty: a list of true values. pv: a list of predict values. ACC: accuracy. Additional Information ====================== This interface was written by Hsiang-Fu Yu from Department of Computer Science, National Taiwan University. If you find this tool useful, please cite LIBLINEAR as follows R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R. Wang, and C.-J. Lin. LIBLINEAR: A Library for Large Linear Classification, Journal of Machine Learning Research 9(2008), 1871-1874. Software available at http://www.csie.ntu.edu.tw/~cjlin/liblinear For any question, please contact Chih-Jen Lin , or check the FAQ page: http://www.csie.ntu.edu.tw/~cjlin/liblinear/faq.html liblinear-1.8+dfsg/python/liblinear.py000066400000000000000000000167551154714501200201140ustar00rootroot00000000000000#!/usr/bin/env python from ctypes import * from ctypes.util import find_library import sys import os # For unix the prefix 'lib' is not considered. if find_library('linear'): liblinear = CDLL(find_library('linear')) elif find_library('liblinear'): liblinear = CDLL(find_library('liblinear')) else: if sys.platform == 'win32': liblinear = CDLL(os.path.join(os.path.dirname(__file__),\ '../windows/liblinear.dll')) else: liblinear = CDLL(os.path.join(os.path.dirname(__file__),\ '../liblinear.so.1')) # Construct constants SOLVER_TYPE = ['L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL',\ 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL'] for i, s in enumerate(SOLVER_TYPE): exec("%s = %d" % (s , i)) PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p) def print_null(s): return def genFields(names, types): return list(zip(names, types)) def fillprototype(f, restype, argtypes): f.restype = restype f.argtypes = argtypes class feature_node(Structure): _names = ["index", "value"] _types = [c_int, c_double] _fields_ = genFields(_names, _types) def gen_feature_nodearray(xi, feature_max=None, issparse=True): if isinstance(xi, dict): index_range = xi.keys() elif isinstance(xi, (list, tuple)): xi = [0] + xi # idx should start from 1 index_range = range(1, len(xi)) else: raise TypeError('xi should be a dictionary, list or tuple') if feature_max: assert(isinstance(feature_max, int)) index_range = filter(lambda j: j <= feature_max, index_range) if issparse: index_range = filter(lambda j:xi[j] != 0, index_range) index_range = sorted(index_range) ret = (feature_node * (len(index_range)+2))() ret[-1].index = -1 # for bias term ret[-2].index = -1 for idx, j in enumerate(index_range): ret[idx].index = j ret[idx].value = xi[j] max_idx = 0 if index_range : max_idx = index_range[-1] return ret, max_idx class problem(Structure): _names = ["l", "n", "y", "x", "bias"] _types = [c_int, c_int, POINTER(c_int), POINTER(POINTER(feature_node)), c_double] _fields_ = genFields(_names, _types) def __init__(self, y, x, bias = -1): if len(y) != len(x) : raise ValueError("len(y) != len(x)") self.l = l = len(y) self.bias = -1 max_idx = 0 x_space = self.x_space = [] for i, xi in enumerate(x): tmp_xi, tmp_idx = gen_feature_nodearray(xi) x_space += [tmp_xi] max_idx = max(max_idx, tmp_idx) self.n = max_idx self.y = (c_int * l)() for i, yi in enumerate(y): self.y[i] = y[i] self.x = (POINTER(feature_node) * l)() for i, xi in enumerate(self.x_space): self.x[i] = xi self.set_bias(bias) def set_bias(self, bias): if self.bias == bias: return if bias >= 0 and self.bias < 0: self.n += 1 node = feature_node(self.n, bias) if bias < 0 and self.bias >= 0: self.n -= 1 node = feature_node(-1, bias) for xi in self.x_space: xi[-2] = node self.bias = bias class parameter(Structure): _names = ["solver_type", "eps", "C", "nr_weight", "weight_label", "weight"] _types = [c_int, c_double, c_double, c_int, POINTER(c_int), POINTER(c_double)] _fields_ = genFields(_names, _types) def __init__(self, options = None): if options == None: options = '' self.parse_options(options) def show(self): attrs = parameter._names + self.__dict__.keys() values = map(lambda attr: getattr(self, attr), attrs) for attr, val in zip(attrs, values): print(' %s: %s' % (attr, val)) def set_to_default_values(self): self.solver_type = L2R_L2LOSS_SVC_DUAL self.eps = float('inf') self.C = 1 self.nr_weight = 0 self.weight_label = (c_int * 0)() self.weight = (c_double * 0)() self.bias = -1 self.cross_validation = False self.nr_fold = 0 self.print_func = None def parse_options(self, options): argv = options.split() self.set_to_default_values() self.print_func = cast(None, PRINT_STRING_FUN) weight_label = [] weight = [] i = 0 while i < len(argv) : if argv[i] == "-s": i = i + 1 self.solver_type = int(argv[i]) elif argv[i] == "-c": i = i + 1 self.C = float(argv[i]) elif argv[i] == "-e": i = i + 1 self.eps = float(argv[i]) elif argv[i] == "-B": i = i + 1 self.bias = float(argv[i]) elif argv[i] == "-v": i = i + 1 self.cross_validation = 1 self.nr_fold = int(argv[i]) if self.nr_fold < 2 : raise ValueError("n-fold cross validation: n must >= 2") elif argv[i].startswith("-w"): i = i + 1 self.nr_weight += 1 nr_weight = self.nr_weight weight_label += [int(argv[i-1][2:])] weight += [float(argv[i])] elif argv[i] == "-q": self.print_func = PRINT_STRING_FUN(print_null) else : raise ValueError("Wrong options") i += 1 liblinear.set_print_string_function(self.print_func) self.weight_label = (c_int*self.nr_weight)() self.weight = (c_double*self.nr_weight)() for i in range(self.nr_weight): self.weight[i] = weight[i] self.weight_label[i] = weight_label[i] if self.eps == float('inf'): if self.solver_type in [L2R_LR, L2R_L2LOSS_SVC]: self.eps = 0.01 elif self.solver_type in [L2R_L2LOSS_SVC_DUAL, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L2R_LR_DUAL]: self.eps = 0.1 elif self.solver_type in [L1R_L2LOSS_SVC, L1R_LR]: self.eps = 0.01 class model(Structure): _names = ["param", "nr_class", "nr_feature", "w", "label", "bias"] _types = [parameter, c_int, c_int, POINTER(c_double), POINTER(c_int), c_double] _fields_ = genFields(_names, _types) def __init__(self): self.__createfrom__ = 'python' def __del__(self): # free memory created by C to avoid memory leak if hasattr(self, '__createfrom__') and self.__createfrom__ == 'C': liblinear.free_and_destroy_model(pointer(self)) def get_nr_feature(self): return liblinear.get_nr_feature(self) def get_nr_class(self): return liblinear.get_nr_class(self) def get_labels(self): nr_class = self.get_nr_class() labels = (c_int * nr_class)() liblinear.get_labels(self, labels) return labels[:nr_class] def is_probability_model(self): return (liblinear.check_probability_model(self) == 1) def toPyModel(model_ptr): """ toPyModel(model_ptr) -> model Convert a ctypes POINTER(model) to a Python model """ if bool(model_ptr) == False: raise ValueError("Null pointer") m = model_ptr.contents m.__createfrom__ = 'C' return m fillprototype(liblinear.train, POINTER(model), [POINTER(problem), POINTER(parameter)]) fillprototype(liblinear.cross_validation, None, [POINTER(problem), POINTER(parameter), c_int, POINTER(c_int)]) fillprototype(liblinear.predict_values, c_int, [POINTER(model), POINTER(feature_node), POINTER(c_double)]) fillprototype(liblinear.predict, c_int, [POINTER(model), POINTER(feature_node)]) fillprototype(liblinear.predict_probability, c_int, [POINTER(model), POINTER(feature_node), POINTER(c_double)]) fillprototype(liblinear.save_model, c_int, [c_char_p, POINTER(model)]) fillprototype(liblinear.load_model, POINTER(model), [c_char_p]) fillprototype(liblinear.get_nr_feature, c_int, [POINTER(model)]) fillprototype(liblinear.get_nr_class, c_int, [POINTER(model)]) fillprototype(liblinear.get_labels, None, [POINTER(model), POINTER(c_int)]) fillprototype(liblinear.free_model_content, None, [POINTER(model)]) fillprototype(liblinear.free_and_destroy_model, None, [POINTER(POINTER(model))]) fillprototype(liblinear.destroy_param, None, [POINTER(parameter)]) fillprototype(liblinear.check_parameter, c_char_p, [POINTER(problem), POINTER(parameter)]) fillprototype(liblinear.check_probability_model, c_int, [POINTER(model)]) fillprototype(liblinear.set_print_string_function, None, [CFUNCTYPE(None, c_char_p)]) liblinear-1.8+dfsg/python/liblinearutil.py000066400000000000000000000144311154714501200207770ustar00rootroot00000000000000#!/usr/bin/env python from liblinear import * def svm_read_problem(data_file_name): """ svm_read_problem(data_file_name) -> [y, x] Read LIBSVM-format data from data_file_name and return labels y and data instances x. """ prob_y = [] prob_x = [] for line in open(data_file_name): line = line.split(None, 1) # In case an instance with all zero features if len(line) == 1: line += [''] label, features = line xi = {} for e in features.split(): ind, val = e.split(":") xi[int(ind)] = float(val) prob_y += [int(label)] prob_x += [xi] return (prob_y, prob_x) def load_model(model_file_name): """ load_model(model_file_name) -> model Load a LIBLINEAR model from model_file_name and return. """ model = liblinear.load_model(model_file_name) if not model: print("can't open model file %s" % model_file_name) return None model = toPyModel(model) return model def save_model(model_file_name, model): """ save_model(model_file_name, model) -> None Save a LIBLINEAR model to the file model_file_name. """ liblinear.save_model(model_file_name, model) def evaluations(ty, pv): """ evaluations(ty, pv) -> ACC Calculate accuracy using the true values (ty) and predicted values (pv). """ if len(ty) != len(pv): raise ValueError("len(ty) must equal to len(pv)") total_correct = total_error = 0 for v, y in zip(pv, ty): if y == v: total_correct += 1 l = len(ty) ACC = 100.0*total_correct/l return ACC def train(arg1, arg2=None, arg3=None): """ train(y, x [, 'options']) -> model | ACC train(prob, [, 'options']) -> model | ACC train(prob, param) -> model | ACC Train a model from data (y, x) or a problem prob using 'options' or a parameter param. If '-v' is specified in 'options' (i.e., cross validation) accuracy (ACC) is returned. 'options': -s type : set type of solver (default 1) 0 -- L2-regularized logistic regression (primal) 1 -- L2-regularized L2-loss support vector classification (dual) 2 -- L2-regularized L2-loss support vector classification (primal) 3 -- L2-regularized L1-loss support vector classification (dual) 4 -- multi-class support vector classification by Crammer and Singer 5 -- L1-regularized L2-loss support vector classification 6 -- L1-regularized logistic regression 7 -- L2-regularized logistic regression (dual) -c cost : set the parameter C (default 1) -e epsilon : set tolerance of termination criterion -s 0 and 2 |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2, where f is the primal function, (default 0.01) -s 1, 3, 4, and 7 Dual maximal violation <= eps; similar to liblinear (default 0.1) -s 5 and 6 |f'(w)|_inf <= eps*min(pos,neg)/l*|f'(w0)|_inf, where f is the primal function (default 0.01) -B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1) -wi weight: weights adjust the parameter C of different classes (see README for details) -v n: n-fold cross validation mode -q : quiet mode (no outputs) """ prob, param = None, None if isinstance(arg1, (list, tuple)): assert isinstance(arg2, (list, tuple)) y, x, options = arg1, arg2, arg3 prob = problem(y, x) param = parameter(options) elif isinstance(arg1, problem): prob = arg1 if isinstance(arg2, parameter): param = arg2 else : param = parameter(arg2) if prob == None or param == None : raise TypeError("Wrong types for the arguments") prob.set_bias(param.bias) liblinear.set_print_string_function(param.print_func) err_msg = liblinear.check_parameter(prob, param) if err_msg : raise ValueError('Error: %s' % err_msg) if param.cross_validation: l, nr_fold = prob.l, param.nr_fold target = (c_int * l)() liblinear.cross_validation(prob, param, nr_fold, target) ACC = evaluations(prob.y[:l], target[:l]) print("Cross Validation Accuracy = %g%%" % ACC) return ACC else : m = liblinear.train(prob, param) m = toPyModel(m) # If prob is destroyed, data including SVs pointed by m can remain. m.x_space = prob.x_space return m def predict(y, x, m, options=""): """ predict(y, x, m [, "options"]) -> (p_labels, p_acc, p_vals) Predict data (y, x) with the SVM model m. "options": -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); The return tuple contains p_labels: a list of predicted labels p_acc: testing accuracy. p_vals: a list of decision values or probability estimates (if '-b 1' is specified). If k is the number of classes, for decision values, each element includes results of predicting k binary-class SVMs. if k = 2 and solver is not MCSVM_CS, only one decision value is returned. For probabilities, each element contains k values indicating the probability that the testing instance is in each class. Note that the order of classes here is the same as 'model.label' field in the model structure. """ predict_probability = 0 argv = options.split() i = 0 while i < len(argv): if argv[i] == '-b': i += 1 predict_probability = int(argv[i]) else: raise ValueError("Wrong options") i+=1 nr_class = m.get_nr_class() nr_feature = m.get_nr_feature() is_prob_model = m.is_probability_model() bias = m.bias if bias >= 0: biasterm = feature_node(nr_feature+1, bias) else: biasterm = feature_node(-1, bias) pred_labels = [] pred_values = [] if predict_probability: if not is_prob_model: raise TypeError('probability output is only supported for logistic regression') prob_estimates = (c_double * nr_class)() for xi in x: xi, idx = gen_feature_nodearray(xi, feature_max=nr_feature) xi[-2] = biasterm label = liblinear.predict_probability(m, xi, prob_estimates) values = prob_estimates[:nr_class] pred_labels += [label] pred_values += [values] else: if nr_class <= 2: nr_classifier = 1 else: nr_classifier = nr_class dec_values = (c_double * nr_classifier)() for xi in x: xi, idx = gen_feature_nodearray(xi, feature_max=nr_feature) xi[-2] = biasterm label = liblinear.predict_values(m, xi, dec_values) values = dec_values[:nr_classifier] pred_labels += [label] pred_values += [values] if len(y) == 0: y = [0] * len(x) ACC = evaluations(y, pred_labels) l = len(y) print("Accuracy = %g%% (%d/%d)" % (ACC, int(l*ACC//100), l)) return pred_labels, ACC, pred_values liblinear-1.8+dfsg/train.c000066400000000000000000000171151154714501200155300ustar00rootroot00000000000000#include #include #include #include #include #include #include "linear.h" #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL void print_null(const char *s) {} void exit_with_help() { printf( "Usage: train [options] training_set_file [model_file]\n" "options:\n" "-s type : set type of solver (default 1)\n" " 0 -- L2-regularized logistic regression (primal)\n" " 1 -- L2-regularized L2-loss support vector classification (dual)\n" " 2 -- L2-regularized L2-loss support vector classification (primal)\n" " 3 -- L2-regularized L1-loss support vector classification (dual)\n" " 4 -- multi-class support vector classification by Crammer and Singer\n" " 5 -- L1-regularized L2-loss support vector classification\n" " 6 -- L1-regularized logistic regression\n" " 7 -- L2-regularized logistic regression (dual)\n" "-c cost : set the parameter C (default 1)\n" "-e epsilon : set tolerance of termination criterion\n" " -s 0 and 2\n" " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n" " where f is the primal function and pos/neg are # of\n" " positive/negative data (default 0.01)\n" " -s 1, 3, 4 and 7\n" " Dual maximal violation <= eps; similar to libsvm (default 0.1)\n" " -s 5 and 6\n" " |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,\n" " where f is the primal function (default 0.01)\n" "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)\n" "-wi weight: weights adjust the parameter C of different classes (see README for details)\n" "-v n: n-fold cross validation mode\n" "-q : quiet mode (no outputs)\n" ); exit(1); } void exit_input_error(int line_num) { fprintf(stderr,"Wrong input format at line %d\n", line_num); exit(1); } static char *line = NULL; static int max_line_len; static char* readline(FILE *input) { int len; if(fgets(line,max_line_len,input) == NULL) return NULL; while(strrchr(line,'\n') == NULL) { max_line_len *= 2; line = (char *) realloc(line,max_line_len); len = (int) strlen(line); if(fgets(line+len,max_line_len-len,input) == NULL) break; } return line; } void parse_command_line(int argc, char **argv, char *input_file_name, char *model_file_name); void read_problem(const char *filename); void do_cross_validation(); struct feature_node *x_space; struct parameter param; struct problem prob; struct model* model_; int flag_cross_validation; int nr_fold; double bias; int main(int argc, char **argv) { char input_file_name[1024]; char model_file_name[1024]; const char *error_msg; parse_command_line(argc, argv, input_file_name, model_file_name); read_problem(input_file_name); error_msg = check_parameter(&prob,¶m); if(error_msg) { fprintf(stderr,"Error: %s\n",error_msg); exit(1); } if(flag_cross_validation) { do_cross_validation(); } else { model_=train(&prob, ¶m); if(save_model(model_file_name, model_)) { fprintf(stderr,"can't save model to file %s\n",model_file_name); exit(1); } free_and_destroy_model(&model_); } destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); free(line); return 0; } void do_cross_validation() { int i; int total_correct = 0; int *target = Malloc(int, prob.l); cross_validation(&prob,¶m,nr_fold,target); for(i=0;i=argc) exit_with_help(); switch(argv[i-1][1]) { case 's': param.solver_type = atoi(argv[i]); break; case 'c': param.C = atof(argv[i]); break; case 'e': param.eps = atof(argv[i]); break; case 'B': bias = atof(argv[i]); break; case 'w': ++param.nr_weight; param.weight_label = (int *) realloc(param.weight_label,sizeof(int)*param.nr_weight); param.weight = (double *) realloc(param.weight,sizeof(double)*param.nr_weight); param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); param.weight[param.nr_weight-1] = atof(argv[i]); break; case 'v': flag_cross_validation = 1; nr_fold = atoi(argv[i]); if(nr_fold < 2) { fprintf(stderr,"n-fold cross validation: n must >= 2\n"); exit_with_help(); } break; case 'q': print_func = &print_null; i--; break; default: fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]); exit_with_help(); break; } } set_print_string_function(print_func); // determine filenames if(i>=argc) exit_with_help(); strcpy(input_file_name, argv[i]); if(i max_index) max_index = inst_max_index; if(prob.bias >= 0) x_space[j++].value = prob.bias; x_space[j++].index = -1; } if(prob.bias >= 0) { prob.n=max_index+1; for(i=1;iindex = prob.n; x_space[j-2].index = prob.n; } else prob.n=max_index; fclose(fp); } liblinear-1.8+dfsg/tron.cpp000066400000000000000000000121451154714501200157330ustar00rootroot00000000000000#include #include #include #include #include "tron.h" #ifndef min template static inline T min(T x,T y) { return (x static inline T max(T x,T y) { return (x>y)?x:y; } #endif #ifdef __cplusplus extern "C" { #endif extern double dnrm2_(int *, double *, int *); extern double ddot_(int *, double *, int *, double *, int *); extern int daxpy_(int *, double *, double *, int *, double *, int *); extern int dscal_(int *, double *, double *, int *); #ifdef __cplusplus } #endif static void default_print(const char *buf) { fputs(buf,stdout); fflush(stdout); } void TRON::info(const char *fmt,...) { char buf[BUFSIZ]; va_list ap; va_start(ap,fmt); vsprintf(buf,fmt,ap); va_end(ap); (*tron_print_string)(buf); } TRON::TRON(const function *fun_obj, double eps, int max_iter) { this->fun_obj=const_cast(fun_obj); this->eps=eps; this->max_iter=max_iter; tron_print_string = default_print; } TRON::~TRON() { } void TRON::tron(double *w) { // Parameters for updating the iterates. double eta0 = 1e-4, eta1 = 0.25, eta2 = 0.75; // Parameters for updating the trust region size delta. double sigma1 = 0.25, sigma2 = 0.5, sigma3 = 4; int n = fun_obj->get_nr_variable(); int i, cg_iter; double delta, snorm, one=1.0; double alpha, f, fnew, prered, actred, gs; int search = 1, iter = 1, inc = 1; double *s = new double[n]; double *r = new double[n]; double *w_new = new double[n]; double *g = new double[n]; for (i=0; ifun(w); fun_obj->grad(w, g); delta = dnrm2_(&n, g, &inc); double gnorm1 = delta; double gnorm = gnorm1; if (gnorm <= eps*gnorm1) search = 0; iter = 1; while (iter <= max_iter && search) { cg_iter = trcg(delta, g, s, r); memcpy(w_new, w, sizeof(double)*n); daxpy_(&n, &one, s, &inc, w_new, &inc); gs = ddot_(&n, g, &inc, s, &inc); prered = -0.5*(gs-ddot_(&n, s, &inc, r, &inc)); fnew = fun_obj->fun(w_new); // Compute the actual reduction. actred = f - fnew; // On the first iteration, adjust the initial step bound. snorm = dnrm2_(&n, s, &inc); if (iter == 1) delta = min(delta, snorm); // Compute prediction alpha*snorm of the step. if (fnew - f - gs <= 0) alpha = sigma3; else alpha = max(sigma1, -0.5*(gs/(fnew - f - gs))); // Update the trust region bound according to the ratio of actual to predicted reduction. if (actred < eta0*prered) delta = min(max(alpha, sigma1)*snorm, sigma2*delta); else if (actred < eta1*prered) delta = max(sigma1*delta, min(alpha*snorm, sigma2*delta)); else if (actred < eta2*prered) delta = max(sigma1*delta, min(alpha*snorm, sigma3*delta)); else delta = max(delta, min(alpha*snorm, sigma3*delta)); info("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter); if (actred > eta0*prered) { iter++; memcpy(w, w_new, sizeof(double)*n); f = fnew; fun_obj->grad(w, g); gnorm = dnrm2_(&n, g, &inc); if (gnorm <= eps*gnorm1) break; } if (f < -1.0e+32) { info("warning: f < -1.0e+32\n"); break; } if (fabs(actred) <= 0 && prered <= 0) { info("warning: actred and prered <= 0\n"); break; } if (fabs(actred) <= 1.0e-12*fabs(f) && fabs(prered) <= 1.0e-12*fabs(f)) { info("warning: actred and prered too small\n"); break; } } delete[] g; delete[] r; delete[] w_new; delete[] s; } int TRON::trcg(double delta, double *g, double *s, double *r) { int i, inc = 1; int n = fun_obj->get_nr_variable(); double one = 1; double *d = new double[n]; double *Hd = new double[n]; double rTr, rnewTrnew, alpha, beta, cgtol; for (i=0; iHv(d, Hd); alpha = rTr/ddot_(&n, d, &inc, Hd, &inc); daxpy_(&n, &alpha, d, &inc, s, &inc); if (dnrm2_(&n, s, &inc) > delta) { info("cg reaches trust region boundary\n"); alpha = -alpha; daxpy_(&n, &alpha, d, &inc, s, &inc); double std = ddot_(&n, s, &inc, d, &inc); double sts = ddot_(&n, s, &inc, s, &inc); double dtd = ddot_(&n, d, &inc, d, &inc); double dsq = delta*delta; double rad = sqrt(std*std + dtd*(dsq-sts)); if (std >= 0) alpha = (dsq - sts)/(std + rad); else alpha = (rad - std)/dtd; daxpy_(&n, &alpha, d, &inc, s, &inc); alpha = -alpha; daxpy_(&n, &alpha, Hd, &inc, r, &inc); break; } alpha = -alpha; daxpy_(&n, &alpha, Hd, &inc, r, &inc); rnewTrnew = ddot_(&n, r, &inc, r, &inc); beta = rnewTrnew/rTr; dscal_(&n, &beta, d, &inc); daxpy_(&n, &one, r, &inc, d, &inc); rTr = rnewTrnew; } delete[] d; delete[] Hd; return(cg_iter); } double TRON::norm_inf(int n, double *x) { double dmax = fabs(x[0]); for (int i=1; i= dmax) dmax = fabs(x[i]); return(dmax); } void TRON::set_print_string(void (*print_string) (const char *buf)) { tron_print_string = print_string; } liblinear-1.8+dfsg/tron.h000066400000000000000000000012571154714501200154020ustar00rootroot00000000000000#ifndef _TRON_H #define _TRON_H class function { public: virtual double fun(double *w) = 0 ; virtual void grad(double *w, double *g) = 0 ; virtual void Hv(double *s, double *Hs) = 0 ; virtual int get_nr_variable(void) = 0 ; virtual ~function(void){} }; class TRON { public: TRON(const function *fun_obj, double eps = 0.1, int max_iter = 1000); ~TRON(); void tron(double *w); void set_print_string(void (*i_print) (const char *buf)); private: int trcg(double delta, double *g, double *s, double *r); double norm_inf(int n, double *x); double eps; int max_iter; function *fun_obj; void info(const char *fmt,...); void (*tron_print_string)(const char *buf); }; #endif