Skip to content

Commit

Permalink
added new streaming data generator class for creating Gaussian blobs …
Browse files Browse the repository at this point in the history
…as in NIPS paper for optimal kernel choice for linear time MMD
  • Loading branch information
karlnapf committed Feb 13, 2013
1 parent 7e6bc9c commit 822d40a
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
@@ -0,0 +1,101 @@
/*
* 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.
*
* Written (W) 2013 Heiko Strathmann
*/

#include <shogun/features/streaming/generators/GaussianBlobsDataGenerator.h>

using namespace shogun;

CGaussianBlobsDataGenerator::CGaussianBlobsDataGenerator() :
CStreamingDenseFeatures<float64_t>()
{
init();
}

CGaussianBlobsDataGenerator::CGaussianBlobsDataGenerator(index_t sqrt_num_blobs,
float64_t distance, float64_t epsilon, float64_t angle) :
CStreamingDenseFeatures<float64_t>()
{
init();
set_blobs_model(sqrt_num_blobs, distance, epsilon, angle);
}

CGaussianBlobsDataGenerator::~CGaussianBlobsDataGenerator()
{
}

void CGaussianBlobsDataGenerator::set_blobs_model(index_t sqrt_num_blobs,
float64_t distance, float64_t epsilon, float64_t angle)
{
m_sqrt_num_blobs=sqrt_num_blobs;
m_distance=distance;
m_epsilon=epsilon;
m_angle=angle;

/* precompute cholesky decomposition, start with rotation matrix */
SGMatrix<float64_t> R(2, 2);
R(0, 0)=CMath::cos(angle);
R(0, 1)=-CMath::sin(angle);
R(1, 0)=CMath::sin(angle);
R(1, 1)=CMath::cos(angle);

/* diagonal eigenvalue matrix */
SGMatrix<float64_t> L(2, 2);
L(0, 0)=CMath::sqrt(epsilon);
L(1, 0)=0;
L(0, 1)=0;
L(1, 1)=1;

/* compute and save cholesky for sampling later on */
m_cholesky=SGMatrix<float64_t>::matrix_multiply(R, L);
}

void CGaussianBlobsDataGenerator::init()
{
SG_SWARNING("%s::init(): register parameters!\n", get_name());
m_sqrt_num_blobs=1;
m_distance=0;
m_epsilon=1;
m_angle=0;
m_cholesky=SGMatrix<float64_t>(2, 2);
m_cholesky(0, 0)=1;
m_cholesky(1, 1)=1;
}

bool CGaussianBlobsDataGenerator::get_next_example()
{
SG_SDEBUG("entering CGaussianBlobsDataGenerator::get_next_example()\n");

/* allocate space */
SGVector<float64_t> result=SGVector<float64_t>(2);

/* sample latent distribution to compute offsets */
index_t x_offset=CMath::random(0, m_sqrt_num_blobs-1)*m_distance;
index_t y_offset=CMath::random(0, m_sqrt_num_blobs-1)*m_distance;

/* sample from std Gaussian */
float64_t x=CMath::randn_double();
float64_t y=CMath::randn_double();

/* transform through cholesky and add offset */
result[0]=m_cholesky(0, 0)*x+m_cholesky(0, 1)*y+x_offset;
result[1]=m_cholesky(1, 0)*x+m_cholesky(1, 1)*y+y_offset;

/* save example back to superclass */
CGaussianBlobsDataGenerator::current_vector=result;

SG_SDEBUG("leaving CGaussianBlobsDataGenerator::get_next_example()\n");
return true;
}

void CGaussianBlobsDataGenerator::release_example()
{
SGVector<float64_t> temp=SGVector<float64_t>();
CGaussianBlobsDataGenerator::current_vector=temp;
}

@@ -0,0 +1,65 @@
/*
* 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.
*
* Written (W) 2013 Heiko Strathmann
*/

#ifndef __GAUSSIANBLOBSDATAGENERATOR_H_
#define __GAUSSIANBLOBSDATAGENERATOR_H_

#include <shogun/features/streaming/StreamingDenseFeatures.h>

namespace shogun
{

class CGaussianBlobsDataGenerator: public CStreamingDenseFeatures<float64_t>
{
public:
/** Constructor */
CGaussianBlobsDataGenerator();

/** Constructor
*/
CGaussianBlobsDataGenerator(index_t sqrt_num_blobs, float64_t distance,
float64_t epsilon, float64_t angle);

/** Destructor */
virtual ~CGaussianBlobsDataGenerator();

/** @return name of SG_SERIALIZABLE */
virtual const char* get_name() const
{
return "MeanShiftDataGenerator";
}

/*
* set the blobs model
*
*/
void set_blobs_model(index_t sqrt_num_blobs, float64_t distance,
float64_t epsilon, float64_t angle);

/** get the next example from stream */
bool get_next_example();

/** release the example when done w/ processing */
void release_example();

private:
/** registers all parameters and initializes variables with defaults */
void init();

protected:
index_t m_sqrt_num_blobs;
float64_t m_distance;
float64_t m_epsilon;
float64_t m_angle;
SGMatrix<float64_t> m_cholesky;
};

}

#endif /* __GAUSSIANBLOBSDATAGENERATOR_H_ */

0 comments on commit 822d40a

Please sign in to comment.