Sigmoid (Hyperbolic Tangent) Kernel implementation.
Classes
Sigmoid (Hyperbolic Tangent) Kernel inspired by neural networks.
The Sigmoid Kernel computes the hyperbolic tangent of a scaled dot product, producing a response analogous to a single hidden-layer neural network. It is sometimes called the MLP kernel or tanh kernel.
Constructor
__init__( self, gamma: float = 0.01, coef0: float = 0.0, cache_size: int = 250007, )
Overview
The kernel evaluation proceeds as follows:
- Compute the dot product \langle x, y \rangle
-
Scale by
gammaand add the independent termcoef0 - Apply the hyperbolic tangent function to obtain a value in (-1, 1)
Theory
The sigmoid kernel function is defined as:
K(x, y) = \tanh(\gamma \, \langle x, y \rangle + c_0)
where:
- \gamma --- Scaling coefficient for the dot product
- c_0 --- Independent (bias) term
This kernel is **not positive semi-definite** for all parameter values. It satisfies Mercer's condition only for certain combinations of and . Invalid parameters may lead to non-convergent SVM solutions.
Parameters
gamma
float
= 0.01
Coefficient for the dot product.
coef0
float
= 0.0
Independent (bias) term.
cache_size
int
= 250007
Maximum number of cached kernel evaluations.
Attributes
X\_
np.ndarray
Training data stored after
build().
n_samples\_
int
Number of training samples.
Notes
Complexity:
- Single evaluation: O(p) where p = number of features
- Matrix computation: O(n^2 p) for n samples
- When a neural-network-like non-linearity is desired
- As a proxy for a single-layer perceptron in kernel space
- Experimental comparisons with other kernels (RBF, polynomial)
-
When
gamma > 0andcoef0 < 0for valid Mercer conditions
References
Lin2003
Lin, H.T. and Lin, C.J. (2003).
A Study on Sigmoid Kernels for SVM and the Training of non-PSD Kernels by SMO-type Methods.
National Taiwan University Technical Report.
Scholkopf2002
Schoelkopf, B. and Smola, A.J. (2002).
Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond.
MIT Press.
See Also
Basic usage with a negative bias term:
python
>>> from tuiml.algorithms.svm.kernels import SigmoidKernel
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4]])
>>> kernel = SigmoidKernel(gamma=0.01, coef0=-1.0)
>>> kernel.build(X_train)
SigmoidKernel(...)
>>> value = kernel.evaluate(X_train[0], X_train[1])
Methods
compute_matrix_cross
(self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray
compute_matrix_cross
(self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray
Compute the sigmoid kernel matrix between two sample sets.
Parameters
X1
np.ndarray of shape (n1, n_features)
First set of samples.
X2
np.ndarray of shape (n2, n_features)
Second set of samples.
Returns
K
np.ndarray of shape (n1, n2)
Kernel matrix.