2014-05-02 21:10:55 +08:00
|
|
|
package pairwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2014-05-03 00:55:38 +08:00
|
|
|
"github.com/gonum/matrix/mat64"
|
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)
|
2014-05-03 01:03:29 +08:00
|
|
|
func (self *RBFKernel) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.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-05-03 23:05:24 +08:00
|
|
|
result := math.Exp(-self.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
|
|
|
}
|