38 constexpr
signed int MAXZ = -11282;
49 bool _update,
double _sigma,
bool _saveIntermediate)
55 dim = _examples.theItems[0].size();
61 tmpD.resize(numNeurons);
62 tmpD1.resize(numNeurons);
109 sprintf(s,
"\nTraining Deterministic Annealing step %d of %d....\n", (
int)(
iter + 1), (
int)annSteps);
119 regtmp = exp(tmpregMax -
iter * (tmpregMax - tmpregMin) / (annSteps - 1));
133 sprintf(s,
"Code vectors variation: %g\n", stopError);
150 if (_examples.isNormalized())
154 _som.
unNormalize(_examples.getNormalizationInfo());
157 if (_saveIntermediate)
163 std::ofstream codS(tmpN.c_str());
171 std::ofstream hisStream(tmpN.c_str());
172 _som.printHistogram(hisStream);
179 std::ofstream errStream(tmpN.c_str());
180 _som.printQuantError(errStream);
187 std::ofstream vsStream(tmpN.c_str());
188 vsStream << _examples.theItems[0].size() <<
" " << _som.layout() <<
" " << _som.width() <<
" " << _som.height() <<
" gaussian" << std::endl;
189 for (
size_t i = 0;
i < _examples.size();
i++)
191 int j = _som.fuzzyWinner(
i);
192 vsStream << _som.indexToPos(j).first <<
" " << _som.indexToPos(j).second <<
" " << _som.memb[
i][
j] <<
" " << _examples.theTargets[
i] << std::endl;
200 std::ofstream infS(tmpN.c_str());
201 infS <<
"Kernel Probability Density Estimator SOM algorithm" << std::endl << std::endl;
202 infS <<
"Deterministic annealing step " <<
iter + 1 <<
" out of " << annSteps << std::endl;
203 infS <<
"Number of feature vectors: " << _examples.size() << std::endl;
204 infS <<
"Number of variables: " << _examples.theItems[0].size() << std::endl;
205 infS <<
"Horizontal dimension (Xdim) = " << _som.width() << std::endl;
206 infS <<
"Vertical dimension (Ydim) = " << _som.height() << std::endl;
207 if (_examples.isNormalized())
208 infS <<
"Input data normalized" << std::endl;
210 infS <<
"Input data not normalized" << std::endl;
211 if (_som.layout() ==
"rect")
212 infS <<
"Rectangular topology " << std::endl;
214 infS <<
"Hexagonal topology " << std::endl;
215 infS <<
"Gaussian Kernel function " << std::endl;
216 infS <<
"Total number of iterations = " <<
somNSteps << std::endl;
217 infS <<
"Stopping criteria (eps) = " <<
epsilon << std::endl << std::endl ;
219 infS <<
"Smoothness factor (regularization) = " << regtmp << std::endl;
220 infS <<
"Functional value = " << funct << std::endl;
221 infS <<
"Sigma (Kernel width) = " <<
sigma << std::endl;
225 if (_examples.isNormalized())
229 _som.
Normalize(_examples.getNormalizationInfo());
249 const double& _sigma,
double& _alpha)
259 double irr1 =1.0/( 2.0 * _sigma);
263 double *ptrTmpD=&
tmpD[0];
264 double *ptrTmpD1=&
tmpD1[0];
274 for (
size_t j = 0;
j <
dim;
j++)
276 double tmp=((double)ptrExample[
j] - (
double)ptrCodeVector[
j]);
277 auxDist += tmp * tmp;
280 ptrTmpD[
i] = auxDist;
281 rr2 = -auxDist * irr1;
289 rr2 = ptrTmpD1[
j] - max1;
293 d1 = (double)exp(rr2);
302 tmp = ptrTmpD1[
j] * ir1;
304 _alpha += tmp * ptrTmpD[
j];
327 for (j = 0; j <
dim; j++)
330 return (
double)((_alpha + _reg*q) / (
double)(
numVectors*
dim));
342 double K=-1.0/(2*_sigma);
348 for (
size_t j = 0;
j <
dim;
j++)
350 double diff=(double)(ptrExample[
j]) - (double)(ptrCodeVector[j]);
364 #ifdef UNUSED // detected as unused 29.6.2018 368 double GaussianKerDenSOM::dataDens(
const TS* _examples,
const FeatureVector* _example,
double _sigma)
const 375 for (j = 0; j <
dim; j++)
376 t += ((
double)((*_example)[j]) - (
double)(_examples->theItems[vv][j])) * ((double)((*_example)[
j]) - (
double)(_examples->theItems[
vv][
j]));
377 t = -t / (2.0 * _sigma);
384 double tmp = (double)
dim / 2.0;
385 return (
double)(pow((
double)(2*
PI*_sigma), -tmp)*s / (double)numVectors);
396 double _sigma,
double _reg,
double& _likelihood,
406 t =
codeDens(_som, &(_examples->theItems[vv]), _sigma);
411 _likelihood +=
log(t);
413 _likelihood = -_likelihood;
422 for (j = 0; j <
dim; j++)
424 _penalty += (t * (double)(_som->
theItems[cc][j]) -
428 _penalty = _reg * _penalty / (2.0 * _sigma);
430 return (_likelihood + _penalty);
virtual double updateU(FuzzyMap *_som, const TS *_examples, const double &_sigma, double &_alpha)
std::vector< double > tmpD
virtual void Normalize(const std::vector< ClassicTrainingVectors::statsStruct > &_varStats)
MM memb
Alias for Fuzzy vectors.
void localAve(const SomPos &_center, std::vector< double > &_aveVector) const
SomPos indexToPos(const unsigned &_i) const
virtual void OnReportOperation(const std::string &_rsOp)=0
virtual const unsigned & getVerbosity() const
std::vector< double > tmpD1
virtual const Layout & getLayout() const
String integerToString(int I, int _width, char fill_with)
virtual void unNormalize(const std::vector< ClassicTrainingVectors::statsStruct > &_varStats)
virtual void updateV1(FuzzyMap *_som, const TS *_examples)
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
virtual void classify(const ClassicTrainingVectors *_ts)
std::vector< std::vector< double > > tmpMap
virtual void initU(FuzzyMap *_som)
void log(Image< double > &op)
std::vector< Item > theItems
std::pair< long, long > SomPos
constexpr signed int MAXZ
virtual double mainIterations(FuzzyMap *_som, const TS *_examples, double &_sigma, const double &_reg)
virtual double codeDens(const FuzzyMap *_som, const FeatureVector *_example, double _sigma) const
virtual double numNeig(const FuzzyMap *_som, const SomPos &_center) const =0
String formatString(const char *format,...)
virtual double updateSigmaI(FuzzyMap *_som, const TS *_examples)
std::vector< double > tmpDens
virtual void train(FuzzyMap &_som, TS &_examples, FileName &_fn_vectors, bool _update=false, double _sigma=0, bool _saveIntermediate=false)
std::vector< double > tmpV
virtual void updateU1(FuzzyMap *_som, const TS *_examples)
std::vector< floatFeature > FeatureVector
virtual void calibrate(ClassicTrainingVectors &_ts, Label _def="")
virtual double functional(const TS *_examples, const FuzzyMap *_som, double _sigma, double _reg, double &_likelihood, double &_penalty)
virtual double updateSigmaII(FuzzyMap *_som, const TS *_examples, const double &_reg, const double &_alpha)