Alexandria  2.14.1
Please provide a description of the project.
SOMTrainer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file SOMTrainer.h
21  * @author nikoapos
22  */
23 
24 #ifndef SOM_SOMTRAINER_H
25 #define SOM_SOMTRAINER_H
26 
27 #include "SOM/SOM.h"
28 #include "SOM/NeighborhoodFunc.h"
30 #include "SOM/SamplingPolicy.h"
31 
32 namespace Euclid {
33 namespace SOM {
34 
35 class SOMTrainer {
36 
37 public:
38 
40  LearningRestraintFunc::Signature learning_restraint_func)
41  : m_neighborhood_func(neighborhood_func),
42  m_learning_restraint_func(learning_restraint_func) {
43  }
44 
45  template <std::size_t ND, typename DistFunc, typename InputIter, typename InputToWeightFunc>
46  void train(SOM<ND, DistFunc>& som, std::size_t iter_no, InputIter begin, InputIter end, InputToWeightFunc weight_func,
48 
49  // We repeat the training for iter_no iterations
50  for (std::size_t i = 0; i < iter_no; ++ i) {
51 
52  // Compute the factor of the current iteration
53  auto learn_factor = m_learning_restraint_func(i, iter_no);
54  if (learn_factor == 0) {
55  continue;
56  }
57 
58  // Go through the training sample of the iteration
59  for (auto it = sampling_policy.start(begin, end); it != end; it = sampling_policy.next(it)) {
60 
61  // Get the weights of the input object
62  auto input_weights = weight_func(*it);
63 
64  // Find the coordinates of the BMU for the input
65  std::size_t bmu_x;
66  std::size_t bmu_y;
67  double nd_distance;
68  std::tie(bmu_x, bmu_y, nd_distance) = som.findBMU(*it, weight_func);
69 
70  // Now go through all the cells and update their values according their coordinates
71  for (auto cell_it = som.begin(); cell_it != som.end(); ++ cell_it) {
72 
73  // Compute the factor based on the distance of the BMU and the cell
74  auto cell_x = cell_it.template axisValue<0>();
75  auto cell_y = cell_it.template axisValue<1>();
76  auto neighborhood_factor = m_neighborhood_func({bmu_x, bmu_y}, {cell_x, cell_y}, i, iter_no);
77 
78  // Get the weights of the cell and update them
79  if (neighborhood_factor != 0) {
80  auto& cell_weights = *cell_it;
81  for (std::size_t wi = 0; wi < ND; ++wi) {
82  cell_weights[wi] = cell_weights[wi] + neighborhood_factor * learn_factor * (input_weights[wi] - cell_weights[wi]);
83  }
84  }
85 
86  }
87  }
88  }
89  }
90 
91 private:
92 
95 
96 };
97 
98 }
99 }
100 
101 #endif /* SOM_SOMTRAINER_H */
102 
SOMTrainer(NeighborhoodFunc::Signature neighborhood_func, LearningRestraintFunc::Signature learning_restraint_func)
Definition: SOMTrainer.h:39
T tie(T... args)
NeighborhoodFunc::Signature m_neighborhood_func
Definition: SOMTrainer.h:93
void train(SOM< ND, DistFunc > &som, std::size_t iter_no, InputIter begin, InputIter end, InputToWeightFunc weight_func, const SamplingPolicy::Interface< InputIter > &sampling_policy=SamplingPolicy::FullSet< InputIter >{})
Definition: SOMTrainer.h:46
LearningRestraintFunc::Signature m_learning_restraint_func
Definition: SOMTrainer.h:94