Skip to main content

NATO Optimizer with Fourier Spectral Penalty (FSP), Kakeya Directional Penalty, and N-D FFT gradient filtering

Project description

NATO Optimizer

PyPI version Python 3.8+ License: MIT

NATO — NATO Optimizer with Fourier Spectral Penalty (FSP), Kakeya Directional Penalty, and generalized N-D FFT gradient filtering for PyTorch.

Features

  • 🚀 NATOOptimizer: Custom optimizer with built-in learning rate scheduling
  • 📊 Fourier Spectral Penalty (FSP): Regularization based on frequency-domain analysis
  • 🎯 Kakeya Directional Penalty: Gradient direction consistency regularization
  • 🔧 Low-Pass Gradient Filtering: N-D FFT-based gradient smoothing
  • GPU Accelerated: Full CUDA support for all operations

Installation

From PyPI

pip install nato-opt

From Source (Editable)

git clone https://github.com/Malhar1912/NATO.git
cd NATO
pip install -e .

Quick Start

import torch
from nato_opt import (
    NATOOptimizer,
    fourier_spectral_penalty,
    low_pass_filter_gradients,
    kakeya_directional_penalty
)

model = ...  # any torch.nn.Module
optimizer = NATOOptimizer(model.parameters(), lr=1e-3)
kakeya_state = {}  # persistent state for Kakeya penalty

# Training loop
for epoch in range(num_epochs):
    for inputs, targets in dataloader:
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, targets)
        
        # Compute penalties
        fsp = fourier_spectral_penalty(model, lambda_fsp=1e-6)
        total_loss = loss + fsp
        total_loss.backward()
        
        # Apply Kakeya penalty (optional)
        k_penalty = kakeya_directional_penalty(model, kakeya_state, lambda_k=1e-4)
        
        # Apply gradient filtering before optimizer step
        low_pass_filter_gradients(model, cutoff_ratio=0.5)
        optimizer.step(epoch)  # pass epoch for LR decay

API Reference

NATOOptimizer

NATOOptimizer(params, lr=1e-3, ...)

Custom optimizer extending SGD with built-in learning rate scheduling.

Parameters:

  • params: Iterable of parameters to optimize
  • lr: Learning rate (default: 1e-3)

fourier_spectral_penalty

fourier_spectral_penalty(
    model,
    lambda_fsp=1e-6,
    include_conv=True,
    include_linear=True,
    module_whitelist=None,
    module_blacklist=None,
    device=None
) -> torch.Tensor

Compute Fourier Spectral Penalty (FSP) on model weights.

Parameters:

  • model: PyTorch model
  • lambda_fsp: Penalty coefficient (default: 1e-6)
  • include_conv: Include Conv layers (default: True)
  • include_linear: Include Linear layers (default: True)
  • module_whitelist: Only include these module names
  • module_blacklist: Exclude these module names

Returns: Scalar penalty tensor


kakeya_directional_penalty

kakeya_directional_penalty(
    model,
    state,
    lambda_k=1e-4
) -> torch.Tensor

Compute Kakeya directional penalty based on gradient direction consistency.

Penalizes gradients that maintain high cosine similarity with previous gradients, encouraging exploration of diverse gradient directions during optimization.

Parameters:

  • model: PyTorch model
  • state: Persistent dict to store previous gradients {param: {'prev_grad': tensor}}
  • lambda_k: Penalty coefficient (default: 1e-4)

Returns: Scalar penalty tensor


low_pass_filter_gradients

low_pass_filter_gradients(
    model,
    cutoff_ratio=0.5
)

Apply low-pass FFT filtering to gradients, smoothing high-frequency noise.

Parameters:

  • model: PyTorch model with computed gradients
  • cutoff_ratio: Frequency cutoff (0-1), lower = more filtering

adjust_learning_rate

adjust_learning_rate(optimizer, epoch, ...)

Utility function for learning rate scheduling.

Requirements

  • Python >= 3.8
  • PyTorch
  • NumPy

License

MIT License - see LICENSE for details.

Authors

Citation

If you use NATO Optimizer in your research, please cite:

@software{nato_optimizer,
  title = {NATO Optimizer: Neural Adaptive Training Optimizer},
  author = {Pangarkar, Malhar and Khambete, Atharva},
  year = {2026},
  url = {https://github.com/Malhar1912/NATO}
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nato_opt-0.1.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nato_opt-0.1.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file nato_opt-0.1.2.tar.gz.

File metadata

  • Download URL: nato_opt-0.1.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for nato_opt-0.1.2.tar.gz
Algorithm Hash digest
SHA256 73269dd5f9c234d7aeaa5d1dce4db08cc7c5d1e40b1fbf0c9e49134a22a09624
MD5 fd77d8af000308eb0ff011ffb92d3200
BLAKE2b-256 4c9444d49f901d6db89a59bbfd79f90adeee7151a844ed752b2ceab4fb2fcdd7

See more details on using hashes here.

File details

Details for the file nato_opt-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: nato_opt-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for nato_opt-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 10fbd116c85782395e66799ee36a7dcd67d39b591a598425965e83c32bbc9bbf
MD5 706d43153f27c10ba508eed0630c65ac
BLAKE2b-256 6d60939abbfb20ee73572b972c9314916c702233604afc8efe8e983ea731d39d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page