Skip to main content

A Python inference-only engine for the hashtron binary classifier

Project description

Hashtron Network Implementation

This project implements a hashtron network with feedforward layers and combiners, including a 2D majority pooling layer. The network supports feedforward inference with hashtron-based classifiers but not training. For training, see the original golang version which is CPU and GPU (CUDA) optimized.

Modules

  • datasets: Loader for demo datasets such as MNIST.
  • hash: Implements fast modular hash function.
  • hashtron: Implements the hashtron classifier which repeatedly calls the hash.
  • layer: Defines layer and combiner interfaces, including a 2D majority pooling combiner layer.
  • net: Implements the feedforward network and related utilities.

Usage

To use the network, create an instance of FeedforwardNetwork called e.g. net and add layers using net.new_layer, adding net.new_combiner in between layers. Load your model from ZLIB file using net.io.read_zlib_weights_from_file(file_name). Use net.network.infer(input_number_or_sample) to perform inference.

Examples

pip install hashtron

from pyclassifier.net.feedforward.net import Net
from pyclassifier.layer.majpool2d.layer import MajPool2DLayer
from pyclassifier.datasets.mnist.mnist import MNISTDataset
import urllib.request
import tempfile
import os

# Specify network size
fanout1 = 3
fanout2 = 5
fanout3 = 3
fanout4 = 5
# Create a Hashtron network (MNIST handwritten digits net)
tron = Net.new()
tron.new_layer(fanout1*fanout2*fanout3*fanout4, 0, 1<<fanout4)
tron.new_combiner(MajPool2DLayer(fanout1*fanout2*fanout4, 1, fanout3, 1, fanout4, 1, 1))
tron.new_layer(fanout1*fanout2, 0, 1<<fanout2)
tron.new_combiner(MajPool2DLayer(fanout2, 1, fanout1, 1, fanout2, 1, 1))
tron.new_layer(1, 0)
# load weights from zlib file
filename = os.path.expanduser('~/classifier/cmd/train_mnist/output.77.json.t.zlib')
if os.path.exists(filename):
    ok = tron.io.read_zlib_weights_from_file(filename)
else:
    # load online model weights zlib file
    with urllib.request.urlopen('https://www.hashtron.cloud/dl/mnist/output.77.json.t.zlib') as f:
        with tempfile.NamedTemporaryFile(delete=True) as dst_file:
            data = f.read()
            dst_file.write(data)
            ok = tron.io.read_zlib_weights_from_file(dst_file.name)

if not ok:
    raise Exception('model weights were not loaded')

# test the datasets
for i in range(2):
    # load offline then online mnist
    try:
        dataset = MNISTDataset(True, i == 0)
    except FileNotFoundError:
        dataset = MNISTDataset(True, i == 0, MNISTDataset.download())
    
    correct = 0
    for sample in dataset:
        pred = tron.network.infer(sample).feature(0) & 1
        actual = sample.output().feature(0) & 1
        if pred == actual:
            correct+=1
    print(100 * correct // len(dataset), '% on', len(dataset), 'MNIST samples')

Summary

This Python translation maintains the structure and functionality of the original Go code, adapting Go-specific features to Python equivalents. The translation includes classes for hash functions, hashtron classifiers, layers, and a feedforward network, along with unit tests and a README for documentation.

Contributing

  1. Open issue
  2. Fork the repo
  3. Implement what is needed (no blobs in repo, host datasets or demo models externally)
  4. Add tests as needed
  5. Experimentally install the package: pip install -e .
  6. Run all testcases: pip install pytest and then python3 -m pytest
  7. Contribute a pull request

License

MIT

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

hashtron-0.0.4.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

hashtron-0.0.4-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file hashtron-0.0.4.tar.gz.

File metadata

  • Download URL: hashtron-0.0.4.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for hashtron-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d5ab7272a93aedf81f1d4e27af1d9a6c7ade8184ab7b6dc2074561d1a61de265
MD5 a442cd5e966aab003e6a201be4f9e830
BLAKE2b-256 2e932369c6200390c023acca0907581edf27499be50b0ce01767f0a1814cae1a

See more details on using hashes here.

File details

Details for the file hashtron-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: hashtron-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for hashtron-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2d3d8cccb75ef35dd5e52a24ba6bfcd9bf8d724631c2f45f30878f61fef00734
MD5 5df1f48313a36d6cc5ea823402398862
BLAKE2b-256 dd807bbc2e308947972bb4b0b6041fc4d3b3acc1cca0fc0264571542248b1cdb

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