Scientific Programming With Java Classes Supported With A Scripting Interpreter
Scientific Programming With Java Classes Supported With A Scripting Interpreter
2
is minimum (in order to maximize the margin = 2/w
0
2
is minimum.
19
This minimization can proceed by maximizing the following function with
respect to the variables
i
(Lagrange multipliers) [15, 4, 10]:
W() =
l
i=1
a
i
1
2
l
i=1
l
j=1
i
j
y
i
y
j
< x
i
, x
j
> (1)
subject to the constraints: 0
i
and
l
i=1
i
y
i
= 0. If
i
> 0 then x
i
corresponds to a support vector. The classication of an unknown vector x
is obtained by computing
F(x) = sgn{w
0
x + b
0
}, where w
0
=
l
i=1
i
y
i
x
i
(2)
and the sum accounts only N
s
l nonzero support vectors (i.e. training
set vectors x
i
whose
i
are nonzero). Clearly, after the training, the clas-
sication can be accomplished eciently by taking the dot product of the
optimum weight vector w
0
with the input vector x.
Non-linear Separability of data & Non-Linear SVMs
The case that the data is not linearly separable is handled by introducing
slack variables (
1
,
2
, . . . ,
l
) with
i
0 such that, y
i
(w x
i
+ b
0
)
1
i
, i = 1, . . . , l. The introduction of the variables
i
, allows misclassied
points, which have their corresponding
i
> 1. Thus,
l
i=1
i
is an upper
bound on the number of training errors. The corresponding generalization
of the concept of optimal separating hyperplane is obtained by the solution
of the following optimization problem:
minimize
1
2
w w + C
l
i=1
i
(3)
subject to
y
i
(w x
i
+ b
0
) 1
i
and
i
0, i = 1, . . . , l (4)
20
The control of the learning capacity is achieved by the minimization of
the rst part of Equation 3 while the purpose of the second term is to pun-
ish for misclassication errors. The parameter C is a kind of regularization
parameter, that controls the tradeo between learning capacity and training
set errors. Clearly, a large C corresponds to assigning a higher penalty to
errors.
Finally, the case of nonlinear SVMs should be considered. The input
data in this case are mapped into a high-dimensional feature space through
some nonlinear mapping chosen a priori [9, 8, 14]. The optimal separating
hyperplane is then constructed in this space. Further details on the math-
ematical method can be found at the references, an excellent reference is
[10].
6.2 jLab SVM class interface
Due to space limitations we will not present the whole SVM class interface,
but instead we will limit ourselves to the SVM training routine. This rou-
tine demonstrates the general method of interfacing Java classes in jLab as
extension J-Classes. Each extension J-Class is available as a jLab function
and its functionality can be directly utilized from within jLabs scripting
machinery.
For the particular example, the svmTrain class provides to the jLab the
function
double [] [] svmTrain( double [] [] trainData, int [] trainLabels, String
svmModelFile, String svmType);
21
This jLab function triggers the functionality of the evaluate() method of
the Java class with the same name.
At the rst stage of processing the Interpreter localizes the class le
svmTrain at the jLabs class path (unless the class was already cached either
because it is already used, or by the class preload mechanism). Then the
interpreter utilizes the parameters of the jLabs method svmTrain in order
to prepare the call to evaluate().
Subsequently, the interpreter exploits the Javas reection mechanism [2]
in order to call the evaluation method. The corresponding Java interface to
the LibSVM package [27] is very simple and is performed by the following
chunk of code:
/**returns the trained SVM saved at the corresponding file
* @param operands[0] = train attributes
* @param operands[1] = training labels */
public OperandToken evaluate(Token[] operands)
{
int kernelType = svm_parameter.RBF; // default kernel type
String sKernelType="";
int nargin = getNArgIn(operands); // number of arguments
if (nargin < 3)
throwjExecException("svmTrain: number of arguments != 3");
if (!(operands[0] instanceof NumberToken))
throwjExecException("svmTrain: first argument
must be a matrix train [][]");
if (!(operands[1] instanceof NumberToken))
22
throwjExecException("svmTrain: second argument
must be a matrix targetValue []");
if (!(operands[2] instanceof StringToken))
throwjExecException("svmTrain: third argument
must be a String svmTrainSavedModel for saving trained SVM");
if (nargin == 4) { // fourth argument the SVM kernel type
sKernelType = ( (StringToken)operands[3]).getValue();
if (sKernelType.equalsIgnoreCase("rbf"))
kernelType = svm_parameter.RBF;
else if (sKernelType.equalsIgnoreCase("poly"))
kernelType = svm_parameter.POLY;
}
double [][] train = (double [][])((NumberToken)
operands[0]).getValues();
double [][] targetValues = (double [][])((NumberToken)
operands[1]).getValues();
String svmModelFile = ((StringToken) operands[2]).getValue();
svm_model model;
svm_parameter param = new svm_parameter();
// default values
param.svm_type = svm_parameter.C_SVC;
param.kernel_type = kernelType;
param.degree = 3;
param.gamma = 1; // 1/k
param.coef0 = 0;
param.nu = 0.5;
23
param.cache_size = 40;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.nr_weight = 0;
param.weight_label = new int[0];
param.weight = new double[0];
int NInstances = train.length;
int Dim = train[0].length;
svm_problem prob = new svm_problem();
prob.l = NInstances;
prob.y = new double[prob.l];
prob.x = new svm_node[prob.l][Dim];
for (int i=0; i < prob.l; i++)
for(int j=0;j<Dim;j++)
{
prob.x[i][j] = new svm_node();
prob.x[i][j].index = j+1;
prob.x[i][j].value = train[i][j];
prob.y[i] = targetValues[0][i];
}
model = svm.svm_train(prob, param);
24
int nSVs = model.l;
double [] [] values = new double[nSVs][Dim];
for (int n=0; n<nSVs; n++)
for (int m=0; m<Dim; m++)
values[n][m] = model.SV[n][m].value;
try {
svm.svm_save_model(svmModelFile, model);
}
catch (IOException e) {}
OperandToken result = new NumberToken(values);
return result;
}
}
6.3 jLab Scripting SVM code
The part of the jLab code that performs the SVM training and evaluation
is presented below:
svmModelFile = myDataDir+"sonar.svm";
% the file that will contain the trained SVM model
tic;
25
SVecs = svmTrain(trainData, trainLabels,
svmModelFile, "rbf");
timeTrain = toc();
predictions = svmPredict(testData, svmModelFile);
% evaluate the prediction performance
successCnt=0; failCnt=0;
for (k=1; k<=LenTrain; k=k+1)
if (predictions(k)==testLabels(k)) successCnt = successCnt+1;
else
failCnt=failCnt+1;
end;
str = "Classification Performance: successCnt = "+
successCnt+", failCnt = "+failCnt+" ("+
(successCnt*100.0)/(successcnt+failCnt)+" %)";
disp(str);
str = "SVM Training time = "+timeTrain;
disp(str);
6.4 jLab performance
The execution speed of an algorithm implemented in jLab depend heavily on
the proportion of processing performed in Java related to that implemented
as a J-Script. Clearly, the number crunching code should be coded in Java
and only the control logic should be coded as a J-Script in order to obtain
rapid and exible experimentation. We have performed experiments with
a SVM-Matlab toolbox downloaded from http://asi.insa-rouen.fr/ arako-
tom/toolbox/index.html, that implements in pure Matlab various current
26
kernel and SVM algorithms described also in [28, 29]. The jLab based on
the LibSVM Java implementation [27] is on the average about ten times
faster than the pure Matlab version. However, the LS-SVM Matlab tool-
box of [31] incorporates .MEX code compiled in C++ and is of comparable
speed to our Java based LibSVM implementation. We should note that the
pure C++ implementation of the LibSVM algorithms is only 2 to 3 times
faster than the Java version. This fact surprised us initially, and it can be
explained by the signicant advance at the design and implementation of the
Java virtual machine environment. We have tested both the Java and C++
LibSVM implementations on a Pentium 4 PC at 2.6 GHz clock speed, both
using the Fedora Core 5 Linux (based on 2.6.15 Linux kernel) and the Sun
Solaris 10 operating system running at the same PC also. At both platforms
we have used the recent version of the Java Run time Environment (i.e. JRE
1.5.0 07) supplied by Sun Microsystems and the GNU C++ compiler. We
have tested also the jLab on the Windows XP platform, and the important
point that we have derived is that the execution speed is similar to the Linux
and Solaris based experiments. The only signicant factor that aects the
execution speed is the JRE version: we have observed notable improvement
in execution speed by using later improved versions of the Sun Microsys-
tems JRE. In particular the average training time for the data of the classic
UCI Sonar dataset, on a Pentium 4 1.8 GHz PC capable of multibooting
all the three tested operating systems were: a. Windows XP: 0.36 sec
for the main training accomplished by the Java class le, and 39 sec for
J-Script preparation of data for training b. Linux (Fedora Core 5, with
2.6.13 kernel: 0.41 sec for Java class and 31 sec for J-Script preparation
respectively. c. Sun/Solaris 10: 0.35 sec for Java class and 32 sec for
the J-Script. All the evaluated platforms have used the Sun Microsystems
27
Windows XP Linux Solaris 10
JDK 1.5, SVM Training (Java) 0.36 0.41 0.35
JDK 1.5, jLab Script Data Preprocessing 39 31 32
JDK 1.6, SVM Training (Java) 0.3 0.31 0.29
JDK 1.6, jLab Script Data Preprocessing 34 27 26
Table 1: Some averaged results from the performance of jLab across various
platforms (time in secs).
Java Vitual Machine and JDK, version 1.5. Also, the GNU supplied JRE
(gcj, gjava) succeeds in compiling most of the jLab system (although there
are problems in compiling all the integrated system) but the resulting Java
code does not run as eciently as with the Suns Java Virtual Machine. The
memory requirements and overhead cost of the script interpreter are very
small when no class preloaded is performed. In this case only the accessed
classes are loaded in memory. However, the class preload operation loads
all the extension classes beforehand in memory and therefore consumes size
proportional to the number of extension classes. Table 1 presents some com-
parative averaged performance results. The averaging was performed across
30 trials. The results clearly illustrate the advantage of the Java compiled
code over the scripting jLab that was used only for data preprocessing.
The Java code of jLab is open source and can be downloaded from page:
http://infoman.teikav.edu.gr/stpapad/
7 Conclusions
The paper has presented a powerful scripting language that is executed by
an interpreter implemented in the Java language. This language supports
all the basic programming constructs and an extensive set of built in math-
ematical routines that cover all the basic numerical analysis tasks. These
28
toolboxes can be easily implemented in Java and the corresponding classes
can be dynamically integrated to the system.
The jLab is based on a mixed mode programming paradigm:
Java compiled code for the computationally demanding operations and
Scripting code for fast implementation of the programs structure.
This design permits to obtain both speed eciency and exibility while at
the same time allows the utilization of the vast amounts of scientic software
that is implemented in the Java language. The implementation of jLab in
pure Java allows a much cleaner, faster, platform independent and less error
prone build from source process, than similar C/C++/Fortran based open
source environments (e.g. Scilab, Octave). Specically, the clean and build
all process takes only about 5 to 8 secs at the Netbeans 5.5 IDE. Similar is the
required build time at the Eclipse development platform. We can contrast
this with several (about 15-20) minutes required to run the congure script
and the making process on a Linux Fedora Core 5 installation on a 3.2 GHz
Pentium-4.
We have demonstrated the potentiality of jLab with the implementa-
tion of a Support Vector Machine (SVM) toolkit. Also we have compared
its performance with a C/C++ and a Matlab version and across dierent
computing platforms (i.e. Linux, Sun/Solaris, Windows XP). Neuro-Fuzzy
algorithms can require enormous computation resources and at the same
time an expressive programming environment.
Future work will proceed with the porting of the JOONE library for
neural networks [19] and the WEKA data mining system that can easily
provide an excessive set of routines for data preprocessing and visualization
[18]. Furthermore we work on improving the parser in order to allow more
29
exible contructs, and we improve the eciency of the parsing phase, in or-
der to be able to compete with C/C++ parser implementations (e.g. Scilab,
Octave).
References
[1] Stephen L. Campbell, Jean-Philippe Chancelier, Ramine Nikoukhah,
Modeling and Simulation in Scilab/Scicos, Springer, 2006
[2] Cay Horstmann, Gary Cornell, Core Java 2, Vol I Fundamentals, Vol
II - Advanced Techniques. Sun Microsystems Press, 7th edition, 2005
[3] Principles of Compiler Design, Alfred V. Aho, and Jerey D. Ullman,
Addison-Wesley, 1977
[4] Simon Haykin, Neural Networks, MacMillan College Publishing Com-
pany, Second Edition, 1999
[5] Jung-Hsien Chiang, Pei-Yi Hao, Support Vector Learning Mechanism
for Fuzzy Rule-Based Modeling: A New Approach, Vol. 12, No. 1,
February 2004, pp. 1-12
[6] D. Chakraborty and N. R. Pal, A Neuro-Fuzzy Scheme for Simulta-
neous Feature Selection and Fuzzy Rule-Based Classication, IEEE
Transactions on Neural Networks, Vo. 15, No. 1, January 2004, p. 110-
123
[7] Chang, C.-C., Lin, C.J, LIBSVM: A library for
support vector machines,2001, Available on-line:
http://www.csie.ntu.edu.tw/cjlin/libsvm
30
[8] B. Scholkopf, S. Mika, J. C. Burges, P. Knirsch, K.-R. Muller, G. Ratsch
and A. Smola, Input Space Versus Feature Space in Kernel-Based
Methods, IEEE Trans. On Neural Networks, vol. 10, no. 5, 1999.
[9] B. Scholkopf, A. J. Smola, R. C. Williamson, P. L. Bartlett, New
support vector algorithms, Neural Computation:1207-1245, 2000
[10] Bernhard Scholkopf, Alexander J. Smola, Learning with Kernels: Sup-
port Vector Machines, Regularization and Beyond, MIT Press 2002
[11] T. Joachims, Making Large-Scale SVM Learning Practical, Advances
in Kernel Methods - Support Vector Learning, Bernhard Scholkopf,
Christopher J. C. Burges, and Alexander J. Smola (eds), MIT Press,
Cambridge, USA, 1998
[12] E. Osuna, R. Freund, F. Girosi, An improved training algorithm for
support vector machines, Neural Networks for Signal Processing VII,
Proceedings of the 1997 IEEE Workshop pp. 276-285, Amelia Island,
FL.
[13] D. Mattera, S. Haykin, Support vector machines for dynamic recon-
struction of a chaotic system, in Advances in Kernel Methods - Sup-
port Vector Learning, B. Scholkopf, J. Burges, A. J. Smola,, Eds. Cam-
bridge, MA:MIT Press, 1999, pp. 211-242
[14] C. Cortes, V. Vapnik, Support vector networks, Machine Learning,
vol. 20, pp 1-25, 1995
[15] V. N. Vapnik., 1998, Statistical Learning Theory, New York, Wiley 9.
V. N. Vapnik, An Overview of Statistical Learning Theory, IEEE
Trans. On Neural Networks, Vol. 10, No 5, 1999, pp. 988-999
31
[16] Norman Chonacky, David Winch, 3Ms for Instruction: Reviews of
Maple, Mathematica and Matlab, Computing in Science and Engi-
neering, May/June 2005, Part I, pp. 7-13
[17] Norman Chonacky, David Winch, 3Ms for Instruction: Reviews of
Maple, Mathematica and Matlab, Computing in Science and Engi-
neering, July/August 2005, Part II, pp. 14-23
[18] Ian H. Witten, Eibe Frank, Data Mining: Practical Machine Learning
Tools and Techniques, Second Edition, Morgan Kaufmann Series, 2005
[19] Je T. Heaton, Introduction to Neural Networks with Java, Heaton
Research, 2005
[20] Joseph Bigus, Jennifer Bigus, Constructing Intelligent Agents Using
Java: Professional Developers Guide, 2nd Edition, Wiley 2001
[21] Maassen J., Van Nieuwpoort, Veldema R., Bal H., Kielmann T., Jacobs
C., Hofman R., Ecient Java RMI for Parallel Programming, ACM
Transactions on Programming Languages and Systems (TOPLAS), Vol.
23, Nr. 6, ACM 2001, pp. 747-775
[22] Michael Trott, The Mathematica Guidebook: Programming,
Springer, 2004
[23] Erwin Kreyszig, Maple Computer Guide for Advanced Engineering
Mathematics (8th Ed.), Wiley, 2000
[24] John W. Eaton, GNU Octave Manual, Network Theory Ltd, 2002
[25] Desmond J. Higham, Nicholas J. Higham, Matlab Guide, Second
Edition, SIAM Computational Mathematics, 2005
32
[26] Vugranam C. Sreedhar, Michael Brurke, and Jong-Doek Choi, A
framework for interprocedural optimization in the presence of dynamic
class loading In ACM SIGPLAN Conference on Programming Lan-
guage Design and Implementation, pp 196-207, 2000
[27] R.-E. Fan, P.-H. Chen, and C.-J. Lin, Working set selection using
the second order information for training SVM, Journal of Machine
Learning Research 6, 1889-1918, 2005
[28] A. Rakotomamonjy and S. Canu, Learning, frame, reproducing ker-
nel and regularization, Technical Report TR2002-01, Perception, Sys-
temes et Information, INSA de Rouen, 2002.
[29] Canu S., Mary X., and Rakotomamonjy A., Functional learning
through kernels, volume 190, chapter 5, pages 89-110, IOS Press, ad-
vances in learning theory: methods, models and applications, nato sci-
ence series iii: computer and systems sciences edition, 2003.
[30] Web Page of SVM-Matlab toolbox: http://asi.insa-
rouen.fr/arakotom/toolbox/index.html
[31] J.A.K. Suykens, T. Van Gestel, J. De Brabanter, B. De Moor, J. Van-
dewalle, Least Squares Support Vector Machines, World Scientic, Sin-
gapore, 2002
33
34
35
36
37
38