torchwnn


Nametorchwnn JSON
Version 0.0.0 PyPI version JSON
download
home_pagehttps://github.com/leandro-santiago/torchwnn
SummaryTorcwnn is a Python library for Weightless Neural Network
upload_time2025-02-13 05:35:16
maintainerNone
docs_urlNone
authorLeandro Santiago de Araújo
requires_pythonNone
licenseMIT
keywords wisard weithgless neural network
VCS
bugtrack_url
requirements ucimlrepo torch scikit-learn
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Torchwnn: *Weightless Neural Network*

Torchwnn is a Python library for *Weightless Neural Network* (also known as *RAM-based* and *N-tuple based Neural Network* ).

# Usage
## Installation

First, install PyTorch using their [installation instructions](https://pytorch.org/get-started/locally/). Then, use the following command to install Torchwnn:


```bash
pip install torchwnn
```

Requirements: PyTorch and ucimlrepo to load datasets from UCI repository.


## Quick Start

### Iris Example

To quickly get started with Torchwnn, here's an example using the Iris dataset. Full training code is available in the [examples/iris.py](examples/iris.py) file.

```python
import torch
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from torchwnn.datasets.iris import Iris
from torchwnn.classifiers import Wisard
from torchwnn.encoding import Thermometer

# Use the GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using {} device".format(device))

iris = Iris()
X = iris.features
X = torch.tensor(X.values).to(device)
y = list(iris.labels)
y = torch.tensor(y).squeeze().to(device)

bits_encoding = 20
encoding = Thermometer(bits_encoding).fit(X)    
X_bin = encoding.binarize(X).flatten(start_dim=1)

X_train, X_test, y_train, y_test = train_test_split(X_bin, y, test_size=0.3, random_state = 0)  

entry_size = X_train.shape[1]
tuple_size = 8
model = Wisard(entry_size, iris.num_classes, tuple_size)

with torch.no_grad():
    model.fit(X_train,y_train)
    predictions = model.predict(X_test)  
    acc = accuracy_score(predictions, y_test)
    print("Wisard: Accuracy = ", acc)

```
## Examples

There are several examples in the repository. 

### Bleaching

```python
import torch
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from torchwnn.datasets.iris import Iris
from torchwnn.classifiers import Wisard
from torchwnn.encoding import Thermometer

# Use the GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using {} device".format(device))

iris = Iris()
X = iris.features
X = torch.tensor(X.values).to(device)
y = list(iris.labels)
y = torch.tensor(y).squeeze().to(device)

bits_encoding = 20
encoding = Thermometer(bits_encoding).fit(X)    
X_bin = encoding.binarize(X).flatten(start_dim=1)

X_train, X_test, y_train, y_test = train_test_split(X_bin, y, test_size=0.3, random_state = 0)  

entry_size = X_train.shape[1]
tuple_size = 8
model = Wisard(entry_size, iris.num_classes, tuple_size, bleaching=True)

with torch.no_grad():
    model.fit(X_train,y_train)
    predictions = model.predict(X_test)  
    acc = accuracy_score(predictions, y_test)
    print("Wisard: Accuracy = ", acc)
    
    # Applying bleaching
    model.fit_bleach(X_train,y_train)
    print("Selected bleach: ", model.bleach)
    predictions = model.predict(X_test)  
    acc = accuracy_score(predictions, y_test)
    print("Wisard with bleaching = ", model.bleach,": Accuracy = ", acc)

```
### BloomWisard

Example using BloomWisard is available in the [examples/iris_filters.py](examples/iris_filters.py) file.

## Supported WNN models
Currently, the library supports the following WNN models:

- WiSARD - Neurons based on dictionary.
- BloomWiSARD - Neurons based on Bloom Filters. Reference: [Weightless Neural Networks as Memory Segmented Bloom Filters](https://www.sciencedirect.com/science/article/abs/pii/S0925231220305105?via%3Dihub)

Supported techniques:
- B-bleaching - Bleaching based on binary search. Reference: *B-bleaching : Agile Overtraining Avoidance in the WiSARD Weightless Neural Classifier*.
    - WiSARD
    - BloomWiSARD

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/leandro-santiago/torchwnn",
    "name": "torchwnn",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "wisard, weithgless, neural, network",
    "author": "Leandro Santiago de Ara\u00fajo",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/36/06/6f7f7ed2338f7a8baf692ff5408e7fe1270a5a35d26383ff71ad734a45c7/torchwnn-0.0.0.tar.gz",
    "platform": null,
    "description": "# Torchwnn: *Weightless Neural Network*\n\nTorchwnn is a Python library for *Weightless Neural Network* (also known as *RAM-based* and *N-tuple based Neural Network* ).\n\n# Usage\n## Installation\n\nFirst, install PyTorch using their [installation instructions](https://pytorch.org/get-started/locally/). Then, use the following command to install Torchwnn:\n\n\n```bash\npip install torchwnn\n```\n\nRequirements: PyTorch and ucimlrepo to load datasets from UCI repository.\n\n\n## Quick Start\n\n### Iris Example\n\nTo quickly get started with Torchwnn, here's an example using the Iris dataset. Full training code is available in the [examples/iris.py](examples/iris.py) file.\n\n```python\nimport torch\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\nfrom torchwnn.datasets.iris import Iris\nfrom torchwnn.classifiers import Wisard\nfrom torchwnn.encoding import Thermometer\n\n# Use the GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(\"Using {} device\".format(device))\n\niris = Iris()\nX = iris.features\nX = torch.tensor(X.values).to(device)\ny = list(iris.labels)\ny = torch.tensor(y).squeeze().to(device)\n\nbits_encoding = 20\nencoding = Thermometer(bits_encoding).fit(X)    \nX_bin = encoding.binarize(X).flatten(start_dim=1)\n\nX_train, X_test, y_train, y_test = train_test_split(X_bin, y, test_size=0.3, random_state = 0)  \n\nentry_size = X_train.shape[1]\ntuple_size = 8\nmodel = Wisard(entry_size, iris.num_classes, tuple_size)\n\nwith torch.no_grad():\n    model.fit(X_train,y_train)\n    predictions = model.predict(X_test)  \n    acc = accuracy_score(predictions, y_test)\n    print(\"Wisard: Accuracy = \", acc)\n\n```\n## Examples\n\nThere are several examples in the repository. \n\n### Bleaching\n\n```python\nimport torch\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\nfrom torchwnn.datasets.iris import Iris\nfrom torchwnn.classifiers import Wisard\nfrom torchwnn.encoding import Thermometer\n\n# Use the GPU if available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(\"Using {} device\".format(device))\n\niris = Iris()\nX = iris.features\nX = torch.tensor(X.values).to(device)\ny = list(iris.labels)\ny = torch.tensor(y).squeeze().to(device)\n\nbits_encoding = 20\nencoding = Thermometer(bits_encoding).fit(X)    \nX_bin = encoding.binarize(X).flatten(start_dim=1)\n\nX_train, X_test, y_train, y_test = train_test_split(X_bin, y, test_size=0.3, random_state = 0)  \n\nentry_size = X_train.shape[1]\ntuple_size = 8\nmodel = Wisard(entry_size, iris.num_classes, tuple_size, bleaching=True)\n\nwith torch.no_grad():\n    model.fit(X_train,y_train)\n    predictions = model.predict(X_test)  \n    acc = accuracy_score(predictions, y_test)\n    print(\"Wisard: Accuracy = \", acc)\n    \n    # Applying bleaching\n    model.fit_bleach(X_train,y_train)\n    print(\"Selected bleach: \", model.bleach)\n    predictions = model.predict(X_test)  \n    acc = accuracy_score(predictions, y_test)\n    print(\"Wisard with bleaching = \", model.bleach,\": Accuracy = \", acc)\n\n```\n### BloomWisard\n\nExample using BloomWisard is available in the [examples/iris_filters.py](examples/iris_filters.py) file.\n\n## Supported WNN models\nCurrently, the library supports the following WNN models:\n\n- WiSARD - Neurons based on dictionary.\n- BloomWiSARD - Neurons based on Bloom Filters. Reference: [Weightless Neural Networks as Memory Segmented Bloom Filters](https://www.sciencedirect.com/science/article/abs/pii/S0925231220305105?via%3Dihub)\n\nSupported techniques:\n- B-bleaching - Bleaching based on binary search. Reference: *B-bleaching : Agile Overtraining Avoidance in the WiSARD Weightless Neural Classifier*.\n    - WiSARD\n    - BloomWiSARD\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Torcwnn is a Python library for Weightless Neural Network",
    "version": "0.0.0",
    "project_urls": {
        "Homepage": "https://github.com/leandro-santiago/torchwnn"
    },
    "split_keywords": [
        "wisard",
        " weithgless",
        " neural",
        " network"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "36066f7f7ed2338f7a8baf692ff5408e7fe1270a5a35d26383ff71ad734a45c7",
                "md5": "beded90c1b5bc6a4b43cafcf8b2c014a",
                "sha256": "4717291ce7f15d1278b1bfb6366e5dd5f1086ccae0a27b2095e2e4557f9eaf33"
            },
            "downloads": -1,
            "filename": "torchwnn-0.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "beded90c1b5bc6a4b43cafcf8b2c014a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 12336,
            "upload_time": "2025-02-13T05:35:16",
            "upload_time_iso_8601": "2025-02-13T05:35:16.926547Z",
            "url": "https://files.pythonhosted.org/packages/36/06/6f7f7ed2338f7a8baf692ff5408e7fe1270a5a35d26383ff71ad734a45c7/torchwnn-0.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-13 05:35:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "leandro-santiago",
    "github_project": "torchwnn",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "ucimlrepo",
            "specs": []
        },
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "scikit-learn",
            "specs": []
        }
    ],
    "lcname": "torchwnn"
}
        
Elapsed time: 0.67023s