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

#include <som.h>

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

Public Types

enum  neighType { GAUSSIAN = 0, BUBBLE = 1 }
 Type of neighborhood function. More...
 
- Public Types inherited from ClassificationAlgorithm< ClassificationMap >
typedef ClassificationMap DS
 
typedef DS::TS TS
 

Public Member Functions

 SOM (Descent &_alpha, Descent &_radius, neighType _neighType, unsigned long _nSteps)
 
 SOM (std::istream &_is)
 
virtual ~SOM ()
 
 SOM (const SOM &)=delete
 
void alpha (Descent _alpha)
 
void radius (Descent _radius)
 
void nSteps (const unsigned long &_nSteps)
 
virtual void train (ClassificationMap &_som, ClassicTrainingVectors &_ts) const
 
virtual double test (const ClassificationMap &_som, const TS &_examples) const
 
virtual void clear ()
 
virtual void printSelf (std::ostream &_os) const
 
virtual void readSelf (std::istream &_is)
 
virtual void saveObject (std::ostream &_os) const
 
virtual void loadObject (std::istream &_is)
 
SOMoperator= (const SOM &op1)
 
- Public Member Functions inherited from ClassificationAlgorithm< ClassificationMap >
 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 const std::string & setID () const
 
virtual std::string & setID ()
 
void setListener (BaseListener *_listener)
 

Protected Attributes

Descent somAlpha
 
Descent somRadius
 alpha(t) More...
 
neighType somNeigh
 radius(t) More...
 
unsigned long somNSteps
 Neighborhood type for training (Bubble or Gaussian) More...
 
- Protected Attributes inherited from ClassificationAlgorithm< ClassificationMap >
std::string ID
 
BaseListenerlistener
 

Friends

std::ostream & operator<< (std::ostream &_os, const SOM &_som)
 
std::istream & operator>> (std::istream &_is, SOM &_som)
 

Detailed Description

This class trains a Kohonen's Self Organizing Feature Map

Definition at line 136 of file som.h.

Member Enumeration Documentation

◆ neighType

Type of neighborhood function.

Enumerator
GAUSSIAN 
BUBBLE 

Definition at line 140 of file som.h.

140 { GAUSSIAN = 0, BUBBLE = 1} neighType;
neighType
Type of neighborhood function.
Definition: som.h:140

Constructor & Destructor Documentation

◆ SOM() [1/3]

SOM::SOM ( Descent _alpha,
Descent _radius,
neighType  _neighType,
unsigned long  _nSteps 
)
inline

Constructs the algorithm Parameter: _alpha How is gonna decrease alpha Parameter: _radius How is gonna decrease the radius of neighborhood Parameter: _radius How is gonna decrease the radius of neighborhood Parameter: _neighType Type of neighborhood function Parameter: _nSteps Number of training steps

Definition at line 150 of file som.h.

152  somRadius(_radius), somNeigh(_neighType), somNSteps(_nSteps)
153  {}
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
neighType somNeigh
radius(t)
Definition: som.h:277
Descent somAlpha
Definition: som.h:275
Descent somRadius
alpha(t)
Definition: som.h:276

◆ SOM() [2/3]

SOM::SOM ( std::istream &  _is)

Construct a SOM from the code vectors in a stream Parameter: _is The stream

Definition at line 37 of file som.cpp.

38 {
39  readSelf(_is);
40 }
virtual void readSelf(std::istream &_is)
Definition: som.cpp:209

◆ ~SOM()

virtual SOM::~SOM ( )
inlinevirtual

Virtual destructor

Definition at line 165 of file som.h.

166  {}

◆ SOM() [3/3]

SOM::SOM ( const SOM )
delete

Member Function Documentation

◆ alpha()

void SOM::alpha ( Descent  _alpha)

Sets the alpha descent function Parameter: _alpha alpha(t)

Sets the alpha function Parameter: _alpha alpha(t)

Definition at line 47 of file som.cpp.

48 {
49  somAlpha = _alpha;
50 }
Descent somAlpha
Definition: som.h:275

◆ clear()

void SOM::clear ( )
virtual

Clears the Algorithm

Definition at line 186 of file som.cpp.

187 {
188  somNeigh = GAUSSIAN;
189  somNSteps = 0;
190  listener = NULL; // it can not be deleted here
191 }
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
neighType somNeigh
radius(t)
Definition: som.h:277

◆ loadObject()

void SOM::loadObject ( std::istream &  _is)
virtual

Loads the SOM class from a stream. this method can be used to load the status of the class. Parameter: _is The output stream

Definition at line 248 of file som.cpp.

249 {
250  readSelf(_is);
251 }
virtual void readSelf(std::istream &_is)
Definition: som.cpp:209

◆ nSteps()

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

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

Definition at line 65 of file som.cpp.

66 {
67  somNSteps = _nSteps;
68 }
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278

◆ operator=()

SOM& SOM::operator= ( const SOM op1)
inline

Operator "=" Parameter: op1 SOM

Definition at line 265 of file som.h.

266  {
267  std::stringstream _str;
268  op1.printSelf(_str);
269  readSelf(_str);
270  return *this;
271  }
virtual void printSelf(std::ostream &_os) const
Definition: som.cpp:197
virtual void readSelf(std::istream &_is)
Definition: som.cpp:209

◆ printSelf()

void SOM::printSelf ( std::ostream &  _os) const
virtual

Standard output for a SOM algorithm Parameter: _os The output stream

Reimplemented from ClassificationAlgorithm< ClassificationMap >.

Definition at line 197 of file som.cpp.

198 {
199  _os << (int) somNeigh << std::endl;
200  _os << somNSteps << std::endl;
201  somAlpha.printSelf(_os);
202  somRadius.printSelf(_os);
203 }
virtual void printSelf(std::ostream &_os) const
Definition: som.cpp:280
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
neighType somNeigh
radius(t)
Definition: som.h:277
Descent somAlpha
Definition: som.h:275
Descent somRadius
alpha(t)
Definition: som.h:276

◆ radius()

void SOM::radius ( Descent  _radius)

Sets the radius descent function Parameter: _radius radius(t)

Sets the radius function Parameter: _radius radius(t)

Definition at line 56 of file som.cpp.

57 {
58  somRadius = _radius;
59 }
Descent somRadius
alpha(t)
Definition: som.h:276

◆ readSelf()

void SOM::readSelf ( std::istream &  _is)
virtual

Standard input for a SOM algorithm Parameter: _is The input stream

Standard input for a som algorithm Parameter: _is The input stream

Definition at line 209 of file som.cpp.

210 {
211  clear();
212  try
213  {
214  if (_is)
215  _is >> (int&) somNeigh;
216  if (_is)
217  _is >> somNSteps;
218  somAlpha.readSelf(_is);
219  somRadius.readSelf(_is);
220 
221  }
222  catch (std::exception& e)
223  {
224  std::stringstream msg;
225  msg << e.what() << std::endl << "Error reading the SOM algorithm";
226  throw std::runtime_error(msg.str());
227  }
228 
229 }
virtual void readSelf(std::istream &_is)
Definition: som.cpp:290
virtual void clear()
Definition: som.cpp:186
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
neighType somNeigh
radius(t)
Definition: som.h:277
Descent somAlpha
Definition: som.h:275
Descent somRadius
alpha(t)
Definition: som.h:276

◆ saveObject()

void SOM::saveObject ( std::ostream &  _os) const
virtual

Saves the SOM class into a stream. this method can be used to save the status of the class. Parameter: _os The output stream

Definition at line 237 of file som.cpp.

238 {
239  printSelf(_os);
240 }
virtual void printSelf(std::ostream &_os) const
Definition: som.cpp:197

◆ test()

double SOM::test ( const ClassificationMap _som,
const TS _examples 
) const
virtual

Tests the SOM Parameter: _som The som to test Parameter: _examples The training set of examples returns the quantization error

Tests the SOM Parameter: _som The som to test Parameter: _examples The training set of examples

Definition at line 148 of file som.cpp.

149 {
150 
151  // Defines verbosity level
152  int verbosity = listener->getVerbosity();
153  if (verbosity)
154  {
155  listener->OnReportOperation((std::string) "Estimating quantization error....\n");
156  listener->OnInitOperation(_examples.size());
157  }
158 
159 
160  /* Scan all data entries */
161  double qerror = 0.0;
162  for (size_t i = 0; i < _examples.size(); i++)
163  {
164  SomIn& theBest = _som.test(_examples.theItems[i]); // get the best
165  qerror += euclideanDistance(theBest, _examples.theItems[i]);
166  if (verbosity)
167  {
168  auto tmp = (int)((_examples.size() * 5) / 100);
169  if ((tmp == 0) && (i != 0))
170  tmp = i;
171  else
172  tmp = 1;
173  if ((i % tmp) == 0)
175  }
176  }
177  if (verbosity)
178  listener->OnProgress(_examples.size());
179  return (qerror / (double) _examples.size());
180 }
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
virtual FeatureVector & test(const FeatureVector &_in) const
Definition: code_book.cpp:172
FeatureVector SomIn
Definition: map.h:44
#define i
virtual void OnProgress(unsigned long _it)=0

◆ train()

void SOM::train ( ClassificationMap _som,
ClassicTrainingVectors _ts 
) const
virtual

Trains the SOM Parameter: _som The som to train Parameter: _ts The training set

Definition at line 76 of file som.cpp.

77 {
78  unsigned long t = 0;
79 
80  int verbosity = listener->getVerbosity();
81  if (verbosity)
82  listener->OnReportOperation((std::string) "Training Kohonen SOM....\n");
83  if (verbosity == 1 || verbosity == 3)
85 
86 
87  while (t < somNSteps)
88  {
89  for (std::vector<SomIn>::iterator i = _ts.theItems.begin();
90  t < somNSteps && i < _ts.theItems.end() ; i++, t++)
91  {
92  // get the best matching.
93  SomIn& theBest = _som.test(*i)
94  ;
95  if (somNeigh == BUBBLE)
96  { // Bubble
97  // update the neighborhood around the best one
98  std::vector<unsigned> neig = _som.neighborhood(_som.codVecPos(theBest),
99  ceil(somRadius(t, somNSteps)));
100  for (std::vector<unsigned>::iterator it = neig.begin();it < neig.end();it++)
101  {
102  SomIn& v = _som.theItems[*it]
103  ;
104  for (unsigned j = 0; j < v.size(); j++)
105  v[j] += ((*i)[j] - v[j]) * somAlpha(t, somNSteps);
106  }
107  }
108  else
109  { // Gaussian
110  // update all neighborhood convoluted by a gaussian
111  double radius = somRadius(t, somNSteps);
112  double alpha = somAlpha(t, somNSteps);
113  for (unsigned it = 0 ; it < _som.size(); it++)
114  {
115  double dist = _som.neighDist(_som.codVecPos(theBest), _som.indexToPos(it));
116  double alp = alpha * (double) exp((double)(-dist * dist / (2.0 * radius * radius)));
117  SomIn& v = _som.theItems[it];
118  for (unsigned j = 0; j < v.size(); j++)
119  v[j] += ((*i)[j] - v[j]) * alp;
120  }
121  } // else
122 
123  } // for examples
124 
125  if (verbosity == 1 || verbosity == 3)
126  listener->OnProgress(t);
127  if (verbosity >= 2)
128  {
129  char s[100]
130  ;
131  sprintf(s, "Iteration %d of %d.\n", (int)t, (int)somNSteps);
132  listener->OnReportOperation((std::string) s);
133  }
134  } // while t < somSteps
135 
136 
137  if (verbosity == 1 || verbosity == 3)
139 
140 }
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:306
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
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
void radius(Descent _radius)
Definition: som.cpp:56
std::vector< unsigned > neighborhood(const SomPos &_center, double _radius) const
Definition: map.cpp:167
virtual FeatureVector & test(const FeatureVector &_in) const
Definition: code_book.cpp:172
FeatureVector SomIn
Definition: map.h:44
#define i
neighType somNeigh
radius(t)
Definition: som.h:277
std::vector< Item > theItems
Definition: training_set.h:84
virtual void OnProgress(unsigned long _it)=0
Descent somAlpha
Definition: som.h:275
SomPos codVecPos(SomIn &_v)
Definition: map.cpp:341
#define j
void alpha(Descent _alpha)
Definition: som.cpp:47
Descent somRadius
alpha(t)
Definition: som.h:276
double neighDist(const SomPos &_center, const SomPos &_v) const
Definition: map.cpp:177

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  _os,
const SOM _som 
)
friend

Standard output for a som algorithm Parameter: _os The output stream Parameter: _som The som algorithm to be printed

Definition at line 243 of file som.h.

244  {
245  _som.printSelf(_os);
246  return _os;
247  };
virtual void printSelf(std::ostream &_os) const
Definition: som.cpp:197

◆ operator>>

std::istream& operator>> ( std::istream &  _is,
SOM _som 
)
friend

Standard input for a som algorithm Parameter: _is The input stream Parameter: _som The som algorithm

Definition at line 254 of file som.h.

255  {
256  _som.readSelf(_is);
257  return _is;
258  };
virtual void readSelf(std::istream &_is)
Definition: som.cpp:209

Member Data Documentation

◆ somAlpha

Descent SOM::somAlpha
protected

Definition at line 275 of file som.h.

◆ somNeigh

neighType SOM::somNeigh
protected

radius(t)

Definition at line 277 of file som.h.

◆ somNSteps

unsigned long SOM::somNSteps
protected

Neighborhood type for training (Bubble or Gaussian)

Definition at line 278 of file som.h.

◆ somRadius

Descent SOM::somRadius
protected

alpha(t)

Definition at line 276 of file som.h.


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