Welcome to C-ML-Library!
You'll be diving deep into the fundamentals of Machine Learning using pure C. C-ML-Library is a lightweight, dependency-free tensor math library.
For low-level projects, we cover everything natively. You can build Multi-Layer Perceptrons (MLPs), manage memory, and perform matrix multiplications directly. We provide a starter codebase and simple memory-safe methods for basic tensor manipulation.
Take a deep breath. We're here with you! If you're ever stuck, search it up. Seriously. Whatever math operation you need, it's documented right here.
1. Compilation & Linking
To use the library, include ml.h in your program and compile with the corresponding C files.
No external frameworks are required, just standard C math libraries.
-lm when compiling using gcc or clang!
gcc src/main.c src/ml.c -Iinclude -lm -o ml_app
./ml_app
2. Core API Reference
The fundamental building block of C-ML-Library is the Tensor struct, which holds a 1D
contiguous array of floating-point data wrapped with dimensions.
tensor_create(rows, cols)
Allocates memory for a new tensor of size rows x cols.
tensor_free(Tensor* t)
Safely deallocates the tensor and its inner data array.
tensor_fill(Tensor* t, float val)
Fills an entire tensor with a uniform float scalar value.
tensor_print(Tensor* t)
Prints the tensor dimensions and formats its data for terminal debugging.
3. Math Operations & Activations
We provide basic neural network layer operations to perform forward passes natively in C.
tensor_add(Tensor* a, Tensor* b)
Element-wise addition of two tensors. Requires matching dimensions.
tensor_matmul(Tensor* a, Tensor* b)
Dot-product matrix multiplication (A * B). Requires A.cols == B.rows.
tensor_relu(Tensor* t)
Applies the Rectified Linear Unit activation function in-place (max(0, x)).
tensor_sigmoid(Tensor* t)
Applies the Sigmoid activation function in-place.
4. Full Example: Forward Pass
Below is a full example showing how to initialize weights and inputs, perform a matrix multiplication, and apply a ReLU activation natively.
#include "ml.h"
#include
int main() {
printf("Initializing Neural Network Layers...\n");
// Create weights (2x3) and fill with 0.5
Tensor* weights = tensor_create(2, 3);
tensor_fill(weights, 0.5f);
// Create inputs (3x1) and fill with 2.0
Tensor* inputs = tensor_create(3, 1);
tensor_fill(inputs, 2.0f);
// Forward pass: MatMul
Tensor* output = tensor_matmul(weights, inputs);
// Apply activation
tensor_relu(output);
// Check results
printf("Result of Forward Pass:\n");
tensor_print(output);
// Free memory
tensor_free(weights);
tensor_free(inputs);
tensor_free(output);
return 0;
}