Skip to content

Commit

Permalink
Refactoring of multitask submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jul 18, 2012
1 parent 01d6292 commit 53d384e
Show file tree
Hide file tree
Showing 27 changed files with 375 additions and 137 deletions.
@@ -1,9 +1,9 @@
#include <shogun/labels/RegressionLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/lib/IndexBlockTree.h>
#include <shogun/transfer/multitask/MultitaskLogisticRegression.h>
#include <shogun/transfer/multitask/Task.h>
#include <shogun/transfer/multitask/TaskTree.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
Expand All @@ -17,7 +17,7 @@ void print_message(FILE* target, const char* str)

int main(int argc, char** argv)
{
init_shogun(&print_message);
init_shogun_with_defaults();

// create some data
SGMatrix<float64_t> matrix(2,4);
Expand All @@ -33,22 +33,22 @@ int main(int argc, char** argv)
labels->set_label(2, -1);
labels->set_label(3, +1);

CIndexBlock* first_task = new CIndexBlock(0,2);
CIndexBlock* second_task = new CIndexBlock(2,4);
CIndexBlockGroup* task_group = new CIndexBlockGroup();
task_group->add_block(first_task);
task_group->add_block(second_task);
CTask* first_task = new CTask(0,2);
CTask* second_task = new CTask(2,4);
CTaskGroup* task_group = new CTaskGroup();
task_group->append_task(first_task);
task_group->append_task(second_task);

CMultitaskLogisticRegression* regressor = new CMultitaskLogisticRegression(0.5,features,labels,task_group);
regressor->train();

regressor->set_current_task(0);
regressor->get_w().display_vector();

CIndexBlock* root_task = new CIndexBlock(0,4);
root_task->add_sub_block(first_task);
root_task->add_sub_block(second_task);
CIndexBlockTree* task_tree = new CIndexBlockTree(root_task);
CTask* root_task = new CTask(0,4);
root_task->add_subtask(first_task);
root_task->add_subtask(second_task);
CTaskTree* task_tree = new CTaskTree(root_task);

regressor->set_task_relation(task_tree);
regressor->train();
Expand Down
23 changes: 12 additions & 11 deletions examples/undocumented/libshogun/transfer_multitasklsregression.cpp
@@ -1,8 +1,9 @@
#include <shogun/labels/RegressionLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/transfer/multitask/MultitaskLSRegression.h>
#include <shogun/transfer/multitask/Task.h>
#include <shogun/transfer/multitask/TaskTree.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
Expand All @@ -27,17 +28,17 @@ int main(int argc, char** argv)

// create three labels
CRegressionLabels* labels=new CRegressionLabels(4);
labels->set_label(0, -1);
labels->set_label(1, +1);
labels->set_label(2, -1);
labels->set_label(3, +1);
labels->set_label(0, -1.4);
labels->set_label(1, +1.5);
labels->set_label(2, -1.2);
labels->set_label(3, +1.1);

CIndexBlock* first_task = new CIndexBlock(0,2);
CIndexBlock* second_task = new CIndexBlock(2,4);
CTask* first_task = new CTask(0,2);
CTask* second_task = new CTask(2,4);

CIndexBlockGroup* task_group = new CIndexBlockGroup();
task_group->add_block(first_task);
task_group->add_block(second_task);
CTaskGroup* task_group = new CTaskGroup();
task_group->append_task(first_task);
task_group->append_task(second_task);

CMultitaskLSRegression* regressor = new CMultitaskLSRegression(0.5,features,labels,task_group);
regressor->train();
Expand Down
Expand Up @@ -11,17 +11,17 @@

def transfer_multitask_l1l2_logistic_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):

from modshogun import BinaryLabels, RealFeatures, IndexBlock, IndexBlockGroup, MultitaskL1L2LogisticRegression
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup, MultitaskL1L2LogisticRegression

features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))

n_vectors = features.get_num_vectors()
task_one = IndexBlock(0,n_vectors/2)
task_two = IndexBlock(n_vectors/2,n_vectors)
task_group = IndexBlockGroup()
task_group.add_block(task_one)
task_group.add_block(task_two)
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)

mtlr = MultitaskL1L2LogisticRegression(0.1,0.1,features,labels,task_group)
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
Expand Down
Expand Up @@ -11,17 +11,17 @@

def transfer_multitask_leastsquares_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):

from modshogun import RegressionLabels, RealFeatures, IndexBlock, IndexBlockGroup, MultitaskLSRegression
from modshogun import RegressionLabels, RealFeatures, Task, TaskGroup, MultitaskLSRegression

features = RealFeatures(traindat)
labels = RegressionLabels(label_train)

n_vectors = features.get_num_vectors()
task_one = IndexBlock(0,n_vectors/2)
task_two = IndexBlock(n_vectors/2,n_vectors)
task_group = IndexBlockGroup()
task_group.add_block(task_one)
task_group.add_block(task_two)
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)

mtlsr = MultitaskLSRegression(0.1,features,labels,task_group)
mtlsr.set_regularization(1) # use regularization ratio
Expand Down
Expand Up @@ -11,24 +11,24 @@

def transfer_multitask_logistic_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):

from modshogun import BinaryLabels, RealFeatures, IndexBlock, IndexBlockGroup, MultitaskLogisticRegression
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup, MultitaskLogisticRegression

features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))

n_vectors = features.get_num_vectors()
task_one = IndexBlock(0,n_vectors/2)
task_two = IndexBlock(n_vectors/2,n_vectors)
task_group = IndexBlockGroup()
task_group.add_block(task_one)
task_group.add_block(task_two)
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)

mtlr = MultitaskLogisticRegression(0.1,features,labels,task_group)
mtlr.set_regularization(1) # use regularization ratio
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
mtlr.train()
mtlr.set_current_task(0)
out = mtlr.apply_regression().get_labels()
out = mtlr.apply().get_labels()

return out

Expand Down
Expand Up @@ -11,17 +11,17 @@

def transfer_multitask_trace_logistic_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):

from modshogun import BinaryLabels, RealFeatures, IndexBlock, IndexBlockGroup, MultitaskTraceLogisticRegression
from modshogun import BinaryLabels, RealFeatures, Task, TaskGroup, MultitaskTraceLogisticRegression

features = RealFeatures(hstack((traindat,traindat)))
labels = BinaryLabels(hstack((label_train,label_train)))

n_vectors = features.get_num_vectors()
task_one = IndexBlock(0,n_vectors/2)
task_two = IndexBlock(n_vectors/2,n_vectors)
task_group = IndexBlockGroup()
task_group.add_block(task_one)
task_group.add_block(task_two)
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.append_task(task_one)
task_group.append_task(task_two)

mtlr = MultitaskTraceLogisticRegression(0.1,features,labels,task_group)
mtlr.set_tolerance(1e-2) # use 1e-2 tolerance
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/modular/Transfer.i
Expand Up @@ -17,7 +17,9 @@
%rename(MultitaskKernelPlifNormalizer) CMultitaskKernelPlifNormalizer;

%rename(Task) CTask;
%rename(TaskRelationBase) CTaskRelation;
%rename(TaskTree) CTaskTree;
%rename(TaskGroup) CTaskGroup;
%rename(MultitaskLSRegression) CMultitaskLSRegression;
%rename(MultitaskLogisticRegression) CMultitaskLogisticRegression;
%rename(MultitaskL1L2LogisticRegression) CMultitaskL1L2LogisticRegression;
Expand All @@ -41,7 +43,9 @@
%include <shogun/transfer/multitask/MultitaskKernelPlifNormalizer.h>

%include <shogun/transfer/multitask/Task.h>
%include <shogun/transfer/multitask/TaskRelation.h>
%include <shogun/transfer/multitask/TaskTree.h>
%include <shogun/transfer/multitask/TaskGroup.h>
%include <shogun/transfer/multitask/MultitaskLSRegression.h>
%include <shogun/transfer/multitask/MultitaskLogisticRegression.h>
%include <shogun/transfer/multitask/MultitaskL1L2LogisticRegression.h>
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/modular/Transfer_includes.i
Expand Up @@ -8,7 +8,9 @@

#include <shogun/transfer/multitask/LibLinearMTL.h>
#include <shogun/transfer/multitask/Task.h>
#include <shogun/transfer/multitask/TaskRelation.h>
#include <shogun/transfer/multitask/TaskTree.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/transfer/multitask/MultitaskLSRegression.h>
#include <shogun/transfer/multitask/MultitaskLogisticRegression.h>
#include <shogun/transfer/multitask/MultitaskL1L2LogisticRegression.h>
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/lib/IndexBlockTree.cpp
Expand Up @@ -237,13 +237,13 @@ void CIndexBlockTree::set_root_block(CIndexBlock* root_block)
m_root_block = root_block;
}

SGVector<index_t> CIndexBlockTree::get_SLEP_ind() const
SGVector<index_t> CIndexBlockTree::get_SLEP_ind()
{
SG_SNOTIMPLEMENTED;
return SGVector<index_t>();
}

SGVector<float64_t> CIndexBlockTree::get_SLEP_G() const
SGVector<float64_t> CIndexBlockTree::get_SLEP_G()
{
return m_precomputed_G;
}
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/lib/IndexBlockTree.h
Expand Up @@ -51,9 +51,9 @@ class CIndexBlockTree : public CIndexBlockRelation
/** returns information about blocks in
* SLEP "ind" format
*/
virtual SGVector<index_t> get_SLEP_ind() const;
virtual SGVector<index_t> get_SLEP_ind();

virtual SGVector<float64_t> get_SLEP_G() const;
virtual SGVector<float64_t> get_SLEP_G();

/** returns information about blocks relations
* in SLEP "ind_t" format
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/lib/slep/slep_solver.cpp
Expand Up @@ -275,6 +275,8 @@ slep_result_t slep_solver(
n_blocks = options.n_feature_blocks;
break;
}
SG_SDEBUG("n_tasks = %d, n_blocks = %d\n",n_tasks,n_blocks);
SG_SDEBUG("n_nodes = %d\n",options.n_nodes);

int iter = 1;
bool done = false;
Expand Down
1 change: 0 additions & 1 deletion src/shogun/lib/slep/tree/altra.cpp
Expand Up @@ -18,7 +18,6 @@

void altra(double *x, double *v, int n, double *ind, int nodes, double mult)
{

int i, j;
double lambda,twoNorm, ratio;

Expand Down
Expand Up @@ -10,7 +10,6 @@
#include <shogun/transfer/multitask/MultitaskL1L2LogisticRegression.h>
#include <shogun/lib/slep/malsar_joint_feature_learning.h>
#include <shogun/lib/slep/slep_options.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/lib/SGVector.h>

namespace shogun
Expand All @@ -23,8 +22,8 @@ CMultitaskL1L2LogisticRegression::CMultitaskL1L2LogisticRegression() :

CMultitaskL1L2LogisticRegression::CMultitaskL1L2LogisticRegression(
float64_t rho1, float64_t rho2, CDotFeatures* train_features,
CBinaryLabels* train_labels, CIndexBlockRelation* task_relation) :
CMultitaskLogisticRegression(0.0,train_features,train_labels,task_relation)
CBinaryLabels* train_labels, CTaskGroup* task_group) :
CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
{
set_rho1(rho1);
set_rho2(rho2);
Expand Down Expand Up @@ -60,7 +59,7 @@ bool CMultitaskL1L2LogisticRegression::train_machine(CFeatures* data)
options.termination = m_termination;
options.tolerance = m_tolerance;
options.max_iter = m_max_iter;
SGVector<index_t> ind = ((CIndexBlockGroup*)m_task_relation)->get_SLEP_ind();
SGVector<index_t> ind = ((CTaskGroup*)m_task_relation)->get_SLEP_ind();
options.ind = ind.vector;
options.n_tasks = ind.vlen-1;

Expand Down
Expand Up @@ -34,7 +34,7 @@ class CMultitaskL1L2LogisticRegression : public CMultitaskLogisticRegression
*/
CMultitaskL1L2LogisticRegression(
float64_t rho1, float64_t rho2, CDotFeatures* training_data,
CBinaryLabels* training_labels, CIndexBlockRelation* task_relation);
CBinaryLabels* training_labels, CTaskGroup* task_group);

/** destructor */
virtual ~CMultitaskL1L2LogisticRegression();
Expand Down
21 changes: 10 additions & 11 deletions src/shogun/transfer/multitask/MultitaskLSRegression.cpp
Expand Up @@ -8,12 +8,11 @@
*/

#include <shogun/transfer/multitask/MultitaskLSRegression.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/transfer/multitask/TaskTree.h>
#include <shogun/lib/slep/slep_solver.h>
#include <shogun/lib/slep/slep_options.h>

#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/lib/IndexBlockTree.h>

namespace shogun
{

Expand All @@ -26,7 +25,7 @@ CMultitaskLSRegression::CMultitaskLSRegression() :

CMultitaskLSRegression::CMultitaskLSRegression(
float64_t z, CDotFeatures* train_features,
CRegressionLabels* train_labels, CIndexBlockRelation* task_relation) :
CRegressionLabels* train_labels, CTaskRelation* task_relation) :
CSLEPMachine(z,train_features,(CLabels*)train_labels),
m_current_task(0), m_task_relation(NULL)
{
Expand Down Expand Up @@ -60,13 +59,13 @@ void CMultitaskLSRegression::set_current_task(int32_t task)
w[i] = m_tasks_w(i,task);
}

CIndexBlockRelation* CMultitaskLSRegression::get_task_relation() const
CTaskRelation* CMultitaskLSRegression::get_task_relation() const
{
SG_REF(m_task_relation);
return m_task_relation;
}

void CMultitaskLSRegression::set_task_relation(CIndexBlockRelation* task_relation)
void CMultitaskLSRegression::set_task_relation(CTaskRelation* task_relation)
{
SG_UNREF(m_task_relation);
SG_REF(task_relation);
Expand All @@ -90,12 +89,12 @@ bool CMultitaskLSRegression::train_machine(CFeatures* data)
options.tolerance = m_tolerance;
options.max_iter = m_max_iter;

EIndexBlockRelationType relation_type = m_task_relation->get_relation_type();
ETaskRelationType relation_type = m_task_relation->get_relation_type();
switch (relation_type)
{
case GROUP:
case TASK_GROUP:
{
CIndexBlockGroup* task_group = (CIndexBlockGroup*)m_task_relation;
CTaskGroup* task_group = (CTaskGroup*)m_task_relation;
SGVector<index_t> ind = task_group->get_SLEP_ind();
options.ind = ind.vector;
options.n_tasks = ind.vlen-1;
Expand All @@ -104,9 +103,9 @@ bool CMultitaskLSRegression::train_machine(CFeatures* data)
m_tasks_w = slep_solver(features, y.vector, m_z, options).w;
}
break;
case TREE:
case TASK_TREE:
{
CIndexBlockTree* task_tree = (CIndexBlockTree*)m_task_relation;
CTaskTree* task_tree = (CTaskTree*)m_task_relation;
SGVector<index_t> ind = task_tree->get_SLEP_ind();
options.ind = ind.vector;
SGVector<float64_t> ind_t = task_tree->get_SLEP_ind_t();
Expand Down

0 comments on commit 53d384e

Please sign in to comment.