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

#include <fkcn.h>

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

Public Member Functions

 FuzzyKohonenCMeans (double _m, double _epsilon, unsigned _epochs)
 
virtual ~FuzzyKohonenCMeans ()
 Virtual destructor. More...
 
virtual void train (FuzzyCodeBook &_xmippDS, const TS &_examples) const
 
- Public Member Functions inherited from FuzzyCMeans
 FuzzyCMeans (double _m, double _epsilon, unsigned _epochs)
 
virtual ~FuzzyCMeans ()
 
virtual void train (FuzzyCodeBook &_xmippDS, TS &_examples) const
 
virtual double fuzzyTest (const FuzzyCodeBook &_xmippDS, const TS &_examples) const
 
virtual double test (const FuzzyCodeBook &_xmippDS, const TS &_examples) const
 
double F (const FuzzyCodeBook &_xmippDS) const
 
double H (const FuzzyCodeBook &_xmippDS) const
 
double S (const FuzzyCodeBook &_xmippDS, const TS &_examples) const
 
- Public Member Functions inherited from ClassificationAlgorithm< FuzzyCodeBook >
 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)
 

Additional Inherited Members

- Public Types inherited from ClassificationAlgorithm< FuzzyCodeBook >
typedef FuzzyCodeBook DS
 
typedef DS::TS TS
 
- Protected Member Functions inherited from FuzzyCMeans
void printSelf (std::ostream &_os) const
 print itself on standard output More...
 
- Protected Attributes inherited from FuzzyCMeans
double m
 
double epsilon
 
unsigned epochs
 
- Protected Attributes inherited from ClassificationAlgorithm< FuzzyCodeBook >
std::string ID
 
BaseListenerlistener
 

Detailed Description

This class implements Fuzzy Kohonen Clustering Network Algorithm (Bezdeck) an unsupervised clustering algorithm that is a combination of Fuzzy c-means and Kohonen SOM.

Definition at line 45 of file fkcn.h.

Constructor & Destructor Documentation

◆ FuzzyKohonenCMeans()

FuzzyKohonenCMeans::FuzzyKohonenCMeans ( double  _m,
double  _epsilon,
unsigned  _epochs 
)
inline

Big mega ctor. Creates a Fuzzy codebook, and initializes it Parameter: _m Fuzzy constant Parameter: _epsilon Stopping criterion Parameter: _epochs Number of epochs or iterations

Definition at line 56 of file fkcn.h.

57  : FuzzyCMeans(_m, _epsilon, _epochs)
58  {
59  setID() = "Fuzzy Kohonen Clustering Network";
60  };
virtual const std::string & setID() const
FuzzyCMeans(double _m, double _epsilon, unsigned _epochs)
Definition: fcmeans.h:57

◆ ~FuzzyKohonenCMeans()

virtual FuzzyKohonenCMeans::~FuzzyKohonenCMeans ( )
inlinevirtual

Virtual destructor.

Definition at line 64 of file fkcn.h.

65  {};

Member Function Documentation

◆ train()

void FuzzyKohonenCMeans::train ( FuzzyCodeBook _xmippDS,
const TS _examples 
) const
virtual

Trains the Algorithm Parameter: _xmippDS Data structure to train, a codeBook in this case Parameter: _examples A training set with the training examples

Trains the algorithm Parameter: _xmippDS Data structure to train, a codeBook in this case Parameter: _examples A training set with the training examples

Definition at line 42 of file fkcn.cpp.

43 {
44  using namespace std;
45  // Defines verbosity
46 
47  int verbosity = listener->getVerbosity();
48  if (verbosity)
49  listener->OnReportOperation((std::string) "Training....\n");
50  if (verbosity == 1 || verbosity == 3)
52 
53  // Create auxiliar Codebook
54 
55  FuzzyCodeBook auxCB;
56 
57 
58  // Create auxiliar stuff
59 
60  unsigned numClusters = _xmippDS.size();
61  unsigned numVectors = _examples.size();
62  unsigned i;
63  unsigned j;
64  unsigned k;
65  unsigned cc;
66  unsigned vv;
67  double stopError = 0;
68  double auxError = 0;
69  double auxDist;
70  double auxProd;
71  double auxExp;
72  double auxSum;
73  unsigned t = 0; // Iteration index
74 
75  // Initialize auxiliary Codebook
76  auxCB = _xmippDS;
77  std::vector<FeatureVector> alpha;
78  alpha.resize(numVectors);
79  for (vv = 0; vv < numVectors; vv++)
80  alpha[vv].resize(numClusters, 0.);
81 
82  // Set auxiliar variables
83  double Deltam = (double)(m - 1.) / (double) epochs;
84 
85  // This is the main code of the algorithm. Iterates "epochs" times
86  stopError = epsilon + 1; // Initially must be higher
87 
88  while ((stopError > epsilon) && (t < epochs))
89  {
90 
91  double mt = (m - t * Deltam);
92  if ((mt - 1.) <= 1e-2)
93  auxExp = 2. / 1e-2;
94  else
95  auxExp = 2. / (mt - 1.);
96 
97  // Update Membership matrix
98 
99  FeatureVector tmpD;
100  tmpD.resize(numClusters);
101  for (k = 0; k < numVectors; k++)
102  {
103  auxProd = 0;
104  for (i = 0; i < numClusters; i ++)
105  {
106  auxDist = 0;
107  for (size_t d = 0; d < _examples.theItems[0].size(); d++)
108  auxDist += ((double)(_examples.theItems[k][d]) - (double)(_xmippDS.theItems[i][d])) * ((double)(_examples.theItems[k][d]) - (double)(_xmippDS.theItems[i][d]));
109  auxDist = (double) sqrt((double)auxDist);
110  auxDist = (double) pow((double) auxDist, (double) auxExp);
111  if (auxDist < MAXZERO) auxDist = MAXZERO;
112  if (std::isnan(auxDist)) auxDist = MAXZERO;
113  if (!finite(auxDist)) auxDist = 1e200;
114  auxProd += 1. / auxDist;
115  tmpD[i] = auxDist;
116  }
117  for (j = 0; j < numClusters; j ++)
118  {
119  _xmippDS.memb[k][j] = (floatFeature)(1. / (auxProd * tmpD[j]));
120  }
121  } // for k
122 
123 
124 
125  // Calculate Alpha
126  for (cc = 0; cc < numClusters; cc++)
127  for (vv = 0; vv < numVectors; vv++)
128  alpha[vv][cc] = (floatFeature) pow((double)_xmippDS.memb[vv][cc], (double)mt);
129 
130 
131  /* Step III: Update Code Vectors */
132 
133 
134  for (cc = 0; cc < numClusters; cc++)
135  {
136  std::vector<double> tmpV;
137  tmpV.resize(_examples.theItems[0].size(), 0.);
138  auxSum = 0;
139  for (vv = 0; vv < numVectors; vv++)
140  {
141  for (j = 0; j < _examples.theItems[0].size(); j++)
142  tmpV[j] += (double)((double)(alpha[vv][cc]) * ((double)(_examples.theItems[vv][j]) - (double)(_xmippDS.theItems[cc][j])));
143  auxSum += (double)(alpha[vv][cc]);
144  } // for vv
145  if (auxSum != 0.)
146  for (j = 0; j < _examples.theItems[0].size(); j++)
147  _xmippDS.theItems[cc][j] += (floatFeature)(tmpV[j] / auxSum);
148  } // for cc
149 
150 
151  // Compute stopping criterion
152  stopError = 0;
153  for (i = 0; i < numClusters; i ++)
154  {
155  auxError = euclideanDistance(_xmippDS.theItems[i], auxCB.theItems[i]);
156  stopError += auxError * auxError;
157  } // for i
158 
159  // Update iteration index
160 
161  t++;
162  auxCB = _xmippDS;
163 
164  if (verbosity == 1 || verbosity == 3)
165  listener->OnProgress(t);
166  if (verbosity >= 2)
167  {
168  char s[100];
169  sprintf(s, "Iteration %d of %d. Code vectors variation: %g\n", t, epochs, stopError);
170  listener->OnReportOperation((std::string) s);
171  }
172 
173  } // while
174 
175  if (verbosity == 1 || verbosity == 3)
177 
178 } // FuzzyKohonenCMeans::train
double euclideanDistance(const std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:377
MM memb
Alias for Fuzzy vectors.
double m
Definition: fcmeans.h:163
void sqrt(Image< double > &op)
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
float floatFeature
Definition: data_types.h:72
#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
doublereal * d
double vv
#define MAXZERO
Definition: data_types.h:59
std::vector< Item > theItems
Definition: training_set.h:84
virtual void OnProgress(unsigned long _it)=0
int mt
#define j
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86
double epsilon
Definition: fcmeans.h:164
unsigned epochs
Definition: fcmeans.h:165

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