openGPMP
Open Source Mathematics Package
Public Member Functions | List of all members
gpmp::ml::Trainers Class Reference

Training Algorithms. More...

#include <trainers.hpp>

Public Member Functions

std::vector< double > gradientdesc (const std::vector< std::vector< double >> &X, const std::vector< double > &y, double alpha, int num_iters)
 Perform gradient descent for linear regression. More...
 
std::vector< double > stoch_gradientdesc (const std::vector< std::vector< double >> &X, const std::vector< double > &y, double alpha, int num_iters)
 Perform stochastic gradient descent for linear regression. More...
 
std::vector< double > minibatch_gradientdesc (const std::vector< std::vector< double >> &X, const std::vector< double > &y, double alpha, int num_iters, int batch_size)
 Perform mini-batch gradient descent for linear regression. More...
 

Detailed Description

Training Algorithms.

Examples
mlp.cpp.

Definition at line 54 of file trainers.hpp.

Member Function Documentation

◆ gradientdesc()

std::vector< double > gpmp::ml::Trainers::gradientdesc ( const std::vector< std::vector< double >> &  X,
const std::vector< double > &  y,
double  alpha,
int  num_iters 
)

Perform gradient descent for linear regression.

Given features X, target y, learning rate alpha, and number of iterations num_iters, this function optimizes the parameters theta using gradient descent

Parameters
XFeatures matrix (each row represents a training example)
yTarget vector
alphaLearning rate
num_itersNumber of iterations
Returns
std::vector<double> Optimized parameters theta

Definition at line 37 of file trainers.cpp.

40  {
41  int m = X.size(); // Number of training examples
42  int n = X[0].size(); // Number of features
43  std::vector<double> theta(n, 0.0); // Initialize parameters
44 
45  // Perform gradient descent
46  for (int iter = 0; iter < num_iters; iter++) {
47  std::vector<double> delta_theta(n,
48  0.0); // Initialize change in parameters
49 
50  // Compute predictions and errors
51  for (int i = 0; i < m; i++) {
52  double prediction = 0;
53  for (int j = 0; j < n; j++) {
54  prediction += theta[j] * X[i][j];
55  }
56  double error = prediction - y[i];
57 
58  // Update delta_theta
59  for (int j = 0; j < n; j++) {
60  delta_theta[j] += error * X[i][j];
61  }
62  }
63 
64  // Update parameters
65  for (int j = 0; j < n; j++) {
66  theta[j] -= (alpha / m) * delta_theta[j];
67  }
68  }
69 
70  return theta;
71 }

◆ minibatch_gradientdesc()

std::vector< double > gpmp::ml::Trainers::minibatch_gradientdesc ( const std::vector< std::vector< double >> &  X,
const std::vector< double > &  y,
double  alpha,
int  num_iters,
int  batch_size 
)

Perform mini-batch gradient descent for linear regression.

Given features X, target y, learning rate alpha, number of iterations num_iters, and batch size batch_size, this function optimizes the parameters theta using mini-batch gradient descent

Parameters
XFeatures matrix (each row represents a training example)
yTarget vector
alphaLearning rate
num_itersNumber of iterations
batch_sizeSize of mini-batch
Returns
std::vector<double> Optimized parameters theta

Definition at line 103 of file trainers.cpp.

108  {
109  int m = X.size(); // Number of training examples
110  int n = X[0].size(); // Number of features
111  std::vector<double> theta(n, 0.0); // Initialize parameters
112 
113  std::default_random_engine generator;
114  std::uniform_int_distribution<int> distribution(0, m - 1);
115 
116  for (int iter = 0; iter < num_iters; iter++) {
117  // Select random batch indices
118  std::vector<int> batch_indices;
119  for (int i = 0; i < batch_size; i++) {
120  batch_indices.push_back(distribution(generator));
121  }
122 
123  // Compute gradient for the batch
124  std::vector<double> delta_theta(n, 0.0);
125  for (int idx : batch_indices) {
126  double prediction = 0;
127  for (int j = 0; j < n; j++) {
128  prediction += theta[j] * X[idx][j];
129  }
130  double error = prediction - y[idx];
131 
132  for (int j = 0; j < n; j++) {
133  delta_theta[j] += (error * X[idx][j]);
134  }
135  }
136 
137  // Update parameters using the gradient of the batch
138  for (int j = 0; j < n; j++) {
139  theta[j] -= (alpha / batch_size) * delta_theta[j];
140  }
141  }
142 
143  return theta;
144 }

◆ stoch_gradientdesc()

std::vector< double > gpmp::ml::Trainers::stoch_gradientdesc ( const std::vector< std::vector< double >> &  X,
const std::vector< double > &  y,
double  alpha,
int  num_iters 
)

Perform stochastic gradient descent for linear regression.

Given features X, target y, learning rate alpha, and number of iterations num_iters, this function optimizes the parameters theta using stochastic gradient descent

Parameters
XFeatures matrix (each row represents a training example)
yTarget vector
alphaLearning rate
num_itersNumber of iterations
Returns
std::vector<double> Optimized parameters theta

Definition at line 73 of file trainers.cpp.

77  {
78  int m = X.size(); // Number of training examples
79  int n = X[0].size(); // Number of features
80  std::vector<double> theta(n, 0.0); // Initialize parameters
81 
82  std::default_random_engine generator;
83  std::uniform_int_distribution<int> distribution(0, m - 1);
84 
85  for (int iter = 0; iter < num_iters; iter++) {
86  int random_index =
87  distribution(generator); // Choose a random training example
88  double prediction = 0;
89 
90  for (int j = 0; j < n; j++) {
91  prediction += theta[j] * X[random_index][j];
92  }
93 
94  double error = prediction - y[random_index];
95 
96  for (int j = 0; j < n; j++) {
97  theta[j] -= (alpha * error * X[random_index][j]);
98  }
99  }
100 
101  return theta;
102 }

The documentation for this class was generated from the following files: