Xmipp  v3.23.11-Nereus
fkcn.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Alberto Pascual Montano (pascual@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 //-----------------------------------------------------------------------------
26 // FuzzyKohonenCMeans.cc
27 // Fuzzy Kohonen Clustering Network Algorithm
28 //-----------------------------------------------------------------------------
29 
30 #include "fkcn.h"
31 
32 #ifdef __sun
33 #include <ieeefp.h>
34 #endif
35 
42 void FuzzyKohonenCMeans::train(FuzzyCodeBook& _xmippDS, const TS& _examples) const
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
179 
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 train(FuzzyCodeBook &_xmippDS, const TS &_examples) const
Definition: fkcn.cpp:42
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