Skip to content

Commit

Permalink
replaced SGMatrix with SGSparseMatrix to handle the case of sparse ta…
Browse files Browse the repository at this point in the history
…sk similarities
  • Loading branch information
cwidmer committed Jun 15, 2012
1 parent 5423ead commit 6b71ab4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/shogun/transfer/multitask/LibLinearMTL.cpp
Expand Up @@ -280,12 +280,20 @@ void CLibLinearMTL::solve_l2r_l1l2_svc(const problem *prob, double eps, double C

// we compute the inner sum by looping over tasks
// this update is the main result of MTL_DCD
SGSparseVector<float64_t> ts_row = task_similarity_matrix.sparse_matrix[ti];

float64_t inner_sum = 0;
for (int32_t k=0; k!=num_tasks; k++)
for (int32_t k=0; k!=ts_row.num_feat_entries; k++)
{
//inner_sum += M[t,ti] * all_lt[i] * np.dot(V[t,:], all_xt[i])
float64_t* tmp_w = V.get_column_vector(k);
inner_sum += task_similarity_matrix.matrix[k*num_tasks+ti] * yi * prob->x->dense_dot(i, tmp_w, n);

// get data from sparse matrix
SGSparseVectorEntry<float64_t> e = ts_row.features[k];
int32_t e_i = e.feat_index;
float64_t sim = e.entry;

// fetch vector
float64_t* tmp_w = V.get_column_vector(e_i);
inner_sum += sim * yi * prob->x->dense_dot(i, tmp_w, n);

//possibly deal with bias
//if (prob->use_bias)
Expand Down Expand Up @@ -511,11 +519,15 @@ return obj
{
// look up task similarity
int32_t ti_j = task_indicator_lhs[j];

//TODO: same interface for sparse matrix
/*
float64_t ts = task_similarity_matrix.matrix[ti_i*num_tasks+ti_j];
// compute objective
obj -= 0.5 * ts * alphas[i] * alphas[j] * ((CBinaryLabels*)m_labels)->get_label(i) *
((CBinaryLabels*)m_labels)->get_label(j) * features->dot(i, features,j);
*/
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/shogun/transfer/multitask/LibLinearMTL.h
Expand Up @@ -20,6 +20,7 @@
#include <shogun/base/Parameter.h>
#include <shogun/machine/LinearMachine.h>
#include <shogun/lib/external/shogun_liblinear.h>
#include <shogun/lib/SGSparseMatrix.h>


namespace shogun
Expand Down Expand Up @@ -141,8 +142,6 @@ class CLibLinearMTL : public CLinearMachine
/** set task indicator for lhs */
inline void set_task_indicator_lhs(SGVector<int32_t> ti)
{
// dear god, this is super verbose, isn't there a way to shorten this?
// yes we can! -- Sergey
task_indicator_lhs = ti;
}

Expand All @@ -153,10 +152,12 @@ class CLibLinearMTL : public CLinearMachine
}

/** set task similarity matrix */
/*
inline void set_task_similarity_matrix(SGMatrix<float64_t> tsm)
{
task_similarity_matrix = tsm;
}
*/

/** set graph laplacian */
inline void set_graph_laplacian(SGMatrix<float64_t> lap)
Expand Down Expand Up @@ -193,11 +194,14 @@ class CLibLinearMTL : public CLinearMachine
float64_t* v_s = V.get_column_vector(s);
for (int32_t t=0; t<num_tasks; t++)
{
//TODO: same interface for SparseMatrix!
/*
float64_t sim_ts = task_similarity_matrix.matrix[s*num_tasks+t];
for(int32_t i=0; i<w_size; i++)
{
W.matrix[t*w_size + i] += sim_ts * v_s[i];
}
*/
}
}

Expand Down Expand Up @@ -278,7 +282,8 @@ class CLibLinearMTL : public CLinearMachine
SGVector<int32_t> task_indicator_rhs;

/** task similarity matrix */
SGMatrix<float64_t> task_similarity_matrix;
//SGMatrix<float64_t> task_similarity_matrix;
SGSparseMatrix<float64_t> task_similarity_matrix;

/** task similarity matrix */
SGMatrix<float64_t> graph_laplacian;
Expand Down

0 comments on commit 6b71ab4

Please sign in to comment.