本文整理汇总了Python中scipy.optimize.OptimizeResult.tau_max方法的典型用法代码示例。如果您正苦于以下问题:Python OptimizeResult.tau_max方法的具体用法?Python OptimizeResult.tau_max怎么用?Python OptimizeResult.tau_max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.optimize.OptimizeResult
的用法示例。
在下文中一共展示了OptimizeResult.tau_max方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: optimize_stiefel
# 需要导入模块: from scipy.optimize import OptimizeResult [as 别名]
# 或者: from scipy.optimize.OptimizeResult import tau_max [as 别名]
def optimize_stiefel(func, X0, args=(), tau_max=.5, max_it=1, tol=1e-6,
disp=False, tau_find_freq=100):
"""
Optimize a function over a Stiefel manifold.
:param func: Function to be optimized
:param X0: Initial point for line search
:param tau_max: Maximum step size
:param max_it: Maximum number of iterations
:param tol: Tolerance criteria to terminate line search
:param disp: Choose whether to display output
:param args: Extra arguments passed to the function
"""
tol = float(tol)
assert tol > 0, 'Tolerance must be positive'
max_it = int(max_it)
assert max_it > 0, 'The maximum number of iterations must be a positive '\
+ 'integer'
tau_max = float(tau_max)
assert tau_max > 0, 'The parameter `tau_max` must be positive.'
k = 0
X = X0.copy()
nit = 0
nfev = 0
success = False
if disp:
print 'Stiefel Optimization'.center(80)
print '{0:4s} {1:11s} {2:5s}'.format('It', 'F', '(F - F_old) / F_old')
print '-' * 30
ls_func = LSFunc()
ls_func.func = func
decrease_tau = False
tau_max0 = tau_max
while nit <= max_it:
nit += 1
F, G = func(X, *args)
F_old = F
nfev += 1
A = compute_A(G, X)
ls_func.A = A
ls_func.X = X
ls_func.func_args = args
ls_func.tau_max = tau_max
increased_tau = False
if nit == 1 or decrease_tau or nit % tau_find_freq == 0:
# Need to minimize ls_func with respect to each argument
tau_init = np.linspace(-10, 0., 3)[:, None]
tau_d = np.linspace(-10, 0., 50)[:, None]
tau_all, F_all = pybgo.minimize(ls_func, tau_init, tau_d, fixed_noise=1e-16,
add_at_least=1, tol=1e-2, scale=True,
train_every=1)[:2]
nfev += tau_all.shape[0]
idx = np.argmin(F_all)
tau = np.exp(tau_all[idx, 0]) * tau_max
if tau_max - tau <= 1e-6:
tau_max = 1.2 * tau_max
if disp:
print 'increasing tau_max to {0:1.5e}'.format(tau_max)
increased_tau = True
if decrease_tau:
tau_max = .8 * tau_max
if disp:
print 'decreasing max_tau to {0:1.5e}'.format(tau_max)
decrease_tau = False
F = F_all[idx, 0]
else:
F = ls_func([np.log(tau / tau_max)])
delta_F = (F_old - F) / np.abs(F_old)
if delta_F < 0:
if disp:
print '*** backtracking'
nit -= 1
decrease_tau = True
continue
X_old = X
X = Y_func(tau, X, A)
if disp:
print '{0:4s} {1:1.5e} {2:5e} tau = {3:1.3e}, tau_max = {4:1.3e}'.format(
str(nit).zfill(4), F, delta_F, tau, tau_max)
if delta_F <= tol:
if disp:
print '*** Converged ***'
success = True
break
res = OptimizeResult()
res.tau_max = tau_max
res.X = X
res.nfev = nfev
res.nit = nit
res.fun = F
res.success = success
return res