enchanter.addons.criterions

Unsupervised Time Series Representation Learning

generate_sample_indices

enchanter.addons.criterions.generate_sample_indices(n_rand_samples: int, batch_size: int, length: int) Tuple[numpy.ndarray, int, numpy.ndarray, int, torch.Tensor][source]

generate sample indices.

Parameters
  • n_rand_samples – the number of negative samples

  • batch_size – batch size

  • length – length of time series.

Returns:

generate_anchor_positive_input

enchanter.addons.criterions.generate_anchor_positive_input(n_rand_samples: int, batch_size: int, length: int, original_data: torch.Tensor) Tuple[torch.Tensor, torch.Tensor][source]
Parameters
  • n_rand_samples – the number of negative samples

  • batch_size – batch size

  • length – length of time series

  • original_data – training data

Returns

  • anchor data (torch.Tensor)

  • positive data (torch.Tensor)

generate_negative_input

enchanter.addons.criterions.generate_negative_input()
Parameters
  • begin_neg_samples – Starting points of negative samples

  • len_pos_neg – length of negative and postive samples

  • batch_size – batch size

  • idx – Parameter k on the paper.

  • train – training dataset

  • samples

Returns

negative_input (torch.Tensor) - [batch_size, features, len_pos_neg]

Notes

When running mypy, error: Slice index must be an integer or None is detected on lines 97 and 98. Ignore it for now. Also, I didn’t add type: ignore as a comment because it has a negative impact on TorchScript execution.

positive_criterion_for_triplet_loss

enchanter.addons.criterions.positive_criterion_for_triplet_loss()
\[positive\ loss = -\log\Bigl(\sigma(f(x^{ref}) ^\mathrm{T} f(x^{pos}))\Bigr)\]
Parameters
  • anchor\(f(x^{ref})\) … anchor representation

  • positive\(f(x^{pos})\) … positive representation

Returns

positive loss (torch.Tensor)

negative_criterion_for_triplet_loss

enchanter.addons.criterions.negative_criterion_for_triplet_loss()
\[negative\ loss = - \log\Bigl(\sigma(-f(x^{ref}) ^\mathrm{T}f(x_k^{neg}))\Bigl)\]
Parameters
  • anchor\(f(x^{ref})\) … anchor representation

  • positive\(f(x^{neg})\) … negative representation

Returns

negative loss (torch.Tensor)

calculate_triplet_loss

enchanter.addons.criterions.calculate_triplet_loss()
\[Loss = positive\ loss + α × negative\ loss\]
Parameters
  • positive_loss – output of positive_criterion_for_triplet_loss

  • negative_loss – output of negative_criterion_for_triplet_loss

  • multiplicative_ration\(α\)

Returns

loss (torch.Tensor)