Search in sources :

Example 1 with HessianWrtParameterProvider

use of dr.inference.hmc.HessianWrtParameterProvider in project beast-mcmc by beast-dev.

the class HamiltonianMonteCarloOperatorParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    int nSteps = xo.getAttribute(N_STEPS, 10);
    double stepSize = xo.getDoubleAttribute(STEP_SIZE);
    int runMode = parseRunMode(xo);
    MassPreconditioner.Type preconditioningType = parsePreconditioning(xo);
    double randomStepFraction = Math.abs(xo.getAttribute(RANDOM_STEP_FRACTION, 0.0));
    if (randomStepFraction > 1) {
        throw new XMLParseException("Random step count fraction must be < 1.0");
    }
    int preconditioningUpdateFrequency = xo.getAttribute(PRECONDITIONING_UPDATE_FREQUENCY, 0);
    int preconditioningDelay = xo.getAttribute(PRECONDITIONING_DELAY, 0);
    int preconditioningMemory = xo.getAttribute(PRECONDITIONING_MEMORY, 0);
    AdaptationMode adaptationMode = AdaptationMode.parseMode(xo);
    GradientWrtParameterProvider derivative = (GradientWrtParameterProvider) xo.getChild(GradientWrtParameterProvider.class);
    if (preconditioningType != MassPreconditioner.Type.NONE && !(derivative instanceof HessianWrtParameterProvider)) {
        throw new XMLParseException("Unable precondition without a Hessian provider");
    }
    Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    if (parameter == null) {
        parameter = derivative.getParameter();
    }
    Transform transform = parseTransform(xo);
    boolean dimensionMismatch = derivative.getDimension() != parameter.getDimension();
    if (transform != null && transform instanceof Transform.MultivariableTransform) {
        dimensionMismatch = ((Transform.MultivariableTransform) transform).getDimension() != parameter.getDimension();
    }
    if (dimensionMismatch) {
        throw new XMLParseException("Gradient (" + derivative.getDimension() + ") must be the same dimensions as the parameter (" + parameter.getDimension() + ")");
    }
    Parameter mask = null;
    if (xo.hasChildNamed(MASK)) {
        mask = (Parameter) xo.getElementFirstChild(MASK);
        if (mask.getDimension() != derivative.getDimension()) {
            throw new XMLParseException("Mask (" + mask.getDimension() + ") must be the same dimension as the gradient (" + derivative.getDimension() + ")");
        }
    }
    int gradientCheckCount = xo.getAttribute(GRADIENT_CHECK_COUNT, 0);
    double gradientCheckTolerance = xo.getAttribute(GRADIENT_CHECK_TOLERANCE, 1E-3);
    int maxIterations = xo.getAttribute(MAX_ITERATIONS, 10);
    double reductionFactor = xo.getAttribute(REDUCTION_FACTOR, 0.1);
    double targetAcceptanceProbability = xo.getAttribute(TARGET_ACCEPTANCE_PROBABILITY, // Stan default
    0.8);
    HamiltonianMonteCarloOperator.Options runtimeOptions = new HamiltonianMonteCarloOperator.Options(stepSize, nSteps, randomStepFraction, preconditioningUpdateFrequency, preconditioningDelay, preconditioningMemory, gradientCheckCount, gradientCheckTolerance, maxIterations, reductionFactor, targetAcceptanceProbability);
    return factory(adaptationMode, weight, derivative, parameter, transform, mask, runtimeOptions, preconditioningType, runMode);
}
Also used : HessianWrtParameterProvider(dr.inference.hmc.HessianWrtParameterProvider) MassPreconditioner(dr.inference.operators.hmc.MassPreconditioner) AdaptationMode(dr.inference.operators.AdaptationMode) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) Parameter(dr.inference.model.Parameter) Transform(dr.util.Transform) Util.parseTransform(dr.util.Transform.Util.parseTransform) HamiltonianMonteCarloOperator(dr.inference.operators.hmc.HamiltonianMonteCarloOperator)

Aggregations

GradientWrtParameterProvider (dr.inference.hmc.GradientWrtParameterProvider)1 HessianWrtParameterProvider (dr.inference.hmc.HessianWrtParameterProvider)1 Parameter (dr.inference.model.Parameter)1 AdaptationMode (dr.inference.operators.AdaptationMode)1 HamiltonianMonteCarloOperator (dr.inference.operators.hmc.HamiltonianMonteCarloOperator)1 MassPreconditioner (dr.inference.operators.hmc.MassPreconditioner)1 Transform (dr.util.Transform)1 Util.parseTransform (dr.util.Transform.Util.parseTransform)1