2014-05-02 21:10:55 +08:00
|
|
|
package pairwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2018-03-23 23:39:55 +00:00
|
|
|
"gonum.org/v1/gonum/mat"
|
2014-05-02 21:10:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RBFKernel struct {
|
|
|
|
gamma float64
|
|
|
|
}
|
|
|
|
|
2014-07-18 13:48:28 +03:00
|
|
|
// NewRBFKernel returns a representation of a Radial Basis Function Kernel
|
2014-05-02 21:10:55 +08:00
|
|
|
func NewRBFKernel(gamma float64) *RBFKernel {
|
|
|
|
return &RBFKernel{gamma: gamma}
|
|
|
|
}
|
|
|
|
|
2014-07-18 13:48:28 +03:00
|
|
|
// InnerProduct computes the inner product through a kernel trick
|
2014-05-03 23:05:24 +08:00
|
|
|
// K(x, y) = exp(-gamma * ||x - y||^2)
|
2018-03-23 23:39:55 +00:00
|
|
|
func (r *RBFKernel) InnerProduct(vectorX *mat.Dense, vectorY *mat.Dense) float64 {
|
2014-05-02 21:10:55 +08:00
|
|
|
euclidean := NewEuclidean()
|
2014-05-03 01:03:29 +08:00
|
|
|
distance := euclidean.Distance(vectorX, vectorY)
|
2014-05-02 21:10:55 +08:00
|
|
|
|
2014-07-18 13:58:07 +03:00
|
|
|
result := math.Exp(-r.gamma * math.Pow(distance, 2))
|
2014-05-02 21:10:55 +08:00
|
|
|
|
2014-05-03 01:03:29 +08:00
|
|
|
return result
|
2014-05-02 21:10:55 +08:00
|
|
|
}
|