use of structures._Review 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;
}
use of structures._Review 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;
}
use of structures._Review 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;
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class RegLR method calcLogLikelihood.
// Calculate the function value of the new added instance.
protected double calcLogLikelihood(_AdaptStruct user) {
// log likelihood.
double L = 0;
double Pi = 0;
for (_Review review : user.getReviews()) {
if (review.getType() != rType.ADAPTATION)
// only touch the adaptation data
continue;
Pi = logit(review.getSparse(), user);
if (review.getYLabel() == 1) {
if (Pi > 0.0)
L += Math.log(Pi);
else
L -= Utils.MAX_VALUE;
} else {
if (Pi < 1.0)
L += Math.log(1 - Pi);
else
L -= Utils.MAX_VALUE;
}
}
if (m_LNormFlag)
return L / getAdaptationSize(user);
else
return L;
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class asyncRegLR method train.
// this is online training in each individual user
@Override
public double train() {
double gNorm, gNormOld = Double.MAX_VALUE;
;
int predL, trueL;
_Review doc;
_PerformanceStat perfStat;
initLBFGS();
init();
for (_AdaptStruct user : m_userList) {
while (user.hasNextAdaptationIns()) {
// test the latest model before model adaptation
if (m_testmode != TestMode.TM_batch && (doc = user.getLatestTestIns()) != null) {
perfStat = user.getPerfStat();
predL = predict(doc, user);
trueL = doc.getYLabel();
perfStat.addOnePredResult(predL, trueL);
}
// in batch mode we will not accumulate the performance during adaptation
// prepare to adapt: initialize gradient
Arrays.fill(m_g, 0);
calculateGradients(user);
gNorm = gradientTest();
if (m_displayLv == 1) {
if (gNorm < gNormOld)
System.out.print("o");
else
System.out.print("x");
}
// gradient descent
gradientDescent(user, m_initStepSize, m_g);
gNormOld = gNorm;
}
if (m_displayLv > 0)
System.out.println();
}
setPersonalizedModel();
// we do not evaluate function value
return 0;
}
Aggregations