DOROTHY home

Reference / API reference / dorothy / dorothy.models / dorothy.models.mlp

dorothy.models.mlp

Multi-layer perceptron (MLP) architecture for stellar parameter inference.

This module implements the configurable MLP architecture used by DOROTHY models. The network takes 3-channel spectral data (flux, rho, mask) as input and predicts stellar parameters along with their uncertainties.

Default architecture (DOROTHY standard):

Input: (batch, 3, wavelengths) -> Flatten -> (batch, 3*wavelengths) Hidden: 3*wavelengths -> 5000 -> 2000 -> 1000 -> 500 -> 200 Output: LabelAwareOutput, 200 -> (batch, 2, n_params), with the layer of 100 in each of its two decoders

Input tensor has shape (batch, 3, wavelengths) where:
  • input[:, 0, :] contains the flux values

  • input[:, 1, :] contains rho = log(1 + 1/sigma), the encoded uncertainty

  • input[:, 2, :] contains the mask (1=valid, 0=invalid)

Output tensor has shape (batch, 2, n_params) where:
  • output[:, 0, :] contains the predicted means

  • output[:, 1, :] contains the predicted log-scatter values

Classes

MLP([input_features, output_features, ...])

Configurable multi-layer perceptron for stellar parameter prediction.

class dorothy.models.mlp.MLP(input_features=15300, output_features=22, hidden_layers=None, normalization='layernorm', activation='gelu', dropout=0.0, label_aware_uncertainty=True)[source]

Bases: Module

Configurable multi-layer perceptron for stellar parameter prediction.

The network flattens 3D input tensors (batch, 3, wavelengths) to 2D before processing through the hidden layers. The last module, a LabelAwareOutput, takes over the last hidden layer: its label decoder is that layer and a linear map to the means, and its error decoder is a layer of the same size on the same input and the label errors, then a linear map to the log-scatter. It returns (batch, 2, n_params).

Input format (3-channel):
  • Channel 0: flux values

  • Channel 1: rho = log(1 + 1/sigma), the encoded uncertainty

  • Channel 2: mask (1=valid, 0=invalid)

input_features

Number of input features after flattening (3 * wavelengths).

output_features

Number of output features (2 * n_parameters).

n_parameters

Number of stellar parameters being predicted.

hidden_layers

List of hidden layer sizes.

label_aware_uncertainty

Whether the last layer is a LabelAwareOutput (True) or an AdditiveOutput.

layers

The sequential container of all network layers.

Examples

>>> from dorothy.config import ModelConfig
>>> config = ModelConfig(hidden_layers=[1000, 500, 100])
>>> model = MLP.from_config(config)
>>> x = torch.randn(32, 3, 4506)  # batch of 3-channel spectra
>>> output = model(x)  # shape: (32, 2, n_parameters)
>>> means = output[:, 0, :]  # predicted means
>>> log_scatter = output[:, 1, :]  # predicted log-scatter
forward(x, label_errors=None)[source]

Forward pass through the network.

Parameters:
  • x (Tensor) –

    Input tensor of shape (batch_size, 3, wavelength_bins), or pre-flattened (batch_size, input_features), where:

    • x[:, 0, :] contains the flux values

    • x[:, 1, :] contains rho = log(1 + 1/sigma)

    • x[:, 2, :] contains the mask (1=valid, 0=invalid)

  • label_errors (Tensor | None) – Reported label errors of shape (batch_size, n_parameters), in normalized units, for the log-scatter (see LabelAwareOutput). None uses the learned missing vector for every entry.

Returns:

Output tensor of shape (batch_size, 2, n_parameters) where – - output[:, 0, :] contains the predicted means - output[:, 1, :] contains the predicted log-scatter values

Raises:

ValueError – If label_errors is given to a model built with label_aware_uncertainty=False, or has the wrong shape.

Return type:

Tensor

get_embeddings(x, layer_index=-2)[source]

Extract intermediate embeddings from the network.

This is useful for k-NN anomaly detection and other analyses that require the learned feature representation.

Parameters:
  • x (Tensor) – Input tensor of shape (batch_size, channels, wavelength_bins).

  • layer_index (int) – Index of the module of self.layers whose output is returned. The default, -2, gives the input of the output head. For a label-aware model, whose decoders take over the last hidden layer, that is the layer before it, or the flattened input with one hidden layer; for an old checkpoint, it is the last hidden layer.

Returns:

Embedding tensor of shape (batch_size, embedding_dim); the output layer’s (batch_size, 2, n_params) is flattened.

Raises:

ValueError – If layer_index is outside [-len(self.layers), len(self.layers) - 1].

Return type:

Tensor

classmethod from_config(config)[source]

Create an MLP from a ModelConfig.

Parameters:

config (ModelConfig) – Model configuration specifying architecture parameters.

Returns:

Configured MLP instance.

Return type:

MLP

count_parameters()[source]

Count the trainable parameters.

Returns:

Total number of trainable scalar parameters.

Return type:

int

extra_repr()[source]

Return a string representation of the model configuration.