Rubix ML Documentation

repository·master·Indexed 25 days ago

https://github.com/rubixml/ml

A high-level machine learning and deep learning library for PHP. Rubix ML provides over 40 supervised and unsupervised learning algorithms and tools for the entire ML lifecycle, including ETL, preprocessing, training, cross-validation, and production deployment.

Tokens
78.2K
Snippets
274
Records
540
Agent score
80%

What's inside Rubix ML

  1. Overview of Rubix ML

    master

    Rubix ML is a high-level machine learning and deep learning library for PHP. It provides tools for the entire machine learning lifecycle, including:

    • ETL (Extract, Transform, Load): Data ingestion and preparation.
    • Preprocessing: Cleaning and transforming data.
    • Algorithms: Over 40 supervised and unsupervised learning algorithms.
    • Training & Cross-validation: Evaluating model performance.
    • Production: Deploying models for real-world use.
  2. What is a Persistent Model and how to use it

    master

    A PersistentModel is a meta-estimator that wraps a Persistable learner to provide functionality for saving and loading trained models to and from storage. It acts as a bridge between your machine learning model and a storage backend via a Persister (such as Filesystem).

    When using PersistentModel, you can:

    1. Wrap a learner: Pass a Persistable learner, a Persister, and a Serializer to the constructor.
    2. Save a model: Call the save() method to write the current state of the model to the configured storage.
    3. Load a model: Use the static load() method to reconstruct a model from an existing storage backend.
    use Rubix\ML\PersistentModel;
    use Rubix\ML\Clusterers\KMeans;
    use Rubix\ML\Persisters\Filesystem;
    use Rubix\ML\Serializers\RBX;
    
    $estimator = new PersistentModel(new KMeans(10), new Filesystem('example.model'), new RBX());
  3. What is a Vantage Point Tree (VPTree)?

    master

    A Vantage Point Tree (VPTree) is a binary spatial tree used for efficient nearest neighbor searches in metric spaces. It works by selecting a 'vantage point' (the center of a cluster) and dividing samples based on their distance from that point. Samples closer to the vantage point are assigned to one branch, while samples farther away are assigned to the other.

    It implements the Binary Tree and Spatial interfaces. Its compatibility with specific data types depends on the distance kernel used.

  4. What is a Pipeline and how does it work?

    master

    A Pipeline is a meta-estimator that transforms an input Dataset by applying a sequence of Transformer middleware before passing the data to a base estimator.

    When you call a method on the Pipeline (like train or predict), it automatically fits the training set and transforms the dataset through the provided transformers. The transformed data is then handed to the base estimator.

    Key Behaviors:

    • Dataset Modification: The Pipeline modifies the input dataset during the fitting process. If you need to preserve the original, unmodified dataset in memory, you must clone the dataset object before passing it to the Pipeline.
    • Elastic Mode: If the elastic parameter is set to true, the Pipeline will automatically update the fitting of Elastic transformers during partial training (online learning).
    • Compatibility: The data type compatibility of a Pipeline depends on the specific transformers and the base learner used.
    use Rubix\
    ML\
    Pipeline;
    use Rubix\
    ML\
    Transformers\
    MissingDataImputer;
    use Rubix\
    ML\
    Transformers\
    OneHotEncoder;
    use Rubix\
    ML\
    Transformers\
    PrincipalComponentAnalysis;
    use Rubix\
    ML\
    Classifiers\
    SoftmaxClassifier;
    
    $estimator = new Pipeline([
    	new MissingDataImputer(),
    	new OneHotEncoder(), 
    	new PrincipalComponentAnalysis(20),
    ], new SoftmaxClassifier(128), true);
  5. What is a Committee Machine?

    master

    A CommitteeMachine is a voting ensemble that aggregates predictions from a group of heterogeneous learners, known as experts. It uses an influence scheme to weight the final predictions. Because influence values are automatically normalized upon instantiation, you can provide them on any arbitrary scale.

    It implements the Estimator, Learner, Parallel, and Persistable interfaces, meaning its data type compatibility depends on the base learners provided to it.

  6. Use the Polynomial Expander transformer

    master

    The PolynomialExpander transformer generates polynomials up to a specified degree for each continuous feature in your dataset. This is useful for enabling linear estimators (like Ridge, Logistic Regression, or Softmax Classifier) to model non-linear relationships in the data.

    Important Constraints:

    • Data Type Compatibility: This transformer only works with continuous features. It is not compatible with categorical features.
    • Interface: Implements Transformer.
    use Rubix\
    ML\\Transformers\\PolynomialExpander;
    
    $transformer = new PolynomialExpander(3);
  7. Use the IntervalDiscretizer transformer

    master

    The IntervalDiscretizer is a transformer used to assign continuous features to ordered categories. It achieves this by creating variable-width per-feature histograms based on a fixed number of bins specified by the user. It is compatible with continuous data types and implements the Transformer, Stateful, and Persistable interfaces.

    use Rubix\
    ML\\Transformers\\IntervalDiscretizer;
    
    $transformer = new IntervalDiscretizer(8, false);
  8. Use PReLU activation layers

    master

    Parametric Rectified Linear Units (PReLU) are leaky rectifiers where the leakage coefficient is learned during training. Unlike standard Leaky ReLU layers which have a constant leakage, PReLU layers adjust the leakage coefficient for each node to better suit the model.

    Mathematically, PReLU is defined as:

    $${\displaystyle PReLU = {\begin{cases}\alpha x&{\text{if }}x<0\x&{\text{if }}x\geq 0\end{cases}}}$$

  9. How Stateful Transformers work

    master

    Stateful transformers require a fitting phase before they can be used to transform data. This process is similar to training a machine learning model.

    Fitting a Transformer

    Use the fit(Dataset $dataset) method to pre-compute necessary information (like means or standard deviations) from a dataset. After fitting, you can check the status using the fitted() method, which returns a bool.

    Automatic Fitting

    When using a stateful transformer with a Dataset object via the apply() method, Rubix ML automatically calls fit() with the provided dataset before performing the transformation. This simplifies the workflow for standard training pipelines.

  10. Use the Wild Guess strategy for continuous data

    master

    The WildGuess strategy is used for continuous data types. It works by guessing a random number within the range between the minimum and maximum values computed from the fitted collection of values.

    use Rubix\
    ML\\Strategies\\WildGuess;
    
    $strategy = new WildGuess();
  11. Use the AdaGrad optimizer for Neural Networks

    master

    The AdaGrad (Adaptive Gradient) optimizer adjusts the learning rate for each parameter individually. It speeds up learning for parameters that change infrequently and slows down learning for parameters that are frequently updated.

    Note: Because AdaGrad uses an infinitely decaying step size, training might become slow or fail to converge if the learning rate is set too low.

    use Rubix\
    ML\\NeuralNet\\Optimizers\\AdaGrad;
    
    $optimizer = new AdaGrad(0.125);
  12. Use V-Measure to evaluate clustering performance

    master

    V-Measure is an entropy-based metric used to evaluate the quality of clustering results. It balances two key components: Homogeneity and Completeness. A unique property of V-Measure is that it is symmetric, meaning you can swap predictions and ground-truth labels without affecting the score.

    • Estimator Compatibility: Clusterers
    • Score Range: 0 to 1 (where 1 is a perfect score)

    To use it, instantiate the VMeasure class with a beta parameter that determines the weight given to homogeneity versus completeness.