Skip to content

Commit 8944728

Browse files
committed
Updated Documentation
1 parent d00f6bb commit 8944728

File tree

6 files changed

+280
-87
lines changed

6 files changed

+280
-87
lines changed

docs/README.md

+60-26
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ C-ML is a lightweight and modular machine learning library designed for educatio
3535

3636
- **Layers**: Dense, Dropout
3737
- **Activations**: ReLU, Sigmoid, Tanh, Softmax, ELU, Leaky ReLU, Linear
38-
- **Loss Functions**: Mean Squared Error, Binary Cross-Entropy, Focal Loss, etc.
38+
- **Loss Functions**: Mean Squared Error, Binary Cross-Entropy, Focal Loss, Huber Loss, KLD Loss, Log-Cosh Loss, Poisson Loss, Smooth L1 Loss, Tversky Loss, Cosine Similarity Loss, etc.
3939
- **Optimizers**: SGD, Adam, RMSprop
4040
- **Preprocessing**: Label Encoding, One-Hot Encoding, Standard Scaler, Min-Max Scaler
4141
- **Regularizers**: L1, L2, Combined L1-L2
@@ -103,39 +103,78 @@ C-ML/
103103

104104
## Usage
105105

106-
The `main.c` file demonstrates how to use the library to create a simple neural network. Below is an example:
106+
The `main.c` file demonstrates how to use the library to create a simple neural network with a dense layer, ReLU activation, and mean squared error loss.
107107

108108
```c
109109
#include <stdio.h>
110110
#include <stdlib.h>
111-
#include <math.h>
112-
#include "../src/Layers/dense.h"
113-
#include "../src/Activations/relu.h"
114-
#include "../src/Loss_Functions/meanSquaredError.h"
111+
#include <time.h>
112+
#include "include/Core/training.h"
115113

116114
int main()
117115
{
118-
float input[] = {1.0, 2.0, 3.0};
119-
int input_size = 3;
116+
srand(time(NULL));
117+
NeuralNetwork *network = create_neural_network(2);
120118

121-
float target[] = {0.0, 1.0};
122-
int output_size = 2;
119+
build_network(network, OPTIMIZER_ADAM, 0.01f, LOSS_MSE, 0.0f, 0.0f);
120+
model_add(network, LAYER_DENSE, ACTIVATION_RELU, 2, 4, 0.0f, 0, 0);
121+
model_add(network, LAYER_DENSE, ACTIVATION_TANH, 4, 4, 0.0f, 0, 0);
122+
model_add(network, LAYER_DENSE, ACTIVATION_SIGMOID, 4, 1, 0.0f, 0, 0);
123123

124-
DenseLayer dense_layer = {NULL, NULL, 0, 0};
125-
initializeDense(&dense_layer, input_size, output_size);
124+
int num_samples = 4;
125+
float **X_train = (float **)cm_safe_malloc(num_samples * sizeof(float *), __FILE__, __LINE__);
126+
float **y_train = (float **)cm_safe_malloc(num_samples * sizeof(float *), __FILE__, __LINE__);
126127

127-
float dense_output[2];
128-
forwardDense(&dense_layer, input, dense_output);
128+
for (int i = 0; i < num_samples; i++)
129+
{
130+
X_train[i] = (float *)cm_safe_malloc(2 * sizeof(float), __FILE__, __LINE__);
131+
y_train[i] = (float *)cm_safe_malloc(1 * sizeof(float), __FILE__, __LINE__);
132+
}
133+
134+
X_train[0][0] = 0.0f;
135+
X_train[0][1] = 0.0f;
136+
y_train[0][0] = 0.0f;
137+
X_train[1][0] = 0.0f;
138+
X_train[1][1] = 1.0f;
139+
y_train[1][0] = 1.0f;
140+
141+
X_train[2][0] = 1.0f;
142+
X_train[2][1] = 0.0f;
143+
y_train[2][0] = 1.0f;
144+
145+
X_train[3][0] = 1.0f;
146+
X_train[3][1] = 1.0f;
147+
y_train[3][0] = 1.0f;
148+
149+
summary(network);
150+
train_network(network, X_train, y_train, num_samples, 2, 1, 1, 300);
151+
152+
MetricType metrics[] = {METRIC_R2_SCORE};
129153

130-
for (int i = 0; i < output_size; i++)
154+
int num_metrics = sizeof(metrics) / sizeof(metrics[0]);
155+
float results[num_metrics];
156+
157+
test_network(network, X_train, y_train, num_samples, 2, 1, (int *)metrics, num_metrics, results);
158+
printf("R2 Score: %.2f\n", results[0]);
159+
160+
for (int i = 0; i < num_samples; i++)
131161
{
132-
dense_output[i] = relu(dense_output[i]);
162+
float prediction = 0.0f;
163+
forward_pass(network, X_train[i], &prediction, 2, 1, 0);
164+
printf("Input: [%.0f, %.0f], Expected: %.0f, Predicted: %.4f\n",
165+
X_train[i][0], X_train[i][1], y_train[i][0], prediction);
133166
}
134167

135-
float loss = meanSquaredError(target, dense_output, output_size);
136-
printf("Loss: %f\n", loss);
168+
free_neural_network(network);
169+
170+
for (int i = 0; i < num_samples; i++)
171+
{
172+
cm_safe_free((void **)&X_train[i]);
173+
cm_safe_free((void **)&y_train[i]);
174+
}
175+
cm_safe_free((void **)&X_train);
176+
cm_safe_free((void **)&y_train);
137177

138-
freeDense(&dense_layer);
139178
return 0;
140179
}
141180
```
@@ -248,15 +287,10 @@ Each module has a corresponding test file in the `test/` directory. The tests va
248287

249288
## Contributing
250289

251-
Contributions are welcome! Please follow these steps:
252-
253-
1. Fork the repository.
254-
2. Create a new branch for your feature or bug fix.
255-
3. Commit your changes with clear messages.
256-
4. Submit a pull request.
290+
Contributions are welcome! Feel free to open issues or submit pull requests.
257291

258292
---
259293

260294
## License
261295

262-
This project is licensed under the DBaJ-NC-CFL License. See the [LICENSE](../LICENCE.md) file for details.
296+
This project is licensed under the DBaJ-NC-CFL [License](./LICENCE.md).

docs/modules/core.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Core Modules
2+
3+
This section documents the core modules of the C-ML library, which provide essential functionalities for building and training neural networks.
4+
5+
## Training Module
6+
7+
- **Description**: Provides functions for creating, building, training, and evaluating neural networks.
8+
- **Functions**:
9+
- `NeuralNetwork *create_neural_network(int input_size)`: Creates a new neural network.
10+
- `CM_Error build_network(NeuralNetwork *network, OptimizerType optimizer_type, float learning_rate, int loss_function, float l1_lambda, float l2_lambda)`: Builds the neural network by setting the optimizer, loss function, and regularization parameters.
11+
- `CM_Error add_layer(NeuralNetwork *network, LayerConfig config)`: Adds a layer to the neural network.
12+
- `CM_Error model_add(NeuralNetwork *network, LayerType type, ActivationType activation, int input_size, int output_size, float rate, int kernel_size, int stride)`: Adds a layer to the neural network using a simplified interface.
13+
- `CM_Error forward_pass(NeuralNetwork *network, float *input, float *output, int input_size, int output_size, int is_training)`: Performs a forward pass through the network.
14+
- `float calculate_loss(float *predicted, float *actual, int size, LossType loss_type)`: Calculates the loss between predicted and actual values.
15+
- `void calculate_loss_gradient(float *predicted, float *actual, float *gradient, int size, LossType loss_type)`: Calculates the gradient of the loss function.
16+
- `CM_Error train_network(NeuralNetwork *network, float **X_train, float **y_train, int num_samples, int input_size, int output_size, int batch_size, int epochs)`: Trains the neural network.
17+
- `CM_Error evaluate_network(NeuralNetwork *network, float **X_test, float **y_test, int num_samples, int input_size, int output_size, int *metrics, int num_metrics, float *results)`: Evaluates the neural network on a given dataset.
18+
- `CM_Error test_network(NeuralNetwork *network, float **X_test, float **y_test, int num_samples, int input_size, int output_size, int *metrics, int num_metrics, float *results)`: Tests the neural network (alias for evaluate_network).
19+
- `CM_Error free_neural_network(NeuralNetwork *network)`: Frees memory allocated for the neural network.
20+
- `void summary(NeuralNetwork *network)`: Prints a summary of the neural network architecture.
21+
- **File**: [`training.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Core/training.c)
22+
23+
## Memory Management Module
24+
25+
- **Description**: Provides safe memory allocation and deallocation functions.
26+
- **Functions**:
27+
- `void *cm_safe_malloc(size_t size, const char *file, int line)`: Allocates memory safely and logs the file and line number in case of failure.
28+
- `void cm_safe_free(void **ptr)`: Frees allocated memory safely and sets the pointer to NULL.
29+
- **File**: [`memory_management.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Core/memory_management.c)

docs/modules/loss_functions.md

+35
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,38 @@
3434
- **Description**: Computes the mean of an array of values.
3535
- **Function**: `reduce_mean(float *loss, int size)`
3636
- **File**: [`reduce_mean.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/reduce_mean.c)
37+
38+
## Huber Loss
39+
- **Description**: Huber Loss function.
40+
- **Function**: `huber_loss(float *y, float *yHat, int n)`
41+
- **File**: [`huber_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/huber_loss.c)
42+
43+
## KLD Loss
44+
- **Description**: Kullback-Leibler Divergence Loss function.
45+
- **Function**: `kld_loss(float *p, float *q, int n)`
46+
- **File**: [`kld_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/kld_loss.c)
47+
48+
## Log-Cosh Loss
49+
- **Description**: Log-Cosh Loss function.
50+
- **Function**: `log_cosh_loss(float *y, float *yHat, int n)`
51+
- **File**: [`log_cosh_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/log_cosh_loss.c)
52+
53+
## Poisson Loss
54+
- **Description**: Poisson Loss function.
55+
- **Function**: `poisson_loss(float *y, float *yHat, int n)`
56+
- **File**: [`poisson_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/poisson_loss.c)
57+
58+
## Smooth L1 Loss
59+
- **Description**: Smooth L1 Loss function.
60+
- **Function**: `smooth_l1_loss(float *y, float *yHat, int n)`
61+
- **File**: [`smooth_l1_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/smooth_l1_loss.c)
62+
63+
## Tversky Loss
64+
- **Description**: Tversky Loss function.
65+
- **Function**: `tversky_loss(float *y, float *yHat, int n)`
66+
- **File**: [`tversky_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/tversky_loss.c)
67+
68+
## Cosine Similarity Loss
69+
- **Description**: Cosine Similarity Loss function.
70+
- **Function**: `cosine_similarity_loss(float *y, float *yHat, int n)`
71+
- **File**: [`cosine_similarity_loss.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Loss_Functions/cosine_similarity_loss.c)

docs/modules/metrics.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Metrics
2+
3+
## Accuracy
4+
- **Description**: Calculates the accuracy of the model.
5+
- **Function**: `accuracy(float *y, float *yHat, int n)`
6+
- **File**: [`accuracy.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/accuracy.c)
7+
8+
---
9+
10+
## Balanced Accuracy
11+
- **Description**: Calculates the balanced accuracy of the model.
12+
- **Function**: `balanced_accuracy(float *y, float *yHat, int n)`
13+
- **File**: [`balanced_accuracy.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/balanced_accuracy.c)
14+
15+
---
16+
17+
## Cohen's Kappa
18+
- **Description**: Calculates Cohen's Kappa coefficient.
19+
- **Function**: `cohens_kappa(float *y, float *yHat, int n)`
20+
- **File**: [`cohens_kappa.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/cohens_kappa.c)
21+
22+
---
23+
24+
## F1 Score
25+
- **Description**: Calculates the F1 score of the model.
26+
- **Function**: `f1_score(float *y, float *yHat, int n)`
27+
- **File**: [`f1_score.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/f1_score.c)
28+
29+
---
30+
31+
## IOU (Intersection over Union)
32+
- **Description**: Calculates the Intersection over Union.
33+
- **Function**: `iou(float *y, float *yHat, int n)`
34+
- **File**: [`iou.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/iou.c)
35+
36+
---
37+
38+
## MCC (Matthews Correlation Coefficient)
39+
- **Description**: Calculates the Matthews Correlation Coefficient.
40+
- **Function**: `mcc(float *y, float *yHat, int n)`
41+
- **File**: [`mcc.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/mcc.c)
42+
43+
---
44+
45+
## Mean Absolute Error
46+
- **Description**: Calculates the Mean Absolute Error.
47+
- **Function**: `mean_absolute_error(float *y, float *yHat, int n)`
48+
- **File**: [`mean_absolute_error.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/mean_absolute_error.c)
49+
50+
---
51+
52+
## Mean Absolute Percentage Error
53+
- **Description**: Calculates the Mean Absolute Percentage Error.
54+
- **Function**: `mean_absolute_percentage_error(float *y, float *yHat, int n)`
55+
- **File**: [`mean_absolute_percentage_error.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/mean_absolute_percentage_error.c)
56+
57+
---
58+
59+
## Precision
60+
- **Description**: Calculates the precision of the model.
61+
- **Function**: `precision(float *y, float *yHat, int n)`
62+
- **File**: [`precision.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/precision.c)
63+
64+
---
65+
66+
## R2 Score
67+
- **Description**: Calculates the R2 score (coefficient of determination).
68+
- **Function**: `r2_score(float *y_true, float *y_pred, int size)`
69+
- **File**: [`r2_score.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/r2_score.c)
70+
71+
---
72+
73+
## Recall
74+
- **Description**: Calculates the recall of the model.
75+
- **Function**: `recall(float *y, float *yHat, int n)`
76+
- **File**: [`recall.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/recall.c)
77+
78+
---
79+
80+
## Root Mean Squared Error
81+
- **Description**: Calculates the Root Mean Squared Error.
82+
- **Function**: `root_mean_squared_error(float *y, float *yHat, int n)`
83+
- **File**: [`root_mean_squared_error.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/root_mean_squared_error.c)
84+
85+
---
86+
87+
## Specificity
88+
- **Description**: Calculates the Specificity.
89+
- **Function**: `Specificity(float *y, float *yHat, int n)`
90+
- **File**: [`Specificity.c`](https://github.com/jaywyawhare/C-ML/tree/master/src/Metrics/Specificity.c)
91+
92+
---

0 commit comments

Comments
 (0)