File size: 21,949 Bytes
d301b87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 | import torch
import torch.autograd
import math
from pytracking.libs import TensorList
from pytracking.utils.plotting import plot_graph
from ltr.models.layers.activation import softmax_reg
class L2Problem:
"""Base class for representing an L2 optimization problem."""
def __call__(self, x: TensorList) -> TensorList:
"""Shall compute the residuals of the problem."""
raise NotImplementedError
def ip_input(self, a, b):
"""Inner product of the input space."""
return sum(a.view(-1) @ b.view(-1))
def ip_output(self, a, b):
"""Inner product of the output space."""
return sum(a.view(-1) @ b.view(-1))
def M1(self, x):
"""M1 preconditioner."""
return x
def M2(self, x):
"""M2 preconditioner."""
return x
class MinimizationProblem:
"""General minimization problem."""
def __call__(self, x: TensorList) -> TensorList:
"""Shall compute the loss."""
raise NotImplementedError
def ip_input(self, a, b):
"""Inner product of the input space."""
return sum(a.view(-1) @ b.view(-1))
def M1(self, x):
return x
def M2(self, x):
return x
class ConjugateGradientBase:
"""Conjugate Gradient optimizer base class. Implements the CG loop."""
def __init__(self, fletcher_reeves = True, standard_alpha = True, direction_forget_factor = 0, debug = False):
self.fletcher_reeves = fletcher_reeves
self.standard_alpha = standard_alpha
self.direction_forget_factor = direction_forget_factor
self.debug = debug
# State
self.p = None
self.rho = torch.ones(1)
self.r_prev = None
# Right hand side
self.b = None
def reset_state(self):
self.p = None
self.rho = torch.ones(1)
self.r_prev = None
def run_CG(self, num_iter, x=None, eps=0.0):
"""Main conjugate gradient method.
args:
num_iter: Number of iterations.
x: Initial guess. Assumed zero if None.
eps: Stop if the residual norm gets smaller than this.
"""
# Apply forgetting factor
if self.direction_forget_factor == 0:
self.reset_state()
elif self.p is not None:
self.rho /= self.direction_forget_factor
if x is None:
r = self.b.clone()
else:
r = self.b - self.A(x)
# Norms of residuals etc for debugging
resvec = None
if self.debug:
normr = self.residual_norm(r)
resvec = torch.zeros(num_iter+1)
resvec[0] = normr
# Loop over iterations
for ii in range(num_iter):
# Preconditioners
y = self.M1(r)
z = self.M2(y)
rho1 = self.rho
self.rho = self.ip(r, z)
if self.check_zero(self.rho):
if self.debug:
print('Stopped CG since rho = 0')
if resvec is not None:
resvec = resvec[:ii+1]
return x, resvec
if self.p is None:
self.p = z.clone()
else:
if self.fletcher_reeves:
beta = self.rho / rho1
else:
rho2 = self.ip(self.r_prev, z)
beta = (self.rho - rho2) / rho1
beta = beta.clamp(0)
self.p = z + self.p * beta
q = self.A(self.p)
pq = self.ip(self.p, q)
if self.standard_alpha:
alpha = self.rho / pq
else:
alpha = self.ip(self.p, r) / pq
# Save old r for PR formula
if not self.fletcher_reeves:
self.r_prev = r.clone()
# Form new iterate
if x is None:
x = self.p * alpha
else:
x += self.p * alpha
if ii < num_iter - 1 or self.debug:
r -= q * alpha
if eps > 0.0 or self.debug:
normr = self.residual_norm(r)
if self.debug:
self.evaluate_CG_iteration(x)
resvec[ii+1] = normr
if eps > 0 and normr <= eps:
if self.debug:
print('Stopped CG since norm smaller than eps')
break
if resvec is not None:
resvec = resvec[:ii+2]
return x, resvec
def A(self, x):
# Implements the left hand operation
raise NotImplementedError
def ip(self, a, b):
# Implements the inner product
return a.view(-1) @ b.view(-1)
def residual_norm(self, r):
res = self.ip(r, r).sum()
if isinstance(res, (TensorList, list, tuple)):
res = sum(res)
return res.sqrt()
def check_zero(self, s, eps = 0.0):
ss = s.abs() <= eps
if isinstance(ss, (TensorList, list, tuple)):
ss = sum(ss)
return ss.item() > 0
def M1(self, x):
# M1 preconditioner
return x
def M2(self, x):
# M2 preconditioner
return x
def evaluate_CG_iteration(self, x):
pass
class ConjugateGradient(ConjugateGradientBase):
"""Conjugate Gradient optimizer, performing single linearization of the residuals in the start."""
def __init__(self, problem: L2Problem, variable: TensorList, cg_eps = 0.0, fletcher_reeves = True,
standard_alpha = True, direction_forget_factor = 0, debug = False, plotting = False, visdom=None):
super().__init__(fletcher_reeves, standard_alpha, direction_forget_factor, debug or plotting)
self.problem = problem
self.x = variable
self.plotting = plotting
self.fig_num = (10,11)
self.visdom = visdom
self.cg_eps = cg_eps
self.f0 = None
self.g = None
self.dfdxt_g = None
self.residuals = torch.zeros(0)
self.losses = torch.zeros(0)
def clear_temp(self):
self.f0 = None
self.g = None
self.dfdxt_g = None
def run(self, num_cg_iter):
"""Run the oprimizer with the provided number of iterations."""
if num_cg_iter == 0:
return
lossvec = None
if self.debug:
lossvec = torch.zeros(2)
self.x.requires_grad_(True)
# Evaluate function at current estimate
self.f0 = self.problem(self.x)
# Create copy with graph detached
self.g = self.f0.detach()
if self.debug:
lossvec[0] = self.problem.ip_output(self.g, self.g)
self.g.requires_grad_(True)
# Get df/dx^t @ f0
self.dfdxt_g = TensorList(torch.autograd.grad(self.f0, self.x, self.g, create_graph=True))
# Get the right hand side
self.b = - self.dfdxt_g.detach()
# Run CG
delta_x, res = self.run_CG(num_cg_iter, eps=self.cg_eps)
self.x.detach_()
self.x += delta_x
if self.debug:
self.f0 = self.problem(self.x)
lossvec[-1] = self.problem.ip_output(self.f0, self.f0)
self.residuals = torch.cat((self.residuals, res))
self.losses = torch.cat((self.losses, lossvec))
if self.visdom is not None:
self.visdom.register(self.losses, 'lineplot', 3, 'Loss')
self.visdom.register(self.residuals, 'lineplot', 3, 'CG residuals')
elif self.plotting:
plot_graph(self.losses, self.fig_num[0], title='Loss')
plot_graph(self.residuals, self.fig_num[1], title='CG residuals')
self.x.detach_()
self.clear_temp()
def A(self, x):
dfdx_x = torch.autograd.grad(self.dfdxt_g, self.g, x, retain_graph=True)
return TensorList(torch.autograd.grad(self.f0, self.x, dfdx_x, retain_graph=True))
def ip(self, a, b):
return self.problem.ip_input(a, b)
def M1(self, x):
return self.problem.M1(x)
def M2(self, x):
return self.problem.M2(x)
class GaussNewtonCG(ConjugateGradientBase):
"""Gauss-Newton with Conjugate Gradient optimizer."""
def __init__(self, problem: L2Problem, variable: TensorList, cg_eps = 0.0, fletcher_reeves = True,
standard_alpha = True, direction_forget_factor = 0, debug = False, analyze = False, plotting = False,
visdom=None):
super().__init__(fletcher_reeves, standard_alpha, direction_forget_factor, debug or analyze or plotting)
self.problem = problem
self.x = variable
self.analyze_convergence = analyze
self.plotting = plotting
self.fig_num = (10,11,12)
self.visdom = visdom
self.cg_eps = cg_eps
self.f0 = None
self.g = None
self.dfdxt_g = None
self.residuals = torch.zeros(0)
self.losses = torch.zeros(0)
self.gradient_mags = torch.zeros(0)
def clear_temp(self):
self.f0 = None
self.g = None
self.dfdxt_g = None
def run_GN(self, *args, **kwargs):
return self.run(*args, **kwargs)
def run(self, num_cg_iter, num_gn_iter=None):
"""Run the optimizer.
args:
num_cg_iter: Number of CG iterations per GN iter. If list, then each entry specifies number of CG iterations
and number of GN iterations is given by the length of the list.
num_gn_iter: Number of GN iterations. Shall only be given if num_cg_iter is an integer.
"""
if isinstance(num_cg_iter, int):
if num_gn_iter is None:
raise ValueError('Must specify number of GN iter if CG iter is constant')
num_cg_iter = [num_cg_iter]*num_gn_iter
num_gn_iter = len(num_cg_iter)
if num_gn_iter == 0:
return
if self.analyze_convergence:
self.evaluate_CG_iteration(0)
# Outer loop for running the GN iterations.
for cg_iter in num_cg_iter:
self.run_GN_iter(cg_iter)
if self.debug:
if not self.analyze_convergence:
self.f0 = self.problem(self.x)
loss = self.problem.ip_output(self.f0, self.f0)
self.losses = torch.cat((self.losses, loss.detach().cpu().view(-1)))
if self.visdom is not None:
self.visdom.register(self.losses, 'lineplot', 3, 'Loss')
self.visdom.register(self.residuals, 'lineplot', 3, 'CG residuals')
if self.analyze_convergence:
self.visdom.register(self.gradient_mags, 'lineplot', 4, 'Gradient magnitude')
elif self.plotting:
plot_graph(self.losses, self.fig_num[0], title='Loss')
plot_graph(self.residuals, self.fig_num[1], title='CG residuals')
if self.analyze_convergence:
plot_graph(self.gradient_mags, self.fig_num[2], 'Gradient magnitude')
self.x.detach_()
self.clear_temp()
return self.losses, self.residuals
def run_GN_iter(self, num_cg_iter):
"""Runs a single GN iteration."""
self.x.requires_grad_(True)
# Evaluate function at current estimate
self.f0 = self.problem(self.x)
# Create copy with graph detached
self.g = self.f0.detach()
if self.debug and not self.analyze_convergence:
loss = self.problem.ip_output(self.g, self.g)
self.losses = torch.cat((self.losses, loss.detach().cpu().view(-1)))
self.g.requires_grad_(True)
# Get df/dx^t @ f0
self.dfdxt_g = TensorList(torch.autograd.grad(self.f0, self.x, self.g, create_graph=True))
# Get the right hand side
self.b = - self.dfdxt_g.detach()
# Run CG
delta_x, res = self.run_CG(num_cg_iter, eps=self.cg_eps)
self.x.detach_()
self.x += delta_x
if self.debug:
self.residuals = torch.cat((self.residuals, res))
def A(self, x):
dfdx_x = torch.autograd.grad(self.dfdxt_g, self.g, x, retain_graph=True)
return TensorList(torch.autograd.grad(self.f0, self.x, dfdx_x, retain_graph=True))
def ip(self, a, b):
return self.problem.ip_input(a, b)
def M1(self, x):
return self.problem.M1(x)
def M2(self, x):
return self.problem.M2(x)
def evaluate_CG_iteration(self, delta_x):
if self.analyze_convergence:
x = (self.x + delta_x).detach()
x.requires_grad_(True)
# compute loss and gradient
f = self.problem(x)
loss = self.problem.ip_output(f, f)
grad = TensorList(torch.autograd.grad(loss, x))
# store in the vectors
self.losses = torch.cat((self.losses, loss.detach().cpu().view(-1)))
self.gradient_mags = torch.cat((self.gradient_mags, sum(grad.view(-1) @ grad.view(-1)).cpu().sqrt().detach().view(-1)))
class GradientDescentL2:
"""Gradient descent with momentum for L2 problems."""
def __init__(self, problem: L2Problem, variable: TensorList, step_length: float, momentum: float = 0.0, debug = False, plotting = False, visdom=None):
self.problem = problem
self.x = variable
self.step_legnth = step_length
self.momentum = momentum
self.debug = debug or plotting
self.plotting = plotting
self.fig_num = (10,11)
self.visdom = visdom
self.losses = torch.zeros(0)
self.gradient_mags = torch.zeros(0)
self.residuals = None
self.clear_temp()
def clear_temp(self):
self.f0 = None
self.dir = None
def run(self, num_iter, dummy = None):
if num_iter == 0:
return
lossvec = None
if self.debug:
lossvec = torch.zeros(num_iter+1)
grad_mags = torch.zeros(num_iter+1)
for i in range(num_iter):
self.x.requires_grad_(True)
# Evaluate function at current estimate
self.f0 = self.problem(self.x)
# Compute loss
loss = self.problem.ip_output(self.f0, self.f0)
# Compute grad
grad = TensorList(torch.autograd.grad(loss, self.x))
# Update direction
if self.dir is None:
self.dir = grad
else:
self.dir = grad + self.momentum * self.dir
self.x.detach_()
self.x -= self.step_legnth * self.dir
if self.debug:
lossvec[i] = loss.item()
grad_mags[i] = sum(grad.view(-1) @ grad.view(-1)).sqrt().item()
if self.debug:
self.x.requires_grad_(True)
self.f0 = self.problem(self.x)
loss = self.problem.ip_output(self.f0, self.f0)
grad = TensorList(torch.autograd.grad(loss, self.x))
lossvec[-1] = self.problem.ip_output(self.f0, self.f0).item()
grad_mags[-1] = sum(grad.view(-1) @ grad.view(-1)).cpu().sqrt().item()
self.losses = torch.cat((self.losses, lossvec))
self.gradient_mags = torch.cat((self.gradient_mags, grad_mags))
if self.visdom is not None:
self.visdom.register(self.losses, 'lineplot', 3, 'Loss')
self.visdom.register(self.gradient_mags, 'lineplot', 4, 'Gradient magnitude')
elif self.plotting:
plot_graph(self.losses, self.fig_num[0], title='Loss')
plot_graph(self.gradient_mags, self.fig_num[1], title='Gradient magnitude')
self.x.detach_()
self.clear_temp()
class NewtonCG(ConjugateGradientBase):
"""Newton with Conjugate Gradient. Handels general minimization problems."""
def __init__(self, problem: MinimizationProblem, variable: TensorList, init_hessian_reg = 0.0, hessian_reg_factor = 1.0,
cg_eps = 0.0, fletcher_reeves = True, standard_alpha = True, direction_forget_factor = 0,
debug = False, analyze = False, plotting = False, fig_num=(10, 11, 12)):
super().__init__(fletcher_reeves, standard_alpha, direction_forget_factor, debug or analyze or plotting)
self.problem = problem
self.x = variable
self.analyze_convergence = analyze
self.plotting = plotting
self.fig_num = fig_num
self.hessian_reg = init_hessian_reg
self.hessian_reg_factor = hessian_reg_factor
self.cg_eps = cg_eps
self.f0 = None
self.g = None
self.residuals = torch.zeros(0)
self.losses = torch.zeros(0)
self.gradient_mags = torch.zeros(0)
def clear_temp(self):
self.f0 = None
self.g = None
def run(self, num_cg_iter, num_newton_iter=None):
if isinstance(num_cg_iter, int):
if num_cg_iter == 0:
return
if num_newton_iter is None:
num_newton_iter = 1
num_cg_iter = [num_cg_iter] * num_newton_iter
num_newton_iter = len(num_cg_iter)
if num_newton_iter == 0:
return
if self.analyze_convergence:
self.evaluate_CG_iteration(0)
for cg_iter in num_cg_iter:
self.run_newton_iter(cg_iter)
self.hessian_reg *= self.hessian_reg_factor
if self.debug:
if not self.analyze_convergence:
loss = self.problem(self.x)
self.losses = torch.cat((self.losses, loss.detach().cpu().view(-1)))
if self.plotting:
plot_graph(self.losses, self.fig_num[0], title='Loss')
plot_graph(self.residuals, self.fig_num[1], title='CG residuals')
if self.analyze_convergence:
plot_graph(self.gradient_mags, self.fig_num[2], 'Gradient magnitude')
self.x.detach_()
self.clear_temp()
return self.losses, self.residuals
def run_newton_iter(self, num_cg_iter):
self.x.requires_grad_(True)
# Evaluate function at current estimate
self.f0 = self.problem(self.x)
if self.debug and not self.analyze_convergence:
self.losses = torch.cat((self.losses, self.f0.detach().cpu().view(-1)))
# Gradient of loss
self.g = TensorList(torch.autograd.grad(self.f0, self.x, create_graph=True))
# Get the right hand side
self.b = - self.g.detach()
# Run CG
delta_x, res = self.run_CG(num_cg_iter, eps=self.cg_eps)
self.x.detach_()
self.x += delta_x
if self.debug:
self.residuals = torch.cat((self.residuals, res))
def A(self, x):
return TensorList(torch.autograd.grad(self.g, self.x, x, retain_graph=True)) + self.hessian_reg * x
def ip(self, a, b):
# Implements the inner product
return self.problem.ip_input(a, b)
def M1(self, x):
return self.problem.M1(x)
def M2(self, x):
return self.problem.M2(x)
def evaluate_CG_iteration(self, delta_x):
if self.analyze_convergence:
x = (self.x + delta_x).detach()
x.requires_grad_(True)
# compute loss and gradient
loss = self.problem(x)
grad = TensorList(torch.autograd.grad(loss, x))
# store in the vectors
self.losses = torch.cat((self.losses, loss.detach().cpu().view(-1)))
self.gradient_mags = torch.cat((self.gradient_mags, sum(grad.view(-1) @ grad.view(-1)).cpu().sqrt().detach().view(-1)))
class GradientDescent:
"""Gradient descent for general minimization problems."""
def __init__(self, problem: MinimizationProblem, variable: TensorList, step_length: float, momentum: float = 0.0,
debug = False, plotting = False, fig_num=(10,11)):
self.problem = problem
self.x = variable
self.step_legnth = step_length
self.momentum = momentum
self.debug = debug or plotting
self.plotting = plotting
self.fig_num = fig_num
self.losses = torch.zeros(0)
self.gradient_mags = torch.zeros(0)
self.residuals = None
self.clear_temp()
def clear_temp(self):
self.dir = None
def run(self, num_iter, dummy = None):
if num_iter == 0:
return
lossvec = None
if self.debug:
lossvec = torch.zeros(num_iter+1)
grad_mags = torch.zeros(num_iter+1)
for i in range(num_iter):
self.x.requires_grad_(True)
# Evaluate function at current estimate
loss = self.problem(self.x)
# Compute grad
grad = TensorList(torch.autograd.grad(loss, self.x))
# Update direction
if self.dir is None:
self.dir = grad
else:
self.dir = grad + self.momentum * self.dir
self.x.detach_()
self.x -= self.step_legnth * self.dir
if self.debug:
lossvec[i] = loss.item()
grad_mags[i] = sum(grad.view(-1) @ grad.view(-1)).sqrt().item()
if self.debug:
self.x.requires_grad_(True)
loss = self.problem(self.x)
grad = TensorList(torch.autograd.grad(loss, self.x))
lossvec[-1] = loss.item()
grad_mags[-1] = sum(grad.view(-1) @ grad.view(-1)).cpu().sqrt().item()
self.losses = torch.cat((self.losses, lossvec))
self.gradient_mags = torch.cat((self.gradient_mags, grad_mags))
if self.plotting:
plot_graph(self.losses, self.fig_num[0], title='Loss')
plot_graph(self.gradient_mags, self.fig_num[1], title='Gradient magnitude')
self.x.detach_()
self.clear_temp() |