File: optimize.py

package info (click to toggle)
python-bayespy 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,132 kB
  • sloc: python: 22,402; makefile: 156
file content (53 lines) | stat: -rw-r--r-- 1,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
################################################################################
# Copyright (C) 2011-2013 Jaakko Luttinen
#
# This file is licensed under the MIT License.
################################################################################


import numpy as np
from scipy import optimize


_epsilon = np.sqrt(np.finfo(float).eps)


def minimize(f, x0, maxiter=None, verbose=False):
    """
    Simple wrapper for SciPy's optimize.

    The given function must return a tuple: (value, gradient).
    """
    options = {'disp': verbose}
    if maxiter is not None:
        options['maxiter'] = maxiter
    opt = optimize.minimize(f, x0, jac=True, method='CG', options=options)
    return opt.x

def check_gradient(f, x0, verbose=True, epsilon=_epsilon, return_abserr=False):
    """
    Simple wrapper for SciPy's gradient checker.

    The given function must return a tuple: (value, gradient).

    Returns absolute and relative errors
    """
    df = f(x0)[1]
    df_num = optimize.approx_fprime(x0, 
                                    lambda x: f(x)[0], 
                                    epsilon)
    abserr = np.linalg.norm(df-df_num)
    norm_num = np.linalg.norm(df_num)
    if abserr == 0 and norm_num == 0:
        err = 0
    else:
        err = abserr / norm_num
    if verbose:
        print("Norm of numerical gradient: %g" % np.linalg.norm(df_num))
        print("Norm of function gradient:  %g" % np.linalg.norm(df))
        print("Gradient relative error = %g and absolute error = %g" % 
              (err,
               abserr))

    return (abserr, err)