Skip to main content

torchortho 📐

arXiv PyPI version License: GPL-3.0 Python Versions PWC

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 torchortho activations): 🔗 GitHub
  • Language models (GPT-2 with torchortho activations): 🔗 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 torchortho activations 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}
}

Metadata

Release files for torchortho 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchortho 0.1.2
File Size Uploaded Provenance Actions
torchortho-0.1.2.tar.gz 21.5 kB With provenance Details for torchortho-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torchortho 0.1.2
File Interpreter ABI Platform Actions
torchortho-0.1.2-py3-none-any.whl Python 3 none any Details for torchortho-0.1.2-py3-none-any.whl

Total release size: 45.8 kB

Release files / torchortho-0.1.2.tar.gz

Download URL torchortho-0.1.2.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
470107cc658faeae1265b37a80d2272c349f2a06eec0fb8765907bb78ad54b24
BLAKE2b-256 checksum
How to use checksums
23de5c26224aade1d4eeb559145b6aac94c319adf65042eca415ef75d1d83361
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 26, 2025.

Transparency log

Release files / torchortho-0.1.2-py3-none-any.whl

Download URL torchortho-0.1.2-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7b8cb36bd114d13d34116e92b027235d6e8312c11d802a24e5c2334aa6f5228
BLAKE2b-256 checksum
How to use checksums
f0656bc72f56b0e0dabeb6261e671f0c2fd81b7446eced1201d28915cc622692
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 26, 2025.

Transparency log

Security for torchortho 0.1.2

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for torchortho-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 26, 2025.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page