48 #include "drain/Variable.h"
55 typedef std::vector<double> SomVector;
64 template <
class T = std::vector<
double> >
69 Som(
int width=0,
int height=0);
78 this->height = height;
80 for (
int j = 0; j < height; ++j) {
88 void fill(
const T & sample){
90 for (
int i=0; i<width; i++){
91 for (
int j=0; j<height; j++){
107 for (
int i=0; i<width; i++){
108 for (
int j=0; j<height; j++){
116 void setNeighbourhoodRadius(
double r){
121 void setLearningCoefficient(
double c){
127 void setDistanceFunction(
double (* d)(
const T & x1,
const T & x2)) {
131 void setMixingFunction(
void (* mix)(
const T & x1,
const T & x2,
double coeff, T & m)) {
157 double dBest = std::numeric_limits<double>::max();
159 for (
int j=0; j<height; j++){
160 for (
int i=0; i<width; i++){
161 d = (*distanceFunction)( sample,
map[j][i]);
180 train(x, iBest, jBest);
196 void train(
const T &x,
int iBest,
int jBest){
198 for (
int j=0; j<height; ++j){
199 for (
int i=0; i<width; ++i){
200 f = defaultNeighborhoodFunction(i-iBest, j-jBest);
201 (*mixingFunction)(
map[j][i], x, f,
map[j][i]);
208 void toStream(std::ostream &ostr)
const{
210 std::string delimiter =
"";
212 ostr << title <<
"\n";
213 for (
int j=0; j<height; j++){
214 for (
int i=0; i<width; i++){
215 ostr << j <<
',' << i <<
':';
216 ostr <<
'\t' <<
map[j][i] <<
'\n';
227 double defaultNeighborhoodFunction(
const int & i,
const int & j)
const {
232 double defaultDistanceMetric(
const T & x1,
const T & x2);
235 void defaultMixingFunction(
const T & x1,
const T & x2,
double coeff, T & m);
241 std::vector< std::vector<T> >
map;
280 double euclideanDistance2(
const T & x1,
const T & x2) {
282 double d, result = 0.0;
284 for (
size_t i = 0; i < x1.size(); ++i) {
294 void vectorMix(
const T & x1,
const T & x2,
double coeff, T & m){
296 for (
size_t k = 0; k < x1.size(); ++k)
297 m[k] = (1.0-coeff)*x1[k] + coeff*x2[k];
302 Som<>::Som(
int width,
int height){
303 setGeometry(width, height);
304 setDistanceFunction( euclideanDistance2 );
305 setMixingFunction( vectorMix );
314 for (
size_t k = 0; k < x.size(); ++k)
315 x[k] =
static_cast<T
>(rand() & 0xff);
374 std::ostream & operator<<(std::ostream & ostr,
const std::vector<T> & n){
376 for (
typename std::vector<T>::const_iterator it = n.begin(); it !=
385 std::ostream & operator<<(std::ostream & ostr,
const Som<T> & som){
double(* distanceFunction)(const T &x1, const T &x2)
The similarity metric applied in finding the best-matching unit.
Definition: Som.h:249
void(* mixingFunction)(const T &x1, const T &x2, double alpha, T &result)
Given objects x1 and x1 and a mixing coefficient coeff , outputs mixed object m.
Definition: Som.h:255
void setGeometry(int width, int height)
Definition: Som.h:76
void fill(const T &sample)
Definition: Som.h:88
double learningCoefficient
The radius of the neighbourhood kernel applied in training;.
Definition: Som.h:270
void train(const T &x)
Training with a single sample.
Definition: Som.h:176
int getHeight() const
The number of rows in the map.
Definition: Som.h:138
void train(const T &x, int iBest, int jBest)
Definition: Som.h:196
double radius2
The radius of the neighbourhood kernel applied in training;.
Definition: Som.h:267
std::vector< std::vector< T > > map
The actual neural network, two-dimensional map of units.
Definition: Som.h:241
void findBestMatchingUnit(const T &sample, int &iBest, int &jBest)
Finds the unit with state closesest to that of sample.
Definition: Som.h:154
void initialise(void(*init)(T &x))
Initialise the map with a user-defined function.
Definition: Som.h:105
int getWidth() const
The number of columns in the map.
Definition: Som.h:144
Definition: DataSelector.cpp:1277
void uniformRandomVector256(std::vector< T > &x)
For initialising vector Soms.
Definition: Som.h:312