Skip to content

Commit

Permalink
Optimize code in transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 authored and vigsterkr committed Jul 11, 2018
1 parent 9710962 commit 87e13af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
9 changes: 2 additions & 7 deletions src/shogun/preprocessor/NormOne.cpp
Expand Up @@ -9,6 +9,7 @@
#include <shogun/features/Features.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/preprocessor/DensePreprocessor.h>
#include <shogun/preprocessor/NormOne.h>

Expand Down Expand Up @@ -59,11 +60,5 @@ SGMatrix<float64_t> CNormOne::apply_to_matrix(SGMatrix<float64_t> matrix)
/// result in feature matrix
SGVector<float64_t> CNormOne::apply_to_feature_vector(SGVector<float64_t> vector)
{
float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen);
float64_t norm = std::sqrt(linalg::dot(vector, vector));

for (int32_t i=0; i<vector.vlen; i++)
normed_vec[i]=vector.vector[i]/norm;

return SGVector<float64_t>(normed_vec,vector.vlen);
return linalg::scale(vector, 1.0 / linalg::norm(vector));
}
33 changes: 11 additions & 22 deletions src/shogun/preprocessor/PruneVarSubMean.cpp
Expand Up @@ -102,7 +102,7 @@ void CPruneVarSubMean::cleanup()
SGMatrix<float64_t>
CPruneVarSubMean::apply_to_matrix(SGMatrix<float64_t> matrix)
{
ASSERT(m_initialized)
REQUIRE(m_initialized, "Transformer has not been fitted.\n");

auto num_vectors = matrix.num_cols;
auto result = matrix;
Expand Down Expand Up @@ -132,31 +132,18 @@ CPruneVarSubMean::apply_to_matrix(SGMatrix<float64_t> matrix)
/// result in feature matrix
SGVector<float64_t> CPruneVarSubMean::apply_to_feature_vector(SGVector<float64_t> vector)
{
float64_t* ret=NULL;
REQUIRE(m_initialized, "Transformer has not been fitted.\n");

if (m_initialized)
{
ret=SG_MALLOC(float64_t, m_num_idx);
SGVector<float64_t> out(m_num_idx);

if (m_divide_by_std)
{
for (int32_t i=0; i<m_num_idx; i++)
ret[i]=(vector.vector[m_idx[i]]-m_mean[i])/m_std[i];
}
else
{
for (int32_t i=0; i<m_num_idx; i++)
ret[i]=(vector.vector[m_idx[i]]-m_mean[i]);
}
}
else
for (auto i : range(m_num_idx))
{
ret=SG_MALLOC(float64_t, vector.vlen);
for (int32_t i=0; i<vector.vlen; i++)
ret[i]=vector.vector[i];
out[i] = vector[m_idx[i]] - m_mean[i];
if (m_divide_by_std)
out[i] /= m_std[i];
}

return SGVector<float64_t>(ret,m_num_idx);
return out;
}

void CPruneVarSubMean::init()
Expand All @@ -171,7 +158,9 @@ void CPruneVarSubMean::init()

void CPruneVarSubMean::register_parameters()
{
SG_ADD(&m_initialized, "initialized", "The prerpocessor is initialized", MS_NOT_AVAILABLE);
SG_ADD(
&m_initialized, "initialized", "The preprocessor is initialized",
MS_NOT_AVAILABLE);
SG_ADD(&m_divide_by_std, "divide_by_std", "Divide by standard deviation", MS_AVAILABLE);
SG_ADD(&m_num_idx, "num_idx", "Number of elements in idx_vec", MS_NOT_AVAILABLE);
SG_ADD(&m_std, "std_vec", "Standard dev vector", MS_NOT_AVAILABLE);
Expand Down

0 comments on commit 87e13af

Please sign in to comment.