torch_kmeans.utils package

class torch_kmeans.utils.LpDistance(**kwargs)[source]

Bases: BaseDistance

Initializes internal Module state, shared by both nn.Module and ScriptModule.

compute_mat(query_emb: Tensor, ref_emb: Optional[Tensor] = None) Tensor[source]

Compute the batched p-norm distance between each pair of the two collections of row vectors.

Parameters
  • query_emb (Tensor) –

  • ref_emb (Optional[Tensor]) –

Return type

Tensor

pairwise_distance(query_emb: Tensor, ref_emb: Tensor) Tensor[source]

Computes the pairwise distance between vectors v1, v2 using the p-norm

Parameters
  • query_emb (Tensor) –

  • ref_emb (Tensor) –

Return type

Tensor

training: bool
class torch_kmeans.utils.DotProductSimilarity(**kwargs)[source]

Bases: BaseDistance

Initializes internal Module state, shared by both nn.Module and ScriptModule.

compute_mat(query_emb: Tensor, ref_emb: Tensor) Tensor[source]
Parameters
  • query_emb (Tensor) –

  • ref_emb (Tensor) –

Return type

Tensor

pairwise_distance(query_emb: Tensor, ref_emb: Tensor) Tensor[source]
Parameters
  • query_emb (Tensor) –

  • ref_emb (Tensor) –

Return type

Tensor

training: bool
class torch_kmeans.utils.CosineSimilarity(**kwargs)[source]

Bases: DotProductSimilarity

Initializes internal Module state, shared by both nn.Module and ScriptModule.

training: bool
class torch_kmeans.utils.ClusterResult(labels: LongTensor, centers: Tensor, inertia: Tensor, x_org: Tensor, x_norm: Tensor, k: LongTensor, soft_assignment: Optional[Tensor] = None)[source]

Bases: tuple

Named and typed result tuple for kmeans algorithms

Parameters
  • labels (LongTensor) – label for each sample in x

  • centers (Tensor) – corresponding coordinates of cluster centers

  • inertia (Tensor) – sum of squared distances of samples to their closest cluster center

  • x_org (Tensor) – original x

  • x_norm (Tensor) – normalized x which was used for cluster centers and labels

  • k (LongTensor) – number of clusters

  • soft_assignment (Optional[Tensor]) – assignment probabilities of soft kmeans

Create new instance of ClusterResult(labels, centers, inertia, x_org, x_norm, k, soft_assignment)

labels: LongTensor

Alias for field number 0

centers: Tensor

Alias for field number 1

inertia: Tensor

Alias for field number 2

x_org: Tensor

Alias for field number 3

x_norm: Tensor

Alias for field number 4

k: LongTensor

Alias for field number 5

soft_assignment: Optional[Tensor]

Alias for field number 6

Submodules