Einfunc
A convenient way of applying functions to tensors. einfunc is incredibly similar to einsum, but with one key difference. The ability to apply your own custom function instead of multiplication. einfunc also allows for the ability to choose how reductions occur within the operation.
einfunc is a simple interface, which utilizes Pytorch's torchdim. I highly recommend checking out torchdim as it is by far the best way to do readable tensor operations in pytorch. einfunc is just a convenient way of tapping into torchdim with a function similar to einsum.
Table of Contents
- Installation
- API
- Why you shouldn't use einfunc
- Why you should use einfunc
- Additional Examples
- Planned Work
- Acknowledgements
Installation
einfunc requires torch >= 2.0 and python >= 3.11.
pip install einfunc
API
Using einfunc is similar to einsum however you also pass a function and a mode of reduction. Take this math equation for example.
$$ \frac{1}{K} \sum_j^K \left(\log (z_j) / \prod_i^B x_i^2 - e^{y_{i,j}} \right)$$
We can use einfunc to represent this math equation with 2 lines of code.
inner_exp = einfunc(x, y, 'b, b k -> k', lambda a, b : a ** 2 - torch.exp(b), reduce='prod')
final_exp = einfunc(z, inner_exp, 'k, k -> ', lambda a, b : torch.log(a) / b, reduce='mean')
Creating functions
While lambda functions are simple, any function can work as long as it takes the correct number of inputs. For example when looking at the following expression:
inner_exp = einfunc(x, y, 'i, i j -> j', lambda a, b : a ** 2 - torch.exp(b), reduce='prod')
x maps to a and y maps to b. This means that the order of the function variables is passed in the order that tensors are passed to einfunc.
Reducing
Currently, einfunc supports 5 different types of reduction.
- Mean
$$ \frac{1}{I} \sum_i^I x_{i,j} - y_{k, i} $$
result = einfunc(x, y, 'i j, k i -> j k', lambda a, b : a - b, reduce='mean')
- Sum
$$ \sum_i^I x_{i,j} - y_{k, i} $$
result = einfunc(x, y, 'i j, k i -> j k', lambda a, b : a - b, reduce='sum')
- Prod
$$ \prod_i^I x_{i,j} - y_{k, i} $$
result = einfunc(x, y, 'i j, k i -> j k', lambda a, b : a - b, reduce='prod')
- Max
$$ Max(x_{i,j} - y_{k, i}, \quad dim=i) $$
result = einfunc(x, y, 'i j, k i -> j k', lambda a, b : a - b, reduce='max')
- Min
$$ Min(x_{i,j} - y_{k, i}, \quad dim=i) $$
result = einfunc(x, y, 'i j, k i -> j k', lambda a, b : a - b, reduce='min')
One thing to note is that if reduce is not passed then 'sum' is assumed by einfunc.
Why you shouldn't use einfunc
Einfunc is just a convenient way of interfacing with PyTorch and Torchdim. This creates some overhead when operating on tensors, compared to vanilla operations and torchdim operations. This means that it will be much faster to use vanilla pytorch operations if doing a simple operation, or just use torchdim if trying to do something more complex.
Why you should use einfunc
It's convenient and slightly more readable than torchdim IMO. Understanding exactly what is happening in an operation can be hard, and einfunc makes it a lot simpler by boiling operations down to a single expression, while using einstein notation to indicate indexing.
Additional Examples
Coming Soon :)
Planned Work
Currently, einfunc does support parenthesis and ellipses. I will be working on implementing this as soon as I can.
Acknowledgements
Metadata
Release files for einfunc 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| einfunc-0.0.4.tar.gz | 6.2 kB | Without provenance | Details for einfunc-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| einfunc-0.0.4-py3-none-any.whl | Python 3 | none | any | Details for einfunc-0.0.4-py3-none-any.whl |
Total release size: 11.7 kB
Release files / einfunc-0.0.4.tar.gz
| Download URL | einfunc-0.0.4.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19177c56f8ef7caf84a95ec2984191eb6cbe4ad02e380414f764c6048eb13af8
|
|
BLAKE2b-256 checksum How to use checksums |
60c35a66516db12626eec1b90e8c5642cc85ad2903837f38632c9ac2645e37ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.27.0
|
Release files / einfunc-0.0.4-py3-none-any.whl
| Download URL | einfunc-0.0.4-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0d0ae11e4f886b70ebbe7170ad80242b562ab7de1b2ced676bd9ebbc168213f
|
|
BLAKE2b-256 checksum How to use checksums |
49b2114bc816813f5aa909393c3b1b018f6f461b9b795aab9680bccf8124893c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.27.0
|
Security for einfunc 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.