torch-legendre
A PyTorch layer for expanding input features into Legendre polynomial bases.
Useful for building models with polynomial feature expansions while keeping the
workflow compatible with standard PyTorch nn.Module layers.
Features
- Computes Legendre polynomial terms P₀(x), P₁(x), …, Pₙ₋₁(x) for each input feature
- Supports arbitrary input dimensionality
- Optional trainable linear projection after expansion
- Drop-in compatible with
torch.nn.Sequential - Efficient recurrence-based computation (no loops over batches)
Installation
pip install -e .
Usage
You can see a full demonstration of the LegendreLayer in the provided example.ipynb notebook.
1. Basic Legendre Expansion
import torch
from torch_legendre import LegendreLayer
# Example: 2 input features, expand to degree 4 (P₀...P₃)
layer = LegendreLayer(in_features=2, degree=4)
x = torch.tensor([[0.1, -0.3],
[0.5, 0.2]]) # shape (batch=2, in_features=2)
y = layer(x)
print(y.shape) # (2, 2 * 4) = (2, 8)
2. With Trainable Projection
# Expand then project down to 3 outputs
layer = LegendreLayer(in_features=2, degree=4, out_features=3)
x = torch.rand(5, 2)
y = layer(x)
print(y.shape) # (5, 3)
3. Stacking in a Sequential Model
import torch.nn as nn
model = nn.Sequential(
LegendreLayer(in_features=1, degree=5, out_features=10),
LegendreLayer(in_features=10, degree=3, out_features=1)
)
API
LegendreLayer
Expands each input feature into its Legendre polynomial basis and optionally applies a trainable linear projection.
Parameters
-
in_features (int): Number of input features.
-
degree (int): Number of polynomial degrees to compute per input feature.
degree = 1means only the constant termP₀(x) = 1. -
out_features (int, optional): If provided, a final
nn.Linearlayer maps from(in_features * degree)→out_features. IfNone, returns the raw expanded features. -
bias (bool, default=True): Whether to include a bias term in the optional linear mapping.
Shapes
- Input:
(batch_size, in_features) - Output:
- If
out_features is None:(batch_size, in_features * degree) - Else:
(batch_size, out_features)
- If
License
This project is licensed under the MIT License — see the LICENSE file for details.
Metadata
Release files for torch-legendre 0.1.1
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 |
|---|---|---|---|---|
| torch_legendre-0.1.1.tar.gz | 157.7 kB | Without provenance | Details for torch_legendre-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_legendre-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for torch_legendre-0.1.1-py3-none-any.whl |
Total release size: 162.9 kB
Release files / torch_legendre-0.1.1.tar.gz
| Download URL | torch_legendre-0.1.1.tar.gz |
|---|---|
| Size | 157.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f12d4de97d6b267cd5e1e3c8f6faa02e7bf0e174f986ef0978ae96ffe840b74c
|
|
BLAKE2b-256 checksum How to use checksums |
6c8fc38abfb1dc610f6315c8214c2b59099672fac6e28fcfb2cc83ffc3586186
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / torch_legendre-0.1.1-py3-none-any.whl
| Download URL | torch_legendre-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dcb4a89f62b145a29ff0afab011f9113d770678b118a4cfe750482cad3743130
|
|
BLAKE2b-256 checksum How to use checksums |
1f73e9811c9fcfe328e6ac48b3a31fb3c749800158233528d39a33b7601a91f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Security for torch-legendre 0.1.1
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.