Skip to content

Commit

Permalink
Fixed a bunch of bugs in multitask logistic regression
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 26, 2012
1 parent a937587 commit 3ee917a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
54 changes: 39 additions & 15 deletions src/shogun/lib/slep/slep_mt_lr.cpp
Expand Up @@ -28,7 +28,8 @@ slep_result_t slep_mt_lr(
double lambda, lambda_max, beta;
double funcp = 0.0, func = 0.0;

int n_tasks = options.n_nodes;
int n_tasks = options.n_tasks;
//SG_SPRINT("N tasks = %d \n", n_tasks);

int iter = 1;
bool done = false;
Expand Down Expand Up @@ -64,9 +65,9 @@ slep_result_t slep_mt_lr(
for (i=task_ind_start; i<task_ind_end; i++)
{
if (y[i]>0)
b = m1[t]/(m1[t]+m2[t]);
b = double(m1[t])/(m1[t]+m2[t]);
else
b = -m2[t]/(m1[t]+m2[t]);
b = -double(m2[t])/(m1[t]+m2[t]);

features->add_to_dense_vec(b,i,ATb+t*n_feats,n_feats);
}
Expand All @@ -76,21 +77,23 @@ slep_result_t slep_mt_lr(
double q_bar = 0.0;
if (options.q==1)
q_bar = CMath::ALMOST_INFTY;
else if (options.q>1e-6)
if (options.q>1e-6)
q_bar = 1;
else
q_bar = options.q/(options.q-1);

q_bar = options.q/(options.q-1);
lambda_max = 0.0;

for (t=0; t<n_tasks; t++)
for (i=0; i<n_feats; i++)
{
double sum = 0.0;
for (t=0; t<n_tasks; t++)
sum += CMath::pow(fabs(ATb[t*n_feats+i]),q_bar);
lambda_max =
CMath::max(lambda_max,
SGVector<float64_t>::qnorm(ATb+t*n_feats, n_feats, q_bar));
CMath::max(lambda_max, CMath::pow(sum,1.0/q_bar));
}

lambda_max /= n_vecs;

lambda = z*lambda_max;
}
else
Expand All @@ -105,8 +108,7 @@ slep_result_t slep_mt_lr(
w(i,j) = options.initial_w[j*n_feats+i];
}
SGVector<double> c(n_tasks);
for (t=0; t<n_tasks; t++)
c[t] = CMath::log(m1[t]/m2[t]);
c.zero();

double* s = SG_CALLOC(double, n_feats*n_tasks);
double* sc = SG_CALLOC(double, n_tasks);
Expand All @@ -121,6 +123,7 @@ slep_result_t slep_mt_lr(
for (i=task_ind_start; i<task_ind_end; i++)
Aw[i] = features->dense_dot(i,w.matrix+t*n_feats,n_feats);
}

double* Av = SG_MALLOC(double, n_vecs);
double* As = SG_MALLOC(double, n_vecs);

Expand Down Expand Up @@ -149,9 +152,10 @@ slep_result_t slep_mt_lr(

double fun_x = 0.0;

while (!done && iter < options.max_iter)
while (!done && iter <= options.max_iter)
{
beta = (alphap-1.0)/alpha;
//SG_SPRINT("beta = %f \n", beta);

for (i=0; i<n_feats*n_tasks; i++)
s[i] = w[i] + beta*wwp[i];
Expand All @@ -161,7 +165,11 @@ slep_result_t slep_mt_lr(
for (i=0; i<n_vecs; i++)
As[i] = Aw[i] + beta*(Aw[i]-Awp[i]);

//SG_SPRINT("As = %f\n",SGVector<float64_t>::dot(As,As,n_vecs));

double fun_s = 0.0;
for (i=0; i<n_tasks*n_feats; i++)
g[i] = 0.0;
for (t=0; t<n_tasks; t++)
{
int task_ind_start = options.ind[t];
Expand All @@ -183,6 +191,10 @@ slep_result_t slep_mt_lr(
}
}
fun_s /= n_vecs;

//SG_SPRINT("fun_s = %f\n", fun_s);

//SG_SPRINT("g = %f\n", SGVector<float64_t>::dot(g,g,n_feats*n_tasks));

for (i=0; i<n_feats*n_tasks; i++)
wp[i] = w[i];
Expand All @@ -193,17 +205,23 @@ slep_result_t slep_mt_lr(
for (i=0; i<n_vecs; i++)
Awp[i] = Aw[i];

while (true)
int inner_iter = 1;
while (inner_iter < 100)
{
// v = s - g / L
for (i=0; i<n_feats*n_tasks; i++)
v[i] = s[i] - g[i]*(1.0/L);

for (t=0; t<n_tasks; t++)
c[t] = sc[t] - gc[t]*(1.0/L);

eppMatrix(w.matrix, v, n_feats, n_tasks, lambda/L, options.q);

//SG_SPRINT("params [%d,%d,%f,%f]\n", n_feats, n_tasks, lambda/L, options.q);

//w.display_matrix();
//SG_SPRINT("w = %f \n", SGVector<float64_t>::dot(w.matrix,w.matrix,n_feats*n_tasks));

// v = x - s
for (i=0; i<n_feats*n_tasks; i++)
v[i] = w[i] - s[i];
Expand All @@ -224,6 +242,9 @@ slep_result_t slep_mt_lr(
}
fun_x /= n_vecs;

//SG_SPRINT("Aw = %f\n", SGVector<float64_t>::dot(Aw,Aw,n_vecs));
//c.display_vector();

double r_sum = SGVector<float64_t>::dot(v,v,n_feats*n_tasks);
double l_sum = fun_x - fun_s - SGVector<float64_t>::dot(v,g,n_feats*n_tasks);

Expand All @@ -234,6 +255,8 @@ slep_result_t slep_mt_lr(
}
r_sum /= 2.0;

//SG_SPRINT("sums = [%f, %f, %f]\n", r_sum, l_sum, fun_x);

if (r_sum <= 1e-20)
{
gradient_break = true;
Expand All @@ -244,6 +267,7 @@ slep_result_t slep_mt_lr(
break;
else
L = CMath::max(2*L, l_sum/r_sum);
inner_iter++;
}

alphap = alpha;
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/transfer/multitask/MultitaskLogisticRegression.cpp
Expand Up @@ -83,7 +83,7 @@ bool CMultitaskLogisticRegression::train_machine(CFeatures* data)
ASSERT(features);
ASSERT(m_labels);

SGVector<float64_t> y = ((CRegressionLabels*)m_labels)->get_labels();
SGVector<float64_t> y = ((CBinaryLabels*)m_labels)->get_labels();

slep_options options = slep_options::default_options();
options.q = m_q;
Expand All @@ -100,7 +100,7 @@ bool CMultitaskLogisticRegression::train_machine(CFeatures* data)
CTaskGroup* task_group = (CTaskGroup*)m_task_relation;
SGVector<index_t> ind = task_group->get_SLEP_ind();
options.ind = ind.vector;
options.n_nodes = ind.vlen-1;
options.n_tasks = ind.vlen-1;

slep_result_t result = slep_mt_lr(features, y.vector, m_z, options);
m_tasks_w = result.w;
Expand Down

0 comments on commit 3ee917a

Please sign in to comment.