Skip to main content

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.
  • classifier: 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 hashtron.net.feedforward.net import Net
from hashtron.layer.majpool2d.layer import MajPool2DLayer
from hashtron.layer.full.layer import FullLayer
from hashtron.datasets.mnist.mnist import load_mnist
import urllib.request
import tempfile
import os
import random

# Specify network size
fanout1 = 1
fanout2 = 5
fanout3 = 1
fanout4 = 4
fanout5 = 1
fanout6 = 4
# Create a Hashtron network (MNIST handwritten digits net)
tron = Net.new()
tron.new_layer(fanout1*fanout2*fanout3*fanout4*fanout5*fanout6, 0, 1<<((fanout6*fanout6*2)//3))
tron.new_combiner(MajPool2DLayer(fanout1*fanout2*fanout3*fanout4*fanout6, 1, fanout5, 1, fanout6, 1, 1))
tron.new_layer(fanout1*fanout2*fanout3*fanout4, 0, 1<<((fanout4*fanout4*2)//3))
tron.new_combiner(MajPool2DLayer(fanout1*fanout2*fanout4, 1, fanout3, 1, fanout4, 1, 1))
tron.new_layer(fanout1*fanout2, 0, 1<<((fanout2*fanout2*2)//3))
tron.new_combiner(FullLayer(fanout2, 1, 1))
# load weights from zlib file
filename = os.path.expanduser('~/classifier/cmd/train_mnist/output.78.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://hashtron.cloud/dl/classifier_models_v0.1/infer_mnist.78.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')

# load offline then online mnist
try:
    dataset, _, _, _, = load_mnist('~/pyclassifier/datasets/mnist/')
except FileNotFoundError:
    dataset, _, _, _, = load_mnist()

random.shuffle(dataset)

correct = 0
for sample in dataset:
    pred = tron.network.infer(sample) % 10
    actual = sample.output() % 10
    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

Metadata

Release files for hashtron 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hashtron 0.1.0
File Size Uploaded Provenance Actions
hashtron-0.1.0.tar.gz 16.0 kB Without provenance Details for hashtron-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hashtron 0.1.0
File Interpreter ABI Platform Actions
hashtron-0.1.0-py3-none-any.whl Python 3 none any Details for hashtron-0.1.0-py3-none-any.whl

Total release size: 38.5 kB

Release files / hashtron-0.1.0.tar.gz

Download URL hashtron-0.1.0.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
13fea2c6443c9793a29ac9d2e07f93cc7d86c0cb71f0fe880a157d159f8eae4b
BLAKE2b-256 checksum
How to use checksums
35afa0943d3021415badfadf9c573de9143c72eda3776e49d3273de1e6988655
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / hashtron-0.1.0-py3-none-any.whl

Download URL hashtron-0.1.0-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
955d9f65b1ccde6f95a4689d5d157a90c3467a0ff5820df9a72355f2358d9997
BLAKE2b-256 checksum
How to use checksums
1ec3dec006a4205dc251ee5030aad373ca837e233c1ab3be2da89a278ccbcb3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Security for hashtron 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page