torchortho is a simple PyTorch package that efficiently implements the Hermite Activation function and other activation functions.
Project description
torchortho 📐
torchortho is a PyTorch library for learnable activation functions based on:
- Hermite Polynomials 🧙♂️
- Fourier Series 〰
- Tropical Polynomials & Rational Functions 🌴
These adaptive activations dynamically adjust during training, offering improved expressivity, better gradient flow, and enhanced generalization for vision and language models.
📜 Paper Reference
This library is based on the paper:
📄 Learnable Polynomial, Trigonometric, and Tropical Activations (Khalfaoui-Hassani & Kesselheim, 2025).
For experimental results, check our repos:
- Vision models (ConvNeXt with
torchorthoactivations): 🔗 GitHub - Language models (GPT-2 with
torchorthoactivations): 🔗 GitHub
📦 Installation
Install from PyPI:
pip install torchortho
or install directly from GitHub:
pip install git+https://github.com/K-H-Ismail/torchortho.git
📝 Usage
You can use torchortho activations just like any other PyTorch activation:
Example: Using Hermite Activation
import torch
from torchortho import HermiteActivation
# Define a learnable Hermite activation
degree = 5
activation = HermiteActivation(degree)
# Forward pass
x = torch.rand(7, 4, 3, 2)
y = activation(x)
# Compute gradients
loss = y.sum()
loss.backward()
print("Gradients of activation coefficients:", activation.coefficients.grad)
print("Output:", y)
Example: Using Fourier Activation in a Neural Network
import torch
import torch.nn as nn
from torchortho import FourierActivation
class CustomMLP(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super().__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.activation = FourierActivation(degree=4) # Learnable Fourier activation
self.fc2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = self.fc1(x)
x = self.activation(x)
return self.fc2(x)
# Initialize the model
model = CustomMLP(input_dim=10, hidden_dim=32, output_dim=1)
x = torch.randn(5, 10)
output = model(x)
print("Model Output:", output)
⚡ Why Use torchortho?
1️⃣ Adaptive and Learnable Activations
Unlike static activations (ReLU, GELU), torchortho functions dynamically adapt during training, allowing models to learn optimal activation functions for different tasks.
| Activation Type | Strengths |
|---|---|
| Hermite Activation | Adaptive polynomial approximation, variance-preserving, smooth optimization |
| Fourier Activation | Captures periodic structures in data (useful for NLP, physics-based models, and time-series) |
| Tropical Polynomial Activation | Convex activation for structured learning (e.g., decision boundaries, optimization landscapes) |
| Rational Activation | Generalizes standard activation functions (e.g., Pade approximants can replicate ReLU, GELU, or even SwiGLU) |
2️⃣ Improved Expressivity and Gradient Flow
- Better function approximation → Increases expressivity for deep networks.
- Variance-preserving initialization → Ensures stable training, avoiding vanishing/exploding gradients.
- More flexible than ReLU/SwiGLU → Adapts activation behavior based on data.
3️⃣ Benchmarked on Real-World Models
The effectiveness of torchortho activations has been validated on large-scale deep learning benchmarks:
✅ Image Classification (ConvNeXt-T on ImageNet-1K)
- Replacing GELU with
torchorthoactivations improves top-1 accuracy.
✅ Language Modeling (GPT-2 on OpenWebText)
- Learnable activations reduce perplexity compared to GELU-based models.
For full benchmarks, see:
📜 License
This project is licensed under the GPL-3.0 License. See LICENSE for details.
🙌 Contributing
We welcome contributions! Feel free to submit issues, open PRs, or suggest improvements.
📬 Contact
For questions or collaborations, reach out via GitHub Issues.
📚 Citation
If you use torchortho in your research, please cite the following paper:
@article{khalfaoui2025learnable,
title={Learnable polynomial, trigonometric, and tropical activations},
author={Khalfaoui-Hassani, Ismail and Kesselheim, Stefan},
journal={arXiv preprint arXiv:2502.01247},
year={2025}
}
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchortho-0.1.1.tar.gz.
File metadata
- Download URL: torchortho-0.1.1.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
608aa728d8cbf3095a7043ddb30a7c87127defbb6d9437860ffed460a241c7f3
|
|
| MD5 |
7349aa2f5235749bff764cc142e6e1d4
|
|
| BLAKE2b-256 |
6d2d61f817e58a1d6451524f2161203a59b95134e24419d649a58bb39313d847
|
Provenance
The following attestation bundles were made for torchortho-0.1.1.tar.gz:
Publisher:
python-publish.yml on K-H-Ismail/torchortho
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchortho-0.1.1.tar.gz -
Subject digest:
608aa728d8cbf3095a7043ddb30a7c87127defbb6d9437860ffed460a241c7f3 - Sigstore transparency entry: 169341072
- Sigstore integration time:
-
Permalink:
K-H-Ismail/torchortho@76690f8ea7bcddf33bebc65d58b9bdd41da7363a -
Branch / Tag:
refs/tags/v.0.1.1 - Owner: https://github.com/K-H-Ismail
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@76690f8ea7bcddf33bebc65d58b9bdd41da7363a -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchortho-0.1.1-py3-none-any.whl.
File metadata
- Download URL: torchortho-0.1.1-py3-none-any.whl
- Upload date:
- Size: 24.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8179f7a42ff122105a2941c52358f03a75761fc3bf4209178093c5f4bb3005ee
|
|
| MD5 |
22215a4979456362140315a940fecaa6
|
|
| BLAKE2b-256 |
546298f8cd58bea6e7267aea411b2751c8259458bd5ab9b9c0ab146cb5fed241
|
Provenance
The following attestation bundles were made for torchortho-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on K-H-Ismail/torchortho
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchortho-0.1.1-py3-none-any.whl -
Subject digest:
8179f7a42ff122105a2941c52358f03a75761fc3bf4209178093c5f4bb3005ee - Sigstore transparency entry: 169341073
- Sigstore integration time:
-
Permalink:
K-H-Ismail/torchortho@76690f8ea7bcddf33bebc65d58b9bdd41da7363a -
Branch / Tag:
refs/tags/v.0.1.1 - Owner: https://github.com/K-H-Ismail
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@76690f8ea7bcddf33bebc65d58b9bdd41da7363a -
Trigger Event:
release
-
Statement type: