Skip to content

Commit

Permalink
Moved task structures out of multitask submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jul 5, 2012
1 parent e22c517 commit a75088e
Show file tree
Hide file tree
Showing 26 changed files with 561 additions and 588 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -84,6 +84,7 @@ TARGETS = basic_minimal \
statistics_quadratic_time_mmd \
statistics_linear_time_mmd \
transfer_multitasklsregression \
transfer_multitasklogisticregression \

all: $(TARGETS)

Expand Down
@@ -1,7 +1,7 @@
#include <shogun/labels/RegressionLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/transfer/multitask/Task.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/transfer/multitask/MultitaskLogisticRegression.h>
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
Expand All @@ -26,18 +26,18 @@ int main(int argc, char** argv)
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);

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

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

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

CMultitaskLogisticRegression* regressor = new CMultitaskLogisticRegression(0.5,features,labels,task_group);
regressor->train();
Expand Down
@@ -1,7 +1,7 @@
#include <shogun/labels/RegressionLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/transfer/multitask/Task.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/transfer/multitask/MultitaskLSRegression.h>
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
Expand Down Expand Up @@ -32,12 +32,12 @@ int main(int argc, char** argv)
labels->set_label(2, -1);
labels->set_label(3, +1);

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

CTaskGroup* task_group = new CTaskGroup();
task_group->add_task(first_task);
task_group->add_task(second_task);
CIndexBlockGroup* task_group = new CIndexBlockGroup();
task_group->add_block(first_task);
task_group->add_block(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_group_regression(fm_train=traindat,fm_test=testdat,label_train=label_traindat):

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

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

n_vectors = features.get_num_vectors()
task_one = Task(0,n_vectors/2)
task_two = Task(n_vectors/2,n_vectors)
task_group = TaskGroup()
task_group.add_task(task_one)
task_group.add_task(task_two)
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)

mtlsr = MultitaskLSRegression(0.1,features,labels,task_group)
mtlsr.train()
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces/modular/Library.i
Expand Up @@ -17,6 +17,11 @@
%rename(StructuredData) CStructuredData;
%rename(DynamicObjectArray) CDynamicObjectArray;

%rename(IndexBlock) CIndexBlock;
%rename(IndexBlockRelation) CIndexBlockRelation;
%rename(IndexBlockGroup) CIndexBlockGroup;
%rename(IndexBlockTree) CIndexBlockTree;

%ignore RADIX_STACK_SIZE;
%ignore NUMTRAPPEDSIGS;
%ignore TRIE_TERMINAL_CHARACTER;
Expand Down Expand Up @@ -449,3 +454,7 @@ namespace shogun
%include <shogun/lib/StructuredDataTypes.h>
%include <shogun/lib/StructuredData.h>
%include <shogun/lib/DynamicObjectArray.h>
%include <shogun/lib/IndexBlock.h>
%include <shogun/lib/IndexBlockRelation.h>
%include <shogun/lib/IndexBlockGroup.h>
%include <shogun/lib/IndexBlockTree.h>
4 changes: 4 additions & 0 deletions src/interfaces/modular/Library_includes.i
Expand Up @@ -21,4 +21,8 @@
#include <shogun/lib/StructuredDataTypes.h>
#include <shogun/lib/StructuredData.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/lib/IndexBlock.h>
#include <shogun/lib/IndexBlockRelation.h>
#include <shogun/lib/IndexBlockGroup.h>
#include <shogun/lib/IndexBlockTree.h>
%}
7 changes: 0 additions & 7 deletions src/interfaces/modular/Transfer.i
Expand Up @@ -16,9 +16,6 @@
%rename(MultitaskKernelMaskPairNormalizer) CMultitaskKernelMaskPairNormalizer;
%rename(MultitaskKernelPlifNormalizer) CMultitaskKernelPlifNormalizer;

%rename(Task) CTask;
%rename(TaskGroup) CTaskGroup;
%rename(TaskTree) CTaskTree;
%rename(MultitaskLSRegression) CMultitaskLSRegression;
%rename(MultitaskLogisticRegression) CMultitaskLogisticRegression;

Expand All @@ -39,10 +36,6 @@
%include <shogun/transfer/multitask/MultitaskKernelMaskPairNormalizer.h>
%include <shogun/transfer/multitask/MultitaskKernelPlifNormalizer.h>

%include <shogun/transfer/multitask/TaskRelation.h>
%include <shogun/transfer/multitask/Task.h>
%include <shogun/transfer/multitask/TaskGroup.h>
%include <shogun/transfer/multitask/TaskTree.h>
%include <shogun/transfer/multitask/MultitaskLSRegression.h>
%include <shogun/transfer/multitask/MultitaskLogisticRegression.h>

Expand Down
5 changes: 0 additions & 5 deletions src/interfaces/modular/Transfer_includes.i
Expand Up @@ -7,11 +7,6 @@
#include <shogun/transfer/multitask/MultitaskKernelPlifNormalizer.h>

#include <shogun/transfer/multitask/LibLinearMTL.h>

#include <shogun/transfer/multitask/TaskRelation.h>
#include <shogun/transfer/multitask/Task.h>
#include <shogun/transfer/multitask/TaskGroup.h>
#include <shogun/transfer/multitask/TaskTree.h>
#include <shogun/transfer/multitask/MultitaskLSRegression.h>
#include <shogun/transfer/multitask/MultitaskLogisticRegression.h>

Expand Down
54 changes: 54 additions & 0 deletions src/shogun/lib/IndexBlock.cpp
@@ -0,0 +1,54 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2012 Sergey Lisitsyn
*/

#include <shogun/lib/IndexBlock.h>

using namespace shogun;

CIndexBlock::CIndexBlock() : CSGObject(),
m_block_name("block"),
m_min_index(0), m_max_index(0),
m_weight(1.0), m_sub_blocks(NULL)
{
m_sub_blocks = new CList(true);
SG_REF(m_sub_blocks);
}

CIndexBlock::CIndexBlock(index_t min_index, index_t max_index,
float64_t weight, const char* name) :
CSGObject(), m_block_name(name),
m_min_index(min_index), m_max_index(max_index),
m_weight(weight), m_sub_blocks(NULL)
{
m_sub_blocks = new CList(true);
SG_REF(m_sub_blocks);
}

CIndexBlock::~CIndexBlock()
{
SG_UNREF(m_sub_blocks);
}

void CIndexBlock::add_sub_block(CIndexBlock* sub_block)
{
ASSERT(sub_block->get_min_index()>=m_min_index);
ASSERT(sub_block->get_max_index()<=m_max_index);
m_sub_blocks->append_element(sub_block);
}

CList* CIndexBlock::get_sub_blocks()
{
SG_REF(m_sub_blocks);
return m_sub_blocks;
}

int32_t CIndexBlock::get_num_sub_blocks()
{
return m_sub_blocks->get_num_elements();
}
Expand Up @@ -7,8 +7,8 @@
* Copyright (C) 2012 Sergey Lisitsyn
*/

#ifndef TASK_H_
#define TASK_H_
#ifndef INDEXBLOCK_H_
#define INDEXBLOCK_H_

#include <shogun/lib/SGVector.h>
#include <shogun/lib/List.h>
Expand All @@ -18,29 +18,24 @@ namespace shogun

/** @brief used to represent tasks in multitask learning
*/
class CTask : public CSGObject
class CIndexBlock : public CSGObject
{
public:

/** default constructor */
CTask();
CIndexBlock();

/** constructor
* @param min_index smallest index of vector in task
* @param max_index largest index of vector in task
* @param min_index smallest index of the index block
* @param max_index largest index of the index block
* @param weight weight (optional)
* @param name name of task (optional)
*/
CTask(index_t min_index, index_t max_index,
CIndexBlock(index_t min_index, index_t max_index,
float64_t weight=1.0, const char* name="task");

/** destructor */
~CTask();

/** adds subtask
* @param subtask subtask to add
*/
void add_subtask(CTask* subtask);
~CIndexBlock();

/** get min index */
index_t get_min_index() const { return m_min_index; }
Expand All @@ -55,32 +50,38 @@ class CTask : public CSGObject
/** set weight */
void set_weight(float64_t weight) { m_weight = weight; }


/** get name */
virtual const char* get_name() const { return "Task"; };

/** get subtasks */
CList* get_subtasks();
CList* get_sub_blocks();

/** get num subtasks */
int32_t get_num_subtasks();
int32_t get_num_sub_blocks();

protected:

/** adds sub-block
* @param sub_block subtask to add
*/
void add_sub_block(CIndexBlock* sub_block);

private:

/** name of task */
const char* m_task_name;
/** name of the block */
const char* m_block_name;

/** lind */
/** min index */
index_t m_min_index;

/** rind */
/** max index */
index_t m_max_index;

/** weight */
float64_t m_weight;

/** subtasks */
CList* m_subtasks;
CList* m_sub_blocks;

};

Expand Down
53 changes: 53 additions & 0 deletions src/shogun/lib/IndexBlockGroup.cpp
@@ -0,0 +1,53 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2012 Sergey Lisitsyn
*/

#include <shogun/lib/IndexBlockGroup.h>

using namespace shogun;

CIndexBlockGroup::CIndexBlockGroup() : CIndexBlockRelation()
{
m_blocks = new CList(true);
}

CIndexBlockGroup::~CIndexBlockGroup()
{
SG_UNREF(m_blocks);
}

void CIndexBlockGroup::add_block(CIndexBlock* block)
{
m_blocks->push(block);
}

void CIndexBlockGroup::remove_block(CIndexBlock* block)
{
SG_NOTIMPLEMENTED;
}

SGVector<index_t> CIndexBlockGroup::get_SLEP_ind()
{
check_blocks_list(m_blocks);
int32_t n_sub_blocks = m_blocks->get_num_elements();
SG_DEBUG("Number of sub-blocks = %d\n", n_sub_blocks);
SGVector<index_t> ind(n_sub_blocks+1);

CIndexBlock* iterator = (CIndexBlock*)(m_blocks->get_first_element());
ind[0] = 0;
int32_t i = 0;
do
{
ind[i+1] = iterator->get_max_index();
SG_UNREF(iterator);
i++;
}
while ((iterator = (CIndexBlock*)m_blocks->get_next_element()) != NULL);

return ind;
}

0 comments on commit a75088e

Please sign in to comment.