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