Search in sources :

Example 1 with Feature

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

the class MultiTaskSVM 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 a liblinear model based on all reviews.
    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) * (m_userSize + 1);
        // bias term in liblinear.
        libProblem.bias = 1;
    } else {
        libProblem.n = m_featureSize * (m_userSize + 1);
        // no bias term in liblinear.
        libProblem.bias = -1;
    }
    // solver type: SVM
    SolverType type = SolverType.L2R_L1LOSS_SVC_DUAL;
    m_libModel = Linear.train(libProblem, new Parameter(type, m_C, SVM.EPS));
    setPersonalizedModel();
    return 0;
}
Also used : ArrayList(java.util.ArrayList) SolverType(Classifier.supervised.liblinear.SolverType) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature) structures._Review(structures._Review) Parameter(Classifier.supervised.liblinear.Parameter) Problem(Classifier.supervised.liblinear.Problem)

Example 2 with Feature

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

the class MultiTaskSVM method createLibLinearFV.

// create a training instance of svm.
// for MT-SVM feature vector construction: we put user models in front of global model
public Feature[] createLibLinearFV(_Review r, int userIndex) {
    int fIndex;
    double fValue;
    _SparseFeature fv;
    _SparseFeature[] fvs = r.getSparse();
    int userOffset, globalOffset;
    // 0-th: x//sqrt(u); t-th: x.
    Feature[] node;
    if (m_bias) {
        userOffset = (m_featureSize + 1) * userIndex;
        globalOffset = (m_featureSize + 1) * m_userSize;
        node = new Feature[(1 + fvs.length) * 2];
    } else {
        userOffset = m_featureSize * userIndex;
        globalOffset = m_featureSize * m_userSize;
        node = new Feature[fvs.length * 2];
    }
    for (int i = 0; i < fvs.length; i++) {
        fv = fvs[i];
        // liblinear's feature index starts from one
        fIndex = fv.getIndex() + 1;
        fValue = fv.getValue();
        // Construct the user part of the training instance.
        node[i] = new FeatureNode(userOffset + fIndex, fValue);
        // Construct the global part of the training instance.
        if (m_bias)
            // global model's bias term has to be moved to the last
            node[i + fvs.length + 1] = new FeatureNode(globalOffset + fIndex, fValue / m_u);
        else
            // global model's bias term has to be moved to the last
            node[i + fvs.length] = new FeatureNode(globalOffset + fIndex, fValue / m_u);
    }
    if (m_bias) {
        // add the bias term
        // user model's bias
        node[fvs.length] = new FeatureNode((m_featureSize + 1) * (userIndex + 1), 1.0);
        // global model's bias
        node[2 * fvs.length + 1] = new FeatureNode((m_featureSize + 1) * (m_userSize + 1), 1.0 / m_u);
    }
    return node;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Example 3 with Feature

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

the class MultiTaskSVMWithClusters method createLibLinearFV.

// create a training instance of svm with cluster information.
// for MT-SVM feature vector construction: we put user models in front of global model
@Override
public Feature[] createLibLinearFV(_Review r, int userIndex) {
    int fIndex, clusterIndex = m_userClusterIndex[userIndex];
    double fValue;
    _SparseFeature fv;
    _SparseFeature[] fvs = r.getSparse();
    int userOffset, clusterOffset, globalOffset;
    // 0-th: x//sqrt(u); t-th: x.
    Feature[] node;
    if (m_bias) {
        userOffset = (m_featureSize + 1) * userIndex;
        clusterOffset = (m_featureSize + 1) * (m_userSize + clusterIndex);
        globalOffset = (m_featureSize + 1) * (m_userSize + m_clusterNo);
        // It consists of three parts.
        node = new Feature[(1 + fvs.length) * 3];
    } else {
        userOffset = m_featureSize * userIndex;
        clusterOffset = m_featureSize * (m_userSize + clusterIndex);
        globalOffset = m_featureSize * (m_userSize + m_clusterNo);
        node = new Feature[fvs.length * 3];
    }
    for (int i = 0; i < fvs.length; i++) {
        fv = fvs[i];
        // liblinear's feature index starts from one
        fIndex = fv.getIndex() + 1;
        fValue = fv.getValue();
        // Construct the user part of the training instance.
        node[i] = new FeatureNode(userOffset + fIndex, fValue * m_i);
        // Construct the cluster and global part of the training instance.
        if (m_bias) {
            // cluster part
            node[i + fvs.length + 1] = new FeatureNode(clusterOffset + fIndex, m_c == 0 ? 0 : fValue / m_c);
            // global part
            node[i + 2 * fvs.length + 2] = new FeatureNode(globalOffset + fIndex, m_u == 0 ? 0 : fValue / m_u);
        } else {
            // cluster part
            node[i + fvs.length] = new FeatureNode(clusterOffset + fIndex, m_c == 0 ? 0 : fValue / m_c);
            // global part
            node[i + 2 * fvs.length] = new FeatureNode(globalOffset + fIndex, m_u == 0 ? 0 : fValue / m_u);
        }
    }
    if (m_bias) {
        // add the bias term
        // user model's bias
        node[fvs.length] = new FeatureNode((m_featureSize + 1) * (userIndex + 1), m_i == 0 ? 0 : 1.0 / m_i);
        // cluster model's bias
        node[2 * fvs.length + 1] = new FeatureNode((m_featureSize + 1) * (m_userSize + clusterIndex + 1), m_c == 0 ? 0 : 1.0 / m_c);
        // global model's bias
        node[3 * fvs.length + 2] = new FeatureNode((m_featureSize + 1) * (m_userSize + m_clusterNo + 1), m_u == 0 ? 0 : 1.0 / m_u);
    }
    return node;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature)

Example 4 with Feature

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

the class LinearSVMMetricLearning method createLinearFeature_diff.

// Calculate the new sample according to two documents.
// Since cross-product will be symmetric, we don't need to store the whole matrix
Feature[] createLinearFeature_diff(_Doc d1, _Doc d2) {
    _SparseFeature[] fv1 = d1.getProjectedFv(), fv2 = d2.getProjectedFv();
    if (fv1 == null || fv2 == null)
        return null;
    _SparseFeature[] diffVct = Utils.diffVector(fv1, fv2);
    Feature[] features = new Feature[diffVct.length * (diffVct.length + 1) / 2];
    int pi, pj, spIndex = 0;
    double value = 0;
    for (int i = 0; i < diffVct.length; i++) {
        pi = diffVct[i].getIndex();
        for (int j = 0; j < i; j++) {
            pj = diffVct[j].getIndex();
            // Currently, we use one dimension array to represent V*V features
            // this might be too small to count
            value = 2 * diffVct[i].getValue() * diffVct[j].getValue();
            features[spIndex++] = new FeatureNode(getIndex(pi, pj), value);
        }
        // this might be too small to count
        value = diffVct[i].getValue() * diffVct[i].getValue();
        features[spIndex++] = new FeatureNode(getIndex(pi, pi), value);
    }
    return features;
}
Also used : FeatureNode(Classifier.supervised.liblinear.FeatureNode) structures._SparseFeature(structures._SparseFeature) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Example 5 with Feature

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

the class IndividualSVM 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_supFlag ? m_supUserList : m_userList) {
        trainSize = 0;
        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));
        // Set users in the same cluster.
        if (m_supFlag)
            setPersonalizedModelInCluster(user.getUser().getClusterIndex());
        else
            setPersonalizedModel(user);
    }
    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) structures._SparseFeature(structures._SparseFeature)

Aggregations

Feature (Classifier.supervised.liblinear.Feature)14 structures._SparseFeature (structures._SparseFeature)10 FeatureNode (Classifier.supervised.liblinear.FeatureNode)7 Parameter (Classifier.supervised.liblinear.Parameter)6 Problem (Classifier.supervised.liblinear.Problem)6 ArrayList (java.util.ArrayList)6 structures._Review (structures._Review)3 Model (Classifier.supervised.liblinear.Model)2 SolverType (Classifier.supervised.liblinear.SolverType)2 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)2 structures._Doc (structures._Doc)2 LambdaRank (Ranker.LambdaRank)1 LambdaRankParallel (Ranker.LambdaRankParallel)1 RankNet (Ranker.RankNet)1 File (java.io.File)1 structures._ChildDoc (structures._ChildDoc)1 structures._Query (structures._Query)1 structures._Word (structures._Word)1