Skip to content

Commit

Permalink
expand_if_required now more accurate and sort_features modified for c…
Browse files Browse the repository at this point in the history
…orrect memory freeing in StreamingSparseFeatures.
  • Loading branch information
frx committed Aug 28, 2011
1 parent 04d6769 commit 620710c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion data
Submodule data updated 2 files
+0 −3 toy/hemisphere.dat
+0 −1,000 toy/hemisphere_color.dat
37 changes: 20 additions & 17 deletions src/shogun/features/StreamingSparseFeatures.h
Expand Up @@ -208,11 +208,11 @@ template <class T> class CStreamingSparseFeatures : public CStreamingDotFeatures
inline virtual void expand_if_required(float32_t*& vec, int32_t &len)
{
int32_t dim = get_dim_feature_space();
if (dim+1 > len)
if (dim > len)
{
vec = SG_REALLOC(float32_t, vec, dim+1);
memset(&vec[len], 0, (dim+1-len) * sizeof(float32_t));
len = dim+1;
vec = SG_REALLOC(float32_t, vec, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float32_t));
len = dim;
}
}

Expand All @@ -227,11 +227,11 @@ template <class T> class CStreamingSparseFeatures : public CStreamingDotFeatures
inline virtual void expand_if_required(float64_t*& vec, int32_t &len)
{
int32_t dim = get_dim_feature_space();
if (dim+1 > len)
if (dim > len)
{
vec = SG_REALLOC(float64_t, vec, dim+1);
memset(&vec[len], 0, (dim+1-len) * sizeof(float64_t));
len = dim+1;
vec = SG_REALLOC(float64_t, vec, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float64_t));
len = dim;
}
}

Expand Down Expand Up @@ -487,6 +487,8 @@ template <class T> class CStreamingSparseFeatures : public CStreamingDotFeatures

/**
* Ensure features of the current vector are in ascending order.
* It modifies the current_vector in-place, though a temporary
* vector is created and later freed.
*/
void sort_features()
{
Expand All @@ -511,15 +513,17 @@ template <class T> class CStreamingSparseFeatures : public CStreamingDotFeatures
for (int32_t i=0; i<len; i++)
sf_new[i]=sf_orig[orig_idx[i]];

current_vector=sf_new;

// sanity check
for (int32_t i=0; i<len-1; i++)
ASSERT(sf_new[i].feat_index<sf_new[i+1].feat_index);

// Copy new vector back to original
for (int32_t i=0; i<len; i++)
sf_orig[i]=sf_new[i];

SG_FREE(orig_idx);
SG_FREE(feat_idx);
SG_FREE(sf_orig);
SG_FREE(sf_new);
}

/**
Expand Down Expand Up @@ -603,10 +607,6 @@ template <class T> class CStreamingSparseFeatures : public CStreamingDotFeatures
void init(CStreamingFile *file, bool is_labelled, int32_t size);

protected:

/// feature weighting in combined dot features
float32_t combined_weight;

/// The parser object, which reads from input and returns parsed example objects.
CInputParser< SGSparseVectorEntry<T> > parser;

Expand Down Expand Up @@ -707,15 +707,18 @@ bool CStreamingSparseFeatures<T>::get_next_example()
current_length,
current_label);

if (!ret_value)
return false;

// Update number of features based on highest index
for (int32_t i=0; i<current_length; i++)
{
if (current_vector[i].feat_index > current_num_features)
current_num_features = current_vector[i].feat_index+1;
}

current_vec_index++;
return ret_value;

return true;
}

template <class T>
Expand Down

0 comments on commit 620710c

Please sign in to comment.