Close

Customizing Conditional Probability using Code Generation with SamIam

Even though every machine learning practitioner and researcher would like to modify and tweak both the algorithm and parameters, there are limited options for automated code generation in the machine learning world. SamIam by Automated Reasoning Group, UCLA is the tool designed for “Sensitivity Analysis, Modeling, Inference and more”. Currently the sensitivity, MAP, MPE and EM Learning tools are supported by Code Bandit, the automated code generation module in the SamIam. This helps us to write Java code to execute MAP programmatically or write Java code to demonstrate how to create a CPT and edit its parameters dynamically.

The whole process is fairly easy; open up any network.

 

Then go to Tools => code bandit and select the activity you'd like to perform.

Here I will get the java code demonstrating how to create a CPT and edit the parameters dynamically.

The sections of code can easily be modified. I am choosing Shenoy-Shafer here as the default algorithm but you can modify it both programatically or via UI.

System.out.println( "Editing CPT..." );

//(1) Retrieve child variable
FiniteVariable newChild = (FiniteVariable) model.forID( "newchild" );

//(2) Get the CPTShell (TableShell)
TableShell shell = (TableShell) newChild.getCPTShell( DSLNodeType.CPT );

//(3) Get the TableIndex
TableIndex index = shell.index();

//(4) Get the Table
Table table = shell.getCPT();

//(5) Retrieve parent variables
FiniteVariable parent00 = (FiniteVariable) model.forID( "parent00" );
FiniteVariable parent01 = (FiniteVariable) model.forID( "parent01" );
FiniteVariable parent02 = (FiniteVariable) model.forID( "parent02" );
FiniteVariable parent03 = (FiniteVariable) model.forID( "parent03" );
FiniteVariable parent04 = (FiniteVariable) model.forID( "parent04" );

//(6) Create an int[] to
//hold the multi-dimensional indices
int mindex[] = new int[ index.getNumVariables() ];

//(7) Choose the desired condition
//{ parent00 = false, parent01 = true, parent02 = false, parent03 = false, parent04 = false }
mindex[ index.variableIndex( parent00 ) ] = parent00.index( "false" );
mindex[ index.variableIndex( parent01 ) ] = parent01.index( "true" );
mindex[ index.variableIndex( parent02 ) ] = parent02.index( "false" );
mindex[ index.variableIndex( parent03 ) ] = parent03.index( "false" );
mindex[ index.variableIndex( parent04 ) ] = parent04.index( "false" );
mindex[ index.variableIndex( newChild ) ] = 0;

//(8) Use TableIndex to calculate
//the first linear index for the desired condition
int linear = index.index( mindex );
System.out.println( "Linear index? " + linear );

//(9) For the purposes of demonstation,
//set this condition deterministically for value "medium"
double zero = (double)0.0;
double one = (double)1.0;
table.setCP( linear + newChild.index( "low" ), zero );
table.setCP( linear + newChild.index( "medium" ), one );
table.setCP( linear + newChild.index( "high" ), zero );

//Print out information about the edited cpt.
System.out.println( "Edited cpt has min value: " + table.min() );
System.out.println( " max value: " + table.max() );
System.out.println( " entropy: " + table.entropy() );
return;

and the output from the console window appears as follows.

 

choosing recursive conditioning
Compilation Time (sec): 0.037
Memory used (Mb): 4.2724609375E-4
(Cumulative) Propagation Time (sec): 0.001

Compiling C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\MAPTutorial.java...
C:\Program Files\Java\jdk1.7.0_03\bin\javac.exe -classpath C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\inflib.jar C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\MAPTutorial.java
Note: C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\MAPTutorial.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Executing MAPTutorial...
C:\Program Files\Java\jdk1.7.0_03\bin\java.exe -classpath .;C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\inflib.jar MAPTutorial
Approximate MAP, P(MAP,e)= 0.8
	 P(MAP|e)= 0.8
	 instantiation: {A=Absent}

Initialization time, cpu: 0, elapsed: 11
Search time, cpu: 0, elapsed: 1

Executing MAPTutorial...
C:\Program Files\Java\jdk1.7.0_03\bin\java.exe -classpath .;C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\inflib.jar MAPTutorial
Approximate MAP, P(MAP,e)= 0.8
	 P(MAP|e)= 0.8
	 instantiation: {A=Absent}

Initialization time, cpu: 0, elapsed: 9
Search time, cpu: 0, elapsed: 1

choosing shenoy-shafer
Compilation Time (sec): 0.023
Memory used (Mb): 624.0
(Cumulative) Propagation Time (sec): 0.001

Compiling C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\CPTTutorial.java...
C:\Program Files\Java\jdk1.7.0_03\bin\javac.exe -classpath C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\inflib.jar C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\CPTTutorial.java
Note: C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\CPTTutorial.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Executing CPTTutorial...
C:\Program Files\Java\jdk1.7.0_03\bin\java.exe -classpath .;C:\Users\adnan.masood\Desktop\edu\samiam30_windows_amd64\samiam\inflib.jar CPTTutorial
Created a new cpt for variable newchild
with 5 parents.
The new cpt has 96 parameters.
The class of the shell is "edu.ucla.belief.TableShell".
Editing CPT...
Linear index? 69
Edited cpt has min value: 0.0
               max value: 1.0
               entropy:   49.13383752235577

The generated code can therefore be used as a stand-alone program.

 

Happy Inferencing!


Share

1 thought on “Customizing Conditional Probability using Code Generation with SamIam

Comments are closed.