Gradient Ascent - Pytorch
Project description
Gradient Ascent
Gradient Ascent is just the opposite of Gradient Descent. While Gradient Descent adjusts the parameters in the opposite direction of the gradient to minimize a loss function, Gradient Ascent adjusts the parameters in the direction of the gradient to maximize some objective function.
I got the idea for this while playing basketball, I don't know why or how but this is my attempt to implement it.
Appreciation
- Lucidrains
- Agorians
Install
pip install gradient-ascent
Usage
import torch
from gradient_ascent.main import GradientAscent
class SimpleModel(torch.nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = torch.nn.Linear(1, 1)
def forward(self, x):
return self.fc(x)
# Test the optimizer
model = SimpleModel()
optimizer = GradientAscent(model.parameters(), lr=0.01)
# General some sample data
data = torch.tensor([[2.0]])
target = torch.tensor([[3.0]])
for _ in range(1000):
optimizer.zero_grad()
output = model(data)
# Negative loss as we are maximizing]
loss = -torch.nn.functional.mse_loss(output, target)
loss.backward()
optimizer.step()
print("Final output after training: ", model(data))
Architecture
Theoretical Overview
For a function ( f(\theta) ), the update step in gradient ascent is given by:
[ \theta_{new} = \theta_{old} + \alpha \nabla f(\theta_{old}) ]
Where:
- ( \theta ) are the parameters.
- ( \alpha ) is the learning rate.
- ( \nabla f(\theta_{old}) ) is the gradient of the function with respect to the parameters.
Gradient Ascent Pseudocode
Algorithm: GradientAscentOptimizer
1. Input:
- Objective function f(θ)
- Initial parameters θ₀
- Learning rate α
- Maximum iterations max_iter
2. For iteration = 1 to max_iter:
a. Compute gradient: ∇θ = gradient of f(θ) w.r.t θ
b. Update parameters: θ = θ + α * ∇θ
3. Return final parameters θ
3 New Features
-
Non-Convexity: Many problems in deep learning involve non-convex optimization landscapes. Gradient ascent, like gradient descent, can get stuck in local maxima when dealing with such landscapes. Adding mechanisms to escape from these local optima can be necessary.
-
Momentum: Momentum can be integrated to accelerate gradient vectors in any consistent direction, which can help in faster convergence and also in avoiding getting stuck in shallow local maxima.
-
Adaptive Learning Rates: The learning rate might need to adapt based on the recent history of gradients, allowing the optimization to move faster during the early stages and slow down during fine-tuning. This is seen in optimizers like AdaGrad, RMSProp, and Adam.
Applications:
The Gradient Ascent with features like momentum and adaptive learning rates, as discussed, is tailored to handle challenges in non-convex optimization landscapes. Here are some tasks and scenarios where this optimizer would be particularly beneficial:
-
Maximizing Likelihoods:
- Many models in machine learning are framed as maximum likelihood estimation (MLE) problems, where the goal is to adjust parameters to maximize the likelihood of the observed data. Examples include Gaussian Mixture Models, Hidden Markov Models, and some Neural Network configurations. Gradient ascent is directly applicable in such cases.
-
Generative Adversarial Networks (GANs):
- The generator in a GAN tries to maximize an objective function where it fools the discriminator. While traditional GANs use gradient descent with a flipped objective, using gradient ascent can be a more direct way to express this maximization problem.
-
Game Theoretic Frameworks:
- In scenarios where multiple agents are in competition or cooperation, and their objectives are to maximize certain rewards, gradient ascent can be used. This applies to multi-agent reinforcement learning and certain types of equilibrium-seeking networks.
-
Policy Gradient Methods in Reinforcement Learning:
- Policy gradient methods aim to maximize the expected reward by adjusting a policy in the direction that increases expected returns. Gradient ascent is a natural fit for this optimization problem.
-
Eigenproblems:
- In tasks where the goal is to find the maximum eigenvalue of a matrix or the corresponding eigenvector, gradient ascent techniques can be applied.
-
Feature Extraction and Representation Learning:
- When the goal is to learn features that maximize the variance or mutual information (e.g., Principal Component Analysis or some Information Maximization approaches), gradient ascent can be used to optimize the objective directly.
-
Sparse Coding:
- The aim here is to find a representation that maximizes the sparsity under certain constraints. The problem can be reframed as a maximization problem solvable with gradient ascent.
For scenarios with non-convex landscapes, the features like momentum help escape shallow local maxima, and adaptive learning rates ensure efficient traversal of the optimization landscape, adapting the step sizes based on the gradient's recent history.
However, while this optimizer can be effective in the above scenarios, one should always consider the specific nuances of the problem. It's essential to remember that no optimizer is universally the best, and empirical testing is often necessary to determine the most effective optimizer for a particular task.
Benchmarks
python benchmarks.py
Benchmark 1: 9.999994277954102
Benchmark 2: 1.375625112855263e-23
Benchmark 3: -131395.9375
Benchmark 4: -333186848.0
Benchmark 5: -166376013824.0
Benchmark 6: 0.31278279423713684
Benchmark 7: [1.375625112855263e-23, 1.375625112855263e-23]
Benchmark 8: -28.793724060058594
Benchmark 9: 1.0
Benchmark 10: 0.8203693628311157
License
MIT
Todo
- Provide metric logging + make more dynamic
- Add more benchmarks
- Validate by training a small Hidden Markov Model or another model
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
File details
Details for the file gradient_ascent-0.0.2.tar.gz
.
File metadata
- Download URL: gradient_ascent-0.0.2.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.0 Darwin/22.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aebdb82c39b0c1dfe04046e65f4a0fa1023c81f1656a6d4cfd22ae66fa4a01d2 |
|
MD5 | 4302747a3c1d2fe05bddcd0a1bf95a76 |
|
BLAKE2b-256 | ad4a3a34b23e08e0e6c9b2bb46327e8240af60aa04ae1d13a0ae4cbacc646de8 |
File details
Details for the file gradient_ascent-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: gradient_ascent-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.0 Darwin/22.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69372838d62e647becd6854abaeaba98815cbd6fa8c2dd841202ee0175249779 |
|
MD5 | fffde81149310ea8150907ca2b97d907 |
|
BLAKE2b-256 | caab9cc7bf410c14a0a564fd9c74f6deceb92a7821c0c09eabab256d7d601a7f |