Skip to main content

Torchhd is a Python library for Hyperdimensional Computing

Project description

GitHub license pypi version conda version tests status PRs Welcome

Torchhd

Torchhd is a Python library for Hyperdimensional Computing.

  • Easy-to-use: Torchhd makes it painless to develop a wide range of Hyperdimensional Computing (HDC) applications and algorithms. For someone new to the field we provide Pythonic abstractions and examples to get you started fast. For the experienced researchers we made the library modular by design, giving you endless flexibility to prototype new ideas in no-time.
  • Performant: The library is build on top of the high-performance PyTorch library, giving you optimized tensor execution without the headaches. Moreover, PyTorch makes it effortless to accelerate your code on a GPU.

Installation

Torchhd is hosted on PyPi, use the following command to install:

pip install torch-hd

Documentation

You can find documentation for Torchhd on the website.

Check out the Getting Started page for a quick overview.

The API documentation is divided into several sections:

You can improve the documentation by sending pull requests to this repository.

Examples

We have several examples in the repository. Here is a simple one to get you started:

import torch, torchhd

d = 10000  # number of dimensions

# create the hypervectors for each symbol
country = torchhd.random_hv(1, d)
capital = torchhd.random_hv(1, d)
currency = torchhd.random_hv(1, d)

usa = torchhd.random_hv(1, d)  # United States
mex = torchhd.random_hv(1, d)  # Mexico

wdc = torchhd.random_hv(1, d)  # Washington D.C.
mxc = torchhd.random_hv(1, d)  # Mexico City

usd = torchhd.random_hv(1, d)  # US Dollar
mxn = torchhd.random_hv(1, d)  # Mexican Peso

# create country representations
keys = torch.cat([country, capital, currency], dim=0)

us_values = torch.cat([usa, wdc, usd])
US = torchhd.functional.hash_table(keys, us_values)

mx_values = torch.cat([mex, mxc, mxn])
MX = torchhd.functional.hash_table(keys, mx_values)

MX_US = torchhd.bind(US, MX)

# query for the dollar of mexico
usd_of_mex = torchhd.bind(MX_US, usd)

memory = torch.cat([keys, us_values, mx_values], dim=0)
torchhd.functional.cosine_similarity(usd_of_mex, memory)
# tensor([ 0.0133,  0.0062, -0.0115,  0.0066, -0.0007,  0.0149, -0.0034,  0.0084,  0.3334])
# The hypervector for the Mexican Peso is the most similar.

This example is from the paper What We Mean When We Say "What's the Dollar of Mexico?": Prototypes and Mapping in Concept Space by Kanerva. It first creates hypervectors for all the symbols that are used in the computation, i.e., the variables for country, capital, and currency and their values for both countries. These hypervectors are then combined to make a single hypervector for each country using a hash table structure. A hash table encodes key-value pairs as: k1 * v1 + k2 * v2 + ... + kn * vn. The hash tables are then bound together to form their combined representation which is finally queried by binding with the Dollar hypervector to obtain the approximate Mexican Peso hypervector. From the similarity output it shows that the Mexican Peso hypervector is indeed the most similar one.

About

Initial development of Torchhd was performed by Mike Heddes and Igor Nunes as part of their research in Hyperdimensional Computing at the University of California, Irvine. The library was extended with significant contributions from Pere Vergés and Dheyay Desai. Torchhd later merged with a project by Rishikanth Chandrasekaran who worked on similar problems as part of his research at the University of California, San Diego.

Contributing

Documentation

To build the documentation locally, use pip install -r docs/requirements.txt to install the required packages. Then, with sphinx-build -b html docs build you can generate the html documentation in the /build directory. To create a clean build, remove the /build and /docs/generated directories.

Creating a New Release

  • A GitHub release triggers a GitHub action that builds the library and publishes it to PyPi and Conda in addition to the documentation website.
  • Before creating a new GitHub release, increment the version number in setup.py using semantic versioning.
  • When creating a new GitHub release, set the tag according to PEP 440, e.g., v1.5.2, and provide a clear description of the changes. You can use GitHub's "auto-generate release notes" button. Look at previous releases for examples.

License

This library is MIT licensed.

Cite

Consider citing our paper if you use Torchhd in your work:

@article{heddes2022torchhd,
  title={Torchhd: An Open-Source Python Library to Support Hyperdimensional Computing Research},
  author={Heddes, Mike and Nunes, Igor and Vergés, Pere and Desai, Dheyay and Givargis, Tony and Nicolau, Alexandru},
  journal={arXiv preprint arXiv:2205.09208},
  year={2022}
}

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

torch-hd-2.2.2.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

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

torch_hd-2.2.2-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file torch-hd-2.2.2.tar.gz.

File metadata

  • Download URL: torch-hd-2.2.2.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for torch-hd-2.2.2.tar.gz
Algorithm Hash digest
SHA256 4ffb7c627441aa71a3e286cab035bbe9c52d7ccfaf2605c613a22cdfea1eeccb
MD5 57e6cd770e00976072f956969bca1109
BLAKE2b-256 d85768fec6c03f3e636ef9ed3e484bcef5e6ed9b6a8358fcb1dac57b23c65fe9

See more details on using hashes here.

File details

Details for the file torch_hd-2.2.2-py3-none-any.whl.

File metadata

  • Download URL: torch_hd-2.2.2-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for torch_hd-2.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53a273178d951e097cbc270f850f56d30d042eefed1bcc9ef2a4f8bd101d74d4
MD5 6ac832a796d4c869c282741b02cef923
BLAKE2b-256 1366e5bbd293b289e50b3dc77bbad2e83fbc008c0024ae97e4ed1bd0fe801cf4

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