File: Linear.php
File: Linear.php
Purpose
The Linear.php file is part of the PML (Palm Machine Learning) framework and contains a class for computing the linear kernel between two matrices. The linear kernel is a fundamental component in many machine learning algorithms, such as Support Vector Machines (SVMs), where it measures the similarity between two vectors.
Key Components
Classes
- Linear: A final class implementing the
Kernelinterface that computes the dot product (linear kernel) between two matrices using optimized matrix multiplication through OpenBLAS.
Functions / Methods
- compute(Tensor $a, Tensor $b): Tensor
- Computes the linear kernel between two tensors
aandb. - Parameters:
$a: The first tensor of typeTensor.$b: The second tensor of typeTensor.
- Returns: A new
Tensorrepresenting the dot product of matricesaandb.
- Computes the linear kernel between two tensors
Important Variables / Constants
- None.
Inputs / Outputs
For ML Components
- Inputs:
- Two tensors (
$aand$b) of compatible shapes for matrix multiplication.- Example input shape for both tensors might be
[n_samples, n_features].
- Example input shape for both tensors might be
- Two tensors (
- Outputs:
- A new
Tensorrepresenting the dot product (linear kernel) of the two input tensors. The output tensor will have a shape depending on the dimensions of the inputs.
- A new
For Utility Files
- None.
Dependencies
- Pml\Tensor: The
Linearclass uses this class for handling tensor operations. - OpenBLAS: Used for efficient matrix multiplication via the
matmulmethod in theTensorclass.
Usage Notes
Integration with the Framework
The Linear class is designed to be used within the PML framework, particularly in SVM implementations where kernel computations are essential. It relies on the Pml\Tensor class for tensor operations and optimization.
Edge Cases
- Ensure that both input tensors (
$aand$b) have compatible shapes for matrix multiplication.- If the number of features in
$adoes not match the number of samples (or vice versa) in$b, an error will occur during execution.
- If the number of features in
Performance Considerations
- The use of OpenBLAS ensures that the matrix multiplication is optimized for memory usage and execution speed, making this kernel suitable for large datasets.
- Ensure that the tensors are pre-allocated with the correct data types to avoid runtime errors.
Example Usage
Here’s an example of how you might use the Linear class in a hypothetical SVM implementation:
use Pml\Kernels\SVM\Linear;
use Pml\Tensor;
// Create two sample matrices
$a = new Tensor([[1, 2], [3, 4]]);
$b = new Tensor([[5, 6], [7, 8]]);
// Create a Linear kernel instance
$kernel = new Linear();
// Compute the linear kernel between the two matrices
$result = $kernel->compute($a, $b);
echo $result; // Output should be the dot product of a and b
This example demonstrates how to compute the linear kernel between two sample matrices using the Linear class. The resulting tensor will contain the computed dot products, which can then be used in further steps such as training an SVM model.