Skip to main content

A low-code Pythonic implementation of a Coalesced Tsetlin Machine

Project description

PyTsetlin

README.md under construction...

A low-code, feature-POOR, Pythonic implementation of a Coalesced Tsetlin Machine. This is not intended to be a feature-rich or speed-optimized implementation; see relevant repositories like TMU and green-tsetlin for that. However, it's intended to be an easy-to-use TM programmed in Python, with the intent of making it accessible to plug-and-play new ideas and be able to get some results, either on an input level or TM memory level. Also, since the implementation is written entirely in Python, the code can be compared with the theoretical concepts presented in the papers, potentially making it easier to grasp.

Even though this repo is not focused on speed, I have made some functions compatible for Numba compilation. Without this, the code would be so slow that it deems the implementation unusable.

Installation

  1. Install package to environment to use in other projects:
pip install git+https://github.com/Sebastianostby/pytsetlin.git
  1. Clone or template this repository and install the required dependencies:
cd pytsetlin
pip install -r requirements.txt

Examples

Basic training example

Here's a basic example of how to use the Tsetlin Machine:

>>> from pytsetlin import TsetlinMachine
>>> from pytsetlin.data.mnist import get_mnist

>>> X_train, X_test, y_train, y_test = get_mnist()

>>> tm = TsetlinMachine(n_clauses=500,
                        threshold=625,
                        s=10.0,
                        n_threads=20)

>>> tm.set_train_data(X_train, y_train)

>>> tm.set_eval_data(X_test, y_test)

>>> r = tm.train(training_epochs=10)

# progress bar for visualization
Eval Acc: 96.31, Best Eval Acc: 96.31 (10): 100%|██████████| 10/10 [01:03<00:00,  6.30s/it]

>>> print(r)
{'train_time': [12.25, 5.77, 5.42, 4.96, 6.83, 4.71, 4.58, 4.88, 4.11, 5.9], 'eval_acc': [91.56, 92.97, 93.45, 94.42, 94.24, 94.71, 94.82, 95.1, 95.11, 96.31], 'best_eval_acc': 96.31, 'best_eval_epoch': 10}

Note performance may vary depending on system!

Investigating TM structure

Since the code is Pythonic, the TM structure can easily be investigated from the TsetlinMachine object:

>>> # xor gate
>>> x = np.array([[0, 0],
                  [0, 1],
                  [1, 0],
                  [1, 1]])

>>> y = np.array([0, 1, 1, 0])

>>> tm = TsetlinMachine(n_clauses=4)

>>> tm.set_train_data(x, y)

>>> tm.train()

>>> print(tm.C) # get clause matrix
[[-35  25  24 -30]
 [-33 -41  12  23]
 [ 18 -38 -34  16]
 [ 17  15 -33 -42]]

>>> print(tm.W) # get weight matrix
[[-19  17 -20  16]
 [ 18 -19  18 -18]]

Saving and loading

Any TM state can easly be saved during of after training

>>> from pytsetlin import TsetlinMachine
>>> from pytsetlin.data.imdb import get_imdb

>>> X_train, X_test, y_train, y_test = get_imdb()

>>> tm = TsetlinMachine(n_clauses=500,
                        threshold=625,
                        s=2.0)

>>> tm.set_train_data(X_train, y_train)

>>> tm.set_eval_data(X_test, y_test)

>>> r = tm.train(training_epochs=10, save_best_state=True) # save during training

>>> tm.save_state(file_name='tm_state.npz') # save after training

Then saved memory, or any memory, can be used for predictions after:

>>> tm = TsetlinMachine()

>>> state = np.load('tm.state.npz')

>>> C = state['C'] # load clause matrix
>>> W = state['W'] # load weight matrix 


>>> clause_outputs = tm.evaluate_clauses(instance, memory=C) # what clauses matched the input
[0, 1, 0, 0, 1]

>>> class_sums = np.dot(W, clause_outputs) # majority voting
[-32, 55]

>>> prediction = np.argmax(class_sums)
1

Literature References

Notes

  1. Input data must be binary (dtype=np.uint8 for features, np.uint32 for labels)
  2. The implementation uses Numba for efficient computation
  3. Memory is allocated automatically when training begins

License

MIT Licence

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

pytsetlin-1.0.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

pytsetlin-1.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file pytsetlin-1.0.0.tar.gz.

File metadata

  • Download URL: pytsetlin-1.0.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for pytsetlin-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7ff12c60743da77e6b9a7dff2828cefb99a4bfe595932d7da54a67d2cedd8694
MD5 ff6bede9e041b998da10e016dd768c2e
BLAKE2b-256 e6c650150def08ccb9a826c499d7d41d5fe6bb6252879826db843db885ba9f4a

See more details on using hashes here.

File details

Details for the file pytsetlin-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytsetlin-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for pytsetlin-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 423e30af7c56b7ae69921dfdeba0118c43d9ef18c6379a9682191d3b9bfca179
MD5 ebfdcc2d598aea166670aab9b4ec338b
BLAKE2b-256 1a9fd1809f91f1f806e5e43b9f943fddfd4d9659e5fea7c7d274a0e2ae205152

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