Search in sources :

Example 1 with ExactBPExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver in project aic-praise by aic-sri-international.

the class RegularSymbolicParameterEstimation method buildGradient.

private Vector<Expression> buildGradient(FeatureBasedModel featureBasedModel) {
    Vector<Expression> gradient = new Vector<Expression>();
    for (Expression condition : featureBasedModel.mapConditionToWeight.keySet()) {
        Expression parameter = featureBasedModel.mapConditionToWeight.get(condition);
        System.out.println("parameter : " + parameter);
        ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
        Expression marginal = solver.solve(condition, expressionBasedModel);
        Expression probabilityOfCondition = convertExpression(marginal);
        Expression probabilityOfConditionWithSigmoidTrick = applySigmoidTrick(probabilityOfCondition, expressionBasedModel.getContext());
        System.out.println("probabilityOfConditionWithSigmoidTrick : " + probabilityOfCondition);
        int numberOfOccurences = countOccurencesGivenParameter(parameter);
        int numberOfEvidences = evidences.size();
        Expression secondTerm = apply(DIVISION, makeSymbol(numberOfOccurences), makeSymbol(numberOfEvidences));
        Expression ithTermOfgradient = apply(MINUS, probabilityOfConditionWithSigmoidTrick, secondTerm);
        System.out.println(ithTermOfgradient);
        gradient.add(ithTermOfgradient);
    }
    return gradient;
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) Vector(java.util.Vector)

Example 2 with ExactBPExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver in project aic-praise by aic-sri-international.

the class ExpressionBasedSolverTest method runTest.

private void runTest(Expression queryExpression, ExpressionBasedModel model, Expression expected, boolean useFactorization) throws AssertionError {
    ExpressionBasedProblem problem = new DefaultExpressionBasedProblem(queryExpression, model);
    ExpressionBasedSolver[] solvers = new ExpressionBasedSolver[] { // new EvaluationExpressionBasedSolver(useFactorization),
    new ExactBPExpressionBasedSolver() };
    for (ExpressionBasedSolver solver : solvers) {
        Expression marginal = solver.solve(problem);
        checkResult(problem, expected, marginal, solver);
    }
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) EvaluationExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)

Example 3 with ExactBPExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver in project aic-praise by aic-sri-international.

the class ParameterEstimationForExpressionBasedModel method getMarginalBeforeTransformation.

/**
 * Return the "raw" marginal, before log and sigmoid transformation.
 */
private Expression getMarginalBeforeTransformation(Entry<Pair<Expression, Expression>, Integer> entry) {
    Pair<Expression, Expression> pairEvidenceQuery = entry.getKey();
    ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
    Expression query = pairEvidenceQuery.first;
    Expression evidence = pairEvidenceQuery.second;
    ExpressionBasedModel conditionedModel = model.getConditionedModel(evidence);
    Expression marginal = solver.solve(query, conditionedModel);
    return marginal;
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)

Example 4 with ExactBPExpressionBasedSolver

use of com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver in project aic-praise by aic-sri-international.

the class GradientLoglikelihoodToOptimize method buildGradient.

/**
 * Build the gradient following the formula described in "Graphical Models in a Nutshell".
 */
private Vector<Expression> buildGradient(Map<Expression, Double> mapParametersToValue) {
    Vector<Expression> gradient = new Vector<Expression>();
    for (Expression condition : featureBasedModel.mapConditionToWeight.keySet()) {
        Expression parameter = featureBasedModel.mapConditionToWeight.get(condition);
        ExpressionBasedSolver solver = new ExactBPExpressionBasedSolver();
        ExpressionBasedModel newExpressionBasedModel = UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel(mapParametersToValue, expressionBasedModel);
        Expression marginal = solver.solve(condition, newExpressionBasedModel);
        // System.out.println("marginal : " + marginal);
        Expression probabilityOfConditionWithSigmoidTrick = applySigmoid(marginal, expressionBasedModel.getContext());
        // System.out.println("probabilityOfConditionWithSigmoidTrick : " + probabilityOfConditionWithSigmoidTrick);
        probabilityOfConditionWithSigmoidTrick = convertExpression(probabilityOfConditionWithSigmoidTrick);
        // System.out.println("probabilityOfConditionWithSigmoidTrick after conversion : " + probabilityOfConditionWithSigmoidTrick);
        int numberOfOccurences = countOccurencesGivenParameter(parameter);
        int numberOfEvidences = evidences.size();
        Expression secondTerm = apply(DIVISION, makeSymbol(numberOfOccurences), makeSymbol(numberOfEvidences));
        Expression ithTermOfgradient = apply(MINUS, probabilityOfConditionWithSigmoidTrick, secondTerm);
        gradient.add(ithTermOfgradient);
    }
    return gradient;
}
Also used : ExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) ExactBPExpressionBasedSolver(com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver) Expression(com.sri.ai.expresso.api.Expression) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) Vector(java.util.Vector)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)4 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)4 ExactBPExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.exactbp.ExactBPExpressionBasedSolver)4 ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)2 Vector (java.util.Vector)2 EvaluationExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.core.byalgorithm.evaluation.EvaluationExpressionBasedSolver)1 ExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem)1 DefaultExpressionBasedProblem (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedProblem)1