Sigmoidal.php
Sigmoidal.php
Purpose
The Sigmoidal.php file contains a class that implements the Sigmoidal (Hyperbolic Tangent) kernel for use in Support Vector Machines (SVM). This kernel is designed to measure similarity between samples in high-dimensional spaces and is particularly useful when the data is non-linearly separable.
Key Components
Classes, Functions, Methods with Signatures
- Class: Sigmoidal
- Implements the
Kernelinterface. - Contains a constructor for initializing the kernel parameters.
- Contains a method
computethat calculates the kernel value between two tensors.
- Implements the
- Method:
__constructpublic function __construct( private readonly float $gamma = 0.001, private readonly float $coef0 = 0.0 ) {}- Initializes the
gammaandcoef0parameters, which control the shape of the kernel.
- Initializes the
- Method:
computepublic function compute(Tensor $a, Tensor $b): Tensor- Computes the Sigmoidal kernel between two tensors.
- Parameters:
$a: ATensorrepresenting one set of samples.$b: ATensorrepresenting another set of samples.
- Returns: A
Tensorcontaining the computed kernel values.
Important Variables and Constants
- Variables:
$gamma: Controls the width of the Gaussian bell curve in the feature space. Default value is0.001.$coef0: Bias term added to the result. Default value is0.0.
Inputs / Outputs
For ML Components
- Inputs:
- Two tensors
$aand$b, each of shape(n_samples, n_features). These tensors represent the samples for which the kernel values are to be computed.
- Two tensors
- Outputs:
- A tensor of shape
(n_samples_a, n_samples_b)containing the Sigmoidal kernel values between each pair of samples from$aand$b.
- A tensor of shape
For Utility Files
- Parameters: None.
- Return Values: None (methods return tensors).
Dependencies
This class depends on the following components:
Pml\Tensor: A tensor library for handling multi-dimensional arrays efficiently.
Usage Notes
How This File Integrates with the Rest of the Framework
The Sigmoidal kernel can be used in SVM models to transform non-linearly separable data into a higher-dimensional space where it becomes linearly separable. It is typically used when the relationship between the input features and the target variable cannot be easily captured by linear functions.
Edge Cases, Performance Considerations
- Edge Cases:
- If
gammais set to zero, the kernel will degenerate into a simple dot product, which may not capture complex relationships in the data. - Large values of
coef0can lead to a more biased fit, potentially overfitting the training data.
- If
- Performance Considerations:
- The computation involves matrix multiplication and element-wise operations. For large datasets, these operations can be computationally expensive. Optimizations such as parallel processing or using specialized libraries (e.g., CUDA for GPU acceleration) may be necessary to handle large-scale data efficiently.
Example Usage
Here’s an example of how you might use the Sigmoidal kernel in a machine learning model:
use Pml\Kernels\SVM\Sigmoidal;
use Pml\Tensor;
// Initialize tensors
$a = new Tensor([[1, 2], [3, 4]]);
$b = new Tensor([[5, 6], [7, 8]]);
// Create a Sigmoidal kernel instance
$kernel = new Sigmoidal(gamma: 0.1, coef0: 0.5);
// Compute the kernel values
$result = $kernel->compute($a, $b);
This example initializes two tensors and computes their Sigmoidal kernel with custom gamma and coef0 parameters.