Skip to main content

A SAT solver-based approach to classifying MNIST dataset using SAT encodings

Project description

SAT MNIST Solver

This project explores the use of SAT solvers for neural network training by leveraging the MNIST dataset. Specifically, we implement a SAT-based approach to solving binary classification problems (digits 0 and 1) by encoding neural network weights as SAT variables, then solving the resulting SAT problem to find an optimal solution.

Table of Contents • Installation • Usage • Testing • License • Acknowledgements

Installation

To install the project dependencies, you’ll need Python 3.7+ and pip installed. Start by cloning the repository:

git clone https://github.com/savannahshannon/sat_mnist_solver.git cd sat_mnist_solver

Then, install the required libraries using:

pip install -r requirements.txt

You will need to install the following dependencies: • PyTorch: For handling MNIST data • PySAT: For SAT solver functionalities

You can manually install PyTorch and PySAT with:

pip install torch torchvision pip install python-sat

Usage

  1. Load Data and Preprocess

The script uses the MNIST dataset and binarizes the pixel values (1 if the pixel is greater than 0.5, otherwise 0). This dataset is used for training the SAT solver to predict whether the given image corresponds to the digit ‘0’ or ‘1’.

from satlib.core import SATMNISTTrainer

Initialize the trainer (for binary classification: 0 vs 1)

trainer = SATMNISTTrainer(num_samples=600, class_labels=(0, 1))

Load data (600 samples of 0 and 1)

images, labels = trainer.load_data()

Encode constraints for SAT solver

trainer.encode_constraints(images, labels)

  1. Solve the SAT Problem

Once the data is preprocessed and constraints are encoded, you can solve the SAT problem to find the optimal neural network weights for the binary classification task.

Solve SAT problem

model = trainer.solve()

Print learned weights

if model is not None: weights = trainer.get_weight_assignments() print("Learned Weights:", weights)

  1. Run Example Script

You can also run the example script to see the complete flow in action.

python examples/run_example.py

Example of Running the Trainer

from satlib.core import SATMNISTTrainer

Initialize the SAT trainer

trainer = SATMNISTTrainer(num_samples=600, class_labels=(0, 1))

Load and preprocess data

images, labels = trainer.load_data()

Encode constraints for the SAT solver

trainer.encode_constraints(images, labels)

Solve the SAT problem to find weights

model = trainer.solve()

Print the learned weights

if model: weights = trainer.get_weight_assignments() print(weights)

Testing

We have included unit tests for verifying the functionality of the main components of the library.

To run the tests:

python -m unittest discover tests

Tests include: • Data loading • Constraint encoding • Solver functionality • Weight assignment retrieval

Project Structure

The project is organized as follows:

sat_mnist_solver/ ├── satlib/ │ ├── init.py │ ├── core.py # Main solver logic │ ├── utils.py # Utility functions like data loading ├── examples/ │ └── run_example.py # Example script demonstrating usage ├── tests/ │ └── test_core.py # Unit tests for core functionalities ├── requirements.txt # List of dependencies ├── README.md # Project documentation ├── LICENSE # Project license ├── setup.py # Packaging and installation setup └── .gitignore # Git ignore file

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements • PySAT: A Python library used for SAT solving. (PySAT GitHub) • PyTorch: A deep learning framework used for MNIST data handling.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sat_mnist_solver-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sat_mnist_solver-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file sat_mnist_solver-1.0.0.tar.gz.

File metadata

  • Download URL: sat_mnist_solver-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for sat_mnist_solver-1.0.0.tar.gz
Algorithm Hash digest
SHA256 046f1b0aebb19ef7e3c283cc359c4784eabe548dc9e5e8c3347d4450b6e63f67
MD5 562bbd075183c990f8b38e17198b7180
BLAKE2b-256 943f2cc5c5b152b94eedf16009adef6deeba150668627b5dd41efd6006e120df

See more details on using hashes here.

File details

Details for the file sat_mnist_solver-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sat_mnist_solver-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5612a10bdc6c1755855b3626917effc5d6c3ad0481caab6e4daf0a1a90be01dd
MD5 de397a64f963409258704482ec839586
BLAKE2b-256 6ef8bdbc0847b87282f2692a02054c9cd7357e08cd4348abf60fabea562f8738

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page