aboutsummaryrefslogtreecommitdiff
path: root/tests/aux/test_kernel_cross.m
blob: 5777afd982cf889be5a4ba75dcec7ddce5bb56ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function test_kernel_cross(kerneltype)

clc;
rand('state', 123456);
more off;

n_1 = 10;
n_2 = 5;
m = 3;

X_1 = rand(n_1, m);
Z_1 = [ones(n_1, 1), X_1];
X_2 = rand(n_2, m);
Z_2 = [ones(n_2, 1), X_2];

for ii=1:n_1
  for jj=1:m+1
    fprintf("matrix_set(data_1->RAW, data_1->m+1, %i, %i, %.16f);\n", ii-1, jj-1, Z_1(ii, jj));
  end
end

fprintf('\n');
for ii=1:n_2
  for jj=1:m+1
    fprintf("matrix_set(data_2->RAW, data_2->m+1, %i, %i, %.16f);\n", ii-1, jj-1, Z_2(ii, jj));
  end
end


K = zeros(n_2, n_1);
if strcmp(kerneltype, 'poly')
  # Polynomial kernel
  # (gamma * <x_1, x_2> + c)^d
  gamma = 1.5;
  c = 3.0;
  d = 1.78;
  
  for ii=1:n_2
    for jj=1:n_1
      K(ii, jj) = (gamma * (X_2(ii, :) * X_1(jj, :)') + c)^d;
    end
  end
elseif strcmp(kerneltype, 'rbf')
  # RBF kernel
  # exp(-gamma * norm(x1 - x2)^2)
  gamma = 0.348
  for ii=1:n_2
    for jj=1:n_1
      K(ii, jj) = exp(-gamma * sum((X_2(ii, :) - X_1(jj, :)).^2));
    end
  end
elseif strcmp(kerneltype, 'sigmoid')
  # Sigmoid kernel
  # tanh(gamma * <x_1, x_2> + c)
  gamma = 1.23;
  c = 1.6;
  for ii=1:n_2
    for jj=1:n_1
      K(ii, jj) = tanh(gamma * (X_2(ii, :) * X_1(jj, :)') + c);
    end
  end
end

fprintf('\n');
for ii=1:n_2
  for jj=1:n_1
    fprintf("mu_assert(fabs(matrix_get(K2, data_1->n, %i, %i) -\n %.16f) < eps,\n\"Incorrect K2 at %i, %i\");\n", ii-1, jj-1, K(ii, jj), ii-1, jj-1);
  end
end