Basic MLP FHE Implementation
This page demonstrates the Basic MLP inference workflow using FHEON.
It provides an example of how to set up the FHE context, configure the FHEONANNStandardController, and perform encrypted-domain inference on MNIST images using a simple Multi-Layer Perceptron (MLP) architecture.
Overview
The workflow includes:
Context and Key Generation
Initializes the FHEONHEController with a leveled context (
generate_leveled_context, no bootstrapping): 21 levels, the depth of the three linear layers (2 levels each) and the two ReLUs (7 levels each). At 128-bit security the ring optimization (minScaleBits = 30) fits it into a2^15ring with a 31-bit scale (Security Levels, Ring Dimension and Packing).Generates rotation keys for fully connected (linear) layers, then serializes them for optimized FHE operations.
Data Preparation
Reads and encodes all the model weights and biases for the fully connected layers.
Reads and preprocesses MNIST images.
Encrypts the image data for inference.
MLP Layers
Implements three fully connected layers (FC1, FC2, FC3) for classification.
Applies encrypted-domain ReLU activations.
Inference Loop
Iterates over the input MNIST images.
Sequentially applies linear transformations (fully connected layers) and ReLU activations.
Decrypts results and writes predicted labels to a file.
Key Functions
The main functions used in this example include:
he_linear()– Implements a fully connected layer on encrypted data.he_relu()– Secure ReLU activation CKKS polynomial approximation.encode_input()– Encodes plaintext weights and inputs for FHE operations.generate_leveled_context()– CKKS context without bootstrapping, sized to the circuit.read_inferenced_label() – Decrypts and retrieves the final predictions.
Full Example Source
You can view and download the full source code of this example: