dmlfw (Daniyal Machine Learning Framework)

Mini-batch stochastic gradient descent training example using DMLFW. More...

#include <dmlfw.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for mini_batch_gd.c:
dmlfw_mat_doublebuffer_matrix_1 = NULL
 
dmlfw_mat_doublebuffer_matrix_1_shuffled = NULL
 
dmlfw_mat_doublebuffer_matrix_2 = NULL
 
dmlfw_mat_doublebuffer_matrix_2_shuffled = NULL
 
uint32_t BUFFER_SIZE = 50
 
void data_loader (void *x, void *y, uint64_t from_row, uint32_t how_many_rows)
 Supplies mini-batch data to gradient descent algorithm.
 
#define FREQUENCY_OF_PRINTING_COST   100
 
void get_from_file_buffer (dmlfw_mat_double **target_matrix, uint64_t from_row, uint32_t how_many_rows)
 Retrieves a data block for training from buffers.
 
dmlfw_gradient_descent_optionsget_gradient_descent_options ()
 Creates and configures options for mini-batch stochastic gradient descent.
 
FILE * gnuplot
 
void init_buffers ()
 Initializes minibatch data buffers before training.
 
#define LEARNING_RATE   0.00001
 
int main ()
 Main entry point running the mini-batch stochastic gradient descent training example.
 
uint32_t MINI_BATCH_SIZE = 46
 
#define MODEL_FILE_NAME   "example-3-model.csv"
 
dmlfw_column_vec_doublemodel_squared_sum_vector = NULL
 
dmlfw_row_vec_doublemodel_transposed = NULL
 
uint64_t number_of_columns_in_training_examples = 0
 
#define NUMBER_OF_ITERATIONS   100000
 
uint64_t number_of_training_examples = 0
 
int on_iteration_complete (uint64_t iteration_number, void *y, void *predicted_y, void *model, double regularization_parameter)
 Callback after each gradient descent iteration to log cost and plot progress.
 
dmlfw_column_vec_doubleprediction_error = NULL
 
dmlfw_row_vec_doubleprediction_error_transposed = NULL
 
void print_error_and_exit ()
 Prints ml-framework error string and terminates program.
 
dmlfw_column_vec_doubleproduct_vector = NULL
 
#define REGULARIZATION_PARAMETER   0.5
 
#define SHOW_GRAPH   1
 
#define TRAINING_DATASET   "IceCreamSales_training_examples.csv"
 
dmlfw_mat_doublex_matrix = NULL
 
dmlfw_mat_doublexy_matrix = NULL
 
dmlfw_column_vec_doubley_vector = NULL
 

Detailed Description

Mini-batch stochastic gradient descent training example using DMLFW.

Author
Mohammed Daniyal
Version
1.0
Date
2025-09-27

This program trains a linear regression model on the IceCreamSales dataset using mini-batch stochastic gradient descent with regularization. It manages data buffering, shuffling, and mini-batch provision for efficient training.

The training progress is logged and optionally visualized with gnuplot showing cost descent and fitted line graphs. The final model parameters are saved to a CSV file.

Global buffers are used for dataset handling, and a custom progress callback logs iteration-wise cost with early termination support.

Usage: ./mini_batch_gd

Macro Definition Documentation

◆ FREQUENCY_OF_PRINTING_COST

#define FREQUENCY_OF_PRINTING_COST   100

Frequency of printing/logging cost

◆ LEARNING_RATE

#define LEARNING_RATE   0.00001

Learning rate for gradient descent

◆ MODEL_FILE_NAME

#define MODEL_FILE_NAME   "example-3-model.csv"

Output model CSV file path

◆ NUMBER_OF_ITERATIONS

#define NUMBER_OF_ITERATIONS   100000

Maximum number of gradient descent iterations

◆ REGULARIZATION_PARAMETER

#define REGULARIZATION_PARAMETER   0.5

Regularization parameter lambda

◆ SHOW_GRAPH

#define SHOW_GRAPH   1

Enable or disable plotting with gnuplot (1 = enabled)

◆ TRAINING_DATASET

#define TRAINING_DATASET   "IceCreamSales_training_examples.csv"

Training dataset CSV file path

Function Documentation

◆ data_loader()

void data_loader ( void *  x,
void *  y,
uint64_t  from_row,
uint32_t  how_many_rows 
)

Supplies mini-batch data to gradient descent algorithm.

Parameters
xPointer to feature matrix pointer.
yPointer to target vector pointer.
from_rowStarting row of data to load.
how_many_rowsNumber of rows to load.

◆ get_from_file_buffer()

void get_from_file_buffer ( dmlfw_mat_double **  target_matrix,
uint64_t  from_row,
uint32_t  how_many_rows 
)

Retrieves a data block for training from buffers.

Parameters
target_matrixMatrix pointer to fill.
from_rowStarting row index in dataset.
how_many_rowsNumber of rows to retrieve.

◆ get_gradient_descent_options()

dmlfw_gradient_descent_options * get_gradient_descent_options ( )

Creates and configures options for mini-batch stochastic gradient descent.

Returns
Pointer to configured options struct or NULL on error.

◆ init_buffers()

void init_buffers ( )

Initializes minibatch data buffers before training.

◆ main()

int main ( )

Main entry point running the mini-batch stochastic gradient descent training example.

Returns
0 on success or error code.

◆ on_iteration_complete()

int on_iteration_complete ( uint64_t  iteration_number,
void *  y,
void *  predicted_y,
void *  model,
double  regularization_parameter 
)

Callback after each gradient descent iteration to log cost and plot progress.

Parameters
iteration_numberCurrent iteration index.
yActual target values.
predicted_yModel predicted values.
modelCurrent model parameters.
regularization_parameterLambda regularization value.
Returns
0 to continue optimization, -1 to terminate early.

◆ print_error_and_exit()

void print_error_and_exit ( )

Prints ml-framework error string and terminates program.

Variable Documentation

◆ buffer_matrix_1

dmlfw_mat_double* buffer_matrix_1 = NULL

Primary buffer matrix holding dataset block for training

◆ buffer_matrix_1_shuffled

dmlfw_mat_double* buffer_matrix_1_shuffled = NULL

Shuffled copy of primary buffer matrix

◆ buffer_matrix_2

dmlfw_mat_double* buffer_matrix_2 = NULL

Secondary buffer matrix holding remaining dataset block

◆ buffer_matrix_2_shuffled

dmlfw_mat_double* buffer_matrix_2_shuffled = NULL

Shuffled copy of secondary buffer matrix

◆ BUFFER_SIZE

uint32_t BUFFER_SIZE = 50

Buffer size, adjustable at runtime for batch loading

◆ gnuplot

FILE* gnuplot

Gnuplot pipe pointer used for plotting graphs

◆ MINI_BATCH_SIZE

uint32_t MINI_BATCH_SIZE = 46

Mini batch size, adjustable at runtime

◆ model_squared_sum_vector

dmlfw_column_vec_double* model_squared_sum_vector = NULL

Vector holding squared sum of model parameters for regularization calculation

◆ model_transposed

dmlfw_row_vec_double* model_transposed = NULL

Transposed vector of current model parameters during callback

◆ number_of_columns_in_training_examples

uint64_t number_of_columns_in_training_examples = 0

Number of columns (features + target) in training dataset

◆ number_of_training_examples

uint64_t number_of_training_examples = 0

Total number of training examples (rows) in dataset

◆ prediction_error

dmlfw_column_vec_double* prediction_error = NULL

Vector holding prediction errors during training progress callback

◆ prediction_error_transposed

dmlfw_row_vec_double* prediction_error_transposed = NULL

Transposed version of prediction_error vector

◆ product_vector

dmlfw_column_vec_double* product_vector = NULL

Temporary vector used for intermediate matrix products

◆ x_matrix

dmlfw_mat_double* x_matrix = NULL

Feature matrix pointer used in data loading for mini-batches

◆ xy_matrix

dmlfw_mat_double* xy_matrix = NULL

Combined feature-target matrix used temporarily for data loading

◆ y_vector

dmlfw_column_vec_double* y_vector = NULL

Target vector pointer used in data loading for mini-batches