How it works

Each training, test and inference example is turned into a vector and encrypted in four steps. The result looks like noise, but a neural network can still learn directly from it.

A handwritten 8 from the MNIST dataset: 28 × 28 pixels.

A handwritten 8 from the MNIST dataset: 28 × 28 pixels.

Step 1

Flatten

A neural network does not see an image. It sees a list of numbers, one for each pixel. Read row by row, this digit becomes a vector of 784 values. Every step that follows works on vectors, so the same method applies to any data that can be written as one: records, measurements, text embeddings.

784 values

Step 2

Padding

Extra values are added to the vector. Here, 280 random values are added after the original values (external padding). Padding can also be interlaced into positions inside the image, or sampled from real data. Padding hides how many dimensions the original data had, and which of them carry information.

784 → 1,064 values

External and interlaced padding compared

Step 3

Perturbation

Every value is shifted by a random amount within a set bound, here at most 0.15 on a scale of 0 to 1. This adds noise to each example while keeping what a network needs to learn from.

Each value changed by up to ±0.15

Step 4

Shuffle

The values are moved to new positions by a secret permutation. The same permutation is used for every example, so each position holds the same kind of information in every encrypted example, but no longer matches its place in the original.

One fixed order for every example

Step 5

Orthogonal transformation

The vector is multiplied by a secret random orthogonal matrix, which rotates it in 1,064 dimensions. Every output value is a weighted mix of all the input values, so none can be traced back to a single pixel. A rotation keeps lengths, distances and angles between vectors exactly as they were.

One 1,064 × 1,064 matrix for every example

Rotated together, the two vectors keep their lengths and the angle between them.
Plaintext
Encrypted

What the network trains on

Between the 8 and the 3 Distance Angle
After padding and perturbation 12.76 51.8°
After shuffle and orthogonal transformation 12.76 51.8°

The encrypted examples look like noise, but the last two steps leave the distance and angle between them unchanged, and that geometry is what a network learns from. Across five benchmark datasets, validation accuracy on encrypted data was 0.0002 to 0.0227 lower than with plaintext training.

Wait, how?

These visualizations apply each step to digits from the MNIST test set, with parameters scaled for visual clarity rather than matched to experimental benchmarks.

External or interlaced padding

Padding can be added after the original values, or interlaced among them at fixed positions that are kept secret and are the same for every example. Interlacing removes the boundary between original values and padding, so an attacker cannot tell from position alone which values came from the data. (Shuffling after external padding has a similar, but not identical, effect.)

Both padded images below contain exactly the same 280 padding values; only their positions differ. Either layout trains equally well: in the experiments, validation accuracy was 0.9821 with external padding and 0.9820 with interlaced padding (P = 0.72).

Original

784 values

External

280 padding values after the original 784

Interlaced

The same 280 values at fixed secret positions

The interlaced image looks streaked because each original row is split by the padding inserted into it.

Why a network can still learn from it

A network learns from how examples relate to each other: which are close together, which point in similar directions, and where the boundaries between classes fall. The shuffle and the orthogonal transformation leave all of these exactly as they were. Padding and perturbation are the only steps that change them, by amounts set in configuration.

There is also a direct mathematical reason. A network's first layer multiplies its input by a matrix of weights, W. If the input has been rotated by an orthogonal matrix Q, the weights WQT give exactly the same result on the rotated input as W gives on the original. Training finds such weights by itself, and the rest of the network is unchanged.

What can be configured

Padding
How many values are added, how they are generated, and where they are located. Padding values can be random, sampled from the data, or structured based on the data. Padding values can be external, interlaced inside the vector dimensions, or a combination.
Perturbation
Any function or functions can be applied to the individual vector values, within specified bounds. Nonlinear and structured perturbations can also hide covariance structure that might otherwise help an attacker.
Shuffle and matrix
One or more secret index shuffles and secret orthogonal matrix transformations.
Rounds and order
Transformation sequences can be applied more than once, and different orders of transformations can be applied.

Not only images

Every step works on vectors, so records, transactions and text embeddings are encrypted the same way. Convolutional networks and language models can be adapted to work on encrypted inputs; these are described as next steps in the white papers.

An image that is recovered approximately can still be recognizable. Records, tables, time series and text cannot be: a number that is slightly off is a different number, and a character that is slightly off is a different character. For this kind of data, anything short of exact recovery is wrong data.

Run Tensorlock yourself

A Google Colab notebook encrypts a public dataset (MNIST, Fashion-MNIST, EMNIST Letters or KMNIST) with Tensorlock, trains a neural network on the encrypted images, and compares it with a network trained on the originals. The notebook runs in the browser, with no local installation required.

  1. Select from different types of padding and perturbation, then encrypt all images with padding, perturbation, index shuffle and orthogonal transformation.
  2. Train one network on plaintext images and one on encrypted images.
  3. Compare test accuracy and errors, class by class.
Open the notebook in Colab (opens in a new tab) Opens in Google Colab; a Google account is needed to run it.
A handwritten zero shown at five stages of encryption. Blue padding values are interlaced into the image, the values are perturbed, then shuffled, and the final orthogonal transformation leaves uniform gray noise.
One digit, four encryption steps Blue marks random padding values interlaced into the image. After the orthogonal transformation, every value mixes all inputs.
The digits zero to nine, each above its encrypted version. Every encrypted version is gray noise with no visible digit.
Ten digits and their encrypted forms The network trains on the bottom row and never sees the top row.
Confusion matrix for the network trained on plaintext images: 9,834 of 10,000 test images on the diagonal.
Trained on the original images 9,834 of 10,000 test digits classified correctly.
Confusion matrix for the network trained on encrypted images: 9,791 of 10,000 test images on the diagonal.
Trained on the encrypted images 9,791 of 10,000 test digits classified correctly.

Figures from one run of the notebook on MNIST.