Expand description
§Tensorlogic-SkleaRS-Kernels
Version: 0.1.0-beta.1 | Status: Production Ready
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 methods, etc.).
§Features
- ✅ Rule Similarity Kernels - Measure similarity by rule satisfaction agreement
- ✅ Predicate Overlap Kernels - Similarity based on shared true predicates
- ✅ Tensor Kernels - Classical kernels (Linear, RBF, Polynomial, Cosine)
- ✅ Kernel Composition - Combine multiple kernels
- ✅ Type-Safe API - Builder pattern with validation
- ✅ Comprehensive Tests - 25+ tests covering all components
§Architecture
§Kernel Trait
All kernels implement the Kernel trait:
use tensorlogic_sklears_kernels::{Kernel, LinearKernel};
let kernel = LinearKernel::new();
let x = vec![1.0, 2.0, 3.0];
let y = vec![4.0, 5.0, 6.0];
let similarity = kernel.compute(&x, &y).unwrap();§Logic-Derived Kernels
§Rule Similarity
Measures similarity based on which rules are satisfied:
use tensorlogic_sklears_kernels::{
RuleSimilarityKernel, RuleSimilarityConfig, Kernel
};
use tensorlogic_ir::TLExpr;
// Define logical rules
let rules = vec![
TLExpr::pred("rule1", vec![]),
TLExpr::pred("rule2", vec![]),
TLExpr::pred("rule3", vec![]),
];
// Configure similarity weights
let config = RuleSimilarityConfig::new()
.with_satisfied_weight(1.0) // Both satisfy
.with_violated_weight(0.5) // Both violate
.with_mixed_weight(0.0); // Disagree
let kernel = RuleSimilarityKernel::new(rules, config).unwrap();
// Compute similarity
let x = vec![1.0, 1.0, 0.0]; // Satisfies first two rules
let y = vec![1.0, 1.0, 1.0]; // Satisfies all three rules
let sim = kernel.compute(&x, &y).unwrap();§Predicate Overlap
Measures similarity by counting shared true predicates:
use tensorlogic_sklears_kernels::{PredicateOverlapKernel, Kernel};
let kernel = PredicateOverlapKernel::new(4);
let x = vec![1.0, 1.0, 0.0, 0.0]; // First two predicates true
let y = vec![1.0, 1.0, 1.0, 0.0]; // First three predicates true
let sim = kernel.compute(&x, &y).unwrap();
// Similarity = 2/4 = 0.5 (two shared true predicates)§Tensor-Based Kernels
§Linear Kernel
use tensorlogic_sklears_kernels::{LinearKernel, Kernel};
let kernel = LinearKernel::new();
let x = vec![1.0, 2.0, 3.0];
let y = vec![4.0, 5.0, 6.0];
let sim = kernel.compute(&x, &y).unwrap();
// Similarity = dot(x, y) = 1*4 + 2*5 + 3*6 = 32§RBF (Gaussian) Kernel
use tensorlogic_sklears_kernels::{RbfKernel, RbfKernelConfig, Kernel};
let config = RbfKernelConfig::new(0.5); // gamma = 0.5
let kernel = RbfKernel::new(config).unwrap();
let x = vec![1.0, 2.0, 3.0];
let y = vec![1.0, 2.0, 3.0];
let sim = kernel.compute(&x, &y).unwrap();
// Similarity = exp(-gamma * ||x-y||^2) = 1.0 (same vectors)§Polynomial Kernel
use tensorlogic_sklears_kernels::{PolynomialKernel, Kernel};
let kernel = PolynomialKernel::new(2, 1.0).unwrap(); // degree=2, constant=1
let x = vec![1.0, 2.0];
let y = vec![3.0, 4.0];
let sim = kernel.compute(&x, &y).unwrap();
// Similarity = (dot(x,y) + c)^d = (11 + 1)^2 = 144§Cosine Similarity
use tensorlogic_sklears_kernels::{CosineKernel, Kernel};
let kernel = CosineKernel::new();
let x = vec![1.0, 2.0, 3.0];
let y = vec![2.0, 4.0, 6.0]; // Parallel to x
let sim = kernel.compute(&x, &y).unwrap();
// Similarity = cos(angle) = 1.0 (parallel vectors)§Kernel Matrix Computation
All kernels support efficient matrix computation:
use tensorlogic_sklears_kernels::{LinearKernel, Kernel};
let kernel = LinearKernel::new();
let inputs = vec![
vec![1.0, 2.0],
vec![3.0, 4.0],
vec![5.0, 6.0],
];
let matrix = kernel.compute_matrix(&inputs).unwrap();
// matrix[i][j] = kernel(inputs[i], inputs[j])
// Symmetric positive semi-definite matrix§Integration with TensorLogic
Kernels can be built from compiled TensorLogic expressions:
ⓘ
use tensorlogic_ir::TLExpr;
use tensorlogic_compiler::CompilerContext;
use tensorlogic_sklears_kernels::RuleSimilarityKernel;
// Define logical rules
let rule = TLExpr::exists(
"x",
"Person",
TLExpr::pred("likes", vec![/* ... */]),
);
// Compile to kernel
let kernel = RuleSimilarityKernel::from_rules(vec![rule]).unwrap();
// Use in machine learning
let data = vec![/* feature vectors */];
let kernel_matrix = kernel.compute_matrix(&data).unwrap();§Use Cases
§1. Kernel SVM with Logical Features
Use rule satisfaction as features for SVM:
ⓘ
let rules = extract_rules_from_knowledge_base();
let kernel = RuleSimilarityKernel::new(rules, config).unwrap();
let svm = KernelSVM::new(kernel);
svm.fit(training_data, labels);§2. Semantic Similarity
Measure semantic similarity between documents/entities:
ⓘ
let predicates = extract_predicates_from_text(document);
let kernel = PredicateOverlapKernel::new(predicates.len());
let similarity = kernel.compute(&doc1_features, &doc2_features).unwrap();§3. Hybrid Kernels
Combine logical and tensor-based features:
ⓘ
let logic_kernel = RuleSimilarityKernel::new(rules, config).unwrap();
let tensor_kernel = RbfKernel::new(RbfKernelConfig::new(0.5)).unwrap();
// Weighted combination
let alpha = 0.7;
let similarity = alpha * logic_kernel.compute(x, y).unwrap()
+ (1.0 - alpha) * tensor_kernel.compute(x_emb, y_emb).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
- Interpretability: Clear mapping from logic to similarity
Re-exports§
pub use ard_kernel::ArdMaternKernel;pub use ard_kernel::ArdRationalQuadraticKernel;pub use ard_kernel::ArdRbfKernel;pub use ard_kernel::ConstantKernel;pub use ard_kernel::DotProductKernel;pub use ard_kernel::KernelGradient;pub use ard_kernel::ScaledKernel;pub use ard_kernel::WhiteNoiseKernel;pub use cache::CacheStats;pub use cache::CachedKernel;pub use cache::KernelMatrixCache;pub use composite_kernel::KernelAlignment;pub use composite_kernel::ProductKernel;pub use composite_kernel::WeightedSumKernel;pub use error::KernelError;pub use error::Result;pub use feature_extraction::FeatureExtractionConfig;pub use feature_extraction::FeatureExtractor;pub use graph_kernel::Graph;pub use graph_kernel::RandomWalkKernel;pub use graph_kernel::SubgraphMatchingConfig;pub use graph_kernel::SubgraphMatchingKernel;pub use graph_kernel::WalkKernelConfig;pub use graph_kernel::WeisfeilerLehmanConfig;pub use graph_kernel::WeisfeilerLehmanKernel;pub use kernel_selection::CrossValidationResult;pub use kernel_selection::GammaSearchResult;pub use kernel_selection::KFoldConfig;pub use kernel_selection::KernelComparison;pub use kernel_selection::KernelSelector;pub use kernel_transform::NormalizedKernel;pub use logic_kernel::PredicateOverlapKernel;pub use logic_kernel::RuleSimilarityKernel;pub use low_rank::NystromApproximation;pub use low_rank::NystromConfig;pub use low_rank::SamplingMethod;pub use multitask::HadamardTaskKernel;pub use multitask::ICMKernel;pub use multitask::ICMKernelWrapper;pub use multitask::IndexKernel;pub use multitask::LMCKernel;pub use multitask::LMCKernelWrapper;pub use multitask::MultiTaskConfig;pub use multitask::MultiTaskKernelBuilder;pub use multitask::TaskInput;pub use online::AdaptiveKernelMatrix;pub use online::ForgetfulConfig;pub use online::ForgetfulKernelMatrix;pub use online::OnlineConfig;pub use online::OnlineKernelMatrix;pub use online::OnlineStats;pub use online::WindowedKernelMatrix;pub use provenance::ComputationResult;pub use provenance::ProvenanceConfig;pub use provenance::ProvenanceId;pub use provenance::ProvenanceKernel;pub use provenance::ProvenanceRecord;pub use provenance::ProvenanceStatistics;pub use provenance::ProvenanceTracker;pub use random_features::KernelType as RffKernelType;pub use random_features::NystroemFeatures;pub use random_features::OrthogonalRandomFeatures;pub use random_features::RandomFourierFeatures;pub use random_features::RffConfig;pub use sparse::SparseKernelMatrix;pub use sparse::SparseKernelMatrixBuilder;pub use spectral_kernel::ExpSineSquaredKernel;pub use spectral_kernel::LocallyPeriodicKernel;pub use spectral_kernel::RbfLinearKernel;pub use spectral_kernel::SpectralComponent;pub use spectral_kernel::SpectralMixtureKernel;pub use string_kernel::EditDistanceKernel;pub use string_kernel::NGramKernel;pub use string_kernel::NGramKernelConfig;pub use string_kernel::SubsequenceKernel;pub use string_kernel::SubsequenceKernelConfig;pub use symbolic::KernelBuilder;pub use symbolic::KernelExpr;pub use symbolic::SymbolicKernel;pub use tensor_kernels::ChiSquaredKernel;pub use tensor_kernels::CosineKernel;pub use tensor_kernels::HistogramIntersectionKernel;pub use tensor_kernels::LaplacianKernel;pub use tensor_kernels::LinearKernel;pub use tensor_kernels::MaternKernel;pub use tensor_kernels::PeriodicKernel;pub use tensor_kernels::PolynomialKernel;pub use tensor_kernels::RationalQuadraticKernel;pub use tensor_kernels::RbfKernel;pub use tensor_kernels::SigmoidKernel;pub use tree_kernel::PartialTreeKernel;pub use tree_kernel::PartialTreeKernelConfig;pub use tree_kernel::SubsetTreeKernel;pub use tree_kernel::SubsetTreeKernelConfig;pub use tree_kernel::SubtreeKernel;pub use tree_kernel::SubtreeKernelConfig;pub use tree_kernel::TreeNode;pub use types::Kernel;pub use types::PredicateOverlapConfig;pub use types::RbfKernelConfig;pub use types::RuleSimilarityConfig;
Modules§
- ard_
kernel - Automatic Relevance Determination (ARD) kernels.
- cache
- Kernel caching infrastructure for performance optimization.
- composite_
kernel - Composite kernels for combining multiple kernel functions.
- error
- Error types for tensorlogic-sklears-kernels.
- feature_
extraction - Automatic feature extraction from logical expressions.
- gradient
- Matrix-level gradient computation for kernel hyperparameter optimization.
- graph_
kernel - Graph kernels for measuring similarity between structured data.
- kernel_
selection - Kernel selection and cross-validation utilities.
- kernel_
transform - Kernel transformation utilities for preprocessing and normalization.
- kernel_
utils - Kernel utility functions for machine learning workflows.
- kpca
- Kernel Principal Component Analysis (KPCA) utilities.
- logic_
kernel - Logic-derived similarity kernels.
- low_
rank - Low-rank kernel matrix approximations
- multitask
- Multi-task kernel learning for related tasks with shared representations.
- online
- Online kernel updates for streaming and incremental learning.
- provenance
- Provenance tracking for kernel computations
- random_
features - Random Fourier Features (RFF) for scalable kernel approximation.
- sparse
- Sparse kernel matrix support for large-scale problems.
- spectral_
kernel - Spectral Mixture kernels for discovering latent periodic components.
- string_
kernel - String kernels for text similarity.
- symbolic
- Symbolic kernel composition and algebraic operations
- tensor_
kernels - Auto-generated module structure
- tree_
kernel - Tree kernels for structured data similarity
- types
- Core types for kernel operations.
Functions§
- logic_
kernel_ similarity Deprecated