为scipy.optimize.fmin(及其他)设置收敛标准 [英] Setting convergence criteria for scipy.optimize.fmin (and others)

查看:233
本文介绍了为scipy.optimize.fmin(及其他)设置收敛标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一项优化任务,其中成本函数评估非常昂贵,并且可以容忍一些错误.我正在使用scipy.optimize中的一些预包装scipy方法来开始使用.我正在使用的第一个是

I am working on an optimization task where cost function evaluations are very expensive, and some error can be tolerated. I'm using some pre-packaged scipy methods from scipy.optimize to get started. The first one I'm working with is fmin, which implements the nelder mead simplex algorithm.

此函数具有两个与收敛有关的参数xtol和ftol,其中(

This function has two convergence related parameters xtol and ftol, which (as I understand it) specifiy a convergence criteria where if x or f (the parameter set, and the cost respectively) change by less than xtol or ftol on an iteration, the function returns.

但是,由于成本函数对我而言是如此昂贵,因此我还希望能够指定一个成本阈值,如果找到一个成本低于阈值的点,它将立即返回.

However, since cost functions are so expensive for me, I want to also be able to specify a cost threshold where it will return immediately if it finds a point with less cost than the threshold.

是否可以为scipy.optimize.fmin指定此阈值?

Is it possible to specify this threshold for scipy.optimize.fmin?

奖金问题:我还没有详细介绍许多其他方法,但是看起来这两个方法都不存在此阈值选项.这是scipy优化方法的典型代表吗?尝试贡献此功能对我来说有价值吗?

Bonus question: I haven't looked in detail at many other methods, but it doesn't look like this threshold option exists for those either. Is this typical for scipy optimization methods? Would it be valuable for me to try to contribute this functionality?

推荐答案

对于可以表示为x的函数的任何条件,都可以停止迭代.这里的想法是劫持callback方法并将异常用于流控制.以下是利用这一思想的两种解决方案:

It is possible to stop the iteration for any criterion that can be expressed as a function of x. The idea here is to hijack the callback method and use exceptions for flow control. Here are two solutions that exploit this idea:

from scipy.optimize import fmin_bfgs
import numpy as np
f = lambda x: np.linalg.norm(x**2)
x0 = np.random.rand(100)

解决方案1:

global result

class SmallEnoughException(Exception):
    pass

def check_conv_criteria(xk):
    global result
    if np.linalg.norm(xk) < 0.1:
        result = xk
        raise SmallEnoughException()

try:
    x, _, _ = fmin_bfgs(f, x0, callback=check_conv_criteria)
except SmallEnoughException:
    x = result

解决方案2:

class StopOptimizingException(Exception):
    pass

class CallbackCollector:

    def __init__(self, f, thresh):
        self._f  = f
        self._thresh = thresh

    def __call__(self, xk):
        if self._f(xk) < self._thresh:
            self.x_opt = xk
            raise StopOptimizingException()

try:
    cb = CallbackCollector(f, thresh=0.2)
    x, _, _ = fmin_bfgs(f, x0, callback=cb)
except StopOptimizingException:
    x = cb.x_opt

这篇关于为scipy.optimize.fmin(及其他)设置收敛标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆