Java線形代数ライブラリjblasに基づくConjugateGraadientとLBFGSの例
作者:金良[email protected])csdnブログ:http://blog.csdn.net/u012176591
1.jblasについて
jblas is a fast linea algebrara library for Java.jblas is based on BLAS and LAPACK,the de-facto industry standard for matrax computations,and uses state-off-the-art implementsations like ATLAS for all compart
ホームページの住所:http://jblas.org/ ライブラリのAPIドキュメントのアドレス:http://jblas.org/javadoc/index.html Javaプロジェクトでは
jar
包のファイルを導入すればいいです.以下はそのパンフレットです.
jblas-Fast matrix computations for Java龚2.共役勾配の低下の例:
import org.jblas.*;
import static org.jblas.DoubleMatrix.*;
import static org.jblas.MatrixFunctions.*;
/** * Example of how to implement conjugate gradienst with jblas. * * Again, the main objective of this code is to show how to use * jblas, not how to package/modularize numerical code in Java ;) * * Closely follows http://en.wikipedia.org/wiki/Conjugate_gradient_method */
public class CG {
public static void main(String[] args) {
new CG().runExample();
}
void runExample() {
int n = 100;
double w = 1;
double lambda = 1e-6;
DoubleMatrix[] ds = sincDataset(n, 0.1);
//ds[0].print();
//ds[1].print();
DoubleMatrix A = gaussianKernel(w, ds[0], ds[0]).add( eye(n).mul(lambda) );
DoubleMatrix x = zeros(n);
DoubleMatrix b = ds[1];
cg(A, b, x, lambda);
}
DoubleMatrix cg(DoubleMatrix A, DoubleMatrix b, DoubleMatrix x, double thresh) {
int n = x.length;
DoubleMatrix r = b.sub(A.mmul(x));
DoubleMatrix p = r.dup();
double alpha = 0, beta = 0;
DoubleMatrix r2 = zeros(n), Ap = zeros(n);
while (true) {
A.mmuli(p, Ap);
alpha = r.dot(r) / p.dot(Ap);
x.addi(p.mul(alpha));
r.subi(Ap.mul(alpha), r2);
double error = r2.norm2();
System.out.printf("Residual error = %f
", error);
if (error < thresh)
break;
beta = r2.dot(r2) / r.dot(r);
r2.addi(p.mul(beta), p);
DoubleMatrix temp = r;
r = r2;
r2 = temp;
}
return x;
}
/** * Compute the Gaussian kernel for the rows of X and Z, and kernel width w. */
public static DoubleMatrix gaussianKernel(double w, DoubleMatrix X, DoubleMatrix Z) {
DoubleMatrix d = Geometry.pairwiseSquaredDistances(X.transpose(), Z.transpose());
return exp(d.div(w).neg());
}
/** * The sinc function (save version). * * This version is save, as it replaces zero entries of x by 1. * Then, sinc(0) = sin(0) / 1 = 1. * */
DoubleMatrix safeSinc(DoubleMatrix x) {
return sin(x).div(x.add(x.eq(0)));
}
/** * Create a sinc data set. * * X ~ uniformly from -4..4 * Y ~ sinc(x) + noise * gaussian noise. */
DoubleMatrix[] sincDataset(int n, double noise) {
DoubleMatrix X = rand(n).mul(8).sub(4);
DoubleMatrix Y = safeSinc(X) .add( randn(n).mul(noise) );
return new DoubleMatrix[] {X, Y};
}
}
3.嶺回帰例import org.jblas.*;
import static org.jblas.DoubleMatrix.*;
import static org.jblas.MatrixFunctions.*;
/** * A simple example which computes kernel ridge regression using jblas. * * This code is by no means meant to be an example of how you should do * machine learning in Java, as there is absolutely no encapsulation. It * is rather just an example of how to use jblas to perform different kinds * of computations. */
public class KRR {
public static void main(String[] args) {
int n = 1000;
double w = Double.valueOf(args[0]);
double lambda = Double.valueOf(args[1]);
new KRR().run(n, w, lambda);
}
void run(int n, double w, double lambda) {
DoubleMatrix[] ds = sincDataset(n, 0.1);
DoubleMatrix alpha = learnKRR(ds[0], ds[1], w, lambda);
DoubleMatrix Yh = predictKRR(ds[0], ds[0], w, alpha);
DoubleMatrix XE = rand(1000).mul(8).sub(4);
System.out.printf("Mean squared error = %.5f
", mse(Yh, ds[1]));
}
/** * The sinc function. * * This version is not save since it divides by zero if one * of the entries of x are zero. */
DoubleMatrix sinc(DoubleMatrix x) {
return sin(x).div(x);
}
/** * The sinc function (save version). * * This version is save, as it replaces zero entries of x by 1. * Then, sinc(0) = sin(0) / 1 = 1. * */
DoubleMatrix safeSinc(DoubleMatrix x) {
return sin(x).div(x.add(x.eq(0)));
}
/** * Create a sinc data set. * * X ~ uniformly from -4..4 * Y ~ sinc(x) + noise * gaussian noise. */
DoubleMatrix[] sincDataset(int n, double noise) {
DoubleMatrix X = rand(n).mul(8).sub(4);
DoubleMatrix Y = safeSinc(X) .add( randn(n).mul(noise) );
return new DoubleMatrix[] {X, Y};
}
/** * Compute the alpha for Kernel Ridge Regression. * * Computes alpha = (K + lambda I)^-1 Y. */
DoubleMatrix learnKRR(DoubleMatrix X, DoubleMatrix Y,
double w, double lambda) {
int n = X.rows;
DoubleMatrix K = gaussianKernel(w, X, X);
K.addi(eye(n).muli(lambda));
DoubleMatrix alpha = Solve.solveSymmetric(K, Y);
return alpha;
}
/** * Compute the Gaussian kernel for the rows of X and Z, and kernel width w. */
DoubleMatrix gaussianKernel(double w, DoubleMatrix X, DoubleMatrix Z) {
DoubleMatrix d = Geometry.pairwiseSquaredDistances(X.transpose(), Z.transpose());
return exp(d.div(w).neg());
}
/** * Predict KRR on XE which has been trained on X, w, and alpha. * * In a real world application, you would put all the data from training * in a class of its own, of course! */
DoubleMatrix predictKRR(DoubleMatrix XE, DoubleMatrix X, double w, DoubleMatrix alpha) {
DoubleMatrix K = gaussianKernel(w, XE, X);
return K.mmul(alpha);
}
double mse(DoubleMatrix Y1, DoubleMatrix Y2) {
DoubleMatrix diff = Y1.sub(Y2);
return pow(diff, 2).mean();
}
}
3.LBFGSの例関連例とライブラリファイルjarパッケージのダウンロードhttp://download.csdn.net/detail/u012176591/8660849
——————————————————————————————
jarパッケージのjavadocドキュメントをインストールします.
jblasのjarパッケージに参加するjavadocドキュメントは、eclipseで方法やオブジェクトのパラメータや使い方を簡単に確認することができます.まずJavadoc圧縮ファイルをどこかに解凍します.次のようにします.Package Explorerビューでは、jarパケットを選択し、右クリックしてProptiesをクリックします.効果は以下の通りです
eclipse設定Javaコマンド行実行パラメータ
下図のようにプログラム実行時には2つのパラメータが必要です.プログラム実行前に設定する必要があります.は、以下のように設定方法です.まず右クリックjavaファイル–運転方式–運転プロファイルを開きます.
引数を設定します.ここでは2つの数字を設定して、それぞれ2つのパラメータに対応します.