diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2018-03-27 19:24:03 +0100 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2018-03-27 19:24:03 +0100 |
| commit | d83e911fe228571171f9ddc379708dc37c4bfddf (patch) | |
| tree | fc396f91eaf92010ef0db33e0bb3b084e69402ac /tests/aux | |
| parent | update training file for zip dataset (diff) | |
| download | gensvm-d83e911fe228571171f9ddc379708dc37c4bfddf.tar.gz gensvm-d83e911fe228571171f9ddc379708dc37c4bfddf.zip | |
Major bugfix for nonlinear GenSVM
Nonlinear GenSVM depends on the eigendecomposition of the
kernel matrix. Mathematically, the Sigma vector in the code
should contain the square root of the eigenvalues. Taking
the square root was however neglected, which resulted in
poor performance of nonlinear GenSVM. This is now fixed,
which means that the performance of nonlinear GenSVM will
be much better.
Diffstat (limited to 'tests/aux')
| -rw-r--r-- | tests/aux/test_eigendecomp.m | 2 | ||||
| -rw-r--r-- | tests/aux/test_kernel_pre.m | 46 | ||||
| -rw-r--r-- | tests/aux/test_train_kernel.m | 30 |
3 files changed, 38 insertions, 40 deletions
diff --git a/tests/aux/test_eigendecomp.m b/tests/aux/test_eigendecomp.m index 921f44b..f3ff4ab 100644 --- a/tests/aux/test_eigendecomp.m +++ b/tests/aux/test_eigendecomp.m @@ -23,7 +23,7 @@ ratios = eigenvalues ./ eigenvalues(end, end); cutoff = 1e-2; realP = fliplr(P(:, ratios > cutoff)); -realSigma = flipud(eigenvalues(ratios > cutoff)); +realSigma = sqrt(flipud(eigenvalues(ratios > cutoff))); r = sum(ratios > cutoff); diff --git a/tests/aux/test_kernel_pre.m b/tests/aux/test_kernel_pre.m index a1c8337..69f6f1e 100644 --- a/tests/aux/test_kernel_pre.m +++ b/tests/aux/test_kernel_pre.m @@ -1,32 +1,32 @@ function test_kernel_pre() - + kerneltype = 'rbf'; rand('state', 123456); n = 10; m = 5; cutoff = 5e-3; - + X = rand(n, m); Z = [ones(n, 1), X]; - + set_matrix(Z, "data->Z", "data->m+1"); - + K = zeros(n, n); if strcmp(kerneltype, 'poly') - # Polynomial kernel - # (gamma * <x_1, x_2> + c)^d + % Polynomial kernel + % (gamma * <x_1, x_2> + c)^d gamma = 1.5; c = 3.0; d = 1.78; - + for ii=1:n for jj=1:n K(ii, jj) = (gamma * (X(ii, :) * X(jj, :)') + c)^d; end end elseif strcmp(kerneltype, 'rbf') - # RBF kernel - # exp(-gamma * norm(x1 - x2)^2) + % RBF kernel + % exp(-gamma * norm(x1 - x2)^2) gamma = 0.348 for ii=1:n for jj=1:n @@ -34,8 +34,8 @@ function test_kernel_pre() end end elseif strcmp(kerneltype, 'sigmoid') - # Sigmoid kernel - # tanh(gamma * <x_1, x_2> + c) + % Sigmoid kernel + % tanh(gamma * <x_1, x_2> + c) gamma = 1.23; c = 1.6; for ii=1:n @@ -44,29 +44,27 @@ function test_kernel_pre() end end end - - K(1, 2) - [P, Sigma] = eig(K); + [P, values] = eig(K); - eigenvalues = diag(Sigma); + eigenvalues = diag(values); ratios = eigenvalues ./ eigenvalues(end, end); - + realP = fliplr(P(:, ratios > cutoff)); - realSigma = flipud(eigenvalues(ratios > cutoff)); - + realSigma = sqrt(flipud(eigenvalues(ratios > cutoff))); + assert_matrix(realSigma, "data->Sigma", "1"); - + r = sum(ratios > cutoff); fprintf("mu_assert(data->r == %i);\n", r); - + M = realP * diag(realSigma); - + newZ = [ones(n, 1) M]; assert_matrix_abs(newZ, "data->Z", "data->r+1"); - + assert_matrix(Z, "data->RAW", "data->m+1"); - + end function set_matrix(A, name, cols) @@ -98,4 +96,4 @@ function assert_matrix_abs(A, name, cols) end end fprintf("\n"); -end
\ No newline at end of file +end diff --git a/tests/aux/test_train_kernel.m b/tests/aux/test_train_kernel.m index 7bf14b8..b561bd4 100644 --- a/tests/aux/test_train_kernel.m +++ b/tests/aux/test_train_kernel.m @@ -1,21 +1,21 @@ function [V] = test_train_kernel() - + clear; more off; rand('state', 654321); - + n = 10; m = 5; classes = 4; cutoff = 5e-3; - + X = rand(n, m); Z = [ones(n, 1), X]; set_matrix(Z, "data->Z", "data->m+1"); - + y = [2 1 3 2 3 2 4 1 3 4]; set_matrix(y, "data->y", "1"); - + p = 1.2143; kappa = 0.90298; lambda = 0.00219038; @@ -36,24 +36,24 @@ function [V] = test_train_kernel() eigenvalues = diag(Sigma); ratios = eigenvalues ./ eigenvalues(end, end); - + realP = fliplr(P(:, ratios > cutoff)); - realSigma = flipud(eigenvalues(ratios > cutoff)); - + realSigma = sqrt(flipud(eigenvalues(ratios > cutoff))); + assert_matrix(realSigma, "data->Sigma", "1"); - + r = sum(ratios > cutoff); fprintf("mu_assert(data->r == %i);\n", r); - + M = realP * diag(realSigma); size(M) - + assert_matrix(Z, "data->RAW", "data->m+1"); - - seedV = zeros(size(M, 2) + 1, classes - 1); + + seedV = zeros(size(M, 2) + 1, classes - 1); [W, t] = msvmmaj(M, y, rho, p, kappa, lambda, epsilon, 'show', 0, seedV); V = [t'; W]; - + fprintf('\n'); assert_matrix_abs(V, "model->V", "model->K-1"); @@ -89,4 +89,4 @@ function assert_matrix_abs(A, name, cols) end end fprintf("\n"); -end
\ No newline at end of file +end |
