A C implementation of neural networks with matrix operations, focusing on clean code and thorough testing. The project consists of two main components:
matrix_lib
: A fundamental matrix operations libraryneural_networks
: Neural network implementation using the matrix library
neural_matrix/
├── matrix_lib/ # Matrix operations library
│ ├── include/ # Header files
│ ├── src/ # Source files
│ └── tests/ # Matrix library tests
├── neural_networks/ # Neural network implementation
│ ├── include/ # Header files
│ ├── src/ # Source files
│ └── tests/ # Neural network tests
└── CMakeLists.txt # Root CMake configuration
- C Compiler (GCC recommended)
- CMake (version 3.10 or higher)
- Ninja build system (optional but recommended)
- Criterion testing framework
For Ubuntu/Pop!_OS:
# Install build essentials and CMake
sudo apt-get update
sudo apt-get install build-essential cmake ninja-build
# Install Criterion testing framework
sudo add-apt-repository ppa:snaipewastaken/ppa
sudo apt-get update
sudo apt-get install criterion-dev
You can build the project in several ways:
From the root directory:
# Create and enter build directory
cmake -B build -G Ninja
cmake --build build
# Run all tests
cd build && ctest
cd matrix_lib
cmake -B build -G Ninja
cmake --build build
# Run matrix library tests
cd build && ctest
cd neural_networks
cmake -B build -G Ninja
cmake --build build
# Run neural network tests
cd build && ctest
Tests are implemented using the Criterion framework. You can run tests in several ways:
From the project root:
cd build && ctest
# Run matrix library tests
cd build/matrix_lib && ctest
# Run neural network tests
cd build/neural_networks && ctest
For verbose test output:
ctest --verbose
Or for a specific test:
ctest -R test_name --verbose
-
Matrix Library Tests:
test_matrix_init
: Matrix initialization and basic operationstest_matrix_math
: Mathematical operationstest_matrix_operations
: Advanced matrix operations
-
Neural Network Tests:
layer_test
: Neural network layer testsdense_layer_test
: Dense layer implementation testsactivation_test
: Activation function testsoptimization_test
: Optimization algorithm testsloss_test
: Loss function tests
- Basic matrix operations (addition, subtraction, multiplication)
- Matrix initialization and memory management
- Advanced operations (transpose, element-wise operations)
- Mathematical operations (exp, log, trigonometric functions)
- Layer abstractions
- Dense (fully connected) layers
- Activation functions (ReLU, Sigmoid, Tanh, Softmax)
- Loss functions (MSE, Cross-Entropy, Binary Cross-Entropy)
- Optimization algorithms (SGD, Momentum, RMSprop, Adam)
#include "matrix.h"
// Create a matrix
matrix* m = matrix_new(3, 3, sizeof(double));
// Perform operations
matrix_set(m, 0, 0, 1.0);
double val = matrix_at(m, 0, 0);
// Clean up
matrix_free(m);
#include "layer.h"
#include "dense_layer.h"
#include "activation.h"
// Create layers
layer* dense = dense_layer_new(input_dim, output_dim, -1.0, 1.0);
layer* activation = activation_layer_new(output_dim, ACTIVATION_RELU);
// Forward pass
matrix* output = dense->forward(dense, input);
matrix* activated = activation->forward(activation, output);
// Clean up
matrix_free(output);
matrix_free(activated);
layer_free(dense);
layer_free(activation);
-
CMake configuration fails:
- Ensure Criterion is properly installed
- Check CMake version is 3.10 or higher
-
Build fails:
- Check compiler installation
- Ensure all dependencies are installed
-
Tests fail:
- Run with verbose output for detailed information
- Check Criterion installation
To clean and rebuild:
rm -rf build
cmake -B build -G Ninja
cmake --build build