Search in sources :

Example 6 with Problem

use of Classifier.supervised.liblinear.Problem in project IR_Base by Linda-sunshine.

the class GlobalSVM method train.

@Override
public double train() {
    init();
    // Transfer all user reviews to instances recognized by SVM, indexed by users.
    int trainSize = 0, validUserIndex = 0;
    ArrayList<Feature[]> fvs = new ArrayList<Feature[]>();
    ArrayList<Double> ys = new ArrayList<Double>();
    // Two for loop to access the reviews, indexed by users.
    ArrayList<_Review> reviews;
    for (_AdaptStruct user : m_userList) {
        reviews = user.getReviews();
        boolean validUser = false;
        for (_Review r : reviews) {
            if (r.getType() == rType.ADAPTATION) {
                // we will only use the adaptation data for this purpose
                fvs.add(createLibLinearFV(r, validUserIndex));
                ys.add(new Double(r.getYLabel()));
                trainSize++;
                validUser = true;
            }
        }
        if (validUser)
            validUserIndex++;
    }
    // Train individual model for each user.
    Problem libProblem = new Problem();
    libProblem.l = trainSize;
    libProblem.x = new Feature[trainSize][];
    libProblem.y = new double[trainSize];
    for (int i = 0; i < trainSize; i++) {
        libProblem.x[i] = fvs.get(i);
        libProblem.y[i] = ys.get(i);
    }
    if (m_bias) {
        // including bias term; global model + user models
        libProblem.n = m_featureSize + 1;
        // bias term in liblinear.
        libProblem.bias = 1;
    } else {
        libProblem.n = m_featureSize;
        // no bias term in liblinear.
        libProblem.bias = -1;
    }
    m_libModel = Linear.train(libProblem, new Parameter(m_solverType, m_C, SVM.EPS));
    setPersonalizedModel();
    return 0;
}
Also used : structures._Review(structures._Review) Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) ArrayList(java.util.ArrayList) Parameter(Classifier.supervised.liblinear.Parameter) Problem(Classifier.supervised.liblinear.Problem) Feature(Classifier.supervised.liblinear.Feature)

Aggregations

Feature (Classifier.supervised.liblinear.Feature)6 Parameter (Classifier.supervised.liblinear.Parameter)6 Problem (Classifier.supervised.liblinear.Problem)6 ArrayList (java.util.ArrayList)4 structures._Review (structures._Review)3 structures._SparseFeature (structures._SparseFeature)3 SolverType (Classifier.supervised.liblinear.SolverType)2 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)2 FeatureNode (Classifier.supervised.liblinear.FeatureNode)1 Model (Classifier.supervised.liblinear.Model)1 File (java.io.File)1 structures._ChildDoc (structures._ChildDoc)1 structures._Doc (structures._Doc)1 structures._Word (structures._Word)1