Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
various sgmatrix compile fixes
  • Loading branch information
Soeren Sonnenburg committed May 8, 2012
1 parent 70b2c60 commit de8733d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 74 deletions.
16 changes: 7 additions & 9 deletions src/shogun/classifier/NearestCentroid.cpp
Expand Up @@ -63,11 +63,9 @@ namespace shogun{
int32_t num_vectors = data->get_num_vectors();
int32_t num_classes = m_labels->get_num_classes();
int32_t num_feats = ((CDenseFeatures<float64_t>*)data)->get_num_features();
float64_t* centroids = SG_CALLOC(float64_t, num_feats*num_classes);
for(int32_t i=0 ; i < num_feats*num_classes ; i++)
{
centroids[i]=0;
}
SGMatrix<float64_t> centroids(num_feats,num_classes);
centroids.zero();

m_centroids->set_num_features(num_feats);
m_centroids->set_num_vectors(num_classes);

Expand All @@ -82,7 +80,7 @@ namespace shogun{
int32_t current_len;
bool current_free;
int32_t current_class = m_labels->get_label(idx);
float64_t* target = centroids + num_feats*current_class;
float64_t* target = centroids.matrix + num_feats*current_class;
float64_t* current = ((CDenseFeatures<float64_t>*)data)->get_feature_vector(idx,current_len,current_free);
CMath::add(target,1.0,target,1.0,current,current_len);
num_per_class[current_class]++;
Expand All @@ -92,7 +90,7 @@ namespace shogun{

for(int32_t i=0 ; i<num_classes ; i++)
{
float64_t* target = centroids + num_feats*i;
float64_t* target = centroids.matrix + num_feats*i;
int32_t total = num_per_class[i];
float64_t scale = 0;
if(total>1)
Expand All @@ -104,7 +102,7 @@ namespace shogun{
}

m_centroids->free_feature_matrix();
m_centroids->set_feature_matrix(centroids,num_feats,num_classes);
m_centroids->set_feature_matrix(centroids);


m_is_trained=true;
Expand All @@ -115,4 +113,4 @@ namespace shogun{
return true;
}

}
}
57 changes: 25 additions & 32 deletions src/shogun/features/DenseFeatures.cpp
Expand Up @@ -76,10 +76,10 @@ template<class ST> ST* CDenseFeatures<ST>::get_feature_vector(int32_t num, int32

len = num_features;

if (feature_matrix)
if (feature_matrix.matrix)
{
dofree = false;
return &feature_matrix[real_num * int64_t(num_features)];
return &feature_matrix.matrix[real_num * int64_t(num_features)];
}

ST* feat = NULL;
Expand Down Expand Up @@ -187,7 +187,7 @@ template<class ST> void CDenseFeatures<ST>::vector_subset(int32_t* idx, int32_t
if (m_subset_stack->has_subsets())
SG_ERROR("A subset is set, cannot call vector_subset\n");

ASSERT(feature_matrix);
ASSERT(feature_matrix.matrix);
ASSERT(idx_len<=num_vectors);

int32_t num_vec = num_vectors;
Expand All @@ -206,8 +206,8 @@ template<class ST> void CDenseFeatures<ST>::vector_subset(int32_t* idx, int32_t
if (i == ii)
continue;

memcpy(&feature_matrix[int64_t(num_features) * i],
&feature_matrix[int64_t(num_features) * ii],
memcpy(&feature_matrix.matrix[int64_t(num_features) * i],
&feature_matrix.matrix[int64_t(num_features) * ii],
num_features * sizeof(ST));
old_ii = ii;
}
Expand All @@ -218,15 +218,15 @@ template<class ST> void CDenseFeatures<ST>::feature_subset(int32_t* idx, int32_t
if (m_subset_stack->has_subsets())
SG_ERROR("A subset is set, cannot call feature_subset\n");

ASSERT(feature_matrix);
ASSERT(feature_matrix.matrix);
ASSERT(idx_len<=num_features);
int32_t num_feat = num_features;
num_features = idx_len;

for (int32_t i = 0; i < num_vectors; i++)
{
ST* src = &feature_matrix[int64_t(num_feat) * i];
ST* dst = &feature_matrix[int64_t(num_features) * i];
ST* src = &feature_matrix.matrix[int64_t(num_feat) * i];
ST* dst = &feature_matrix.matrix[int64_t(num_features) * i];

int32_t old_jj = -1;
for (int32_t j = 0; j < idx_len; j++)
Expand Down Expand Up @@ -258,6 +258,8 @@ template<class ST> SGMatrix<ST> CDenseFeatures<ST>::get_feature_matrix()
&feature_matrix.matrix[real_i * int64_t(num_features)],
num_features * sizeof(ST));
}

return submatrix;
}

template<class ST> SGMatrix<ST> CDenseFeatures<ST>::steal_feature_matrix()
Expand All @@ -283,7 +285,7 @@ template<class ST> ST* CDenseFeatures<ST>::get_feature_matrix(int32_t &num_feat,
{
num_feat = num_features;
num_vec = num_vectors;
return feature_matrix;
return feature_matrix.matrix;
}

template<class ST> CDenseFeatures<ST>* CDenseFeatures<ST>::get_transposed()
Expand Down Expand Up @@ -323,17 +325,9 @@ template<class ST> void CDenseFeatures<ST>::copy_feature_matrix(SGMatrix<ST> src
SG_ERROR("A subset is set, cannot call copy_feature_matrix\n");

free_feature_matrix();
int32_t num_feat = src.num_rows;
int32_t num_vec = src.num_cols;
feature_matrix = SG_MALLOC(ST, ((int64_t) num_feat) * num_vec);
feature_matrix_num_features = num_feat;
feature_matrix_num_vectors = num_vec;

memcpy(feature_matrix, src.matrix,
(sizeof(ST) * ((int64_t) num_feat) * num_vec));

num_features = num_feat;
num_vectors = num_vec;
feature_matrix = src.clone();
num_features = src.num_rows;
num_vectors = src.num_cols;
initialize_cache();
}

Expand All @@ -347,17 +341,15 @@ template<class ST> void CDenseFeatures<ST>::obtain_from_dot(CDotFeatures* df)
ASSERT(num_feat>0 && num_vec>0);

free_feature_matrix();
feature_matrix = SG_MALLOC(ST, ((int64_t) num_feat) * num_vec);
feature_matrix_num_features = num_feat;
feature_matrix_num_vectors = num_vec;
feature_matrix = SGMatrix<ST>(num_feat, num_vec);

for (int32_t i = 0; i < num_vec; i++)
{
SGVector<float64_t> v = df->get_computed_dot_feature_vector(i);
ASSERT(num_feat==v.vlen);

for (int32_t j = 0; j < num_feat; j++)
feature_matrix[i * int64_t(num_feat) + j] = (ST) v.vector[j];
feature_matrix.matrix[i * int64_t(num_feat) + j] = (ST) v.vector[j];
}
num_features = num_feat;
num_vectors = num_vec;
Expand All @@ -370,7 +362,7 @@ template<class ST> bool CDenseFeatures<ST>::apply_preprocessor(bool force_prepro

SG_DEBUG( "force: %d\n", force_preprocessing);

if (feature_matrix && get_num_preprocessors())
if (feature_matrix.matrix && get_num_preprocessors())
{
for (int32_t i = 0; i < get_num_preprocessors(); i++)
{
Expand All @@ -385,7 +377,8 @@ template<class ST> bool CDenseFeatures<ST>::apply_preprocessor(bool force_prepro
{
SG_UNREF(p);
return false;
}SG_UNREF(p);
}
SG_UNREF(p);

}
}
Expand All @@ -394,7 +387,7 @@ template<class ST> bool CDenseFeatures<ST>::apply_preprocessor(bool force_prepro
}
else
{
if (!feature_matrix)
if (!feature_matrix.matrix)
SG_ERROR( "no feature matrix\n");

if (!get_num_preprocessors())
Expand Down Expand Up @@ -560,7 +553,7 @@ template<class ST> CFeatures* CDenseFeatures<ST>::copy_subset(SGVector<index_t>
{
index_t real_idx=m_subset_stack->subset_idx_conversion(indices.vector[i]);
memcpy(&feature_matrix_copy.matrix[i*num_features],
&feature_matrix[real_idx*num_features],
&feature_matrix.matrix[real_idx*num_features],
num_features*sizeof(ST));
}

Expand All @@ -580,16 +573,16 @@ template<class ST> void CDenseFeatures<ST>::init()
num_vectors = 0;
num_features = 0;

feature_matrix = SGMatrix();
feature_matrix = SGMatrix<ST>();
feature_cache = NULL;

set_generic<ST>();

/* not store number of vectors in subset */
SG_ADD(&num_vectors, "num_vectors", "Number of vectors.");
SG_ADD(&num_features, "num_features", "Number of features.");
SG_ADD(&num_vectors, "num_vectors", "Number of vectors.", MS_NOT_AVAILABLE);
SG_ADD(&num_features, "num_features", "Number of features.", MS_NOT_AVAILABLE);
SG_ADD(&feature_matrix, "feature_matrix",
"Matrix of feature vectors / 1 vector per column.");
"Matrix of feature vectors / 1 vector per column.", MS_NOT_AVAILABLE);
}

#define GET_FEATURE_TYPE(f_type, sg_type) \
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/features/FKFeatures.cpp
Expand Up @@ -226,7 +226,7 @@ float64_t* CFKFeatures::set_feature_matrix()

SG_INFO( "allocating FK feature cache of size %.2fM\n", sizeof(float64_t)*num_features*num_vectors/1024.0/1024.0);
free_feature_matrix();
feature_matrix=SG_MALLOC(float64_t, num_features*num_vectors);
feature_matrix=SGMatrix<float64_t>(num_features,num_vectors);

SG_INFO( "calculating FK feature matrix\n");

Expand All @@ -237,15 +237,15 @@ float64_t* CFKFeatures::set_feature_matrix()
else if (!(x % (num_vectors/200+1)))
SG_DEBUG(".");

compute_feature_vector(&feature_matrix[x*num_features], x, len);
compute_feature_vector(&feature_matrix.matrix[x*num_features], x, len);
}

SG_DONE();

num_vectors=get_num_vectors();
num_features=get_num_features();

return feature_matrix;
return feature_matrix.matrix;
}

void CFKFeatures::init()
Expand Down
9 changes: 4 additions & 5 deletions src/shogun/features/RealFileFeatures.cpp
Expand Up @@ -62,7 +62,6 @@ CRealFileFeatures::CRealFileFeatures(int32_t size, FILE* file)

CRealFileFeatures::~CRealFileFeatures()
{
SG_FREE(feature_matrix);
SG_FREE(working_filename);
SG_FREE(labels);
}
Expand Down Expand Up @@ -97,11 +96,11 @@ float64_t* CRealFileFeatures::load_feature_matrix()
{
ASSERT(working_file);
fseek(working_file, filepos, SEEK_SET);
SG_FREE(feature_matrix);
free_feature_matrix();

SG_INFO( "allocating feature matrix of size %.2fM\n", sizeof(double)*num_features*num_vectors/1024.0/1024.0);
free_feature_matrix();
feature_matrix=SG_MALLOC(float64_t, num_features*num_vectors);
feature_matrix=SGMatrix<float64_t>(num_features,num_vectors);

SG_INFO( "loading... be patient.\n");

Expand All @@ -112,11 +111,11 @@ float64_t* CRealFileFeatures::load_feature_matrix()
else if (!(i % (num_vectors/200+1)))
SG_PRINT( ".");

ASSERT(fread(&feature_matrix[num_features*i], doublelen, num_features, working_file)==(size_t) num_features);
ASSERT(fread(&feature_matrix.matrix[num_features*i], doublelen, num_features, working_file)==(size_t) num_features);
}
SG_DONE();

return feature_matrix;
return feature_matrix.matrix;
}

int32_t CRealFileFeatures::get_label(int32_t idx)
Expand Down
12 changes: 6 additions & 6 deletions src/shogun/kernel/AUCKernel.cpp
Expand Up @@ -69,7 +69,7 @@ CLabels* CAUCKernel::setup_auc_maximization(CLabels* labels)
int32_t num_auc = num_pos*num_neg;
SG_INFO("num_pos: %i num_neg: %i num_auc: %i\n", num_pos, num_neg, num_auc);

uint16_t* features_auc = SG_MALLOC(uint16_t, num_auc*2);
SGMatrix<uint16_t> features_auc(2,num_auc);
int32_t* labels_auc = SG_MALLOC(int32_t, num_auc);
int32_t n=0 ;

Expand All @@ -86,14 +86,14 @@ CLabels* CAUCKernel::setup_auc_maximization(CLabels* labels)
// create about as many positively as negatively labeled examples
if (n%2==0)
{
features_auc[n*2]=i;
features_auc[n*2+1]=j;
features_auc.matrix[n*2]=i;
features_auc.matrix[n*2+1]=j;
labels_auc[n]=1;
}
else
{
features_auc[n*2]=j;
features_auc[n*2+1]=i;
features_auc.matrix[n*2]=j;
features_auc.matrix[n*2+1]=i;
labels_auc[n]=-1;
}

Expand All @@ -109,7 +109,7 @@ CLabels* CAUCKernel::setup_auc_maximization(CLabels* labels)

// create feature object
CDenseFeatures<uint16_t>* f = new CDenseFeatures<uint16_t>(0);
f->set_feature_matrix(features_auc, 2, num_auc);
f->set_feature_matrix(features_auc);

// create AUC kernel and attach the features
init(f,f);
Expand Down
31 changes: 13 additions & 18 deletions src/shogun/preprocessor/RandomFourierGaussPreproc.cpp
Expand Up @@ -383,34 +383,29 @@ SGMatrix<float64_t> CRandomFourierGaussPreproc::apply_to_feature_matrix(CFeature
"float64_t * CRandomFourierGaussPreproc::apply_to_feature_matrix(CFeatures *f): num_features!=cur_dim_input_space is not allowed\n");
}

if (m) {
float64_t* res = SG_MALLOC(float64_t, num_vectors * cur_dim_feature_space);
if (res == NULL) {
throw ShogunException(
"CRandomFourierGaussPreproc::apply_to_feature_matrix(...): memory allocation failed \n");
}
if (m)
{
SGMatrix<float64_t> res(cur_dim_feature_space,num_vectors);

float64_t val = CMath::sqrt(2.0 / cur_dim_feature_space);

for (int32_t vec = 0; vec < num_vectors; vec++) {
for (int32_t od = 0; od < cur_dim_feature_space; ++od) {
res[od + vec * cur_dim_feature_space] = val * cos(
for (int32_t vec = 0; vec < num_vectors; vec++)
{
for (int32_t od = 0; od < cur_dim_feature_space; ++od)
{
res.matrix[od + vec * cur_dim_feature_space] = val * cos(
randomcoeff_additive[od]
+ CMath::dot(m+vec * num_features,
randomcoeff_multiplicative+od*cur_dim_input_space,
cur_dim_input_space));
}
}
((CDenseFeatures<float64_t>*) features)->set_feature_matrix(res,
cur_dim_feature_space, num_vectors);
((CDenseFeatures<float64_t>*) features)->set_feature_matrix(res);

m = ((CDenseFeatures<float64_t>*) features)->get_feature_matrix(
num_features, num_vectors);
ASSERT(num_features==cur_dim_feature_space);

return SGMatrix<float64_t>(res,num_vectors,cur_dim_feature_space);
} else {
return SGMatrix<float64_t>();
return res;
}
else
return SGMatrix<float64_t>();
}

void CRandomFourierGaussPreproc::cleanup()
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/ui/GUIClassifier.cpp
Expand Up @@ -1167,7 +1167,7 @@ CLabels* CGUIClassifier::classify()
break;
};

return false;
return NULL;
}

CLabels* CGUIClassifier::classify_kernelmachine()
Expand Down

0 comments on commit de8733d

Please sign in to comment.