| 74 | |
| 75 | |
| 76 | def test_radial_basis_kernel(N=1): |
| 77 | np.random.seed(12345) |
| 78 | i = 0 |
| 79 | while i < N: |
| 80 | N = np.random.randint(1, 100) |
| 81 | M = np.random.randint(1, 100) |
| 82 | C = np.random.randint(1, 1000) |
| 83 | gamma = np.random.rand() |
| 84 | |
| 85 | X = np.random.rand(N, C) |
| 86 | Y = np.random.rand(M, C) |
| 87 | |
| 88 | # sklearn (gamma) <-> mine (sigma) conversion: |
| 89 | # gamma = 1 / (2 * sigma^2) |
| 90 | # sigma = np.sqrt(1 / 2 * gamma) |
| 91 | |
| 92 | mine = RBFKernel(sigma=np.sqrt(1 / (2 * gamma)))(X, Y) |
| 93 | gold = sk_rbf(X, Y, gamma=gamma) |
| 94 | |
| 95 | np.testing.assert_almost_equal(mine, gold) |
| 96 | print("PASSED") |
| 97 | i += 1 |
| 98 | |
| 99 | |
| 100 | ####################################################################### |