Skip to content

Commit

Permalink
More uniform API and initial embedding fixes (#35)
Browse files Browse the repository at this point in the history
* Some fixes and convenience functions

* Formatting

* Add _presetEmbedding option to tsne parameters

* Fix: Use preset embedding
  • Loading branch information
alxvth authored Apr 22, 2024
1 parent 9020e44 commit 1460636
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ namespace hdi{
void reset();
//! Reset the class and remove all the data points
void clear();


//! Wheter initialization flag is true
bool isInitialized() { return _initialized == true; }

//! Get the position in the embedding for a data point
void getEmbeddingPosition(scalar_vector_type& embedding_position, data_handle_type handle)const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ namespace hdi{
utils::secureLogValue(_logger,"Number of data points",_P.size());

computeHighDimensionalDistribution(probabilities);
initializeEmbeddingPosition(params._seed, params._rngRange);

if (!params._presetEmbedding) {
initializeEmbeddingPosition(_params._seed, _params._rngRange);
}

_iteration = 0;

Expand Down Expand Up @@ -152,7 +155,10 @@ namespace hdi{
utils::secureLogValue(_logger,"Number of data points",_P.size());

_P = distribution;
initializeEmbeddingPosition(params._seed, params._rngRange);

if (!params._presetEmbedding) {
initializeEmbeddingPosition(_params._seed, _params._rngRange);
}

_iteration = 0;

Expand Down
4 changes: 4 additions & 0 deletions hdi/dimensionality_reduction/tsne.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace hdi{
double _mom_switching_iter;
double _exaggeration_factor;
int _remove_exaggeration_iter;
bool _presetEmbedding;
};


Expand All @@ -95,6 +96,9 @@ namespace hdi{
//! Return the number of data points
int size(){ return static_cast<int>(_high_dimensional_data.size()); }

//! Wheter initialization flag is true
bool isInitialized() { return _initialized == true; }

//! Get the dimensionality of the data
int dimensionality(){return _dimensionality;}
//! Get the high dimensional descriptor for a data point
Expand Down
10 changes: 6 additions & 4 deletions hdi/dimensionality_reduction/tsne_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ namespace hdi{
_final_momentum(0.8),
_mom_switching_iter(250),
_exaggeration_factor(4),
_remove_exaggeration_iter(250)
_remove_exaggeration_iter(250),
_presetEmbedding(false)
{}

/////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -143,13 +144,14 @@ namespace hdi{
//compute distances between data-points
computeHighDimensionalDistances();
//Compute gaussian distributions
computeGaussianDistributions(params._perplexity);
computeGaussianDistributions(_init_params._perplexity);
//Compute High-dimensional distribution
computeHighDimensionalDistribution();


//Initialize Embedding position
initializeEmbeddingPosition(params._seed);
if (!_init_params._presetEmbedding) {
initializeEmbeddingPosition(_init_params._seed);
}

_iteration = 0;

Expand Down
2 changes: 1 addition & 1 deletion hdi/dimensionality_reduction/tsne_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace hdi {
double _exaggeration_factor; //! exaggeration factor for the attractive forces. Note: it shouldn't be too high when few points are used
unsigned int _remove_exaggeration_iter; //! iterations with complete exaggeration of the attractive forces
unsigned int _exponential_decay_iter; //! iterations required to remove the exaggeration using an exponential decay
bool _presetEmbedding; //! Used at initialization to use the input embedding positions
bool _presetEmbedding; //! Used at initialization to use the input embedding positions
};
}
}
Expand Down
31 changes: 6 additions & 25 deletions hdi/dimensionality_reduction/wtsne.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <unordered_map>
#include "hdi/data/embedding.h"
#include "hdi/data/map_mem_eff.h"
#include "tsne_parameters.h"

namespace hdi{
namespace dr{
Expand All @@ -57,37 +58,19 @@ namespace hdi{
typedef std::vector<scalar_type> scalar_vector_type;
typedef uint32_t data_handle_type;

public:
//! Parameters used for the initialization of the algorithm
class Parameters{
public:
Parameters();
int _seed;
int _embedding_dimensionality;

double _minimum_gain;
double _eta; //! constant multiplicator of the gradient
double _momentum;
double _final_momentum;
double _mom_switching_iter; //! momentum switching iteration
double _exaggeration_factor; //! exaggeration factor for the attractive forces. Note: it shouldn't be too high when few points are used
unsigned int _remove_exaggeration_iter; //! iterations with complete exaggeration of the attractive forces
unsigned int _exponential_decay_iter; //! iterations required to remove the exaggeration using an exponential decay
};


public:
WeightedTSNE();
//! Initialize the class with a list of distributions. A joint-probability distribution will be computed as in the tSNE algorithm
void initialize(const sparse_scalar_matrix& probabilities, data::Embedding<scalar_type>* embedding, Parameters params = Parameters());
void initialize(const sparse_scalar_matrix& probabilities, data::Embedding<scalar_type>* embedding, TsneParameters params = TsneParameters());
//! Initialize the class with a joint-probability distribution. Note that it must be provided non initialized and with the weight of each row equal to 2.
void initializeWithJointProbabilityDistribution(const sparse_scalar_matrix& distribution, data::Embedding<scalar_type>* embedding, Parameters params = Parameters());
void initializeWithJointProbabilityDistribution(const sparse_scalar_matrix& distribution, data::Embedding<scalar_type>* embedding, TsneParameters params = TsneParameters());
//! Reset the internal state of the class but it keeps the inserted data-points
void reset();
//! Reset the class and remove all the data points
void clear();


bool isInitialized() { return _initialized == true; }

//! Get the position in the embedding for a data point
void getEmbeddingPosition(scalar_vector_type& embedding_position, data_handle_type handle)const;

Expand Down Expand Up @@ -141,8 +124,6 @@ namespace hdi{
//! Compute the exaggeration factor based on the current iteration
scalar_type exaggerationFactor();



private:
data::Embedding<scalar_type>* _embedding; //! embedding
typename data::Embedding<scalar_type>::scalar_vector_type* _embedding_container;
Expand All @@ -159,7 +140,7 @@ namespace hdi{
scalar_vector_type _gain; //! Gain
scalar_type _theta; //! value of theta used in the Barnes-Hut approximation. If a value of 1 is provided the exact tSNE computation is used.

Parameters _params;
TsneParameters _params;
unsigned int _iteration;

utils::AbstractLog* _logger;
Expand Down
51 changes: 12 additions & 39 deletions hdi/dimensionality_reduction/wtsne_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,8 @@
#include <dispatch/dispatch.h>
#endif

#pragma warning( push )
#pragma warning( disable : 4267)
#pragma warning( push )
#pragma warning( disable : 4291)
#pragma warning( push )
#pragma warning( disable : 4996)
#pragma warning( push )
#pragma warning( disable : 4018)
#pragma warning( push )
#pragma warning( disable : 4244)
//#define FLANN_USE_CUDA
#include "flann/flann.h"
#pragma warning( pop )
#pragma warning( pop )
#pragma warning( pop )
#pragma warning( pop )
#pragma warning( pop )

namespace hdi{
namespace dr{
/////////////////////////////////////////////////////////////////////////

template <typename scalar, typename sparse_scalar_matrix>
WeightedTSNE<scalar, sparse_scalar_matrix>::Parameters::Parameters():
_seed(-1),
_embedding_dimensionality(2),
_minimum_gain(0.1),
_eta(200),
_momentum(0.2),
_final_momentum(0.5),
_mom_switching_iter(250),
_exaggeration_factor(4),
_remove_exaggeration_iter(250),
_exponential_decay_iter(150)
{}

/////////////////////////////////////////////////////////////////////////

template <typename scalar, typename sparse_scalar_matrix>
Expand Down Expand Up @@ -119,7 +85,7 @@ namespace hdi{


template <typename scalar, typename sparse_scalar_matrix>
void WeightedTSNE<scalar, sparse_scalar_matrix>::initialize(const sparse_scalar_matrix& probabilities, data::Embedding<scalar_type>* embedding, Parameters params){
void WeightedTSNE<scalar, sparse_scalar_matrix>::initialize(const sparse_scalar_matrix& probabilities, data::Embedding<scalar_type>* embedding, TsneParameters params){
utils::secureLog(_logger,"Initializing W-tSNE...");
{//Aux data
_params = params;
Expand All @@ -139,7 +105,11 @@ namespace hdi{
utils::secureLogValue(_logger,"Number of data points",_P.size());

computeHighDimensionalDistribution(probabilities);
initializeEmbeddingPosition(params._seed);

if (!params._presetEmbedding) {
initializeEmbeddingPosition(_params._seed, _params._rngRange);
}

computeWeights();

_iteration = 0;
Expand All @@ -149,7 +119,7 @@ namespace hdi{
}

template <typename scalar, typename sparse_scalar_matrix>
void WeightedTSNE<scalar, sparse_scalar_matrix>::initializeWithJointProbabilityDistribution(const sparse_scalar_matrix& distribution, data::Embedding<scalar_type>* embedding, Parameters params){
void WeightedTSNE<scalar, sparse_scalar_matrix>::initializeWithJointProbabilityDistribution(const sparse_scalar_matrix& distribution, data::Embedding<scalar_type>* embedding, TsneParameters params){
utils::secureLog(_logger,"Initializing W-tSNE with a user-defined joint-probability distribution...");
{//Aux data
_params = params;
Expand All @@ -166,11 +136,14 @@ namespace hdi{
_gain.resize(size*params._embedding_dimensionality,1);
}


utils::secureLogValue(_logger,"Number of data points",_P.size());

_P = distribution;
initializeEmbeddingPosition(params._seed);

if (!params._presetEmbedding) {
initializeEmbeddingPosition(_params._seed, _params._rngRange);
}

computeWeights();

_iteration = 0;
Expand Down

0 comments on commit 1460636

Please sign in to comment.