示例#1
0
# %%
# Evaluate the hessian of the function at a particular point
hessianMatrix = f.hessian(x)
hessianMatrix

# %%
# Change the gradient method to a non centered finite difference method
step = [1e-7] * f.getInputDimension()
gradient = ot.NonCenteredFiniteDifferenceGradient(step, f.getEvaluation())
f.setGradient(gradient)
gradient

# %%
# Change the hessian method to a centered finite difference method
step = [1e-7] * f.getInputDimension()
hessian = ot.CenteredFiniteDifferenceHessian(step, f.getEvaluation())
f.setHessian(hessian)
hessian

# %%
# Get the number of times the function has been evaluated
f.getEvaluationCallsNumber()

# %%
# Get the number of times the gradient has been evaluated
f.getGradientCallsNumber()

# %%
# Get the number of times the hessian has been evaluated
f.getHessianCallsNumber()
    x1, x2, x3, x4 = X
    return [np.cos(x2 * x2 + x4) / (x1 * x1 + 1. + x3**4)]


myFunc = ot.PythonFunction(4, 1, myPythonFunction)

# %%
# For instance, a user-defined constant step value can be considered
gradEpsilon = [1e-8] * 4
hessianEpsilon = [1e-7] * 4
gradStep = ot.ConstantStep(gradEpsilon)  # Costant gradient step
hessianStep = ot.ConstantStep(hessianEpsilon)  # Constant Hessian step
myFunc.setGradient(
    ot.CenteredFiniteDifferenceGradient(gradStep, myFunc.getEvaluation()))
myFunc.setHessian(
    ot.CenteredFiniteDifferenceHessian(hessianStep, myFunc.getEvaluation()))

# %%
# Alternatively, we can consider a finite difference step value which
# depends on the location in the input space by relying on the BlendedStep class:
gradEpsilon = [1e-8] * 4
hessianEpsilon = [1e-7] * 4
gradStep = ot.BlendedStep(gradEpsilon)  # Costant gradient step
hessianStep = ot.BlendedStep(hessianEpsilon)  # Constant Hessian step
myFunc.setGradient(
    ot.CenteredFiniteDifferenceGradient(gradStep, myFunc.getEvaluation()))
myFunc.setHessian(
    ot.CenteredFiniteDifferenceHessian(hessianStep, myFunc.getEvaluation()))

# %%
# We can then proceed in the same way as before
示例#3
0
        print('df(', x[0], ')=%.4e' % df, 'df (FD)=%.4e' % df2)
        if abs(df) > 1e-5:
            err_g = abs(df2 / df - 1.)
        else:
            err_g = abs(df - df2)
        if err_g > 1e-5:
            print('GRADIENT ERROR! check ' + func +
                  ' gradient, err=%.12g' % err_g)
    try:
        d2f = f.hessian(x)[0, 0, 0]
    except:
        pass
    else:
        f.setHessian(
            ot.CenteredFiniteDifferenceHessian(
                ot.ResourceMap.GetAsScalar(
                    'CenteredFiniteDifferenceHessian-DefaultEpsilon'),
                f.getEvaluation()))
        d2f2 = f.hessian(x)[0, 0, 0]
        print('d2f(', x[0], ')=%.4e' % d2f, 'd2f (FD)=%.4e' % d2f2)
        if abs(d2f) > 1e-5:
            err_h = abs(d2f2 / d2f - 1.)
        else:
            err_h = abs(d2f - d2f2)
        if err_h > 1e-4:
            print('HESSIAN ERROR! check ' + func +
                  ' hessian, err=%.12g' % err_h)

nmf = ot.SymbolicFunction(['x0', 'x1'], ['x0+x1', 'x0-x1'])
marginal0 = nmf.getMarginal(0)
marginal1 = nmf.getMarginal(1)
print('marginal 0=', marginal0)
示例#4
0
#! /usr/bin/env python

from __future__ import print_function
import openturns as ot

eps = 1e-2
# Instance creation
myFunc = ot.NumericalMathFunction(['x1', 'x2'], ['f1', 'f2', 'f3'], [
                                  'x1*sin(x2)', 'cos(x1+x2)', '(x2+1)*exp(x1-2*x2)'])
epsilon = ot.NumericalPoint(myFunc.getInputDimension(), eps)
inPoint = ot.NumericalPoint(epsilon.getDimension(), 1.0)
myHessian = ot.CenteredFiniteDifferenceHessian(
    epsilon, myFunc.getEvaluation())

print("myHessian=", repr(myHessian))
print("myFunc.hessian(", repr(inPoint), ")=",
      repr(myFunc.hessian(inPoint)))
print("myHessian.hessian(", repr(inPoint), ")=",
      repr(myHessian.hessian(inPoint)))

# Substitute the hessian
myFunc.setHessian(ot.CenteredFiniteDifferenceHessian(myHessian))
print("myFunc.hessian(", repr(inPoint), ")=", repr(
    myFunc.hessian(inPoint)), " (after substitution)")