secmlt.models package#

Subpackages#

Submodules#

secmlt.models.base_language_model module#

Basic wrapper for generic language model.

class secmlt.models.base_language_model.BaseLanguageModel[source]#

Bases: ABC

Abstract base class defining the common interface for language models.

abstract decode(ids: LongTensor, **kwargs) str | list[str][source]#

Convert token IDs back into text.

Parameters:

ids (torch.LongTensor) – Token IDs tensor.

Returns:

Decoded text(s).

Return type:

str or list of str

abstract encode(text: str | list[str], **kwargs) LongTensor[source]#

Convert input text into token IDs.

Parameters:

text (str or list of str) – Input text(s) to tokenize.

Returns:

Token IDs tensor.

Return type:

torch.LongTensor

abstract generate(prompts: list[str], **kwargs) list[str][source]#

Generate text continuations from given prompts.

Parameters:

prompts (list of str) – List of prompt strings.

Returns:

Generated text outputs.

Return type:

list of str

abstract hidden_states(input_ids: LongTensor, **kwargs) list[Tensor][source]#

Return hidden states of the model for given input.

Parameters:

input_ids (torch.LongTensor) – Input token IDs.

Returns:

Hidden representations per layer.

Return type:

list of torch.Tensor

abstract logprobs(prompts: list[str], targets: list[str], **kwargs) list[Tensor][source]#

Compute log-probabilities for each token in the target continuations.

Parameters:
  • prompts (list of str) – Conditioning prompts.

  • targets (list of str) – Target continuations.

Returns:

List of log-probability tensors, one per sample in the batch. Each tensor has shape [target_len_i].

Return type:

list of torch.Tensor

abstract predict(input_ids: LongTensor, **kwargs) Tensor[source]#

Predict logits for the next token given an input sequence.

Parameters:

input_ids (torch.LongTensor) – Input token IDs.

Returns:

Logits for the next token (shape [batch, vocab_size]).

Return type:

torch.Tensor

secmlt.models.base_model module#

Basic wrapper for generic model.

class secmlt.models.base_model.BaseModel(preprocessing: DataProcessing | None = None, postprocessing: DataProcessing | None = None)[source]#

Bases: ABC

Basic model wrapper.

decision_function(x: Tensor, *args, **kwargs) Tensor[source]#

Return the decision function from the model.

Requires override to specify custom args and kwargs passing.

Parameters:

x (torch.Tensor) – Input damples.

Returns:

Model output scores.

Return type:

torch.Tensor

abstract predict(x: Tensor, *args, **kwargs) Tensor[source]#

Return output predictions for given model.

Parameters:

x (torch.Tensor) – Input samples.

Returns:

Predictions from the model.

Return type:

torch.Tensor

abstract train(dataloader: DataLoader) BaseModel[source]#

Train the model with the given dataloader.

Parameters:

dataloader (DataLoader) – Train data loader.

secmlt.models.base_trainer module#

Model trainers.

class secmlt.models.base_trainer.BaseTrainer[source]#

Bases: object

Abstract class for model trainers.

abstract train(model: BaseModel, dataloader: DataLoader) BaseModel[source]#

Train a model with the given dataloader.

Parameters:
  • model (BaseModel) – Model to train.

  • dataloader (DataLoader) – Training dataloader.

Returns:

The trained model.

Return type:

BaseModel

Module contents#

Machine learning models and wrappers.