Semiring programming in PyTorch
Project description
semiring_torch
Run your pytorch code on any semiring with a single line of code! Semiring_torch is built on top of autoray.
Warning: this is a proof of concept. Expect bugs and missing features.
Example
By using the logarithmic semiring, you can easily write numerically stable code. In the following example, we compute a matrix product in log-space.
Regular torch | semiring_torch |
---|---|
import torch
x1 = torch.tensor([[0.1, 0.6], [0.1, 0.4]])
x2 = torch.tensor([[0.5, 0.3], [0.2, 0.1]])
x1 = x1.log()
x2 = x2.log()
result = x1[:, :, None] + x2[None, :, :]
result = torch.logsumexp(result, dim=1)
result = result.exp()
|
from autoray import numpy as torch
from semiring_torch import logarithmic_semiring
with logarithmic_semiring:
x1 = torch.tensor([[0.1, 0.6], [0.1, 0.4]])
x2 = torch.tensor([[0.5, 0.3], [0.2, 0.1]])
result = x1 @ x2
|
Supported semirings
Currently only the logarithmic semiring is supported, but more semirings can be added easily.
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
semiring_torch-0.0.1.tar.gz
(21.4 kB
view details)
Built Distribution
File details
Details for the file semiring_torch-0.0.1.tar.gz
.
File metadata
- Download URL: semiring_torch-0.0.1.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 851face16ca2c7a2529665f6ceb9c824226ac7111d50bc2b748cd7dc57cfd228 |
|
MD5 | a7ec055600b699ff2f45d7eeab121c7c |
|
BLAKE2b-256 | f0312bbc5a2107eb7398b1094a1335308ce0f7b6c0e53c9164bdcb35ccc74c8e |
File details
Details for the file semiring_torch-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: semiring_torch-0.0.1-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 563769309478503cfb8b57cac8b6cbbe66e58b2cf2118414ba3a5d89c2e90f73 |
|
MD5 | 2d689089370dfa998881754feb275f9a |
|
BLAKE2b-256 | fa58bdaa399d0c34524683031df7034257b11e815a9485776affc34b4813775a |