|
| 1 | +# ================================================================ |
| 2 | +# AbstractGraphKernel |
| 3 | +# ================================================================ |
| 4 | + |
| 5 | +""" |
| 6 | + abstract type AbstractGraphKernel |
| 7 | +
|
| 8 | +A kernel function between two graphs. |
| 9 | +
|
| 10 | +Subtypes of `AbstractGraphKernel` should implement `preprocessed_form` and `apply_preprocessed`. |
| 11 | +When `(k::AbstractGraphKernel)(g1, g2)` is invoked on two graphs, then |
| 12 | +``` |
| 13 | +apply_preprocessed(k, preprocessed_form(k, g1), preprocessed_form(k, g2)) |
| 14 | +``` |
| 15 | +is called to calculate the kernel function. Therefore one should implement `preprocessed_form` |
| 16 | +that transforms a single graph into a suitable representation and `apply_preprocessed` that |
| 17 | +takes the representations for both graphs and calculates the kernel function. |
| 18 | +
|
| 19 | +### See also |
| 20 | +[`preprocessed_form`](@ref), [`apply_preprocessed`](@ref), [`kernel_matrix`](@ref), [`kernel_matrix_diag`](@ref) |
| 21 | +
|
| 22 | +""" |
| 23 | +abstract type AbstractGraphKernel end |
| 24 | + |
| 25 | +""" |
| 26 | + preprocessed_form(k::AbstractGraphKernel, g::AbstractGraph) = g |
| 27 | +
|
| 28 | +Transform a graph `g` into a suitable form for a graph kernel `k` |
| 29 | +
|
| 30 | +When calculating a pairwise kernel matrix for multiple graphs, this preprocessed form |
| 31 | +allows us to calculate the transformation only once for each graph, so that we can cache |
| 32 | +the result. By default this simply returns `g` without any transformation. |
| 33 | +
|
| 34 | +When implementing a custom graph kernel, it might be a good idea to implement this |
| 35 | +method. |
| 36 | +
|
| 37 | +### See also |
| 38 | +[`AbstractGraphKernel`](@ref), [`apply_preprocessed`](@ref) |
| 39 | +""" |
| 40 | +preprocessed_form(::AbstractGraphKernel, g::AbstractGraph) = g |
| 41 | + |
| 42 | +function (kernel::AbstractGraphKernel)(g1, g2) |
| 43 | + |
| 44 | + return apply_preprocessed(kernel, preprocessed_form(kernel, g1), preprocessed_form(kernel, g2)) |
| 45 | +end |
| 46 | + |
| 47 | +## --------------------------------------------------------------- |
| 48 | +## kernelmatrix & kernelmatrix_diag |
| 49 | +## --------------------------------------------------------------- |
| 50 | + |
| 51 | +function _map_preprocessed_form(kernel::AbstractGraphKernel, graphs) |
| 52 | + |
| 53 | + # TODO we should be able to avoid collecting the graphs |
| 54 | + # but currently ThreadX cannot split them otherwise, |
| 55 | + # maybe we can create some wrapper type that is splitable around graphs |
| 56 | + return ThreadsX.map(g -> preprocessed_form(kernel, g), collect(graphs)) |
| 57 | +end |
| 58 | + |
| 59 | +""" |
| 60 | + kernelmatrix(kernel, graphs) |
| 61 | +Return a matrix of running the kernel on all pairs of graphs. |
| 62 | +
|
| 63 | +### See also |
| 64 | +[`kernelmatrix_diag`](@ref) |
| 65 | +""" |
| 66 | +function kernelmatrix(kernel::AbstractGraphKernel, graphs) |
| 67 | + |
| 68 | + pre = _map_preprocessed_form(kernel, graphs) |
| 69 | + |
| 70 | + # this simply a guard to make the code more type save, maybe we can get |
| 71 | + # rid of it at some point |
| 72 | + return _kernelmatrix_from_preprocessed(kernel, pre) |
| 73 | +end |
| 74 | + |
| 75 | +function _kernelmatrix_from_preprocessed(kernel, pre) |
| 76 | + |
| 77 | + n = length(pre) |
| 78 | + |
| 79 | + # TODO maybe we should make the matrix only symmetric afterwards |
| 80 | + # so that we avoid false sharing when using multiple threads |
| 81 | + # TODO create some triangle generator instead of allocating a vector |
| 82 | + # TODO apparently ThreadsX can do load balancing so we should consider that here |
| 83 | + G = Matrix{Float64}(undef, n, n) |
| 84 | + indices = [(i, j) for i in 1:n for j in i:n] |
| 85 | + Threads.@threads for idx in indices |
| 86 | + i, j = idx |
| 87 | + @inbounds v = apply_preprocessed(kernel, pre[i], pre[j]) |
| 88 | + @inbounds G[i, j] = v |
| 89 | + @inbounds G[j, i] = v |
| 90 | + end |
| 91 | + |
| 92 | + return G |
| 93 | +end |
| 94 | + |
| 95 | +""" |
| 96 | + kernelmatrix_diag(kernel::AbstractGraphKernel, graphs) |
| 97 | +
|
| 98 | +Calculate the diagonal of the kernelmatrix matrix of the graphs. |
| 99 | +
|
| 100 | +### See also |
| 101 | +[`kernelmatrix`](@ref) |
| 102 | +""" |
| 103 | +function kernelmatrix_diag(kernel::AbstractGraphKernel, graphs) |
| 104 | + |
| 105 | + n = length(graphs) |
| 106 | + pre = _map_preprocessed_form(kernel, graphs) |
| 107 | + |
| 108 | + D = Vector{Float64}(undef, n) |
| 109 | + Threads.@threads for i in 1:n |
| 110 | + @inbounds D[i] = apply_preprocessed(kernel, pre[i], pre[i]) |
| 111 | + end |
| 112 | + return D |
| 113 | +end |
| 114 | + |
| 115 | +""" |
| 116 | + kernelmatrix(kernel::AbstractGraphKernel, graphs1, graphs2) |
| 117 | +
|
| 118 | +Calculate a matrix of invoking the kernel on all pairs. |
| 119 | +Entry `(i, j)` of the resulting matrix contains `kernel(graphs1[i], graphs2[j]`. |
| 120 | +""" |
| 121 | +function kernelmatrix(kernel::AbstractGraphKernel, graphs1, graphs2) |
| 122 | + |
| 123 | + n_rows = length(graphs1) |
| 124 | + n_cols = length(graphs2) |
| 125 | + |
| 126 | + M = Matrix{Float64}(undef, n_rows, n_cols) |
| 127 | + |
| 128 | + pre1 = _map_preprocessed_form(kernel, graphs1) |
| 129 | + pre2 = _map_preprocessed_form(kernel, graphs2) |
| 130 | + |
| 131 | + Threads.@threads for i in 1:n_rows |
| 132 | + for j in 1:n_cols |
| 133 | + @inbounds M[i, j] = apply_preprocessed(kernel, pre1[i], pre2[j]) |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + return M |
| 138 | +end |
| 139 | + |
0 commit comments