tensorlogic-sklears-kernels
Logic-derived similarity kernels for machine learning integration
This crate provides kernel functions that measure similarity based on logical rule satisfaction patterns, enabling TensorLogic to integrate with traditional machine learning algorithms (SVMs, kernel PCA, kernel ridge regression, etc.).
Features
Logic, Graph & Tree Kernels
- Rule Similarity Kernels - Measure similarity by rule satisfaction agreement
- Predicate Overlap Kernels - Similarity based on shared true predicates
- Graph Kernels - Subgraph matching, random walk, Weisfeiler-Lehman kernels
- Tree Kernels - Subtree, subset tree, and partial tree kernels for hierarchical data
- TLExpr Conversion - Automatic graph and tree extraction from logical expressions
Classical Kernels
- Linear Kernel - Inner product in feature space
- RBF (Gaussian) Kernel - Infinite-dimensional feature mapping
- Polynomial Kernel - Polynomial feature relationships
- Cosine Similarity - Angle-based similarity
- Laplacian Kernel - L1 distance, robust to outliers
- Sigmoid Kernel - Neural network inspired (tanh)
- Chi-Squared Kernel - For histogram data
- Histogram Intersection - Direct histogram overlap
Advanced Gaussian Process Kernels
- Matérn Kernel - Generalized RBF with smoothness control (nu=0.5, 1.5, 2.5)
- Rational Quadratic Kernel - Scale mixture of RBF kernels
- Periodic Kernel - For seasonal and cyclic patterns
ARD (Automatic Relevance Determination) Kernels
- ArdRbfKernel - ARD version of RBF/Gaussian kernel with per-dimension length scales
- ArdMaternKernel - ARD Matern kernel (nu=0.5, 1.5, 2.5)
- ArdRationalQuadraticKernel - ARD Rational Quadratic kernel
- KernelGradient - Gradient computation for hyperparameter optimization
GP Utility Kernels
- WhiteNoiseKernel - i.i.d. observation noise (K(x,y) = sigma^2 if x==y, else 0)
- ConstantKernel - Constant covariance (K(x,y) = sigma^2)
- DotProductKernel - Linear kernel with variance and bias
- ScaledKernel - Generic wrapper to scale any kernel
Spectral Kernels
- SpectralMixtureKernel - Mixture of spectral components for pattern discovery
- ExpSineSquaredKernel - Periodic kernel (scikit-learn compatible)
- LocallyPeriodicKernel - RBF x Periodic for decaying periodicity
- RbfLinearKernel - RBF x Linear product kernel
Kernel Selection and Cross-Validation
- KernelSelector - Kernel target alignment (KTA), leave-one-out error, K-fold CV
- KFoldConfig - K-fold CV configuration with shuffle support
- CrossValidationResult - Fold scores with statistics
- KernelComparison - Multi-kernel comparison results
- GammaSearchResult - Grid search results for RBF gamma
Random Fourier Features (Scalable Kernel Approximation)
- RandomFourierFeatures - O(nd) approximate kernel computation
- OrthogonalRandomFeatures - Improved variance via orthogonal projection
- NystroemFeatures - Nystrom-based feature approximation
- Supports RBF, Laplacian, Matern kernels
Kernel Gradient Computation
- Element-wise gradients for RBF, Polynomial, Matern, Laplacian, RationalQuadratic
- Matrix-level gradient computation (dK/dTheta)
- KernelGradientMatrix and GradientComponent structures
- Utilities for Gaussian Process hyperparameter optimization
Kernel PCA (KPCA)
- KernelPCA - Full KPCA implementation with fit/transform interface
- Eigenvalue-based variance analysis with explained_variance_ratio
- Automatic component selection via select_n_components
- Reconstruction error analysis
Online Kernel Updates
- OnlineKernelMatrix - Incremental O(n) updates
- WindowedKernelMatrix - Sliding window for time series
- ForgetfulKernelMatrix - Exponential decay for concept drift
- AdaptiveKernelMatrix - Automatic bandwidth adjustment
Multi-Task Kernel Learning
- IndexKernel - Task-based similarity
- ICMKernel - Intrinsic Coregionalization Model (B tensor K)
- LMCKernel - Linear Model of Coregionalization
- HadamardTaskKernel - Element-wise product
- MultiTaskKernelBuilder - Builder pattern for multi-task kernels
Batch Kernel Computation and Gram Matrix (v0.1.6)
- KernelCache - LRU cache with symmetric key normalization and hit rate statistics
- BatchKernelComputer - O(n²/2) Gram matrix computation with optional caching
- GramMatrix - Symmetry check, trace, Frobenius norm, PSD diagonal check
- KernelMatrixStats - Aggregate statistics for computed kernel matrices
Kernel Alignment (v0.1.18)
- KernelMatrix - Kernel matrix with centering, Frobenius inner product, Frobenius norm, and trace operations
- KernelTargetAlignment (KTA) - Uncentered alignment between a kernel matrix and an ideal label kernel
- CenteredKernelAlignment (CKA) - HSIC-normalized centered KTA robust to different-scale kernels
- HilbertSchmidtIndependenceCriterion (HSIC) - Statistical independence criterion between two kernel matrices
- AlignmentResult - Raw score, centered score, and HSIC value
- KernelAlignmentGridSearch - Exhaustive parameter grid search scored by CKA
- KernelAlignmentGradientAscent - Finite-difference gradient-free ascent to maximize alignment
- AlignmentError - Typed error enum for dimension mismatches and degenerate matrices
Composite and Performance Features
- Weighted Sum Kernels - Combine multiple kernels with weights
- Product Kernels - Multiplicative kernel combinations
- Kernel Caching - LRU cache with hit rate statistics (CachedKernel, KernelMatrixCache)
- Sparse Matrices - CSR format for memory-efficient storage (SparseKernelMatrix)
- Low-Rank Approximations - Nystrom method with three sampling strategies
- Performance Benchmarks - 5 benchmark suites with 47 benchmark groups
Text and Feature Processing
- String Kernels - N-gram, subsequence, edit distance kernels
- Feature Extraction - Automatic TLExpr to vector conversion (FeatureExtractor)
- Vocabulary Building - Predicate-based feature encoding
Kernel Transformations
- Matrix Normalization - Normalize to unit diagonal
- Matrix Centering - Center for kernel PCA
- Matrix Standardization - Combined normalization + centering
- NormalizedKernel - Auto-normalizing wrapper
Provenance Tracking
- Automatic Tracking - Track all kernel computations transparently
- Rich Metadata - Timestamps, computation time, input/output dimensions
- Query Interface - Filter by kernel type, tags, or time range
- JSON Export/Import - Serialize provenance for analysis and archival
- Performance Analysis - Aggregate statistics and profiling
- Tagged Experiments - Organize computations with custom tags
Symbolic Kernel Composition
- KernelExpr - Build kernels using algebraic operations (scale, add, multiply, power)
- SymbolicKernel - Evaluate symbolic expressions
- KernelBuilder - Declarative builder pattern for readability
- Expression simplification and PSD property checking
Quality Assurance
- 451 Tests - Comprehensive test coverage (100% passing)
- Zero Warnings - Strict code quality enforcement (clippy clean)
- Type-Safe API - Builder pattern with validation
- Production Ready - Battle-tested implementations
- Pure Rust RNG -
rand_09/rand_distr_05removed fromsklearsfeature; all random number generation usesscirs2_core::random
Quick Start
use ;
use TLExpr;
// Linear kernel for baseline
let linear = new;
let x = vec!;
let y = vec!;
let sim = linear.compute.unwrap;
// RBF (Gaussian) kernel
let rbf = new.unwrap;
let sim = rbf.compute.unwrap;
// Logic-based similarity
let rules = vec!;
let config = new;
let logic_kernel = new.unwrap;
let sim = logic_kernel.compute.unwrap;
Kernel Matrix Computation
All kernels support efficient matrix computation:
use ;
let kernel = new;
let inputs = vec!;
let matrix = kernel.compute_matrix.unwrap;
// matrix[i][j] = kernel(inputs[i], inputs[j])
// Symmetric positive semi-definite matrix
ARD Kernels
Automatic Relevance Determination kernels learn per-dimension length scales:
use ;
// Per-dimension length scales: [1.0, 2.0, 0.5]
let kernel = new;
let x = vec!;
let y = vec!;
let sim = kernel.compute.unwrap;
Kernel PCA
Nonlinear dimensionality reduction:
use ;
let kernel = Boxnew;
let config = KernelPCAConfig ;
let mut kpca = new;
// Fit and transform training data
kpca.fit.unwrap;
let projected = kpca.transform_training.unwrap;
// Access eigenvalues and explained variance
let ratios = kpca.explained_variance_ratio;
println!;
Random Fourier Features
Scalable kernel approximation for large datasets:
use ;
let config = RffConfig ;
let rff = new.unwrap;
// Transform inputs to approximate feature space
let features = rff.transform.unwrap;
// Approximate kernel via inner product
let approx = rff.approximate_kernel.unwrap;
Kernel Selection
Tools for hyperparameter tuning and model selection:
use ;
// K-fold cross-validation error
let config = KFoldConfig ;
let cv_result = k_fold_cv.unwrap;
println!;
// Grid search for RBF gamma
let result = grid_search_rbf_gamma.unwrap;
println!;
Integration with TensorLogic
Kernels integrate with compiled TensorLogic expressions:
use TLExpr;
use ;
// Extract features from TLExpr automatically
let mut extractor = new;
extractor.fit.unwrap;
let features = extractor.transform.unwrap;
// Build kernel from extracted features
let kernel = new.unwrap;
let kernel_matrix = kernel.compute_matrix.unwrap;
Design Philosophy
- Backend Independence: Kernels work with any feature representation
- Composability: Mix logical and tensor-based similarities
- Type Safety: Compile-time validation where possible
- Performance: Efficient matrix operations with caching and approximation
- Interpretability: Clear mapping from logic to similarity
- Extensibility: Symbolic composition and builder patterns
Testing
# 451 tests, all passing, zero warnings
Benchmarking
# 5 suites, 47 groups
Benchmark groups:
- Kernel computation (10 groups)
- Matrix operations (10 groups)
- Caching performance (8 groups)
- Composite kernels (10 groups)
- Graph kernels (9 groups)
License
Apache-2.0
Status: Production Ready (v0.1.0 Stable) Last Updated: 2026-04-06 Tests: 451/451 passing (100%) Benchmarks: 5 suites, 47 benchmark groups Part of: TensorLogic Ecosystem