use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.
the class SVM method libSVMTrain.
public static Model libSVMTrain(Collection<_Doc> trainSet, int fSize, SolverType type, double C, double bias) {
Feature[][] fvs = new Feature[trainSet.size()][];
double[] y = new double[trainSet.size()];
// file id
int fid = 0;
for (_Doc d : trainSet) {
if (bias > 0)
fvs[fid] = Utils.createLibLinearFV(d, fSize);
else
fvs[fid] = Utils.createLibLinearFV(d, 0);
y[fid] = d.getYLabel();
fid++;
}
Problem libProblem = new Problem();
libProblem.l = fid;
libProblem.n = bias >= 0 ? 1 + fSize : fSize;
libProblem.x = fvs;
libProblem.y = y;
libProblem.bias = bias;
return Linear.train(libProblem, new Parameter(type, C, SVM.EPS));
}
use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.
the class SVM method libSVMTrain.
public static Model libSVMTrain(ArrayList<Feature[]> featureArray, ArrayList<Integer> targetArray, int fSize, SolverType type, double C, double bias) {
Feature[][] featureMatrix = new Feature[featureArray.size()][];
double[] targetMatrix = new double[targetArray.size()];
for (int i = 0; i < featureArray.size(); i++) {
featureMatrix[i] = featureArray.get(i);
targetMatrix[i] = targetArray.get(i);
}
Problem libProblem = new Problem();
libProblem.l = featureMatrix.length;
libProblem.n = fSize;
libProblem.x = featureMatrix;
libProblem.y = targetMatrix;
libProblem.bias = bias;
return Linear.train(libProblem, new Parameter(type, C, SVM.EPS));
}
use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.
the class ACCTM_CZLR method updateFeatureWeight.
public void updateFeatureWeight(_ParentDoc pDoc, int iter, File weightIterFolder) {
int totalChildWordNum = 0;
int featureLen = 0;
ArrayList<Double> targetValList = new ArrayList<Double>();
ArrayList<Feature[]> featureList = new ArrayList<Feature[]>();
for (_ChildDoc cDoc : pDoc.m_childDocs) {
for (_Word w : cDoc.getWords()) {
double[] wordFeatures = w.getFeatures();
double x = w.getX();
featureLen = wordFeatures.length;
Feature[] featureVec = new Feature[featureLen];
for (int i = 0; i < featureLen; i++) {
featureVec[i] = new FeatureNode(i + 1, wordFeatures[i]);
}
featureList.add(featureVec);
targetValList.add(x);
}
}
totalChildWordNum = featureList.size();
double[] targetVal = new double[totalChildWordNum];
Feature[][] featureMatrix = new Feature[totalChildWordNum][];
for (int i = 0; i < totalChildWordNum; i++) {
featureMatrix[i] = featureList.get(i);
}
for (int i = 0; i < totalChildWordNum; i++) {
targetVal[i] = targetValList.get(i);
}
Problem problem = new Problem();
problem.l = totalChildWordNum;
// featureNum
problem.n = featureLen + 1;
problem.x = featureMatrix;
problem.y = targetVal;
SolverType solver = SolverType.L2R_LR;
double C = 1.0;
double eps = 0.01;
Parameter param = new Parameter(solver, C, eps);
Model model = Linear.train(problem, param);
int featureNum = model.getNrFeature();
for (int i = 0; i < featureNum; i++) pDoc.m_featureWeight[i] = model.getDecfunCoef(i, 0);
String weightFile = pDoc.getName() + ".txt";
File modelFile = new File(weightIterFolder, weightFile);
try {
// if((iter>200)&&(iter%100==0))
model.save(modelFile);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.
the class L2RMetricLearning method L2RModelTraining.
protected void L2RModelTraining() {
// select the training pairs
createTrainingCorpus();
if (m_ranker == 0) {
ArrayList<Feature[]> fvs = new ArrayList<Feature[]>();
ArrayList<Integer> labels = new ArrayList<Integer>();
for (_Query q : m_queries) q.extractPairs4RankSVM(fvs, labels);
Model rankSVM = SVM.libSVMTrain(fvs, labels, RankFVSize, SolverType.L2R_L1LOSS_SVC_DUAL, m_tradeoff, -1);
m_weights = rankSVM.getFeatureWeights();
System.out.format("RankSVM training performance:\nMAP: %.4f\n", evaluate(OptimizationType.OT_MAP));
} else if (m_ranker == 1) {
// all the rest use LambdaRank with different evaluator
LambdaRank lambdaRank;
if (m_multithread) {
/**
** multi-thread version ***
*/
lambdaRank = new LambdaRankParallel(RankFVSize, m_tradeoff, m_queries, OptimizationType.OT_MAP, 10);
lambdaRank.setSigns(getRankingFVSigns());
// lambdaRank specific parameters
lambdaRank.train(100, 100, 1.0, 0.95);
} else {
/**
** single-thread version ***
*/
lambdaRank = new LambdaRank(RankFVSize, m_tradeoff, m_queries, OptimizationType.OT_MAP);
lambdaRank.setSigns(getRankingFVSigns());
// lambdaRank specific parameters
lambdaRank.train(300, 20, 1.0, 0.98);
}
m_weights = lambdaRank.getWeights();
} else if (m_ranker == 2) {
RankNet ranknet = new RankNet(RankFVSize, 5.0);
ArrayList<double[]> rfvs = new ArrayList<double[]>();
for (_Query q : m_queries) q.extractPairs4RankNet(rfvs);
ranknet.setSigns(getRankingFVSigns());
double likelihood = ranknet.train(rfvs);
m_weights = ranknet.getWeights();
System.out.format("RankNet training performance:\nlog-likelihood: %.4f\t MAP: %.4f\n", likelihood, evaluate(OptimizationType.OT_MAP));
}
for (int i = 0; i < RankFVSize; i++) System.out.format("%.5f ", m_weights[i]);
System.out.println();
}
use of Classifier.supervised.liblinear.Feature in project IR_Base by Linda-sunshine.
the class LinearSVMMetricLearning method trainLibLinear.
// In this training process, we want to get the weight of all pairs of samples.
protected Model trainLibLinear(int bound) {
// creating feature projection first (this is done by choosing important SVM features)
selFeatures(m_trainSet, m_L1C);
if (!m_learningBased)
return null;
int mustLink = 0, cannotLink = 0, label, PP = 0, NN = 0;
// MyPriorityQueue<Double> maxSims = new MyPriorityQueue<Double>(1000, true), minSims = new MyPriorityQueue<Double>(1500, false);
// In the problem, the size of feature size is m'*m'. (m' is the reduced feature space by L1-SVM)
Feature[] fv;
ArrayList<Feature[]> featureArray = new ArrayList<Feature[]>();
ArrayList<Integer> targetArray = new ArrayList<Integer>();
for (int i = 0; i < m_trainSet.size(); i++) {
_Doc di = m_trainSet.get(i);
for (int j = i + 1; j < m_trainSet.size(); j++) {
_Doc dj = m_trainSet.get(j);
if (di.getYLabel() == dj.getYLabel()) {
// start from the extreme case? && (d1.getYLabel()==0 || d1.getYLabel()==4)
label = 1;
if (di.getYLabel() == 1)
PP++;
else
NN++;
if (PP > NN + 1000)
continue;
} else if (Math.abs(di.getYLabel() - dj.getYLabel()) > bound)
label = -1;
else
continue;
// else
if (label == 1 && mustLink > cannotLink + 2000 || label == -1 && mustLink + 2000 < cannotLink)
continue;
else if ((fv = createLinearFeature(di, dj)) == null)
continue;
else {
featureArray.add(fv);
targetArray.add(label);
if (label == 1)
mustLink++;
else
cannotLink++;
}
}
}
System.out.format("Generating %d must-links and %d cannot-links.\n", mustLink, cannotLink);
int fSize = m_selectedFVs.size() * (1 + m_selectedFVs.size()) / 2;
return SVM.libSVMTrain(featureArray, targetArray, fSize, SolverType.L2R_L1LOSS_SVC_DUAL, m_metricC, -1);
}
Aggregations