Last active
June 17, 2024 21:55
-
-
Save tuelwer/0b52817e9b6251d940fd8e2921ec5e20 to your computer and use it in GitHub Desktop.
pytorch-L-BFGS-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.optim as optim | |
import matplotlib.pyplot as plt | |
# 2d Rosenbrock function | |
def f(x): | |
return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2 | |
# Gradient descent | |
x_gd = 10*torch.ones(2, 1) | |
x_gd.requires_grad = True | |
gd = optim.SGD([x_gd], lr=1e-5) | |
history_gd = [] | |
for i in range(100): | |
gd.zero_grad() | |
objective = f(x_gd) | |
objective.backward() | |
gd.step() | |
history_gd.append(objective.item()) | |
# L-BFGS | |
def closure(): | |
lbfgs.zero_grad() | |
objective = f(x_lbfgs) | |
objective.backward() | |
return objective | |
x_lbfgs = 10*torch.ones(2, 1) | |
x_lbfgs.requires_grad = True | |
lbfgs = optim.LBFGS([x_lbfgs], | |
history_size=10, | |
max_iter=4, | |
line_search_fn="strong_wolfe") | |
history_lbfgs = [] | |
for i in range(100): | |
history_lbfgs.append(f(x_lbfgs).item()) | |
lbfgs.step(closure) | |
# Plotting | |
plt.semilogy(history_gd, label='GD') | |
plt.semilogy(history_lbfgs, label='L-BFGS') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this gist
I think so, but maybe lbfgs.step might need to be called twice to work. I'm using it on full dataset training, and running it just once doesn't seem to update anything. There's a couple
if state['n_iter'] == 1:
sections in lbfgs.step that initialize variables that are later computed when n_iter is greater than one.