Torcwnn is a Python library for Weightless Neural Network
Project description
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. Then, use the following command to install Torchwnn:
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 file.
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
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 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
Supported techniques:
- B-bleaching - Bleaching based on binary search. Reference: B-bleaching : Agile Overtraining Avoidance in the WiSARD Weightless Neural Classifier.
- WiSARD
- BloomWiSARD
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file torchwnn-0.0.0.tar.gz.
File metadata
- Download URL: torchwnn-0.0.0.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4717291ce7f15d1278b1bfb6366e5dd5f1086ccae0a27b2095e2e4557f9eaf33
|
|
| MD5 |
beded90c1b5bc6a4b43cafcf8b2c014a
|
|
| BLAKE2b-256 |
36066f7f7ed2338f7a8baf692ff5408e7fe1270a5a35d26383ff71ad734a45c7
|
Provenance
The following attestation bundles were made for torchwnn-0.0.0.tar.gz:
Publisher:
torchwnn-publish.yml on leandro-santiago/torchwnn
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchwnn-0.0.0.tar.gz -
Subject digest:
4717291ce7f15d1278b1bfb6366e5dd5f1086ccae0a27b2095e2e4557f9eaf33 - Sigstore transparency entry: 170870936
- Sigstore integration time:
-
Permalink:
leandro-santiago/torchwnn@89500fa605729d06b61b3cdb0651ba7e40aec9db -
Branch / Tag:
refs/tags/v0.0.0 - Owner: https://github.com/leandro-santiago
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
torchwnn-publish.yml@89500fa605729d06b61b3cdb0651ba7e40aec9db -
Trigger Event:
release
-
Statement type: