forum.hugin.com
User Forums => HUGIN API Discussion => Java => Topic started by: Ajay Tiwari on January 04, 2008, 07:24:47
-
Can anybody please tell me how to pass a .dat file for EM learning using JAVA HUGIN API.
-
Can anybody please tell me how to pass a .dat file for EM learning using JAVA HUGIN API.
This is the procedure for using EM-learning:
1. Create instance of Domain (either by Loading a NET-file or by manually construct the network from the code)
2. Load data (Domain.parseCases)
3. Compile domain (Domain.compile)
4. Run EM-algorithm (Domain.learnTables)
Here is example code for loading a random network with discrete chance nodes, parsing a random data file and running EM-algorithm for computing new maximal likelihoods for the CPTs. After learning, the new CPTs are printed.
import java.util.*;
import COM.hugin.HAPI.*;
class EMLearning {
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("use arguments: <network.net> <data.dat>");
return;
}
String netFile = args[0];
String dataFile = args[1];
try {
//load domain
Domain dom = new Domain(netFile, new DefaultClassParseListener());
//compile using defaults
dom.compile();
NodeList nodes = dom.getNodes();
ListIterator li = nodes.listIterator();
while (li.hasNext()) {
Node n = (Node)li.next();
if (n instanceof DiscreteChanceNode) {
//make sure that all discrete chance nodes have experience tables with entries set to 0
//to compute maximal likelihood estimates of CPTs
DiscreteChanceNode dcc = (DiscreteChanceNode)n;
Table experience = dcc.getExperienceTable();
int size = experience.getSize();
for (int i = 0; i < size; i++) {
experience.setDataItem(i, 0.0);
}
}
}
//load data file
dom.parseCases(dataFile, new DefaultClassParseListener());
/* use these for configuring when EM should terminate
dom.setMaxNumberOfEMIterations(0);
dom.setLogLikelihoodTolerance(0.0001);
*/
dom.learnTables();
//print CPTs after EM-learning
System.out.println("New tables after EM-learning:");
li = nodes.listIterator();
while (li.hasNext()) {
Node n = (Node)li.next();
if (n instanceof DiscreteChanceNode) {
DiscreteChanceNode dcc = (DiscreteChanceNode)n;
Table CPT = dcc.getTable();
printTable(CPT);
}
}
} catch (ExceptionHugin eh) {
System.err.println(eh);
}
}
//print tables for all discrete chance nodes
public static void printTable (Table CPT) throws ExceptionHugin {
NodeList nodes = CPT.getNodes();
int[] configuration = new int[nodes.size()];
int tableSize = CPT.getSize();
for (int i = 0; i < tableSize; i++) {
CPT.getConfiguration(configuration, i);
for (int j = 0; j < nodes.size(); j++) {
Node n = (Node)nodes.get(j);
System.out.print(n.getName() + "=" + configuration[j] + " ");
}
System.out.println(":= " + CPT.getDataItem(i));
}
}
}
Compile code with javac:
javac -classpath <path to hapiXX.jar> -d <dir to place compiled class files> <path to source files *.java>
And run with java:
java -classpath <path to hapiXX.jar>;<dir with compiled class files> -Djava.library.path=<path to dir with hapiXX.dll> EMLearning (arguments)
(where (arguments) are the netfile to load and the datafile to use for EM-learning, e.g. asia.dat datafile.dat)
For more specific details of the EM-learning functionally in Hugin check out the API-Manual section 11.6 Learning conditional probability tables.
For details on compiling with the Hugin Java API (and APIs for other languages) refer to the API-manual section 1.3 Using the HUGIN API on Windows platforms.
-
Hi,
I tried to compile this with the instructions given but got three errors:
kari@kant:~$ javac -classpath /usr/local/hugin/lib64/hapi71-64.jar -d EMclassfiles EMlearning.java
EMlearning.java:29: possible loss of precision
found : long
required: int
int size = experience.getSize();
^
EMlearning.java:67: possible loss of precision
found : long
required: int
int tableSize = CPT.getSize();
^
EMlearning.java:69: getConfiguration(long[],long) in COM.hugin.HAPI.Table cannot be applied to (int[],int)
CPT.getConfiguration(configuration, i);
^
3 errors
Any suggestions how to get rid of them? I am running Hugin Developer 7.1
-
Hi Kari,
You are trying to compile the example with the 64-bit version of the HUGIN Java API. To compile the example successfully you can either 1) use the 32-bit version of the HUGIN Java API or 2) change int to long in the following declarations
int size = experience.getSize();
int[] configuration = new int[nodes.size()];
int tableSize = CPT.getSize();
int i = 0;
-
Hi martin,
do we parseCases before domain compile or after domain compile? based on your description parseCases should be called before compiling the domain, but the coding seem to be another way round.
thanks :)
In edit mode, there is a EM Learning Wizard button which allow me to select .dat file , set iteration and tolerance.
In run mode, i will need to load the Data via "File->Load Case".
so the domain.parseCases actually simulate the Load case in run mode? or the EM Learning in edit mode?
From
Thomas
-
The order of Domain.parseCases and Domain.compile does not matter.
When a set of cases has been loaded with Domain.parseCases the entire set can be used for doing EM Domain.learnTables(), and/or a single case can be entered as evidence in a compiled domain using Domain.enterCase(casenum).