本文整理汇总了Python中Math.abs方法的典型用法代码示例。如果您正苦于以下问题:Python Math.abs方法的具体用法?Python Math.abs怎么用?Python Math.abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Math
的用法示例。
在下文中一共展示了Math.abs方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: proximal_descent
# 需要导入模块: import Math [as 别名]
# 或者: from Math import abs [as 别名]
def proximal_descent(g, g_prime, h_prox, x0, iterations = 1000, gamma = 1.0, epsilon = 1e-4):
"""
minimizes a non-differentiable function f(x) = g(x) + h(x)
PARAMS
g: function
g(x), the differentiable part of f
g_prime: function
g'(x) aka the gradient of g
returns the direction of steepest increase along g
h_prox: function
h_prox(x, gamma) returns proximal operator of h at x using gamma as a distance weighting param
h_prox gives a new x' which is a tradeoff of reducing h and staying close to x
x0: vector
initial stariting point
iterations: self explanitory
gamma: step size
epsilon: self explanitory
RETURNS
x* = argmin_x { f(x) } if x* is reachable in the given num iterations. else None
"""
# initialize current guess at x0
xk = x0
gk = g(xk)
for _ in range(iterations):
xk_old = xk
# compute gradient for differentiable part of f
gk_gradient = g_prime(xk)
# take gradient step to reduce g(x)
xk_gradient = xk - gamma * gk_gradient
# proximal update to reduce h(x) but stay close to xk_gradient
xk = h_prox(xk_gradient, gamma)
if Math.abs(xk - xk_old) < epsilon:
return xk
return None