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 hashes)
Built Distribution
Close
Hashes for semiring_torch-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 563769309478503cfb8b57cac8b6cbbe66e58b2cf2118414ba3a5d89c2e90f73 |
|
MD5 | 2d689089370dfa998881754feb275f9a |
|
BLAKE2b-256 | fa58bdaa399d0c34524683031df7034257b11e815a9485776affc34b4813775a |