SOM functions

Map initialization

There are two functions for initializing a codebook: randinit_codes which produces a random codebook and lininit_codes which produces a linearly initalized codebook based on the eigenvectors of the data. In these functions xdim and ydim specify the dimensions of the map, topology type is set to topol and neighborhood to neigh. Vector dimension is taken from Data.
struct entries *randinit_codes(struct entries *data, int topol, int neigh, int xdim, int ydim)
Creates a random codebook. Returns a ready to use codebook.

struct entries *lininit_codes(struct entries *data, int topol, int neigh, int xdim, int ydim)
Creates a linearly initialized codebook. Codebook vectors lie in the plane defined by the mean and the two largest eigenvectors of data. Returns a ready to use codebook.

Map distances

These functions calculate distances between the units on the 2D map.
float hexa_dist(int bx, int by, int tx, int ty)
Calculates distance of two map units when using hexagonal topology.

float rect_dist(int bx, int by, int tx, int ty)
Calculates distance of two map units when using rectangular topology.

Neighborhood adaptation

In the following functions sample is the data vector, bx and by are the coordinated of the winning codebook vector, radius is the radius of the neighborhood and alpha is the learning parameter. The functions to calculate distances and adapt individual vectors, etc. are taken from teach.
void bubble_adapt(struct teach_params *teach, struct data_entry *sample, int bx, int by, float radius, float alpha)
Adaptation function for bubble-neighborhood.

void gaussian_adapt(struct teach_params *teach, struct data_entry *sample, int bx, int by, float radius, float alpha)
Adaptation function for gaussian neighborhood.

SOM training

struct entries *som_training(struct teach_params *teach)
Train a SOM. Radius of the neighborhood decreases linearly from the initial value to one and the learning parameter decreases linearly from its initial value to zero. All parameters are taken from teach (see Customizing SOM/LVQ_PAK for more information).

Quantization error calculations

float find_qerror(struct teach_params *teach)
Calculates quantization error.

float find_qerror2(struct teach_params *teach)
Calculates quantization error in a different way. Instead of only calculating the difference between the winning codebook vector and the data vector also the quantization error with other vectors in the neighborhood is calculated weighted with the neighborhood function.

float bubble_qerror(struct teach_params *teach, struct data_entry *sample, int bx, int by, float radius)
Calculates quantization error over the neighborhood of the winning entry.

float gaussian_qerror(struct teach_params *teach, struct data_entry *sample, int bx, int by, float radius)
Calculates quantization error over the neighborhood of the winning entry.

Other functions

void normalize(float *v, int n)
Normalizes an array of floats (v). N is the length of the array.

float dotprod(float *v, float *w, int n)
Calculated the dot product of two vectors (float arrays v and w). Length of vectors is n.

int gram_schmidt(float *v, int n, int e)
Has something to do with finding the eigenvectors, I guess.

struct data_entry *find_eigenvectors(struct entries *data)
Finds eigenvectors of data. Returns a pointer to a list of three data_entrys: first is the mean, second is the first eigenvector and third is the second eigenvector. The next-field in the data_entry-structure points to the next entry.

int set_som_params(struct teach_params *params)
Sets some SOM specific stuff in the params if they aren't set already. Sets the mapdist and neigh_adapt functions according to the flags in params.


SOM/LVQ Programming info / SOM/LVQ Home Page / Neural Networks Research Centre