来自Scipy.minimise(Python)的回调梯度标准

发布于 2025-01-26 04:59:16 字数 427 浏览 4 评论 0原文

我使用'cg'方法使用scipy.minimize,我想在每次迭代中调用梯度标准。到目前为止,我已经能够在每次迭代中回电:

def min_method(fn, grad, x0):
    all_fn = [fn(x0).item()]
    def store(x): # callback function
    all_fn.append(fn(x).item())

    ans = minimize(fn, x0, method='CG', jac=grad, callback=store,
           options={'disp':True,'gtol': 1e-06})
    return ans, all_fn

如何将行添加到store()函数以在每次迭代中获取梯度标准?

I am using scipy.minimize with the 'CG' method and I want to callback the gradient norm at each iteration. So far, I have been able to call back the function at each iteration using this:

def min_method(fn, grad, x0):
    all_fn = [fn(x0).item()]
    def store(x): # callback function
    all_fn.append(fn(x).item())

    ans = minimize(fn, x0, method='CG', jac=grad, callback=store,
           options={'disp':True,'gtol': 1e-06})
    return ans, all_fn

How can I add a line to the store() function in order to get the gradient norm at each iteration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2025-02-02 04:59:16

您可以使用aid_fprime函数。 如果您想要“ CG”的Jacobian,则可能是以下内容:

import numpy as np
from scipy.optimize import minimize
from scipy.optimize.optimize import approx_fprime


def f_(x):   # The rosenbrock function
    return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2



def min_method(fn, grad, x0):
    all_fn = [fn(x0).item(),0]

    def store(x, *_):  # callback function
        aprox = approx_fprime(x, fn, 1E-8)
        all_fn.append([fn(x).item(),aprox])


    ans = minimize(fn, x0, method='CG', jac=grad, callback=store,
                   options={'disp': True, 'gtol': 1e-06})
    return ans, all_fn


x0 = np.array([2, 3], dtype=np.double)
ans, all_fn =min_method(f_, '2-point',x0)
print(ans)
print(all_fn)

## Update

,我需要修改通过scipy.optimize.minimize调用的函数。

_minimize.py中转到函数最小化

def minimize(fun, x0, args=(), method=None, jac=None, hess=None,
             hessp=None, bounds=None, constraints=(), tol=None,
             callback=None, options=None):

找到“ CG” Methode Line 673 _minimize_cg _minimize_cg

elif meth == 'cg':
    res = _minimize_cg(fun, x0, args, jac, callback, **options)

,然后在emotizize.py 在第1681行周围替换

    if callback is not None:
        callback(xk)

    if callback is not None: 
        callback(xk,gfk,gnorm)

然后您可以访问回调函数中的jacobian

import numpy as np
from scipy.optimize import minimize

def f_(x):   # The rosenbrock function
    return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2


def min_method(fn, grad, x0):
    all_fn = [[fn(x0).item(),0]]

    def store(x, gfk,gnorm):  # callback function
        all_fn.append([fn(x).item(),gfk,gnorm])

    ans = minimize(fn, x0, method='CG', jac=grad, callback=store,
                   options={'disp': True, 'gtol': 1e-06})
    return ans, all_fn


x0 = np.array([2, 3], dtype=np.double)
ans, all_fn =min_method(f_, '2-point',x0)
print(ans)
print(all_fn)

You could use the approx_fprime function. It could be something like:

import numpy as np
from scipy.optimize import minimize
from scipy.optimize.optimize import approx_fprime


def f_(x):   # The rosenbrock function
    return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2



def min_method(fn, grad, x0):
    all_fn = [fn(x0).item(),0]

    def store(x, *_):  # callback function
        aprox = approx_fprime(x, fn, 1E-8)
        all_fn.append([fn(x).item(),aprox])


    ans = minimize(fn, x0, method='CG', jac=grad, callback=store,
                   options={'disp': True, 'gtol': 1e-06})
    return ans, all_fn


x0 = np.array([2, 3], dtype=np.double)
ans, all_fn =min_method(f_, '2-point',x0)
print(ans)
print(all_fn)

##Update

If you want the jacobian from the 'CG', I thing you need to modify the function that is called through the scipy.optimize.minimize.

in _minimize.py go to the function minimize

def minimize(fun, x0, args=(), method=None, jac=None, hess=None,
             hessp=None, bounds=None, constraints=(), tol=None,
             callback=None, options=None):

then find the call to the 'CG' methode line 673 _minimize_cg

elif meth == 'cg':
    res = _minimize_cg(fun, x0, args, jac, callback, **options)

in that function in the optimize.py around line 1681 replace

    if callback is not None:
        callback(xk)

with

    if callback is not None: 
        callback(xk,gfk,gnorm)

then you can have access to the jacobian in the callback function

import numpy as np
from scipy.optimize import minimize

def f_(x):   # The rosenbrock function
    return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2


def min_method(fn, grad, x0):
    all_fn = [[fn(x0).item(),0]]

    def store(x, gfk,gnorm):  # callback function
        all_fn.append([fn(x).item(),gfk,gnorm])

    ans = minimize(fn, x0, method='CG', jac=grad, callback=store,
                   options={'disp': True, 'gtol': 1e-06})
    return ans, all_fn


x0 = np.array([2, 3], dtype=np.double)
ans, all_fn =min_method(f_, '2-point',x0)
print(ans)
print(all_fn)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文