Skip to content

Commit

Permalink
udpate test
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabh7 committed Jun 29, 2016
1 parent eb3941a commit aed17e6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/shogun/features/DenseFeatures.cpp
Expand Up @@ -639,7 +639,6 @@ CFeatures* CDenseFeatures<ST>::copy_dimension_subset(SGVector<index_t> dims)
template<class ST>
CFeatures* CDenseFeatures<ST>::shallow_subset_copy()
{
SG_SDEBUG("Entering!\n");
CFeatures* shallow_copy_features=nullptr;

SG_SDEBUG("Using underlying feature matrix with %d dimensions and %d feature vectors!\n", num_features, num_vectors);
Expand All @@ -648,7 +647,6 @@ CFeatures* CDenseFeatures<ST>::shallow_subset_copy()
if (m_subset_stack->has_subsets())
shallow_copy_features->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());

SG_SDEBUG("Leaving!\n");
return shallow_copy_features;
}

Expand Down
1 change: 0 additions & 1 deletion src/shogun/labels/MulticlassLabels.cpp
Expand Up @@ -145,6 +145,5 @@ CLabels* CMulticlassLabels::shallow_subset_copy()
if (m_subset_stack->has_subsets())
shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());

SG_SDEBUG("Leaving!\n");
return shallow_copy_labels;
}
22 changes: 13 additions & 9 deletions src/shogun/machine/BaggingMachine.cpp
Expand Up @@ -131,16 +131,16 @@ bool CBaggingMachine::train_machine(CFeatures* data)

/*
TODO: enable multi-threaded learning. This requires views support
on CFeatures
#pragma omp parallel for num_threads(parallel->get_num_threads())
*/
on CFeatures*/
#pragma omp parallel for
for (int32_t i = 0; i < m_num_bags; ++i)
{
CMachine* c=dynamic_cast<CMachine*>(m_machine->clone());
ASSERT(c != NULL);
SGVector<index_t> idx(get_bag_size());
idx.random(0, m_features->get_num_vectors()-1);
m_labels->add_subset(idx);
CLabels* labels = m_labels->shallow_subset_copy();
labels->add_subset(idx);
/* TODO:
if it's a binary labeling ensure that
there's always samples of both classes
Expand All @@ -157,19 +157,23 @@ bool CBaggingMachine::train_machine(CFeatures* data)
}
}
*/
m_features->add_subset(idx);
CFeatures* features = m_features->shallow_subset_copy();
features->add_subset(idx);
set_machine_parameters(c,idx);
c->set_labels(m_labels);
c->train(m_features);
m_features->remove_subset();
m_labels->remove_subset();
c->set_labels(labels);
c->train(features);
features->remove_subset();
labels->remove_subset();

#pragma omp critical
{
// get out of bag indexes
CDynamicArray<index_t>* oob = get_oob_indices(idx);
m_oob_indices->push_back(oob);

// add trained machine to bag array
m_bags->push_back(c);
}

SG_UNREF(c);
}
Expand Down
7 changes: 3 additions & 4 deletions src/shogun/multiclass/tree/CARTree.cpp
Expand Up @@ -603,8 +603,6 @@ int32_t CCARTree::compute_best_attribute(const SGMatrix<float64_t>& mat, const S
{
SGVector<float64_t> feats(num_vecs);
SGVector<index_t> sorted_args(num_vecs);
SGVector<int32_t> temp_count_indices(count_indices.size());
memcpy(temp_count_indices.vector, count_indices.vector, sizeof(int32_t)*count_indices.size());

if (m_pre_sort)
{
Expand All @@ -615,12 +613,13 @@ int32_t CCARTree::compute_best_attribute(const SGMatrix<float64_t>& mat, const S
{
if (indices_mask[sorted_indices[j]]>=0)
{
while(temp_count_indices[sorted_indices[j]]>0)
int32_t count_idx = count_indices[sorted_indices[j]];
while(count_idx>0)
{
feats[count]=temp_col[j];
sorted_args[count]=indices_mask[sorted_indices[j]];
++count;
--temp_count_indices[sorted_indices[j]];
--count_idx;
}
if (count==num_vecs)
break;
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/features/DenseFeatures_unittest.cc
Expand Up @@ -119,3 +119,33 @@ TEST(DenseFeaturesTest, copy_dimension_subset_with_subsets)
SG_UNREF(features);
SG_UNREF(f_reduced);
}

TEST(DenseFeaturesTest, shallow_copy_subset_data)
{
index_t dim=5;
index_t n=10;

SGMatrix<float64_t> data(dim, n);
for (index_t i=0; i<dim; ++i)
for (index_t j=0; j<n; ++j)
data(i,j)=CMath::random(0, n-1);

SGVector<index_t> inds(n/2);
for (index_t i=0; i<inds.vlen; ++i)
inds[i]=CMath::random(0, n-1);

CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(data);
features->add_subset(inds);
CFeatures* features_copy = features->shallow_subset_copy();

SGMatrix<float64_t> orig_matrix=features->get_feature_matrix();
SGMatrix<float64_t> copy_matrix=static_cast<CDenseFeatures<float64_t>*>(features_copy)->get_feature_matrix();


for (index_t i=0; i<dim; ++i)
for (index_t j=0; j<inds.size(); ++j)
EXPECT_EQ(orig_matrix(i,j), copy_matrix(i,j));

SG_UNREF(features_copy);
SG_UNREF(features);
}

0 comments on commit aed17e6

Please sign in to comment.