Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def para_solver(L):
# Levenberg-Marquardt
# HT = H + (L-1)**2*np.diag(np.diag(H))
# Attempt to use plain Levenberg
HT = H + (L-1)**2*np.eye(len(H))
logger.debug("Inverting Scaled Hessian:\n")
logger.debug(" G:\n")
pvec1d(G,precision=5, loglevel=DEBUG)
logger.debug(" HT: (Scal = %.4f)\n" % (1+(L-1)**2))
pmat2d(HT,precision=5, loglevel=DEBUG)
Hi = invert_svd(HT)
dx = flat(-1 * np.dot(Hi, col(G)))
logger.debug(" dx:\n")
pvec1d(dx,precision=5, loglevel=DEBUG)
sol = flat(0.5*multi_dot([row(dx), H, col(dx)]))[0] + np.dot(dx,G)
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dx = np.insert(dx, i, 0)
return dx, sol
"""
from scipy import optimize
X, G, H = (data['X0'], data['G0'], data['H0']) if self.bhyp else (data['X'], data['G'], data['H'])
H1 = H.copy()
H1 = np.delete(H1, self.excision, axis=0)
H1 = np.delete(H1, self.excision, axis=1)
Eig = eig(H1)[0] # Diagonalize Hessian
Emin = min(Eig)
if Emin < self.eps: # Mix in SD step if Hessian minimum eigenvalue is negative
# Experiment.
Adj = max(self.eps, 0.01*abs(Emin)) - Emin
logger.info("Hessian has a small or negative eigenvalue (%.1e), mixing in some steepest descent (%.1e) to correct this.\n" % (Emin, Adj))
logger.info("Eigenvalues are:\n") ###
pvec1d(Eig) ###
H += Adj*np.eye(H.shape[0])
if self.bhyp:
G = np.delete(G, self.excision)
H = np.delete(H, self.excision, axis=0)
H = np.delete(H, self.excision, axis=1)
xkd = np.delete(xk, self.excision)
if self.Objective.Penalty.fmul != 0.0:
warn_press_key("Using the multiplicative hyperbolic penalty is discouraged!")
# This is the gradient and Hessian without the contributions from the hyperbolic constraint.
Obj0 = {'X':X,'G':G,'H':H}
class Hyper(object):
def __init__(self, HL, Penalty):
self.H = HL.copy()
self.dx = 1e10 * np.ones(len(HL),dtype=float)
self.Val = 0
H0 = H.copy()
G = np.delete(G, self.excision)
H = np.delete(H, self.excision, axis=0)
H = np.delete(H, self.excision, axis=1)
logger.debug("Inverting Hessian:\n") ###
logger.debug(" G:\n") ###
pvec1d(G,precision=5, loglevel=DEBUG) ###
logger.debug(" H:\n") ###
pmat2d(H,precision=5, loglevel=DEBUG) ###
Hi = invert_svd(np.mat(H))
dx = flat(-1 * Hi * col(G))
logger.debug(" dx:\n") ###
pvec1d(dx,precision=5, loglevel=DEBUG) ###
# dxa = -solve(H, G) # Take Newton Raphson Step ; use -1*G if want steepest descent.
# dxa = flat(dxa)
# print " dxa:" ###
# pvec1d(dxa,precision=5) ###
logger.info('\n') ###
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dx = np.insert(dx, i, 0)
def para_solver(L):
# Levenberg-Marquardt
# HT = H + (L-1)**2*np.diag(np.diag(H))
# Attempt to use plain Levenberg
HT = H + (L-1)**2*np.eye(len(H))
logger.debug("Inverting Scaled Hessian:\n") ###
logger.debug(" G:\n") ###
pvec1d(G,precision=5, loglevel=DEBUG) ###
def para_solver(L):
# Levenberg-Marquardt
# HT = H + (L-1)**2*np.diag(np.diag(H))
# Attempt to use plain Levenberg
HT = H + (L-1)**2*np.eye(len(H))
logger.debug("Inverting Scaled Hessian:\n") ###
logger.debug(" G:\n") ###
pvec1d(G,precision=5, loglevel=DEBUG) ###
logger.debug(" HT: (Scal = %.4f)\n" % (1+(L-1)**2)) ###
pmat2d(HT,precision=5, loglevel=DEBUG) ###
Hi = invert_svd(np.mat(HT))
dx = flat(-1 * Hi * col(G))
logger.debug(" dx:\n") ###
pvec1d(dx,precision=5, loglevel=DEBUG) ###
# dxa = -solve(HT, G)
# dxa = flat(dxa)
# print " dxa:" ###
# pvec1d(dxa,precision=5) ###
# print ###
sol = flat(0.5*row(dx)*np.mat(H)*col(dx))[0] + np.dot(dx,G)
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dx = np.insert(dx, i, 0)
return dx, sol
dx2, sol2 = Opt2[0], Opt2[1]
dxb, sol = (dx1, sol1) if sol1 <= sol2 else (dx2, sol2)
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dxb = np.insert(dxb, i, 0)
return dxb, sol
else:
# G0 and H0 are used for determining the expected function change.
G0 = G.copy()
H0 = H.copy()
G = np.delete(G, self.excision)
H = np.delete(H, self.excision, axis=0)
H = np.delete(H, self.excision, axis=1)
logger.debug("Inverting Hessian:\n") ###
logger.debug(" G:\n") ###
pvec1d(G,precision=5, loglevel=DEBUG) ###
logger.debug(" H:\n") ###
pmat2d(H,precision=5, loglevel=DEBUG) ###
Hi = invert_svd(np.mat(H))
dx = flat(-1 * Hi * col(G))
logger.debug(" dx:\n") ###
pvec1d(dx,precision=5, loglevel=DEBUG) ###
# dxa = -solve(H, G) # Take Newton Raphson Step ; use -1*G if want steepest descent.
# dxa = flat(dxa)
# print " dxa:" ###
# pvec1d(dxa,precision=5) ###
logger.info('\n') ###
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dx = np.insert(dx, i, 0)
H0 = H.copy()
G = np.delete(G, self.excision)
H = np.delete(H, self.excision, axis=0)
H = np.delete(H, self.excision, axis=1)
logger.debug("Inverting Hessian:\n")
logger.debug(" G:\n")
pvec1d(G,precision=5, loglevel=DEBUG)
logger.debug(" H:\n")
pmat2d(H,precision=5, loglevel=DEBUG)
Hi = invert_svd(H)
dx = flat(-1 * np.dot(Hi, col(G)))
logger.debug(" dx:\n")
pvec1d(dx,precision=5, loglevel=DEBUG)
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dx = np.insert(dx, i, 0)
def para_solver(L):
# Levenberg-Marquardt
# HT = H + (L-1)**2*np.diag(np.diag(H))
# Attempt to use plain Levenberg
HT = H + (L-1)**2*np.eye(len(H))
logger.debug("Inverting Scaled Hessian:\n")
logger.debug(" G:\n")
pvec1d(G,precision=5, loglevel=DEBUG)
logger.debug(" HT: (Scal = %.4f)\n" % (1+(L-1)**2))
pmat2d(HT,precision=5, loglevel=DEBUG)
Hi = invert_svd(HT)
dx = flat(-1 * np.dot(Hi, col(G)))
def para_solver(L):
# Levenberg-Marquardt
# HT = H + (L-1)**2*np.diag(np.diag(H))
# Attempt to use plain Levenberg
HT = H + (L-1)**2*np.eye(len(H))
logger.debug("Inverting Scaled Hessian:\n")
logger.debug(" G:\n")
pvec1d(G,precision=5, loglevel=DEBUG)
logger.debug(" HT: (Scal = %.4f)\n" % (1+(L-1)**2))
pmat2d(HT,precision=5, loglevel=DEBUG)
Hi = invert_svd(HT)
dx = flat(-1 * np.dot(Hi, col(G)))
logger.debug(" dx:\n")
pvec1d(dx,precision=5, loglevel=DEBUG)
sol = flat(0.5*multi_dot([row(dx), H, col(dx)]))[0] + np.dot(dx,G)
for i in self.excision: # Reinsert deleted coordinates - don't take a step in those directions
dx = np.insert(dx, i, 0)
return dx, sol