hashtron


Namehashtron JSON
Version 0.0.10 PyPI version JSON
download
home_pagehttps://github.com/neurlang/pyclassifier
SummaryA Python inference-only engine for the hashtron binary classifier
upload_time2025-01-14 17:50:45
maintainerNone
docs_urlNone
authorNeurlang Project
requires_python>=3.6
licenseMIT License Copyright (c) 2025 neurlang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords binary classifier machine-learning hashtron
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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`

```python
from hashtron.net.feedforward.net import Net
from hashtron.layer.majpool2d.layer import MajPool2DLayer
from hashtron.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) & 1
        actual = sample.output() & 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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/neurlang/pyclassifier",
    "name": "hashtron",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "binary, classifier, machine-learning, hashtron",
    "author": "Neurlang Project",
    "author_email": "Neurlang Project <77860779+neurlang@users.noreply.github.com>",
    "download_url": "https://files.pythonhosted.org/packages/44/9f/317a931c719531655f2545c686019e3e10012dc5a1e16f02fb85415885c9/hashtron-0.0.10.tar.gz",
    "platform": null,
    "description": "# Hashtron Network Implementation\n\nThis project implements a hashtron network with feedforward layers and combiners,\nincluding a 2D majority pooling layer. The network supports feedforward inference\nwith hashtron-based classifiers but not training. For training, see the original\ngolang version which is CPU and GPU (CUDA) optimized.\n\n## Modules\n\n- `datasets`: Loader for demo datasets such as MNIST.\n- `hash`: Implements fast modular hash function.\n- `classifier`: Implements the hashtron classifier which repeatedly calls the hash.\n- `layer`: Defines layer and combiner interfaces, including a 2D majority pooling combiner layer.\n- `net`: Implements the feedforward network and related utilities.\n\n## Usage\n\nTo use the network, create an instance of `FeedforwardNetwork` called e.g. `net` and add layers\nusing `net.new_layer`, adding `net.new_combiner` in between layers.\nLoad your model from ZLIB file using `net.io.read_zlib_weights_from_file(file_name)`.\nUse `net.network.infer(input_number_or_sample)` to perform inference.\n\n## Examples\n\n`pip install hashtron`\n\n```python\nfrom hashtron.net.feedforward.net import Net\nfrom hashtron.layer.majpool2d.layer import MajPool2DLayer\nfrom hashtron.datasets.mnist.mnist import MNISTDataset\nimport urllib.request\nimport tempfile\nimport os\n\n# Specify network size\nfanout1 = 3\nfanout2 = 5\nfanout3 = 3\nfanout4 = 5\n# Create a Hashtron network (MNIST handwritten digits net)\ntron = Net.new()\ntron.new_layer(fanout1*fanout2*fanout3*fanout4, 0, 1<<fanout4)\ntron.new_combiner(MajPool2DLayer(fanout1*fanout2*fanout4, 1, fanout3, 1, fanout4, 1, 1))\ntron.new_layer(fanout1*fanout2, 0, 1<<fanout2)\ntron.new_combiner(MajPool2DLayer(fanout2, 1, fanout1, 1, fanout2, 1, 1))\ntron.new_layer(1, 0)\n# load weights from zlib file\nfilename = os.path.expanduser('~/classifier/cmd/train_mnist/output.77.json.t.zlib')\nif os.path.exists(filename):\n    ok = tron.io.read_zlib_weights_from_file(filename)\nelse:\n    # load online model weights zlib file\n    with urllib.request.urlopen('https://www.hashtron.cloud/dl/mnist/output.77.json.t.zlib') as f:\n        with tempfile.NamedTemporaryFile(delete=True) as dst_file:\n            data = f.read()\n            dst_file.write(data)\n            ok = tron.io.read_zlib_weights_from_file(dst_file.name)\n\nif not ok:\n    raise Exception('model weights were not loaded')\n\n# test the datasets\nfor i in range(2):\n    # load offline then online mnist\n    try:\n        dataset = MNISTDataset(True, i == 0)\n    except FileNotFoundError:\n        dataset = MNISTDataset(True, i == 0, MNISTDataset.download())\n    \n    correct = 0\n    for sample in dataset:\n        pred = tron.network.infer(sample) & 1\n        actual = sample.output() & 1\n        if pred == actual:\n            correct+=1\n    print(100 * correct // len(dataset), '% on', len(dataset), 'MNIST samples')\n```\n\n### Summary\n\nThis Python translation maintains the structure and functionality of the original\nGo code, adapting Go-specific features to Python equivalents. The translation\nincludes classes for hash functions, hashtron classifiers, layers, and a\nfeedforward network, along with unit tests and a README for documentation.\n\n### Contributing\n\n1. Open issue\n2. Fork the repo\n3. Implement what is needed (no blobs in repo, host datasets or demo models externally)\n4. Add tests as needed\n5. Experimentally install the package: `pip install -e .`\n6. Run all testcases: `pip install pytest` and then `python3 -m pytest`\n7. Contribute a pull request\n\n### License\n\nMIT\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2025 neurlang  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "A Python inference-only engine for the hashtron binary classifier",
    "version": "0.0.10",
    "project_urls": {
        "Documentation": "https://hashtron.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/neurlang/pyclassifier"
    },
    "split_keywords": [
        "binary",
        " classifier",
        " machine-learning",
        " hashtron"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65d7d8f0f4322a23ed859ee54d31302cf808b135db450f68a4ef0760b8279a1b",
                "md5": "4e2c251b5032a23b4fdc1b99572c9a08",
                "sha256": "ea960503552e4c02e569d674581ea601beaaa5b494c70cbefe2c835eb3cc2696"
            },
            "downloads": -1,
            "filename": "hashtron-0.0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4e2c251b5032a23b4fdc1b99572c9a08",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 15761,
            "upload_time": "2025-01-14T17:50:42",
            "upload_time_iso_8601": "2025-01-14T17:50:42.942192Z",
            "url": "https://files.pythonhosted.org/packages/65/d7/d8f0f4322a23ed859ee54d31302cf808b135db450f68a4ef0760b8279a1b/hashtron-0.0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "449f317a931c719531655f2545c686019e3e10012dc5a1e16f02fb85415885c9",
                "md5": "cfc4cdf7facdc0363137082ecf1614a4",
                "sha256": "b33b49901a69fd2be7a7b30ddea3796198a241d04ca746e392daf47097628577"
            },
            "downloads": -1,
            "filename": "hashtron-0.0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "cfc4cdf7facdc0363137082ecf1614a4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 12031,
            "upload_time": "2025-01-14T17:50:45",
            "upload_time_iso_8601": "2025-01-14T17:50:45.434833Z",
            "url": "https://files.pythonhosted.org/packages/44/9f/317a931c719531655f2545c686019e3e10012dc5a1e16f02fb85415885c9/hashtron-0.0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-14 17:50:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "neurlang",
    "github_project": "pyclassifier",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "hashtron"
}
        
Elapsed time: 0.47581s