machine_learning Namespace Reference

Machine learning algorithms. More...

## Classes

class | adaline

## Functions

int | save_u_matrix (const char *fname, const std::vector< std::vector< std::valarray< double >>> &W)
double | update_weights (const std::valarray< double > &X, std::vector< std::vector< std::valarray< double >>> *W, std::vector< std::valarray< double >> *D, double alpha, int R)
void | kohonen_som (const std::vector< std::valarray< double >> &X, std::vector< std::vector< std::valarray< double >>> *W, double alpha_min)
void | update_weights (const std::valarray< double > &x, std::vector< std::valarray< double >> *W, std::valarray< double > *D, double alpha, int R)
void | kohonen_som_tracer (const std::vector< std::valarray< double >> &X, std::vector< std::valarray< double >> *W, double alpha_min)
template<typename T >
std::ostream & | operator<< (std::ostream &out, std::vector< std::valarray< T >> const &A)
template<typename T >
std::ostream & | operator<< (std::ostream &out, const std::pair< T, T > &A)
template<typename T >
std::ostream & | operator<< (std::ostream &out, const std::valarray< T > &A)
template<typename T >
std::valarray< T > | insert_element (const std::valarray< T > &A, const T &ele)
template<typename T >
std::valarray< T > | pop_front (const std::valarray< T > &A)
template<typename T >
std::valarray< T > | pop_back (const std::valarray< T > &A)
template<typename T >
void | equal_shuffle (std::vector< std::vector< std::valarray< T >>> &A, std::vector< std::vector< std::valarray< T >>> &B)
template<typename T >
void | uniform_random_initialization (std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape, const T &low, const T &high)
template<typename T >
void | unit_matrix_initialization (std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape)
template<typename T >
void | zeroes_initialization (std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape)
template<typename T >
T | sum (const std::vector< std::valarray< T >> &A)
template<typename T >
std::pair< size_t, size_t > | get_shape (const std::vector< std::valarray< T >> &A)
template<typename T >
std::vector< std::vector< std::valarray< T > > > | minmax_scaler (const std::vector< std::vector< std::valarray< T >>> &A, const T &low, const T &high)
template<typename T >
size_t | argmax (const std::vector< std::valarray< T >> &A)
template<typename T >
std::vector< std::valarray< T > > | apply_function (const std::vector< std::valarray< T >> &A, T(*func)(const T &))
template<typename T >
std::vector< std::valarray< T > > | operator* (const std::vector< std::valarray< T >> &A, const T &val)
template<typename T >
std::vector< std::valarray< T > > | operator/ (const std::vector< std::valarray< T >> &A, const T &val)
template<typename T >
std::vector< std::valarray< T > > | transpose (const std::vector< std::valarray< T >> &A)
template<typename T >
std::vector< std::valarray< T > > | operator+ (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)
template<typename T >
std::vector< std::valarray< T > > | operator- (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)
template<typename T >
std::vector< std::valarray< T > > | multiply (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)
template<typename T >
std::vector< std::valarray< T > > | hadamard_product (const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)

## Variables

constexpr double | MIN_DISTANCE = 1e-4

## Detailed Description

Machine learning algorithms.

Machine Learning algorithms.

## Function Documentation

## apply_function()

template<typename T >

std::vector<std::valarray<T> > machine_learning::apply_function | ( | const std::vector< std::valarray< T >> & | A,
|  | T(*)(const T &) | func
| ) |  |

Function which applys supplied function to every element of 2D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector on which function will be applied
  func | Function to be applied

Returns
  new resultant vector

{
std::vector<std::valarray<double>> B =
A; // New vector to store resultant vector
for (auto &b : B) { // For every row in vector
b = b.apply(func); // Apply function to that row
}
return B; // Return new resultant 2D vector
}
std::vector

## argmax()

template<typename T >

size_t machine_learning::argmax | ( | const std::vector< std::valarray< T >> & | A | ) |

Function to get index of maximum element in 2D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector for which maximum index is required

Returns
  index of maximum element

{
const auto shape = get_shape(A);
// As this function is used on predicted (or target) vector, shape should be
// (1, X)
if (shape.first != 1) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Supplied vector is ineligible for argmax" << std::endl;
std::exit(EXIT_FAILURE);
}
// Return distance of max element from first element (i.e. index)
return std::distance(std::begin(A[0]),
std::max_element(std::begin(A[0]), std::end(A[0])));
}
std::cerr
std::begin
T begin(T... args)
std::distance
T distance(T... args)
std::end
T end(T... args)
std::endl
T endl(T... args)
std::exit
T exit(T... args)
std::max_element
T max_element(T... args)
machine_learning::get_shape
std::pair< size_t, size_t > get_shape(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:247
Here is the call graph for this function:

## equal_shuffle()

template<typename T >

void machine_learning::equal_shuffle | ( | std::vector< std::vector< std::valarray< T >>> & | A,
|  | std::vector< std::vector< std::valarray< T >>> & | B
| ) |  |

Function to equally shuffle two 3D vectors (used for shuffling training data)

Template Parameters

  T | typename of the vector

Parameters

  A | First 3D vector
  B | Second 3D vector

{
// If two vectors have different sizes
if (A.size() != B.size()) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr
<< "Can not equally shuffle two vectors with different sizes: ";
std::cerr << A.size() << " and " << B.size() << std::endl;
std::exit(EXIT_FAILURE);
}
for (size_t i = 0; i < A.size(); i++) { // For every element in A and B
// Genrating random index < size of A and B
std::srand(std::chrono::system_clock::now().time_since_epoch().count());
size_t random_index = std::rand() % A.size();
// Swap elements in both A and B with same random index
std::swap(A[i], A[random_index]);
std::swap(B[i], B[random_index]);
}
return;
}
std::chrono::system_clock::now
T now(T... args)
std::rand
T rand(T... args)
std::vector::size
T size(T... args)
std::srand
T srand(T... args)
std::swap
T swap(T... args)
Here is the call graph for this function:

## get_shape()

template<typename T >

std::pair<size_t, size_t> machine_learning::get_shape | ( | const std::vector< std::valarray< T >> & | A | ) |

Function to get shape of given 2D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector for which shape is required

Returns
  shape as pair

{
const size_t sub_size = (*A.begin()).size();
for (const auto &a : A) {
// If supplied vector don't have same shape in all rows
if (a.size() != sub_size) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Supplied vector is not 2D Matrix" << std::endl;
std::exit(EXIT_FAILURE);
}
}
return std::make_pair(A.size(), sub_size); // Return shape as pair
}
std::make_pair
T make_pair(T... args)

## hadamard_product()

template<typename T >

std::vector<std::valarray<T> > machine_learning::hadamard_product | ( | const std::vector< std::valarray< T >> & | A,
|  | const std::vector< std::valarray< T >> & | B
| ) |  |

Function to get hadamard product of two 2D vectors

Template Parameters

  T | typename of the vector

Parameters

  A | First 2D vector
  B | Second 2D vector

Returns
  new resultant vector

{
const auto shape_a = get_shape(A);
const auto shape_b = get_shape(B);
// If vectors are not eligible for hadamard product
if (shape_a.first != shape_b.first || shape_a.second != shape_b.second) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Vectors have different shapes ";
std::cerr << shape_a << " and " << shape_b << std::endl;
std::exit(EXIT_FAILURE);
}
std::vector<std::valarray<T>> C; // Vector to store result
for (size_t i = 0; i < A.size(); i++) {
C.push_back(A[i] * B[i]); // Elementwise multiplication
}
return C; // Return new resultant 2D vector
}
std::vector::push_back
T push_back(T... args)
Here is the call graph for this function:

## insert_element()

template<typename T >

std::valarray<T> machine_learning::insert_element | ( | const std::valarray< T > & | A,
|  | const T & | ele
| ) |  |

Function to insert element into 1D vector

Template Parameters

  T | typename of the 1D vector and the element

Parameters

  A | 1D vector in which element will to be inserted
  ele | element to be inserted

Returns
  new resultant vector

{
std::valarray<T> B; // New 1D vector to store resultant vector
B.resize(A.size() + 1); // Resizing it accordingly
for (size_t i = 0; i < A.size(); i++) { // For every element in A
B[i] = A[i]; // Copy element in B
}
B[B.size() - 1] = ele; // Inserting new element in last position
return B; // Return resultant vector
}
std::valarray

## kohonen_som()

void machine_learning::kohonen_som | ( | const std::vector< std::valarray< double >> & | X,
|  | std::vector< std::vector< std::valarray< double >>> * | W,
|  | double | alpha_min
| ) |  |

Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.

Parameters

  [in] | X | data set
  [in,out] | W | weights matrix
  [in] | alpha_min | terminal value of alpha

{
size_t num_samples = X.size(); // number of rows
// size_t num_features = X[0].size(); // number of columns
size_t num_out = W->size(); // output matrix size
size_t R = num_out >> 2, iter = 0;
double alpha = 1.f;
std::vector<std::valarray<double>> D(num_out);
for (int i = 0; i < num_out; i++) D[i] = std::valarray<double>(num_out);
double dmin = 1.f; // average minimum distance of all samples
double past_dmin = 1.f; // average minimum distance of all samples
double dmin_ratio = 1.f; // change per step
// Loop alpha from 1 to slpha_min
for (; alpha > 0 && dmin_ratio > 1e-5; alpha -= 1e-4, iter++) {
// Loop for each sample pattern in the data set
for (int sample = 0; sample < num_samples; sample++) {
// update weights for the current input pattern sample
dmin += update_weights(X[sample], W, &D, alpha, R);
}
// every 100th iteration, reduce the neighborhood range
if (iter % 300 == 0 && R > 1) {
R--;
}
dmin /= num_samples;
// termination condition variable -> % change in minimum distance
dmin_ratio = (past_dmin - dmin) / past_dmin;
if (dmin_ratio < 0) {
dmin_ratio = 1.f;
}
past_dmin = dmin;
std::cout << "iter: " << iter << "\t alpha: " << alpha << "\t R: " << R
<< "\t d_min: " << dmin_ratio << "\r";
}
std::cout << "\n";
}
machine_learning::update_weights
double update_weights(const std::valarray< double > &X, std::vector< std::vector< std::valarray< double >>> *W, std::vector< std::valarray< double >> *D, double alpha, int R)
Definition: kohonen_som_topology.cpp:200
Here is the call graph for this function:

## kohonen_som_tracer()

void machine_learning::kohonen_som_tracer | ( | const std::vector< std::valarray< double >> & | X,
|  | std::vector< std::valarray< double >> * | W,
|  | double | alpha_min
| ) |  |

Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset.

Parameters

  [in] | X | data set
  [in,out] | W | weights matrix
  [in] | alpha_min | terminal value of alpha

{
int num_samples = X.size(); // number of rows
// int num_features = X[0].size(); // number of columns
int num_out = W->size(); // number of rows
int R = num_out >> 2, iter = 0;
double alpha = 1.f;
std::valarray<double> D(num_out);
// Loop alpha from 1 to slpha_min
do {
// Loop for each sample pattern in the data set
for (int sample = 0; sample < num_samples; sample++) {
// update weights for the current input pattern sample
update_weights(X[sample], W, &D, alpha, R);
}
// every 10th iteration, reduce the neighborhood range
if (iter % 10 == 0 && R > 1) {
R--;
}
alpha -= 0.01;
iter++;
} while (alpha > alpha_min);
}
machine_learning::update_weights
void update_weights(const std::valarray< double > &x, std::vector< std::valarray< double >> *W, std::valarray< double > *D, double alpha, int R)
Definition: kohonen_som_trace.cpp:103
Here is the call graph for this function:

## minmax_scaler()

template<typename T >

std::vector<std::vector<std::valarray<T> > > machine_learning::minmax_scaler | ( | const std::vector< std::vector< std::valarray< T >>> & | A,
|  | const T & | low,
|  | const T & | high
| ) |  |

Function to scale given 3D vector using min-max scaler

Template Parameters

  T | typename of the vector

Parameters

  A | 3D vector which will be scaled
  low | new minimum value
  high | new maximum value

Returns
  new scaled 3D vector

{
std::vector<std::vector<std::valarray<T>>> B =
A; // Copying into new vector B
const auto shape = get_shape(B[0]); // Storing shape of B's every element
// As this function is used for scaling training data vector should be of
// shape (1, X)
if (shape.first != 1) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr
<< "Supplied vector is not supported for minmax scaling, shape: ";
std::cerr << shape << std::endl;
std::exit(EXIT_FAILURE);
}
for (size_t i = 0; i < shape.second; i++) {
T min = B[0][0][i], max = B[0][0][i];
for (size_t j = 0; j < B.size(); j++) {
// Updating minimum and maximum values
min = std::min(min, B[j][0][i]);
max = std::max(max, B[j][0][i]);
}
for (size_t j = 0; j < B.size(); j++) {
// Applying min-max scaler formula
B[j][0][i] =
((B[j][0][i] - min) / (max - min)) * (high - low) + low;
}
}
return B; // Return new resultant 3D vector
}
std::max
T max(T... args)
std::min
T min(T... args)
Here is the call graph for this function:

## multiply()

template<typename T >

std::vector<std::valarray<T> > machine_learning::multiply | ( | const std::vector< std::valarray< T >> & | A,
|  | const std::vector< std::valarray< T >> & | B
| ) |  |

Function to multiply two 2D vectors

Template Parameters

  T | typename of the vector

Parameters

  A | First 2D vector
  B | Second 2D vector

Returns
  new resultant vector

{
const auto shape_a = get_shape(A);
const auto shape_b = get_shape(B);
// If vectors are not eligible for multiplication
if (shape_a.second != shape_b.first) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Vectors are not eligible for multiplication ";
std::cerr << shape_a << " and " << shape_b << std::endl;
std::exit(EXIT_FAILURE);
}
std::vector<std::valarray<T>> C; // Vector to store result
// Normal matrix multiplication
for (size_t i = 0; i < shape_a.first; i++) {
std::valarray<T> row;
row.resize(shape_b.second);
for (size_t j = 0; j < shape_b.second; j++) {
for (size_t k = 0; k < shape_a.second; k++) {
row[j] += A[i][k] * B[k][j];
}
}
C.push_back(row);
}
return C; // Return new resultant 2D vector
}
Here is the call graph for this function:

## operator*()

template<typename T >

std::vector<std::valarray<T> > machine_learning::operator* | ( | const std::vector< std::valarray< T >> & | A,
|  | const T & | val
| ) |  |

Overloaded operator "*" to multiply given 2D vector with scaler

Template Parameters

  T | typename of both vector and the scaler

Parameters

  A | 2D vector to which scaler will be multiplied
  val | Scaler value which will be multiplied

Returns
  new resultant vector

{
std::vector<std::valarray<double>> B =
A; // New vector to store resultant vector
for (auto &b : B) { // For every row in vector
b = b * val; // Multiply row with scaler
}
return B; // Return new resultant 2D vector
}

## operator+()

template<typename T >

std::vector<std::valarray<T> > machine_learning::operator+ | ( | const std::vector< std::valarray< T >> & | A,
|  | const std::vector< std::valarray< T >> & | B
| ) |  |

Overloaded operator "+" to add two 2D vectors

Template Parameters

  T | typename of the vector

Parameters

  A | First 2D vector
  B | Second 2D vector

Returns
  new resultant vector

{
const auto shape_a = get_shape(A);
const auto shape_b = get_shape(B);
// If vectors don't have equal shape
if (shape_a.first != shape_b.first || shape_a.second != shape_b.second) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Supplied vectors have different shapes ";
std::cerr << shape_a << " and " << shape_b << std::endl;
std::exit(EXIT_FAILURE);
}
std::vector<std::valarray<T>> C;
for (size_t i = 0; i < A.size(); i++) { // For every row
C.push_back(A[i] + B[i]); // Elementwise addition
}
return C; // Return new resultant 2D vector
}
Here is the call graph for this function:

## operator-()

template<typename T >

std::vector<std::valarray<T> > machine_learning::operator- | ( | const std::vector< std::valarray< T >> & | A,
|  | const std::vector< std::valarray< T >> & | B
| ) |  |

Overloaded operator "-" to add subtract 2D vectors

Template Parameters

  T | typename of the vector

Parameters

  A | First 2D vector
  B | Second 2D vector

Returns
  new resultant vector

{
const auto shape_a = get_shape(A);
const auto shape_b = get_shape(B);
// If vectors don't have equal shape
if (shape_a.first != shape_b.first || shape_a.second != shape_b.second) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Supplied vectors have different shapes ";
std::cerr << shape_a << " and " << shape_b << std::endl;
std::exit(EXIT_FAILURE);
}
std::vector<std::valarray<T>> C; // Vector to store result
for (size_t i = 0; i < A.size(); i++) { // For every row
C.push_back(A[i] - B[i]); // Elementwise substraction
}
return C; // Return new resultant 2D vector
}
Here is the call graph for this function:

## operator/()

template<typename T >

std::vector<std::valarray<T> > machine_learning::operator/ | ( | const std::vector< std::valarray< T >> & | A,
|  | const T & | val
| ) |  |

Overloaded operator "/" to divide given 2D vector with scaler

Template Parameters

  T | typename of the vector and the scaler

Parameters

  A | 2D vector to which scaler will be divided
  val | Scaler value which will be divided

Returns
  new resultant vector

{
std::vector<std::valarray<double>> B =
A; // New vector to store resultant vector
for (auto &b : B) { // For every row in vector
b = b / val; // Divide row with scaler
}
return B; // Return new resultant 2D vector
}

## operator<<() [1/3]

template<typename T >

std::ostream& machine_learning::operator<< | ( | std::ostream & | out,
|  | const std::pair< T, T > & | A
| ) |  |

Overloaded operator "<<" to print a pair

Template Parameters

  T | typename of the pair

Parameters

  out | std::ostream to output
  A | Pair to be printed

{
// Setting output precision to 4 in case of floating point numbers
out.precision(4);
// printing pair in the form (p, q)
std::cout << "(" << A.first << ", " << A.second << ")";
return out;
}
std::ostream::precision
T precision(T... args)
Here is the call graph for this function:

## operator<<() [2/3]

template<typename T >

std::ostream& machine_learning::operator<< | ( | std::ostream & | out,
|  | const std::valarray< T > & | A
| ) |  |

Overloaded operator "<<" to print a 1D vector

Template Parameters

  T | typename of the vector

Parameters

  out | std::ostream to output
  A | 1D vector to be printed

{
// Setting output precision to 4 in case of floating point numbers
out.precision(4);
for (const auto &a : A) { // For every element in the vector.
std::cout << a << ' '; // Print element
}
std::cout << std::endl;
return out;
}
Here is the call graph for this function:

## operator<<() [3/3]

template<typename T >

std::ostream& machine_learning::operator<< | ( | std::ostream & | out,
|  | std::vector< std::valarray< T >> const & | A
| ) |  |

Overloaded operator "<<" to print 2D vector

Template Parameters

  T | typename of the vector

Parameters

  out | std::ostream to output
  A | 2D vector to be printed

{
// Setting output precision to 4 in case of floating point numbers
out.precision(4);
for (const auto &a : A) { // For each row in A
for (const auto &x : a) { // For each element in row
std::cout << x << ' '; // print element
}
std::cout << std::endl;
}
return out;
}
Here is the call graph for this function:

## pop_back()

template<typename T >

std::valarray<T> machine_learning::pop_back | ( | const std::valarray< T > & | A | ) |

Function to remove last element from 1D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 1D vector from which last element will be removed

Returns
  new resultant vector

{
std::valarray<T> B; // New 1D vector to store resultant vector
B.resize(A.size() - 1); // Resizing it accordingly
for (size_t i = 0; i < A.size() - 1;
i++) { // For every (except last) element in A
B[i] = A[i]; // Copy element in B
}
return B; // Return resultant vector
}

## pop_front()

template<typename T >

std::valarray<T> machine_learning::pop_front | ( | const std::valarray< T > & | A | ) |

Function to remove first element from 1D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 1D vector from which first element will be removed

Returns
  new resultant vector

{
std::valarray<T> B; // New 1D vector to store resultant vector
B.resize(A.size() - 1); // Resizing it accordingly
for (size_t i = 1; i < A.size();
i++) { // // For every (except first) element in A
B[i - 1] = A[i]; // Copy element in B with left shifted position
}
return B; // Return resultant vector
}

## save_u_matrix()

int machine_learning::save_u_matrix | ( | const char * | fname,
|  | const std::vector< std::vector< std::valarray< double >>> & | W
| ) |  |

Create the distance matrix or U-matrix from the trained 3D weiths matrix and save to disk.

Parameters

  [in] | fname | filename to save in (gets overwriten without confirmation)
  [in] | W | model matrix to save

Returns
  0 if all ok
  -1 if file creation failed

{
std::ofstream fp(fname);
if (!fp) { // error with fopen
std::cerr << "File error (" << fname << "): " << std::strerror(errno)
<< std::endl;
return -1;
}
// neighborhood range
unsigned int R = 1;
for (int i = 0; i < W.size(); i++) { // for each x
for (int j = 0; j < W[0].size(); j++) { // for each y
double distance = 0.f;
int from_x = std::max<int>(0, i - R);
int to_x = std::min<int>(W.size(), i + R + 1);
int from_y = std::max<int>(0, j - R);
int to_y = std::min<int>(W[0].size(), j + R + 1);
int l = 0, m = 0;
#ifdef _OPENMP
#pragma omp parallel for reduction(+ : distance)
#endif
for (l = from_x; l < to_x; l++) { // scan neighborhoor in x
for (m = from_y; m < to_y; m++) { // scan neighborhood in y
auto d = W[i][j] - W[l][m];
double d2 = std::pow(d, 2).sum();
distance += std::sqrt(d2);
// distance += d2;
}
}
distance /= R * R; // mean distance from neighbors
fp << distance; // print the mean separation
if (j < W[0].size() - 1) { // if not the last column
fp << ','; // suffix comma
}
}
if (i < W.size() - 1) { // if not the last row
fp << '\n'; // start a new line
}
}
fp.close();
return 0;
}
std::ofstream
std::pow
T pow(T... args)
std::sqrt
T sqrt(T... args)
std::strerror
T strerror(T... args)
Here is the call graph for this function:

## sum()

template<typename T >

T machine_learning::sum | ( | const std::vector< std::valarray< T >> & | A | ) |

Function to get sum of all elements in 2D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector for which sum is required

Returns
  returns sum of all elements of 2D vector

{
T cur_sum = 0; // Initially sum is zero
for (const auto &a : A) { // For every row in A
cur_sum += a.sum(); // Add sum of that row to current sum
}
return cur_sum; // Return sum
}

## transpose()

template<typename T >

std::vector<std::valarray<T> > machine_learning::transpose | ( | const std::vector< std::valarray< T >> & | A | ) |

Function to get transpose of 2D vector

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector which will be transposed

Returns
  new resultant vector

{
const auto shape = get_shape(A); // Current shape of vector
std::vector<std::valarray<T>> B; // New vector to store result
// Storing transpose values of A in B
for (size_t j = 0; j < shape.second; j++) {
std::valarray<T> row;
row.resize(shape.first);
for (size_t i = 0; i < shape.first; i++) {
row[i] = A[i][j];
}
B.push_back(row);
}
return B; // Return new resultant 2D vector
}
Here is the call graph for this function:

## uniform_random_initialization()

template<typename T >

void machine_learning::uniform_random_initialization | ( | std::vector< std::valarray< T >> & | A,
|  | const std::pair< size_t, size_t > & | shape,
|  | const T & | low,
|  | const T & | high
| ) |  |

Function to initialize given 2D vector using uniform random initialization

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector to be initialized
  shape | required shape
  low | lower limit on value
  high | upper limit on value

{
A.clear(); // Making A empty
// Uniform distribution in range [low, high]
std::default_random_engine generator(
std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_real_distribution<T> distribution(low, high);
for (size_t i = 0; i < shape.first; i++) { // For every row
std::valarray<T>
row; // Making empty row which will be inserted in vector
row.resize(shape.second);
for (auto &r : row) { // For every element in row
r = distribution(generator); // copy random number
}
A.push_back(row); // Insert new row in vector
}
return;
}
std::vector::clear
T clear(T... args)
std::default_random_engine
std::uniform_real_distribution
Here is the call graph for this function:

## unit_matrix_initialization()

template<typename T >

void machine_learning::unit_matrix_initialization | ( | std::vector< std::valarray< T >> & | A,
|  | const std::pair< size_t, size_t > & | shape
| ) |  |

Function to Intialize 2D vector as unit matrix

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector to be initialized
  shape | required shape

{
A.clear(); // Making A empty
for (size_t i = 0; i < shape.first; i++) {
std::valarray<T>
row; // Making empty row which will be inserted in vector
row.resize(shape.second);
row[i] = T(1); // Insert 1 at ith position
A.push_back(row); // Insert new row in vector
}
return;
}

## update_weights() [1/2]

void machine_learning::update_weights | ( | const std::valarray< double > & | x,
|  | std::vector< std::valarray< double >> * | W,
|  | std::valarray< double > * | D,
|  | double | alpha,
|  | int | R
| ) |  |

Update weights of the SOM using Kohonen algorithm

Parameters

  [in] | X | data point
  [in,out] | W | weights matrix
  [in,out] | D | temporary vector to store distances
  [in] | alpha | learning rate \(0<\alpha\le1\)
  [in] | R | neighborhood range

{
int j = 0, k = 0;
int num_out = W->size(); // number of SOM output nodes
// int num_features = x.size(); // number of data features
#ifdef _OPENMP
#pragma omp for
#endif
// step 1: for each output point
for (j = 0; j < num_out; j++) {
// compute Euclidian distance of each output
// point from the current sample
(*D)[j] = (((*W)[j] - x) * ((*W)[j] - x)).sum();
}
// step 2: get closest node i.e., node with snallest Euclidian distance to
// the current pattern
auto result = std::min_element(std::begin(*D), std::end(*D));
// double d_min = *result;
int d_min_idx = std::distance(std::begin(*D), result);
// step 3a: get the neighborhood range
int from_node = std::max(0, d_min_idx - R);
int to_node = std::min(num_out, d_min_idx + R + 1);
// step 3b: update the weights of nodes in the
// neighborhood
#ifdef _OPENMP
#pragma omp for
#endif
for (j = from_node; j < to_node; j++) {
// update weights of nodes in the neighborhood
(*W)[j] += alpha * (x - (*W)[j]);
}
}
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::min_element
T min_element(T... args)
Here is the call graph for this function:

## update_weights() [2/2]

double machine_learning::update_weights | ( | const std::valarray< double > & | X,
|  | std::vector< std::vector< std::valarray< double >>> * | W,
|  | std::vector< std::valarray< double >> * | D,
|  | double | alpha,
|  | int | R
| ) |  |

Update weights of the SOM using Kohonen algorithm

Parameters

  [in] | X | data point - N features
  [in,out] | W | weights matrix - PxQxN
  [in,out] | D | temporary vector to store distances PxQ
  [in] | alpha | learning rate \(0<\alpha\le1\)
  [in] | R | neighborhood range

Returns
  minimum distance of sample and trained weights

{
int x = 0, y = 0;
int num_out_x = static_cast<int>(W->size()); // output nodes - in X
int num_out_y = static_cast<int>(W[0][0].size()); // output nodes - in Y
// int num_features = static_cast<int>(W[0][0][0].size()); // features =
// in Z
double d_min = 0.f;
#ifdef _OPENMP
#pragma omp for
#endif
// step 1: for each output point
for (x = 0; x < num_out_x; x++) {
for (y = 0; y < num_out_y; y++) {
(*D)[x][y] = 0.f;
// compute Euclidian distance of each output
// point from the current sample
auto d = ((*W)[x][y] - X);
(*D)[x][y] = (d * d).sum();
(*D)[x][y] = std::sqrt((*D)[x][y]);
}
}
// step 2: get closest node i.e., node with snallest Euclidian distance
// to the current pattern
int d_min_x = 0, d_min_y = 0;
get_min_2d(*D, &d_min, &d_min_x, &d_min_y);
// step 3a: get the neighborhood range
int from_x = std::max(0, d_min_x - R);
int to_x = std::min(num_out_x, d_min_x + R + 1);
int from_y = std::max(0, d_min_y - R);
int to_y = std::min(num_out_y, d_min_y + R + 1);
// step 3b: update the weights of nodes in the
// neighborhood
#ifdef _OPENMP
#pragma omp for
#endif
for (x = from_x; x < to_x; x++) {
for (y = from_y; y < to_y; y++) {
/* you can enable the following normalization if needed.
personally, I found it detrimental to convergence */
// const double s2pi = sqrt(2.f * M_PI);
// double normalize = 1.f / (alpha * s2pi);
/* apply scaling inversely proportional to distance from the
current node */
double d2 =
(d_min_x - x) * (d_min_x - x) + (d_min_y - y) * (d_min_y - y);
double scale_factor = std::exp(-d2 / (2.f * alpha * alpha));
(*W)[x][y] += (X - (*W)[x][y]) * alpha * scale_factor;
}
}
return d_min;
}
std::exp
T exp(T... args)
get_min_2d
void get_min_2d(const std::vector< std::valarray< double >> &X, double *val, int *x_idx, int *y_idx)
Definition: kohonen_som_topology.cpp:105
machine_learning::sum
T sum(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:232
Here is the call graph for this function:

## zeroes_initialization()

template<typename T >

void machine_learning::zeroes_initialization | ( | std::vector< std::valarray< T >> & | A,
|  | const std::pair< size_t, size_t > & | shape
| ) |  |

Function to Intialize 2D vector as zeroes

Template Parameters

  T | typename of the vector

Parameters

  A | 2D vector to be initialized
  shape | required shape

{
A.clear(); // Making A empty
for (size_t i = 0; i < shape.first; i++) {
std::valarray<T>
row; // Making empty row which will be inserted in vector
row.resize(shape.second); // By default all elements are zero
A.push_back(row); // Insert new row in vector
}
return;
}

## Variable Documentation

## MIN_DISTANCE

constexpr double machine_learning::MIN_DISTANCE = 1e-4

constexpr

Minimum average distance of image nodes