Adaptive Fractional Gradient Descent Optimizers (AdaFGD & AdaNCFGD)
Project description
Adaptive Fractional Gradient Descent Optimizers (AdaFGD & AdaNCFGD)
Description
The adancfgd package implements two advanced optimizers combining fractional gradient descent with adaptive learning rates, along with a comprehensive Spiking Neural Network (SNN) framework. Building on PyTorch's SGD, these algorithms enhance convergence and performance for machine learning tasks using fractional calculus-based gradient adjustments.
Overview
This package provides two novel optimization algorithms and a complete SNN implementation:
Optimizers
- AdaFGD - Adaptive Fractional Gradient Descent: Uses fractional derivatives for gradient adjustment with adaptive learning rates.
- AdaNCFGD - Adaptive Non-Causal Fractional Gradient Descent: Extends AdaFGD by considering multiple previous parameter values.
Spiking Neural Network (SNN) Components
- Core Layers: SNNLinear, SNNConv2d, SNNDropout
- Batch Normalization: SNNBatchNorm1d, SNNBatchNorm2d
- Composite Layers: SNNLinearWithBatchNorm, SNNConv2dWithBatchNorm
- Complete Models: SNN, SNNCNN
Installation
The package has been successfully uploaded to PyPI. You can install it directly from PyPI or from the source code.
From PyPI
pip install adancfgd
From Source
git clone https://github.com/HunLuanZhiZhu/AdaNCFGD.git
cd AdaNCFGD
pip install -e .
Dependencies
- PyTorch >= 1.7.0
- NumPy
- Python >= 3.6
Key Features
AdaFGD
- Utilizes fractional derivatives for gradient adjustment
- Incorporates adaptive learning rates using first and second moment estimates
- Maintains parameter history for fractional gradient calculation
- Parameter updates handled by parent SGD class
- Supports AMSGrad variant
- Compatible with all PyTorch models
AdaNCFGD
- Extends AdaFGD with non-causal fractional gradient descent
- Considers multiple previous parameter values (two steps back)
- Adapts gradient calculation based on parameter direction changes
- Maintains richer parameter history
- Supports all AdaFGD features
- Enhanced convergence for complex models
SNN Components
- Spike-based neural network implementation
- Surrogate gradient approach for training
- Support for both fully connected and convolutional architectures
- Batch normalization for spike data
- Dropout regularization for SNNs
- Complete SNN and SNNCNN models
API Documentation
Optimizers
AdaFGD
AdaFGD(params, lr=0.001, alpha=1.0, epsilon=1e-4, momentum=0.0, dampening=0.0, weight_decay=0.0, nesterov=False, betas=(0.9, 0.999), eps=1e-8, amsgrad=False, maximize=False, foreach=None, differentiable=False, fused=None)
Parameters:
params: Iterable of parameters to optimize or dicts defining parameter groupslr: Learning rate (default: 0.001)alpha: Fractional order (must satisfy 0 < alpha < 2, default: 1.0)epsilon: Small positive constant to avoid division by zero (default: 1e-4)momentum: Momentum factor (default: 0.0)dampening: Dampening for momentum (default: 0.0)weight_decay: Weight decay (L2 penalty, default: 0.0)nesterov: Enables Nesterov momentum (default: False)betas: Coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999))eps: Term added to the denominator to improve numerical stability (default: 1e-8)amsgrad: Whether to use the AMSGrad variant (default: False)
AdaNCFGD
AdaNCFGD(params, lr=0.001, alpha=1.0, epsilon=1e-4, momentum=0.0, dampening=0.0, weight_decay=0.0, nesterov=False, betas=(0.9, 0.999), eps=1e-8, amsgrad=False, maximize=False, foreach=None, differentiable=False, fused=None)
Parameters:
- Same as AdaFGD, with the addition of non-causal fractional gradient calculation
SNN Components
SNNLinear
SNNLinear(in_features, out_features, bias=True, window_t=100, threshold_voltage=15, initial_potential_ratio=0.5, w_mean=0, w_std=1, device=None, dtype=None)
SNNConv2d
SNNConv2d(input_shape, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, window_t=100, threshold_voltage=15, initial_potential_ratio=0.5, w_mean=0, w_std=1, device=None, dtype=None)
SNNCNN
SNNCNN(device=None, dtype=None)
Usage Examples
Example 1: Using AdaFGD with a Simple Model
import torch
import torch.nn as nn
from adancfgd import AdaFGD
# Create a simple model
model = nn.Linear(10, 1)
# Initialize optimizer with default parameters
optimizer = AdaFGD(model.parameters(), lr=0.001, alpha=1.0)
# Training loop
inputs = torch.randn(32, 10)
targets = torch.randn(32, 1)
criterion = nn.MSELoss()
for epoch in range(10):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")
Example 2: Using AdaNCFGD with a CNN
import torch
import torch.nn as nn
from adancfgd import AdaNCFGD
# Create a simple CNN
model = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(32 * 16 * 16, 10)
)
# Initialize optimizer
optimizer = AdaNCFGD(model.parameters(), lr=0.0001, alpha=1.0, betas=(0.9, 0.999))
# Training loop (simplified)
inputs = torch.randn(32, 3, 32, 32)
targets = torch.randint(0, 10, (32,))
criterion = nn.CrossEntropyLoss()
for epoch in range(5):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")
Example 3: Using SNN Components
import torch
from adancfgd import SNNLinear, SNNConv2d, SNNDropout, SNNSequential
# Create a simple SNN model using SNNSequential
snn_model = SNNSequential(
SNNLinear(784, 256),
SNNDropout(p=0.5),
SNNLinear(256, 10)
)
# Create test data
spikes = torch.randn(10, 32, 784) # (time_steps, batch, features)
# Forward pass
output = snn_model.step(spikes)
print(f"Output shape: {output.shape}")
Example 4: Training SNNCNN on MNIST
import torch
from adancfgd import SNNCNN
from adancfgd.train_mnist import train
# Create SNNCNN model
model = SNNCNN()
# Train on MNIST dataset
train(model, 'snncnn_test', epochs=10, batch_size=50)
Performance Comparison
Convergence Speed
AdaFGD and AdaNCFGD have been shown to converge faster than traditional optimizers like SGD and Adam for certain tasks, especially for models with complex loss landscapes.
Accuracy
For image classification tasks, these optimizers can achieve higher final accuracy compared to standard optimizers when used with appropriate hyperparameters.
SNN Performance
The SNN implementation supports efficient training of spiking neural networks using surrogate gradients, achieving competitive performance with traditional ANN models on MNIST and other datasets.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Authors
- Yihe Zhu - Initial work - HunLuanZhiZhu
Acknowledgments
- This work was inspired by recent advances in fractional calculus and adaptive optimization algorithms.
- Built on PyTorch's robust deep learning framework.
Contact
- Email: zhu.yihe@qq.com
- GitHub: https://github.com/HunLuanZhiZhu/AdaNCFGD
Version History
- 0.1.4 - Complete SNN implementation, fixed import issues, improved documentation
- 0.1.3 - Fixed package structure, updated init.py
- 0.1.2 - Initial release with AdaFGD and AdaNCFGD optimizers
Citation
If you use this package in your research, please consider citing:
@software{adancfgd2025,
author = {Yihe Zhu},
title = {adancfgd: Adaptive Fractional Gradient Descent Optimizers and SNN Framework},
year = {2025},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/HunLuanZhiZhu/AdaNCFGD}},
}
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
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 adancfgd-0.1.5.tar.gz.
File metadata
- Download URL: adancfgd-0.1.5.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8248f67ff1a04260171fe3e4cda66323be3660f9e0ff719517b2ab7b51920bc
|
|
| MD5 |
b4648daca675f2b97a27933cf09d009a
|
|
| BLAKE2b-256 |
cbd9ef93f1c0e4926b6e85b22506c37628949b1a91b9611111a70e9d9138bbef
|
File details
Details for the file adancfgd-0.1.5-py3-none-any.whl.
File metadata
- Download URL: adancfgd-0.1.5-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f977cc2b425b8083685eedf2222f3b8bad2f72dcab6a235d66ea0822b7500af
|
|
| MD5 |
a6a286232c16e2052925f2f934ff8153
|
|
| BLAKE2b-256 |
bcdcbafeb878720f07ce4beb266f80a33437a23103ff0cdf20cf58cc813ca489
|