Search in sources :

Example 31 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class UAIModelToExpressionFactorNetwork method convert.

public static ExpressionFactorNetwork convert(UAIModel uaiModel, Theory theory) {
    List<Expression> factorsRepresentedAsExpressions = createListOfExpressionsrepresentingTheFactorsFromAUAIModel(uaiModel);
    if (theory == null) {
        theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new LinearRealArithmeticTheory(false, true), new PropositionalTheory());
    }
    // Add variables in the factors to the context...
    ExpressionBasedModel factorsAndTypes = new UAI_to_ExpressionBased_Translator(factorsRepresentedAsExpressions, uaiModel);
    // Context
    Context context = fillingContext(theory, factorsAndTypes);
    ExpressionFactorNetwork result = expressionFactorNetwork(factorsRepresentedAsExpressions, context);
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) UAI_to_ExpressionBased_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_ExpressionBased_Translator)

Example 32 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel in project aic-praise by aic-sri-international.

the class ExpressionBayesianModel method convertToAnExpressionBasedModelAfterLearning.

/**
 * Converting the learned ExpressionBayesianModel into an ExpressionBasedModel, ready for inferences.
 * Some assumptions: there are no constants in the initial expressions of the nodes (would result in errors when learning the parameters)
 *
 * @param mapFromCategoricalTypeNameToSizeString (the user specifies the categorical types used, usually an empty map)
 * @param additionalTypes (the user specifies the additional types used, usually an empty list)
 *
 * @return the equivalent ExpressionBasedModel for inference
 */
public ExpressionBasedModel convertToAnExpressionBasedModelAfterLearning(Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes) {
    // The nodes of this Bayesian model
    List<? extends Expression> factors = this.getNodes();
    // The definitions of variables
    Map<String, String> mapFromRandomVariableNameToTypeName = map();
    for (ExpressionBayesianNode node : this.getNodes()) {
        ExpressionVariable nodeVariable = node.getChildVariable();
        Context context = node.getContext();
        Type type = context.getTypeOfRegisteredSymbol(nodeVariable);
        mapFromRandomVariableNameToTypeName.put(nodeVariable.toString(), type.toString());
    }
    // The definitions of non-uniquely named constants
    // (we assume that there are no constants in the initial expressions of the nodes - would result in errors when learning)
    Map<String, String> mapFromNonUniquelyNamedConstantNameToTypeName = map();
    // The definitions of uniquely named constants
    // (similar comment as above)
    Map<String, String> mapFromUniquelyNamedConstantNameToTypeName = map();
    // ExpressionBayesianModels are known to be Bayesian models
    boolean isBayesianNetwork = true;
    ExpressionBasedModel convertedModel = new DefaultExpressionBasedModel(factors, mapFromRandomVariableNameToTypeName, mapFromNonUniquelyNamedConstantNameToTypeName, mapFromUniquelyNamedConstantNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isBayesianNetwork);
    return convertedModel;
}
Also used : Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) DefaultExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)

Example 33 with ExpressionBasedModel

use of com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel 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

ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)33 Expression (com.sri.ai.expresso.api.Expression)24 LinkedList (java.util.LinkedList)11 ArrayList (java.util.ArrayList)10 DefaultExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.core.DefaultExpressionBasedModel)8 HOGModel (com.sri.ai.praise.core.representation.classbased.hogm.HOGModel)8 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)8 Pair (com.sri.ai.util.base.Pair)8 ParameterEstimationForExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.ParameterEstimationForExpressionBasedModel)7 UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.buildOptimizedExpressionBasedModel)7 UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel (com.sri.ai.praise.learning.symbolicparameterestimation.util.UsefulOperationsParameterEstimation.parseHOGModelToExpressionBasedModel)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)6 Context (com.sri.ai.grinder.api.Context)5 HOGMProblemError (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.hogm.parsing.HOGMProblemError)5 Type (com.sri.ai.expresso.api.Type)3 HOGMParserWrapper (com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ExpressionBasedSolver (com.sri.ai.praise.core.inference.byinputrepresentation.classbased.expressionbased.api.ExpressionBasedSolver)2