MLP (Diagonal)
This page demonstrates an encrypted multi-layer perceptron on MNIST evaluated with the diagonal primitives.
Overview
The three fully connected layers (784 → 128 → 64 → 10) are evaluated over their diagonals with baby-step giant-step rotations (one level per layer). Every layer returns its outputs in the first slots of a ciphertext that has only as many slots as the layer has diagonals: 1024, 128 and 64 slots.
The model runs in a leveled context, with no bootstrapping (generate_leveled_context): the depth is exactly
the three layers and two ReLUs, and the input is encrypted as low in the modulus chain as the circuit allows, so every
operation works on few towers. A ReLU degree of 27 gives the plaintext accuracy (98.6% on the first 1000 test images)
with one level less than 59.
Key Functions
The main functions used in this example include:
generate_leveled_context()– CKKS context without bootstrapping, sized to the circuit.encode_linear_weights() / encode_linear_bias()– Diagonal encoding of the fully connected layers.he_linear()– Fully connected layer over diagonals (baby-step giant-step).he_relu()– Chebyshev approximation of ReLU.
Full Example Source
You can view and download the full source code of this example: