Skip to content

contrastive loss

polars_ts.clustering._contrastive_loss

Contrastive loss and encoder for time series clustering.

TSEncoder

Bases: Module

1D CNN encoder with projection head for contrastive learning.

Parameters

embedding_dim Dimension of the learned embedding. projection_dim Dimension of the projection head output (used during training). n_filters Base number of convolutional filters.

forward(x)

Return embedding (used after training for clustering).

project(x)

Return projection (used during contrastive training).

nt_xent_loss(z_i, z_j, temperature=0.5)

Compute normalized temperature-scaled cross-entropy loss (NT-Xent).

Parameters

z_i, z_j Projected representations of the two augmented views, shape (batch_size, projection_dim). temperature Temperature scaling parameter.