From a9f4e142dbf322961ca060090fb7e851e4bcbe29 Mon Sep 17 00:00:00 2001 From: zaiyuan49 Date: Sun, 12 Jun 2022 21:29:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A2=AF=E5=BA=A6=E4=B8=8B?= =?UTF-8?q?=E9=99=8D=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base-tutorial/base-tutorial.iml | 316 ------------------ base-tutorial/pom.xml | 7 + .../main/java/org/sikongsphere/jblas/CG.java | 91 +++++ 3 files changed, 98 insertions(+), 316 deletions(-) delete mode 100644 base-tutorial/base-tutorial.iml create mode 100644 base-tutorial/src/main/java/org/sikongsphere/jblas/CG.java diff --git a/base-tutorial/base-tutorial.iml b/base-tutorial/base-tutorial.iml deleted file mode 100644 index 3c576e6..0000000 --- a/base-tutorial/base-tutorial.iml +++ /dev/null @@ -1,316 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/base-tutorial/pom.xml b/base-tutorial/pom.xml index a7fa53f..1ce20f6 100644 --- a/base-tutorial/pom.xml +++ b/base-tutorial/pom.xml @@ -39,6 +39,7 @@ 1.10.0 3.5.7 8.0.16 + 1.2.5 @@ -245,6 +246,12 @@ RELEASE compile + + + org.jblas + jblas + ${jblas.version} + \ No newline at end of file diff --git a/base-tutorial/src/main/java/org/sikongsphere/jblas/CG.java b/base-tutorial/src/main/java/org/sikongsphere/jblas/CG.java new file mode 100644 index 0000000..b51ee9f --- /dev/null +++ b/base-tutorial/src/main/java/org/sikongsphere/jblas/CG.java @@ -0,0 +1,91 @@ +package org.sikongsphere.jblas; + +import org.jblas.*; + +import static org.jblas.DoubleMatrix.*; +import static org.jblas.MatrixFunctions.*; + +/** + * 梯度下降 + * + * @author zaiyuan + * @date 2022-6-12 12:28:51 + */ +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\n", 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}; + } +} -- Gitee