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;
}
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);
}
}
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;
}
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;
}
Aggregations