Skip to main content

MLCore::LogitProjectionOperation

MLCore::LogitProjectionOperation

Supervised linear projection via softmax logit extraction. More…

#include <LogitProjectionOperation.hpp>

Inherits from MLCore::MLSupervisedDimReductionOperation, MLCore::MLModelOperation

Public Classes

Name
struct Impl

Public Functions

Name
LogitProjectionOperation()
~LogitProjectionOperation() override
LogitProjectionOperation(LogitProjectionOperation const & ) =delete
LogitProjectionOperation & operator=(LogitProjectionOperation const & ) =delete
LogitProjectionOperation(LogitProjectionOperation && )
LogitProjectionOperation & operator=(LogitProjectionOperation && )
virtual std::string getName() const override
Unique human-readable name for this model (e.g., “Random Forest”).
virtual MLTaskType getTaskType() const override
The ML task type this operation supports.
virtual std::unique_ptr< MLModelParametersBase > getDefaultParameters() const override
Create a default parameter set for this model.
virtual bool fitTransform(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params, arma::mat & result) override
Train softmax regression and project labeled data to logit space.
virtual bool transform(arma::mat const & features, arma::mat & result) override
Project new data using the fitted weight matrix.
virtual std::size_t outputDimensions() const override
Number of output dimensions (equals number of classes for logit projection).
virtual std::vector< std::string > outputColumnNames() const override
Column names for the output reduced matrix.
virtual bool save(std::ostream & out) const override
Save the trained model to a stream.
virtual bool load(std::istream & in) override
Load a previously saved model from a stream.
virtual bool isTrained() const override
Whether the model has been trained (supervised) or fit (unsupervised).
virtual std::size_t numClasses() const override
Number of classes (supervised) or clusters (unsupervised).
virtual std::size_t numFeatures() const override
Number of features the model was trained on.

Additional inherited members

Public Functions inherited from MLCore::MLSupervisedDimReductionOperation

Name
~MLSupervisedDimReductionOperation() override =default
virtual bool requiresAllClassesForFitting() const
Whether the model requires all class labels for fitting.
virtual bool train(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params) override
Route train() to fitTransform() discarding the projected output.

Public Functions inherited from MLCore::MLModelOperation

Name
virtual ~MLModelOperation() =default
virtual bool train(arma::mat const & features, arma::Row< std::size_t > const & labels, MLModelParametersBase const * params)
Train the model on labeled data.
virtual bool predict(arma::mat const & features, arma::Row< std::size_t > & predictions)
Predict class labels for new observations.
virtual bool predictProbabilities(arma::mat const & features, arma::mat & probabilities)
Predict class probabilities for new observations.
virtual bool trainSequences(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > const & labelSequences, MLModelParametersBase const * params)
Train the model on pre-segmented temporal sequences.
virtual bool predictSequences(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > & predictionSequences)
Predict labels for pre-segmented temporal sequences.
virtual bool predictSequencesConstrained(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > & predictionSequences, std::vector< std::optional< std::size_t > > const & initial_state_constraints)
Predict labels with per-sequence initial state constraints.
virtual bool predictSequencesBidirectionalConstrained(std::vector< arma::mat > const & featureSequences, std::vector< arma::Row< std::size_t > > & predictionSequences, std::vector< std::optional< std::size_t > > const & initial_state_constraints, std::vector< std::optional< std::size_t > > const & terminal_state_constraints)
Predict labels with both initial and terminal state constraints.
virtual bool predictProbabilitiesPerSequence(std::vector< arma::mat > const & featureSequences, std::vector< arma::mat > & probabilitySequences, std::vector< std::optional< std::size_t > > const & initial_state_constraints, std::vector< std::optional< std::size_t > > const & terminal_state_constraints)
Compute per-sequence state probabilities with boundary constraints.
virtual bool isSequenceModel() const
Whether this model natively handles temporal sequences.
virtual bool fit(arma::mat const & features, MLModelParametersBase const * params)
Fit the model to unlabeled data (unsupervised).
virtual bool assignClusters(arma::mat const & features, arma::Row< std::size_t > & assignments)
Assign cluster labels to observations.

Detailed Description

class MLCore::LogitProjectionOperation;

Supervised linear projection via softmax logit extraction.

Given labeled feature data (D × N) and integer class labels, trains a softmax regression model and extracts the weight matrix W (C × D) and bias b (C). The logit projection for new data X is:

result = W * X + b_broadcast (C × N)

Usage


LogitProjectionOperation lp;
LogitProjectionParameters params;
params.lambda = 0.001;

arma::mat logits;
lp.fitTransform(features, labels, &params, logits);  // (C × N) output

arma::mat new_logits;
lp.transform(new_features, new_logits);  // project without labels

// Column names: ["Logit:0", "Logit:1", ...]
auto names = lp.outputColumnNames();

Feature scaling (z-score normalization) is intentionally NOT performed here — it is the caller’s responsibility to pre-scale via FeatureConverter if desired.

Output convention: (C × N), each column is one observation. This matches the mlpack convention used throughout MLCore.

Public Functions Documentation

function LogitProjectionOperation

LogitProjectionOperation()

function ~LogitProjectionOperation

~LogitProjectionOperation() override

function LogitProjectionOperation

LogitProjectionOperation(
    LogitProjectionOperation const & 
) =delete

function operator=

LogitProjectionOperation & operator=(
    LogitProjectionOperation const & 
) =delete

function LogitProjectionOperation

LogitProjectionOperation(
    LogitProjectionOperation && 
)

function operator=

LogitProjectionOperation & operator=(
    LogitProjectionOperation && 
)

function getName

virtual std::string getName() const override

Unique human-readable name for this model (e.g., “Random Forest”).

Reimplements: MLCore::MLModelOperation::getName

Used as the key in MLModelRegistry and displayed in the UI.

function getTaskType

virtual MLTaskType getTaskType() const override

The ML task type this operation supports.

Reimplements: MLCore::MLModelOperation::getTaskType

Enables the registry to filter models by task (classification vs clustering).

function getDefaultParameters

virtual std::unique_ptr< MLModelParametersBase > getDefaultParameters() const override

Create a default parameter set for this model.

Reimplements: MLCore::MLModelOperation::getDefaultParameters

Returns a new instance with algorithm-specific defaults. The caller owns the returned pointer.

function fitTransform

virtual bool fitTransform(
    arma::mat const & features,
    arma::Row< std::size_t > const & labels,
    MLModelParametersBase const * params,
    arma::mat & result
) override

Train softmax regression and project labeled data to logit space.

Parameters:

  • features (D × N) feature matrix in mlpack convention
  • labels (N,) label row with class indices in [0, C)
  • params LogitProjectionParameters or nullptr for defaults
  • result (C × N) output logit matrix

Return: true on success

Precondition:

  • features must not be empty (enforcement: runtime_check)
  • labels.n_elem == features.n_cols (enforcement: runtime_check)
  • labels must contain at least 2 distinct classes (enforcement: runtime_check)
  • params is nullptr or points to a valid MLModelParametersBase (enforcement: runtime_check)
  • all values in features are finite — no NaN or Inf (enforcement: none) [IMPORTANT]

Postcondition:

Reimplements: MLCore::MLSupervisedDimReductionOperation::fitTransform

function transform

virtual bool transform(
    arma::mat const & features,
    arma::mat & result
) override

Project new data using the fitted weight matrix.

Parameters:

  • features (D × N) feature matrix — D must match training data
  • result (C × N) logit output

Return: true on success

Precondition:

  • isTrained() must be true (enforcement: runtime_check)
  • features must not be empty (enforcement: runtime_check)
  • features.n_rows == numFeatures() (enforcement: runtime_check)
  • all values in features are finite — no NaN or Inf (enforcement: none) [IMPORTANT]

Postcondition: result.n_rows == numClasses(), result.n_cols == features.n_cols on success

Reimplements: MLCore::MLSupervisedDimReductionOperation::transform

Computes logits = W * features + b_broadcast without rerunning training.

function outputDimensions

virtual std::size_t outputDimensions() const override

Number of output dimensions (equals number of classes for logit projection).

Reimplements: MLCore::MLSupervisedDimReductionOperation::outputDimensions

function outputColumnNames

virtual std::vector< std::string > outputColumnNames() const override

Column names for the output reduced matrix.

Return: Vector of column names (one per output dimension), e.g. [“Logit:0”, “Logit:1”]

Reimplements: MLCore::MLSupervisedDimReductionOperation::outputColumnNames

Default names are generated as “Logit:0”, “Logit:1”, etc. Concrete implementations may override with class-specific names after fitting.

function save

virtual bool save(
    std::ostream & out
) const override

Save the trained model to a stream.

Parameters:

  • out Output stream (binary or text, model-dependent)

Return: true if save succeeded

Reimplements: MLCore::MLModelOperation::save

The default implementation returns false (no persistence support).

function load

virtual bool load(
    std::istream & in
) override

Load a previously saved model from a stream.

Parameters:

  • in Input stream

Return: true if load succeeded

Reimplements: MLCore::MLModelOperation::load

After successful loading, [isTrained()](/api_generated/Classes/classMLCore_1_1LogitProjectionOperation.md#function-istrained) should return true. The default implementation returns false (no persistence support).

function isTrained

virtual bool isTrained() const override

Whether the model has been trained (supervised) or fit (unsupervised).

Reimplements: MLCore::MLModelOperation::isTrained

Returns true after a successful [train()](/api_generated/Classes/classMLCore_1_1MLSupervisedDimReductionOperation.md#function-train) or [fit()](/api_generated/Classes/classMLCore_1_1MLModelOperation.md#function-fit) call, or after a successful [load()](/api_generated/Classes/classMLCore_1_1LogitProjectionOperation.md#function-load).

function numClasses

virtual std::size_t numClasses() const override

Number of classes (supervised) or clusters (unsupervised).

Reimplements: MLCore::MLModelOperation::numClasses

Returns 0 if the model has not been trained/fit. For binary classification, returns 2.

function numFeatures

virtual std::size_t numFeatures() const override

Number of features the model was trained on.

Reimplements: MLCore::MLModelOperation::numFeatures

Returns 0 if the model has not been trained/fit. Used for validating that prediction data has the correct dimensionality.


Updated on 2026-11-13 at 20:25:42 +0000