From 664b52fe9c27ab4f1cf9fdf744b0b2b25836db51 Mon Sep 17 00:00:00 2001 From: Magi Feeney Date: Mon, 4 Jul 2022 15:28:33 +0800 Subject: [PATCH] Delete bb.py~ --- MagiOPT/algo/unconstrained/bb.py~ | 88 ------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 MagiOPT/algo/unconstrained/bb.py~ diff --git a/MagiOPT/algo/unconstrained/bb.py~ b/MagiOPT/algo/unconstrained/bb.py~ deleted file mode 100644 index 323328e..0000000 --- a/MagiOPT/algo/unconstrained/bb.py~ +++ /dev/null @@ -1,88 +0,0 @@ -from MagiOPT.linesearch import exact -from MagiOPT.utils import evaluate, criteria, isOutlier -from MagiOPT.visual import plot_surface, plot_contour -import torch - - -class Barzilai_Borwein: - def __init__(self, func, sc): - self.func = func - self.sc = sc - self.explorer = exact(func) - - def step(self, x0, epsilon=1e-5): - self.initSeq() - x0 = torch.as_tensor(x0, dtype=torch.float32) - if x0.dim() == 0: - x0 = x0.unsqueeze(0) - self.sequence.append(x0) - g = evaluate(self.func, x0) - d = -g - x1 = x0 + self.magnitude(x0, g) * d - isOutlier(x1) - - while criteria(self.func, x0, x1, epsilon, rule=self.sc): - cof = self.magnitude(x0, g, x1) - x0 = x1 - self.sequence.append(x0) - d = -evaluate(self.func, x0) - x1 = x0 + cof * d - isOutlier(x1) - self.sequence.append(x1) - return x1 - - def diff(self, x1, x0, g): - g1 = evaluate(self.func, x1) - s = x1 - x0 - y = g1 - g - return s, y - - def plot(self, info=None): - if torch.isnan(self.sequence).any() or torch.isinf(self.sequence).any(): - raise ValueError("Occur \"nan\" or \"inf\" value, cannot proceed.") - if self.sequence[0].shape[0] <= 2: - if info == None: - plot_surface(self.func, self.sequence) - else: - plot_surface(self.func, self.sequence, info) - if self.sequence[0].shape[0] == 2: - if info == None: - plot_contour(self.func, self.sequence) - else: - plot_contour(self.func, self.sequence, info) - - def initSeq(self): - self.sequence = [] - - -class BB1(Barzilai_Borwein): - def __init__(self, func, sc="gradNorm"): - super().__init__(func, sc) - - def magnitude(self, x0, g, x1=None): - if x1 != None: - s, y = self.diff(x1, x0, g) - sy = torch.dot(s, y) - yy = torch.dot(y, y) - if sy < 1e-12 or yy < 1e-12: - raise ValueError("Invalid step size, terminate prematurely.") - return sy / yy - else: - return self.explorer.search(x0, -evaluate(self.func, x0)) - - -class BB2(Barzilai_Borwein): - def __init__(self, func, sc="gradNorm"): - super().__init__(func, sc) - - def magnitude(self, x0, g, x1=None): - if x1 != None: - s, y = self.diff(x1, x0, g) - sy = torch.dot(s, y) - yy = torch.dot(y, y) - if sy < 1e-12 or yy < 1e-12: - raise ValueError("Invalid step size, terminate prematurely.") - return sy / yy - else: - return self.explorer.search(x0, -evaluate(self.func, x0)) -