Many Kernels can be described in terms the L2 norm with some operations performed on it. For example, the RBFKernel and RationalQuadraticKernel can both be expressed in terms of the L 2 norm of the two inputs. To simplify the addition of other kernels based on the same norm
| 18 | * @author Edward Raff |
| 19 | */ |
| 20 | public abstract class BaseL2Kernel implements KernelTrick |
| 21 | { |
| 22 | |
| 23 | private static final long serialVersionUID = 2917497058710848085L; |
| 24 | |
| 25 | @Override |
| 26 | abstract public double eval(Vec a, Vec b); |
| 27 | |
| 28 | @Override |
| 29 | abstract public KernelTrick clone(); |
| 30 | |
| 31 | @Override |
| 32 | public boolean supportsAcceleration() |
| 33 | { |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns the squared L<sup>2</sup> norm between two points from the cache values. |
| 39 | * @param i the first index in the vector list |
| 40 | * @param j the second index in the vector list |
| 41 | * @param vecs the list of vectors that make the collection |
| 42 | * @param cache the cache of values for each vector in the collection |
| 43 | * @return the squared norm ||x<sub>i</sub>-x<sub>j</sub>||<sup>2</sup> |
| 44 | */ |
| 45 | protected double getSqrdNorm(int i, int j, List<? extends Vec> vecs, List<Double> cache) |
| 46 | { |
| 47 | if(cache == null) |
| 48 | return Math.pow(vecs.get(i).pNormDist(2.0, vecs.get(j)), 2); |
| 49 | return cache.get(i)+cache.get(j)-2*vecs.get(i).dot(vecs.get(j)); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the squared L<sup>2</sup> norm of the given point from the cache |
| 54 | * @param i the index in the vector list to get the squared norm from |
| 55 | * @param vecs the list of vectors that make the collection |
| 56 | * @param cache the cache of values for each vector in the collection |
| 57 | * @return the squared norm ||x<sub>i</sub>||<sup>2</sup> |
| 58 | */ |
| 59 | protected double getSqrdNorm(int i, List<? extends Vec> vecs, List<Double> cache) |
| 60 | { |
| 61 | return cache.get(i); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the squared L<sup>2</sup> norm between a point in the cache and one with a provided qi value |
| 66 | * @param i the index in the vector list |
| 67 | * @param y the other vector |
| 68 | * @param qi the acceleration values for the other vector |
| 69 | * @param vecs the list of vectors to make the collection |
| 70 | * @param cache the cache of values for each vector in the collection |
| 71 | * @return the squared norm ||x<sub>i</sub>-y||<sup>2</sup> |
| 72 | */ |
| 73 | protected double getSqrdNorm(int i, Vec y, List<Double> qi, List<? extends Vec> vecs, List<Double> cache) |
| 74 | { |
| 75 | if(cache == null) |
| 76 | return Math.pow(vecs.get(i).pNormDist(2.0, y), 2); |
| 77 | return cache.get(i)+qi.get(0)-2*vecs.get(i).dot(y); |
nothing calls this directly
no outgoing calls
no test coverage detected