use of structures._RankItem in project IR_Base by Linda-sunshine.
the class CoLinAdaptWithDiffFeatureGroups method gradientByR2.
// Calculate the gradients for the use in LBFGS.
protected void gradientByR2(_AdaptStruct user) {
// Part 1, gradients from class 0.
super.gradientByR2(user);
_CoLinAdaptDiffFvGroupsStruct ui = (_CoLinAdaptDiffFvGroupsStruct) user, uj;
int Asize = getASize(), offseti = Asize + m_dimB * 2 * ui.getId(), offsetj;
double coef, dA, dB;
// Part 2, gradients from class 1.
for (_RankItem nit : ui.getNeighbors()) {
uj = (_CoLinAdaptDiffFvGroupsStruct) m_userList.get(nit.m_index);
offsetj = Asize + m_dimB * 2 * uj.getId();
coef = 2 * nit.m_value;
for (int k = 0; k < m_dimB; k++) {
dA = coef * m_eta3 * (ui.getScalingB(k) - uj.getScalingB(k));
dB = coef * m_eta4 * (ui.getShiftingB(k) - uj.getShiftingB(k));
// update ui's gradient
m_g[offseti + k] += dA;
m_g[offseti + k + m_dimB] += dB;
// update uj's gradient
m_g[offsetj + k] -= dA;
m_g[offsetj + k + m_dimB] -= dB;
}
}
}
use of structures._RankItem in project IR_Base by Linda-sunshine.
the class CoLinAdaptWithNeighborhoodLearning method calcSimA.
// Calculate the normalized similarity between a user and its neighbors.
public double[] calcSimA(_CoLinAdaptStruct ui, int i) {
int index = 0;
double sum = 0;
// Normalized similarity between every pair of users.
double[] sims = new double[ui.getNeighbors().size()];
double[] Ai = new double[m_dim * 2];
double[] Aj = new double[m_dim * 2];
System.arraycopy(_CoLinAdaptStruct.sharedA, i, Ai, 0, m_dim * 2);
for (_RankItem uj : ui.getNeighbors()) {
System.arraycopy(_CoLinAdaptStruct.sharedA, uj.m_index, Aj, 0, m_dim * 2);
sims[index] = Utils.cosine(Ai, Aj);
sum += sims[index];
index++;
}
// Normalize the similarities.
for (int k = 0; k < sims.length; k++) sims[k] /= sum;
return sims;
}
use of structures._RankItem in project IR_Base by Linda-sunshine.
the class CoLinAdaptWithNeighborhoodLearning method updateNeighborhood.
// Calculate the similarity with the learned weights.
public void updateNeighborhood() {
m_diffSim = 0;
_CoLinAdaptStruct ui;
// Index of neighbors.
int j = 0;
double sim = 0;
for (int i = 0; i < m_userList.size(); i++) {
ui = (_CoLinAdaptStruct) m_userList.get(i);
j = 0;
// Traverse all neighbors.
for (_RankItem nit : ui.getNeighbors()) {
// New similarity between a pair of users.
sim = logit(i, j);
m_diffSim += (sim - nit.m_value) * (sim - nit.m_value);
// Update similarity of neighbor.
nit.m_value = sim;
j++;
}
}
}
use of structures._RankItem in project IR_Base by Linda-sunshine.
the class WeightedAvgAdapt method gradientByFunc.
// shared gradient calculation by batch and online updating
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight) {
_CoLinAdaptStruct ui = (_CoLinAdaptStruct) u;
// general enough to accommodate both LinAdapt and CoLinAdapt
int n, offset = m_dim * ui.getId();
double delta = weight * (review.getYLabel() - logit(review.getSparse(), ui));
if (m_LNormFlag)
delta /= getAdaptationSize(ui);
// Current user's info: Bias term + other features.
// \theta_{ii}*x_0 and x_0=1
m_g[offset] -= delta * ui.getSelfSim();
for (_SparseFeature fv : review.getSparse()) {
n = fv.getIndex() + 1;
// \theta_{ii}*x_d
m_g[offset + n] -= delta * ui.getSelfSim() * fv.getValue();
}
// Neighbors' info.
for (_RankItem nit : ui.getNeighbors()) {
offset = m_dim * nit.m_index;
// neighbors' bias term.
m_g[offset] -= delta * nit.m_value;
for (_SparseFeature fv : review.getSparse()) {
n = fv.getIndex() + 1;
// neighbors' other features.
m_g[offset + n] -= delta * nit.m_value * fv.getValue();
}
}
}
use of structures._RankItem in project IR_Base by Linda-sunshine.
the class WeightedAvgAdapt method logit.
@Override
protected // In this logit function, we need to sum over all the neighbors of the current user.
double logit(_SparseFeature[] fvs, _AdaptStruct user) {
_CoLinAdaptStruct ui = (_CoLinAdaptStruct) user;
// The user itself.
double sum = ui.getSelfSim() * ui.linearFunc(fvs, 0);
// Traverse all neighbors of the current user.
for (_RankItem nit : ui.getNeighbors()) {
_CoLinAdaptStruct uj = (_CoLinAdaptStruct) m_userList.get(nit.m_index);
sum += nit.m_value * uj.linearFunc(fvs, 0);
}
return Utils.logistic(sum);
}
Aggregations