use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class LinAdapt method linearFunc.
protected double linearFunc(_SparseFeature[] fvs, _AdaptStruct u) {
_LinAdaptStruct user = (_LinAdaptStruct) u;
// Bias term: w0*a0+b0.
double value = user.getScaling(0) * m_gWeights[0] + user.getShifting(0);
// feature index and feature group index
int n = 0, k = 0;
for (_SparseFeature fv : fvs) {
n = fv.getIndex() + 1;
k = m_featureGroupMap[n];
value += (user.getScaling(k) * m_gWeights[n] + user.getShifting(k)) * fv.getValue();
}
return value;
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class MTLinAdapt method logit.
// We can do A_i*A_s*w_g*x at the same time to reduce computation.
@Override
protected double logit(_SparseFeature[] fvs, _AdaptStruct u) {
// feature index and feature group index
int n = 0, k = 0;
_CoLinAdaptStruct ui = (_CoLinAdaptStruct) u;
// Bias term: w_s0*a0+b0.
double value = ui.getScaling(0) * getSupWeights(0) + ui.getShifting(0);
for (_SparseFeature fv : fvs) {
n = fv.getIndex() + 1;
k = m_featureGroupMap[n];
value += (ui.getScaling(k) * getSupWeights(n) + ui.getShifting(k)) * fv.getValue();
}
return 1 / (1 + Math.exp(-value));
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class MTLinAdapt method gradientByFunc.
// Gradients from loglikelihood, contributes to both individual user's gradients and super user's gradients.
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight) {
_CoLinAdaptStruct ui = (_CoLinAdaptStruct) u;
// feature index and feature group index
int n, k, s;
// general enough to accommodate both LinAdapt and CoLinAdapt
int offset = 2 * m_dim * ui.getId();
int offsetSup = 2 * m_dim * m_userList.size();
double delta = weight * (review.getYLabel() - logit(review.getSparse(), ui));
if (m_LNormFlag)
delta /= getAdaptationSize(ui);
// Bias term for individual user.
// a[0] = ws0*x0; x0=1
m_g[offset] -= delta * getSupWeights(0);
// b[0]
m_g[offset + m_dim] -= delta;
// Bias term for super user.
// a_s[0] = a_i0*w_g0*x_d0
m_g[offsetSup] -= delta * ui.getScaling(0) * m_gWeights[0];
// b_s[0] = a_i0*x_d0
m_g[offsetSup + m_dimSup] -= delta * ui.getScaling(0);
// Traverse all the feature dimension to calculate the gradient for both individual users and super user.
for (_SparseFeature fv : review.getSparse()) {
n = fv.getIndex() + 1;
k = m_featureGroupMap[n];
// w_si*x_di
m_g[offset + k] -= delta * getSupWeights(n) * fv.getValue();
// x_di
m_g[offset + m_dim + k] -= delta * fv.getValue();
s = m_featureGroupMap4SupUsr[n];
// a_i*w_gi*x_di
m_g[offsetSup + s] -= delta * ui.getScaling(k) * m_gWeights[n] * fv.getValue();
// a_i*x_di
m_g[offsetSup + m_dimSup + s] -= delta * ui.getScaling(k) * fv.getValue();
}
}
use of structures._SparseFeature 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._SparseFeature 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