A Fully Connected Neural Network (FCN) for diabetes prediction implementation using the GGML library with support for both CPU and CUDA backends.
This project demonstrates how to build and run a simple 3-layer fully connected neural network using GGML's tensor operations and computational graph abstraction. The implementation showcases:
- Multi-layer perceptron with ReLU activations
- Support for both CPU and GPU (CUDA) computation
- GGML's backend system for hardware abstraction
- Computational graph construction and execution
- Softmax output layer for classification
- Diabetes prediction using trained model weights
The network consists of:
- Input Layer: 8 neurons (diabetes risk factors)
- Hidden Layer 1: 4 neurons (ReLU activation)
- Hidden Layer 2: 4 neurons (ReLU activation)
- Output Layer: 2 neurons (Softmax activation - binary classification)
- ✅ GGML tensor operations
- ✅ Automatic backend selection (CUDA → CPU fallback)
- ✅ Efficient memory management with backend buffers
- ✅ Computational graph optimization
- ✅ Pre-trained weights and biases included
- ✅ Jupyter notebook for model training
- ✅ Diabetes prediction dataset included
FCN_ggml/
├── fnn.cpp # C++ inference implementation
├── prediction.ipynb # Jupyter notebook for training
├── diabetes_prediction_dataset.csv # Training dataset
└── README.md # Documentation
- C++ compiler with C++11 support (GCC, Clang, or MSVC)
- GGML library
- CUDA toolkit (optional, for GPU acceleration)
- CMake (optional, for building)
The program expects exactly 8 numerical inputs representing diabetes risk factors:
./fnn <input1> <input2> <input3> <input4> <input5> <input6> <input7> <input8>The 8 input features typically represent:
- Age
- Gender
- BMI (Body Mass Index)
- Hypertension status
- Heart disease status
- Smoking history
- HbA1c level
- Blood glucose level
./fnn 0. 5 1.0 0.7 0.0 0.0 0.3 0.6 0.8Output: The program outputs the softmax probabilities for the 2 output classes:
Class 0 (No Diabetes): 0.423156
Class 1 (Diabetes): 0.576844
Use the included Jupyter notebook to train the model on the diabetes dataset:
jupyter notebook prediction.ipynbThe notebook includes:
- Data preprocessing and normalization
- Model training with PyTorch
- Weight extraction for GGML
- Model evaluation and testing
The program first attempts to initialize a CUDA backend for GPU acceleration. If unavailable, it falls back to the CPU backend.
Tensors are created for:
- Input vector (8-dimensional)
- Weight matrices (for each layer)
- Bias vectors (for each layer)
A directed acyclic graph (DAG) is built representing the forward pass:
Input → [W1 × Input + B1] → ReLU → [W2 × Hidden1 + B2] → ReLU → [W3 × Hidden2 + B3] → Softmax → Output
The backend computes the computational graph, performing matrix multiplications, additions, and activations.
Output tensors are copied back from device memory to RAM, and softmax normalization is applied.
// 1. Initialize backend (CUDA or CPU)
ggml_backend_t backend = ggml_backend_cuda_init(0);
if (! backend) {
backend = ggml_backend_cpu_init();
}
// 2. Create context for tensor metadata
struct ggml_init_params params = {
. mem_size = 16*1024*1024,
.mem_buffer = NULL,
.no_alloc = true,
};
struct ggml_context *ctx = ggml_init(params);
// 3. Define tensor shapes
struct ggml_tensor *input = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 8);
struct ggml_tensor *weight_1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 8, 4);
// ... (more tensors)
// 4. Allocate backend buffer and copy data
ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx, backend);
ggml_backend_tensor_set(input, input_data, 0, ggml_nbytes(input));
// 5. Build computational graph
struct ggml_tensor *result1 = ggml_relu(ctx_cgraph,
ggml_add(ctx_cgraph, ggml_mul_mat(ctx_cgraph, weight_1, input), bias_1));
// ... (more layers)
// 6. Execute computation
ggml_backend_graph_compute(backend, gf);
// 7. Retrieve and process results
ggml_backend_tensor_get(result, result_data, 0, ggml_nbytes(result));
softmax(result_data, size_result);- CPU Backend: Suitable for inference on standard hardware
- CUDA Backend: Significantly faster on NVIDIA GPUs
- Memory Usage: ~16MB for model and computation graph
As noted in the code comments, potential improvements include:
- Load weights from GGUF file format instead of hardcoded arrays
- Support for quantized weights (Q4_0, Q8_0, etc.)
- Support for other activation functions
- Context: Stores tensor metadata (shapes, types, pointers)
- Backend Buffer: Stores actual tensor data on device (CPU/GPU)
- Computational Graph: Manages operation dependencies and execution order
All operations are performed using GGML's optimized implementations:
ggml_mul_mat: Matrix multiplication (with automatic transpose)ggml_add: Element-wise additionggml_relu: ReLU activation function- Manual softmax implementation for output normalization
The included diabetes_prediction_dataset.csv contains health metrics for diabetes risk prediction. The dataset includes features such as:
- Age
- Gender
- BMI
- Hypertension
- Heart disease
- Smoking history
- HbA1c level
- Blood glucose level
This project is open source. Please check the repository for specific license information.
Contributions are welcome! Feel free to:
- Report bugs
- Suggest features
- Submit pull requests
- Improve documentation
- Add test cases
- Built with GGML by Georgi Gerganov
- Inspired by modern ML inference frameworks
- Dataset sourced from diabetes prediction research
Author: @AdiistheGoat
Repository: FCN_ggml
Last Updated: December 2025
For questions or support, please open an issue on GitHub.