Segmenter

class daart.models.base.Segmenter(hparams)[source]

Bases: BaseModel

General wrapper class for behavioral segmentation models.

Methods Summary

build_model()

Construct the model using hparams.

forward(x)

Process input data.

predict_labels(data_generator[, ...])

param data_generator:

data generator to serve data batches

training_step(data[, accumulate_grad])

Calculate negative log-likelihood loss for supervised models.

Methods Documentation

build_model()[source]

Construct the model using hparams.

forward(x)[source]

Process input data.

Parameters:

x (torch.Tensor) – input data of shape (n_sequences, sequence_length, n_markers)

Returns:

  • ‘labels’ (torch.Tensor): model classification

    shape of (n_sequences, sequence_length, n_classes)

  • ’labels_weak’ (torch.Tensor): model classification of weak/pseudo labels shape of (n_sequences, sequence_length, n_classes)

  • ’reconstruction’ (torch.Tensor): input decoder prediction shape of (n_sequences, sequence_length, n_markers)

  • ’prediction’ (torch.Tensor): one-step-ahead prediction shape of (n_sequences, sequence_length, n_markers)

  • ’task_prediction’ (torch.Tensor): prediction of regression tasks (n_sequences, sequence_length, n_tasks)

  • ’embedding’ (torch.Tensor): behavioral embedding used for classification/prediction in non-variational models shape of (n_sequences, sequence_length, embedding_dim)

  • ’mean’ (torch.Tensor): mean of appx posterior of latents in variational models shape of (n_sequences, sequence_length, embedding_dim)

  • ’logvar’ (torch.Tensor): logvar of appx posterior of latents in variational models shape of (n_sequences, sequence_length, embedding_dim)

  • ’sample’ (torch.Tensor): sample from appx posterior of latents in variational models shape of (n_sequences, sequence_length, embedding_dim)

Return type:

dict of model outputs/internals as torch tensors

predict_labels(data_generator, return_scores=False, remove_pad=True, mode='eval')[source]
Parameters:
  • data_generator (DataGenerator object) – data generator to serve data batches

  • return_scores (bool) – return scores before they’ve been passed through softmax

  • remove_pad (bool) – remove batch padding from model outputs before returning

  • mode (str) – ‘eval’ | ‘train’

Returns:

  • ‘predictions’ (list of lists): first list is over datasets; second list is over batches in the dataset; each element is a numpy array of the label probability distribution

  • ’weak_labels’ (list of lists): corresponding weak labels

  • ’labels’ (list of lists): corresponding labels

Return type:

dict

training_step(data, accumulate_grad=True, **kwargs)[source]

Calculate negative log-likelihood loss for supervised models.

The batch is split into chunks if larger than a hard-coded chunk_size to keep memory requirements low; gradients are accumulated across all chunks before a gradient step is taken.

Parameters:
  • data (dict) – signals are of shape (n_sequences, sequence_length, n_channels)

  • accumulate_grad (bool, optional) – accumulate gradient for training step

Returns:

  • ‘loss’ (float): total loss (negative log-like under specified noise dist)

  • other loss terms depending on model hyperparameters

Return type:

dict