ProvableHQ / python-sdk

GNU General Public License v3.0
42 stars 49 forks source link

Research of efficient MNIST feature transformations, research zkML-friendly ML models for it, implement in transpiler and examples #10

Open kpandl opened 1 year ago

kpandl commented 1 year ago

The goal is to further improve MNIST performance (classification accuracy and constraint usage). For this, the following milestones need to be completed:

Milestone 1, feature pre-processing techniques

Milestone 2, zkML-friendly ML model exploration

Milestone 3, implementation of further models in the transpiler

kpandl commented 1 year ago

Milestone 1 results The experiments are conducted in the 6 Jupyter notebooks in zkml-research/MNIST_preprocessing. The last notebook (6_presentation) has the most promising results in terms of accuracy and expected computational effort for inference:

I also evaluated binary images, instead of uint8 images. Here are some promising results:

There may be further potential upward (e.g., through iterative pruning, further pre-processing steps), but it seems reasonable to implement it in zk, see where we land constraint-wise and then see what further optimizations are promising

kpandl commented 1 year ago

Milestone 2 results

For milestone 2, I mainly used the mlp1 Leo program, and variations thereof (e.g., by commenting out certain sections or changing few lines of code and measuring the effect on constraints). The program explores the feasibility of MLP neural network inference in Leo, following the promising results of using MLP networks for MNIST in milestone 1. In the following, I list the experimental results and interpretation thereof.

Constraint measurements in demo Leo program:

Constraint extrapolation for a neural network:

This means the core neural network logic should be very efficient (a bit more than 3k constraints), some additional overhead may come from the general Leo program structure / inputting and outputting values. There may also be hidden constraints, so we need to try it out and evaluate the proving times.

Other observations:

Based on these results, it seems reasonable to add MLP neural networks to the supported models in the transpiler, and then evaluate the inference in Leo, for milestone 3.

kpandl commented 1 year ago

Milestone 3 results

For milestone 3, I implemented MLP neural networks in the transpiler, and released a new transpiler version on PyPi. I also updated the decision tree MNIST example to make use of Haar features (and other features), and added an example for MLPs. The MNIST MLP example uses Haar features (and other features), L1 regularization and iterative model pruning in training, and layer-wise fixed-point scaling factors in the inference.

With decision trees, we reach around 86.6% test accuracy @ 27k constraints, and 88.2% accuracy @ 57k constraints. With MLP neural networks, we reach around 93.9% accuracy @ 201k constraints, and higher accuracies (around 97/98% with more constraint usage). There may be further room for improvement in the MLP constraint usage by using fields instead of integers - this feature is implemented in the transpiler but not fully working yet and is part for future work.

Going forward, the focus will be on creating an MNIST web app building upon these transpiled models.