Xmipp  v3.23.11-Nereus
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members

#include <kerdensom.h>

Inheritance diagram for KerDenSOM:
Inheritance graph
[legend]
Collaboration diagram for KerDenSOM:
Collaboration graph
[legend]

Public Member Functions

 KerDenSOM (double _reg0, double _reg1, unsigned long _annSteps, double _epsilon, unsigned long _nSteps)
 
virtual ~KerDenSOM ()
 
void nSteps (const unsigned long &_nSteps)
 
void setAnnSteps (const unsigned long &_annSteps)
 
virtual void train (FuzzyMap &_som, TS &_examples, FileName &_fn_vectors, bool _update=false, double _sigma=0, bool _saveIntermediate=false)=0
 
virtual double test (const FuzzyMap &_som, const TS &_examples) const
 
virtual double functional (const TS *_examples, const FuzzyMap *_som, double _sigma, double _reg, double &_likelihood, double &_penalty)=0
 
- Public Member Functions inherited from ClassificationAlgorithm< FuzzyMap >
 ClassificationAlgorithm (const std::string &_ID="")
 
virtual ~ClassificationAlgorithm ()
 
virtual void train (DS &_ds, const TS &_examples) const
 
virtual void train (DS &_ds, TS &_examples) const
 
virtual double test (const DS &_ds, const TS &_examples) const=0
 
virtual void printSelf (std::ostream &_os) const
 
virtual const std::string & setID () const
 
virtual std::string & setID ()
 
void setListener (BaseListener *_listener)
 

Protected Member Functions

virtual void train (FuzzyMap &_som, const TS &_examples) const
 
virtual double updateU (FuzzyMap *_som, const TS *_examples, const double &_sigma, double &_alpha)=0
 
virtual double updateSigmaII (FuzzyMap *_som, const TS *_examples, const double &_reg, const double &_alpha)=0
 
virtual double codeDens (const FuzzyMap *_som, const FeatureVector *_example, double _sigma) const =0
 
virtual void updateV (FuzzyMap *_som, const TS *_examples, const double &_sigma)
 
virtual double mainIterations (FuzzyMap *_som, const TS *_examples, double &_sigma, const double &_reg)
 
virtual void initU (FuzzyMap *_som)
 
virtual void updateV1 (FuzzyMap *_som, const TS *_examples)
 
virtual void updateU1 (FuzzyMap *_som, const TS *_examples)
 
virtual double updateSigmaI (FuzzyMap *_som, const TS *_examples)
 
void showX (const TS *_ts)
 
void showV (FuzzyMap *_som)
 
void showU (FuzzyMap *_som, const TS *_ts)
 

Protected Attributes

double sigma
 
size_t annSteps
 
double reg0
 
double reg1
 
double epsilon
 
size_t somNSteps
 
size_t numNeurons
 
size_t numVectors
 
size_t dim
 
std::vector< std::vector< double > > tmpMap
 
std::vector< double > tmpD
 
std::vector< double > tmpD1
 
std::vector< double > tmpDens
 
std::vector< double > tmpV
 
- Protected Attributes inherited from ClassificationAlgorithm< FuzzyMap >
std::string ID
 
BaseListenerlistener
 

Additional Inherited Members

- Public Types inherited from ClassificationAlgorithm< FuzzyMap >
typedef FuzzyMap DS
 
typedef DS::TS TS
 

Detailed Description

This class trains a Smoothly Distributed Kernel Probability Density Estimator Self Organizing Map

Definition at line 44 of file kerdensom.h.

Constructor & Destructor Documentation

◆ KerDenSOM()

KerDenSOM::KerDenSOM ( double  _reg0,
double  _reg1,
unsigned long  _annSteps,
double  _epsilon,
unsigned long  _nSteps 
)
inline

Constructs the algorithm Parameter: _reg0 Initial regularization factor Parameter: _reg1 Final regularization factor Parameter: _annSteps Number of steps in deterministic annealing Parameter: _epsilon Stopping criterion Parameter: _nSteps Number of training steps

Definition at line 56 of file kerdensom.h.

58  : ClassificationAlgorithm<FuzzyMap>(), annSteps(_annSteps), reg0(_reg0), reg1(_reg1),
59  epsilon(_epsilon), somNSteps(_nSteps)
60  {};
double reg0
Definition: kerdensom.h:125
size_t somNSteps
Definition: kerdensom.h:128
double epsilon
Definition: kerdensom.h:127
size_t annSteps
Definition: kerdensom.h:124
double reg1
Definition: kerdensom.h:126

◆ ~KerDenSOM()

virtual KerDenSOM::~KerDenSOM ( )
inlinevirtual

Virtual destructor

Definition at line 65 of file kerdensom.h.

66  {};

Member Function Documentation

◆ codeDens()

virtual double KerDenSOM::codeDens ( const FuzzyMap _som,
const FeatureVector _example,
double  _sigma 
) const
protectedpure virtual

Implemented in GaussianKerDenSOM.

◆ functional()

virtual double KerDenSOM::functional ( const TS _examples,
const FuzzyMap _som,
double  _sigma,
double  _reg,
double &  _likelihood,
double &  _penalty 
)
pure virtual

Determines the functional value. Returns the likelihood and penalty parts of the functional

◆ initU()

void KerDenSOM::initU ( FuzzyMap _som)
protectedvirtual

Special Initialization of the Us

Definition at line 334 of file kerdensom.cpp.

335 {
336  unsigned cc;
337  unsigned vv;
338 
339  // Take random samples
341  for (vv = 0; vv < numVectors; vv++)
342  {
343  double t = 0.;
344  for (cc = 0; cc < numNeurons; cc++)
345  {
346  _som->memb[vv][cc] = (floatFeature) rnd_unif();
347  t += _som->memb[vv][cc];
348  }
349  for (cc = 0; cc < numNeurons; cc++)
350  _som->memb[vv][cc] /= (floatFeature) t;
351  }
352 }
MM memb
Alias for Fuzzy vectors.
float floatFeature
Definition: data_types.h:72
size_t numNeurons
Definition: kerdensom.h:133
double rnd_unif()
double vv
unsigned int randomize_random_generator()
size_t numVectors
Definition: kerdensom.h:134

◆ mainIterations()

double KerDenSOM::mainIterations ( FuzzyMap _som,
const TS _examples,
double &  _sigma,
const double &  _reg 
)
protectedvirtual

Definition at line 173 of file kerdensom.cpp.

174 {
175  int verbosity = listener->getVerbosity();
176  double stopError = 1e10;
177  double alpha;
178  double ts2;
179  size_t iter = 0;
180  if (somNSteps == 0)
181  return stopError;
182  if (verbosity == 1 || verbosity == 3)
184  do
185  {
186  iter++;
187  updateU(_som, _examples, _sigma, alpha);
188  ts2 = updateSigmaII(_som, _examples, _reg, alpha);
189  stopError = fabs(_sigma / ts2 - 1.);
190  _sigma = ts2;
191  updateV(_som, _examples, _reg);
192  if (verbosity == 1 || verbosity == 3)
193  listener->OnProgress(iter);
194  if (verbosity >= 2)
195  {
196  char s[100];
197  sprintf(s, "Iteration %d of %d. variation: %g\n", (int)iter, (int)somNSteps, stopError);
198  listener->OnReportOperation((std::string) s);
199  }
200  }
201  while ((stopError > epsilon) && (iter < somNSteps));
202  if (verbosity == 1 || verbosity == 3)
204  return stopError;
205 }
virtual void OnReportOperation(const std::string &_rsOp)=0
virtual double updateSigmaII(FuzzyMap *_som, const TS *_examples, const double &_reg, const double &_alpha)=0
size_t somNSteps
Definition: kerdensom.h:128
virtual void OnInitOperation(unsigned long _est_it)=0
double epsilon
Definition: kerdensom.h:127
virtual const unsigned & getVerbosity() const
Definition: xmipp_funcs.h:1065
glob_prnt iter
virtual double updateU(FuzzyMap *_som, const TS *_examples, const double &_sigma, double &_alpha)=0
virtual void OnProgress(unsigned long _it)=0
virtual void updateV(FuzzyMap *_som, const TS *_examples, const double &_sigma)
Definition: kerdensom.cpp:116

◆ nSteps()

void KerDenSOM::nSteps ( const unsigned long &  _nSteps)

Sets the number of training steps Parameter: _nSteps Number of training steps

Definition at line 40 of file kerdensom.cpp.

41 {
42  somNSteps = _nSteps;
43 }
size_t somNSteps
Definition: kerdensom.h:128

◆ setAnnSteps()

void KerDenSOM::setAnnSteps ( const unsigned long &  _annSteps)

Sets the number of deterministic annealing training steps Parameter: _annSteps Number of steps

Definition at line 61 of file kerdensom.cpp.

62 {
63  annSteps = _annSteps;
64 }
size_t annSteps
Definition: kerdensom.h:124

◆ showU()

void KerDenSOM::showU ( FuzzyMap _som,
const TS _ts 
)
protected

Shows the U ("Membership")

Definition at line 462 of file kerdensom.cpp.

463 {
464  std::cout << " Memberships (1..nd,1..ni,1..nj)" << std::endl;
465  for (size_t i = 0; i < _ts->size(); i++)
466  {
467  for (size_t j = 0; j < _som->size(); j++)
468  {
469  int tmpj = _som->indexToPos(j).first;
470  int tmpi = _som->indexToPos(j).second;
471  std::cout << i + 1 << " " << tmpi + 1 << " " << tmpj + 1 << " " << _som->memb[i][j] << std::endl;
472  }
473  }
474 }
MM memb
Alias for Fuzzy vectors.
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:1033
#define i
#define j

◆ showV()

void KerDenSOM::showV ( FuzzyMap _som)
protected

Shows the code vectors

Definition at line 445 of file kerdensom.cpp.

446 {
447  std::cout << "Code vectors (1..ni, 1..nj, 1..nv) " << std::endl;
448  for (size_t i = 0; i < _som->size(); i++)
449  {
450  int tmpj = _som->indexToPos(i).first;
451  int tmpi = _som->indexToPos(i).second;
452  for (size_t j = 0; j < _som->theItems[0].size(); j++)
453  std::cout << tmpi + 1 << " " << tmpj + 1 << " " << j + 1 << " " << _som->theItems[i][j] << std::endl;
454  }
455 }
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:1033
#define i
std::vector< Item > theItems
Definition: training_set.h:84
#define j

◆ showX()

void KerDenSOM::showX ( const TS _ts)
protected

Shows the data

Definition at line 427 of file kerdensom.cpp.

428 {
429  std::cout << "Data (1..nd, 1..nv) " << std::endl;
430  for (size_t i = 0; i < _ts->size(); i++)
431  {
432  for (size_t j = 0; j < _ts->theItems[0].size(); j++)
433  {
434  std::cout << i + 1 << " " << j + 1 << " " << _ts->theItems[i][j] << std::endl;
435  }
436  }
437 }
#define i
#define j

◆ test()

double KerDenSOM::test ( const FuzzyMap _som,
const TS _examples 
) const
virtual

Tests the KerDenSOM Parameter: _som The KerDenSom to test Parameter: _examples The testing set

Tests the KerDenSOM Parameter: _som The KerDenSom to test Parameter: _examples The training set of examples

Definition at line 74 of file kerdensom.cpp.

75 {
76 
77  // Defines verbosity level
78  int verbosity = listener->getVerbosity();
79  if (verbosity)
80  {
81  listener->OnReportOperation((std::string) "\nEstimating quantization error....\n");
82  listener->OnInitOperation(_examples.size());
83  }
84 
85 
86  /* Scan all data entries */
87  double qerror = 0.0;
88  for (size_t i = 0; i < _examples.size(); i++)
89  {
90  SomIn& theBest = _som.fuzzyTest(i); // get the best
91  qerror += euclideanDistance(theBest, _examples.theItems[i]);
92  if (verbosity)
93  {
94  auto tmp = (int)((_examples.size() * 5) / 100);
95  if ((tmp == 0) && (i != 0))
96  tmp = i;
97  else
98  tmp = 1;
99  if ((i % tmp) == 0)
101  }
102  }
103  if (verbosity)
104  listener->OnProgress(_examples.size());
105  return (qerror / (double) _examples.size());
106 
107 }
double euclideanDistance(const std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:377
virtual void OnReportOperation(const std::string &_rsOp)=0
virtual void OnInitOperation(unsigned long _est_it)=0
virtual const unsigned & getVerbosity() const
Definition: xmipp_funcs.h:1065
FeatureVector SomIn
Definition: map.h:44
#define i
virtual void OnProgress(unsigned long _it)=0
virtual FeatureVector & fuzzyTest(unsigned _in) const

◆ train() [1/2]

virtual void KerDenSOM::train ( FuzzyMap _som,
TS _examples,
FileName _fn_vectors,
bool  _update = false,
double  _sigma = 0,
bool  _saveIntermediate = false 
)
pure virtual

Trains the KerDenSOM Parameter: _som The KerDenSom to train Parameter: _ts The training set Parameter: _update True if uses _som as starting point for training. Parameter: _sigma If update = true, uses this sigma for the training.

◆ train() [2/2]

virtual void KerDenSOM::train ( FuzzyMap _som,
const TS _examples 
) const
inlineprotectedvirtual

Declaration of virtual method

Definition at line 144 of file kerdensom.h.

145  {};

◆ updateSigmaI()

double KerDenSOM::updateSigmaI ( FuzzyMap _som,
const TS _examples 
)
protectedvirtual

Definition at line 211 of file kerdensom.cpp.

212 {
213  double t = 0;
214 
215  // Computing Sigma (Part I)
216  for (size_t vv = 0; vv < numVectors; vv++)
217  {
218  const FeatureVector &example=_examples->theItems[vv];
219  const floatFeature *ptrExample0=&example[0];
220  for (size_t cc = 0; cc < numNeurons; cc++)
221  {
222  double r = 0.0;
223  const floatFeature *ptrExample=ptrExample0;
224  const floatFeature *ptrCodeVector=&(_som->theItems[cc][0]);
225  for (size_t j = 0; j < dim; j++)
226  {
227  double diff=(double)(*ptrExample++) - (double)(*ptrCodeVector++);
228  r += diff*diff;
229  }
230  t += r * (double)(_som->memb[vv][cc]);
231  }
232  }
233  return (double)(t / (double)(numVectors*dim));
234 }
MM memb
Alias for Fuzzy vectors.
size_t dim
Definition: kerdensom.h:135
float floatFeature
Definition: data_types.h:72
size_t numNeurons
Definition: kerdensom.h:133
double vv
std::vector< Item > theItems
Definition: training_set.h:84
#define j
size_t numVectors
Definition: kerdensom.h:134
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86

◆ updateSigmaII()

virtual double KerDenSOM::updateSigmaII ( FuzzyMap _som,
const TS _examples,
const double &  _reg,
const double &  _alpha 
)
protectedpure virtual

◆ updateU()

virtual double KerDenSOM::updateU ( FuzzyMap _som,
const TS _examples,
const double &  _sigma,
double &  _alpha 
)
protectedpure virtual

◆ updateU1()

void KerDenSOM::updateU1 ( FuzzyMap _som,
const TS _examples 
)
protectedvirtual

Special Initialization of Membership Matrix (Fuzzy c-means style)

Definition at line 287 of file kerdensom.cpp.

288 {
289 
290  double auxProd;
291  double auxDist;
292  double tmp;
293  size_t k;
294  size_t j;
295  size_t i;
296 
297  // Update Membership matrix
298  for (k = 0; k < numVectors; k++)
299  {
300  auxProd = 1;
301  for (j = 0; j < numNeurons; j++)
302  auxProd *= euclideanDistance(_som->theItems[j], _examples->theItems[k]);
303 
304  if (auxProd == 0.)
305  { // Apply k-means criterion (Data-CB) must be > 0
306  for (j = 0; j < numNeurons; j ++)
307  if (euclideanDistance(_som->theItems[j], _examples->theItems[k]) == 0.)
308  _som->memb[k][j] = 1.0;
309  else
310  _som->memb[k][j] = 0.0;
311  }
312  else
313  {
314  for (i = 0; i < numNeurons; i ++)
315  {
316  auxDist = 0;
317  for (j = 0; j < numNeurons; j ++)
318  {
319  tmp = euclideanDistance(_som->theItems[i], _examples->theItems[k]) /
320  euclideanDistance(_som->theItems[j], _examples->theItems[k]);
321  auxDist += pow(tmp, 2);
322  } // for j
323  _som->memb[k][i] = (floatFeature) 1.0 / auxDist;
324  } // for i
325  } // if auxProd
326  } // for k
327 }
double euclideanDistance(const std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:377
MM memb
Alias for Fuzzy vectors.
float floatFeature
Definition: data_types.h:72
size_t numNeurons
Definition: kerdensom.h:133
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
std::vector< Item > theItems
Definition: training_set.h:84
#define j
size_t numVectors
Definition: kerdensom.h:134

◆ updateV()

void KerDenSOM::updateV ( FuzzyMap _som,
const TS _examples,
const double &  _reg 
)
protectedvirtual

Update Code Vectors

Definition at line 116 of file kerdensom.cpp.

117 {
118  unsigned t2 = 0; // Iteration index
119 
120  // Calculate Temporal scratch values
121  for (size_t cc = 0; cc < numNeurons; cc++)
122  {
123  double *ptrTmpMap_cc=&(tmpMap[cc][0]);
124  memset(ptrTmpMap_cc,0,dim*sizeof(double));
125  double &tmpDens_cc=tmpDens[cc];
126  if (_reg != 0)
127  tmpDens_cc = _reg * _som->getLayout().numNeig(_som, (SomPos) _som->indexToPos(cc));
128  else
129  tmpDens_cc = 0.;
130  for (size_t vv = 0; vv < numVectors; vv++)
131  {
132  auto tmpU = (double) _som->memb[vv][cc];
133  tmpDens_cc += tmpU;
134  const floatFeature * ptrExample=&(_examples->theItems[vv][0]);
135  for (size_t j = 0; j < dim; j++)
136  ptrTmpMap_cc[j] += tmpU * ptrExample[j];
137  }
138  }
139 
140  // Update Code vectors using a sort of Gauss-Seidel iterative algorithm.
141  // Usually 100 iterations are enough.
142  double convergence = 1;
143  double stopError2;
144  double stopError1;
145  double *ptrTmpV=&(tmpV[0]);
146  while ((convergence > 1e-5) && (t2 < 100))
147  {
148  t2++;
149  stopError2 = 0;
150  stopError1 = 0;
151  for (size_t cc = 0; cc < numNeurons; cc++)
152  {
153  if (_reg != 0)
154  _som->localAve(_som->indexToPos(cc), tmpV);
155  double *ptrTmpMap_cc=&(tmpMap[cc][0]);
156  double iTmpDens_cc=1.0/tmpDens[cc];
157  floatFeature *ptrCodeVector_cc=&(_som->theItems[cc][0]);
158  for (size_t j = 0; j < dim; j++)
159  {
160  double tmpU = (ptrTmpMap_cc[j] + ptrTmpV[j] * _reg) * iTmpDens_cc;
161  stopError1 += fabs(ptrCodeVector_cc[j] - tmpU);
162  stopError2 += fabs(tmpU);
163  ptrCodeVector_cc[j] = (floatFeature) tmpU;
164  }
165  } // for
166  convergence = stopError1 / stopError2;
167  } // while
168 }
MM memb
Alias for Fuzzy vectors.
size_t dim
Definition: kerdensom.h:135
void localAve(const SomPos &_center, std::vector< double > &_aveVector) const
Definition: map.cpp:896
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:1033
virtual const Layout & getLayout() const
Definition: map.h:539
float floatFeature
Definition: data_types.h:72
size_t numNeurons
Definition: kerdensom.h:133
double vv
std::vector< std::vector< double > > tmpMap
Definition: kerdensom.h:136
std::vector< Item > theItems
Definition: training_set.h:84
std::pair< long, long > SomPos
Definition: map.h:45
#define j
virtual double numNeig(const FuzzyMap *_som, const SomPos &_center) const =0
std::vector< double > tmpDens
Definition: kerdensom.h:139
std::vector< double > tmpV
Definition: kerdensom.h:140
size_t numVectors
Definition: kerdensom.h:134

◆ updateV1()

void KerDenSOM::updateV1 ( FuzzyMap _som,
const TS _examples 
)
protectedvirtual

Special Initialization of Code Vectors

Definition at line 246 of file kerdensom.cpp.

247 {
248  for (size_t cc = 0; cc < numNeurons; cc++)
249  {
250  std::vector<double> &tmpMap_cc=tmpMap[cc];
251  double *ptrTmpMap_cc0=&tmpMap_cc[0];
252  memset(ptrTmpMap_cc0,0,dim*sizeof(double));
253  double &tmpDens_cc=tmpDens[cc];
254  tmpDens_cc = 0.0;
255  for (size_t vv = 0; vv < numVectors; vv++)
256  {
257  double tmpU = _som->memb[vv][cc];
258  tmpDens_cc += tmpU;
259  const floatFeature *ptrExample=&(_examples->theItems[vv][0]);
260  double *ptrTmpMap_cc=ptrTmpMap_cc0;
261  for (size_t j = 0; j < dim; j++)
262  {
263  *ptrTmpMap_cc += tmpU * (*ptrExample);
264  ++ptrExample;
265  ++ptrTmpMap_cc;
266  }
267  }
268  }
269 
270  for (size_t cc = 0; cc < numNeurons; cc++)
271  {
272  const std::vector<double> &tmpMap_cc=tmpMap[cc];
273  double itmpDens_cc=1.0/tmpDens[cc];
274  FeatureVector &codevector=_som->theItems[cc];
275  for (size_t j = 0; j < dim; j++)
276  {
277  double tmpU =tmpMap_cc[j] * itmpDens_cc;
278  codevector[j] = (floatFeature) tmpU;
279  }
280  } // for
281 }
MM memb
Alias for Fuzzy vectors.
size_t dim
Definition: kerdensom.h:135
float floatFeature
Definition: data_types.h:72
size_t numNeurons
Definition: kerdensom.h:133
double vv
std::vector< std::vector< double > > tmpMap
Definition: kerdensom.h:136
std::vector< Item > theItems
Definition: training_set.h:84
#define j
std::vector< double > tmpDens
Definition: kerdensom.h:139
size_t numVectors
Definition: kerdensom.h:134
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86

Member Data Documentation

◆ annSteps

size_t KerDenSOM::annSteps
protected

Definition at line 124 of file kerdensom.h.

◆ dim

size_t KerDenSOM::dim
protected

Definition at line 135 of file kerdensom.h.

◆ epsilon

double KerDenSOM::epsilon
protected

Definition at line 127 of file kerdensom.h.

◆ numNeurons

size_t KerDenSOM::numNeurons
protected

Definition at line 133 of file kerdensom.h.

◆ numVectors

size_t KerDenSOM::numVectors
protected

Definition at line 134 of file kerdensom.h.

◆ reg0

double KerDenSOM::reg0
protected

Definition at line 125 of file kerdensom.h.

◆ reg1

double KerDenSOM::reg1
protected

Definition at line 126 of file kerdensom.h.

◆ sigma

double KerDenSOM::sigma
protected

Definition at line 123 of file kerdensom.h.

◆ somNSteps

size_t KerDenSOM::somNSteps
protected

Definition at line 128 of file kerdensom.h.

◆ tmpD

std::vector<double> KerDenSOM::tmpD
protected

Definition at line 137 of file kerdensom.h.

◆ tmpD1

std::vector<double> KerDenSOM::tmpD1
protected

Definition at line 138 of file kerdensom.h.

◆ tmpDens

std::vector<double> KerDenSOM::tmpDens
protected

Definition at line 139 of file kerdensom.h.

◆ tmpMap

std::vector< std::vector<double> > KerDenSOM::tmpMap
protected

Definition at line 136 of file kerdensom.h.

◆ tmpV

std::vector<double> KerDenSOM::tmpV
protected

Definition at line 140 of file kerdensom.h.


The documentation for this class was generated from the following files: