src.model.layers.tf_cure.tf_cluster module

class module

Use this module to manage a class object.

src.model.layers.tf_cure.tf_cluster.compute_dst(rep: Tensor, adv: RaggedTensor) Tensor
src.model.layers.tf_cure.tf_cluster.pp_dst(points: Tensor, adv: Tensor) Tensor
src.model.layers.tf_cure.tf_cluster.ragged_tf_concat(points, slice_idx, delta) Tensor
src.model.layers.tf_cure.tf_cluster.ragged_tf_swap(points, new_point, slice_idx, delta) Tensor
src.model.layers.tf_cure.tf_cluster.rep_loop_body(selected_rep, representors, points)
src.model.layers.tf_cure.tf_cluster.static_get_idx(c_id: Tensor, ids: Tensor) Tensor
src.model.layers.tf_cure.tf_cluster.static_merge_clst(clusters: RaggedTensor, idx: Tensor) Tensor
src.model.layers.tf_cure.tf_cluster.static_select_representors(points: Tensor, n_rep: Tensor) Tensor

select_representors.

Select the representors for the given cluster.

Parameters:
  • points (tf.Tensor) – points of the cluster

  • n_rep (tf.Tensor) – number of representors to select

Returns:

representors for the cluster

Return type:

tf.Tensor

class src.model.layers.tf_cure.tf_cluster.tf_clusters(points: Tensor, n_rep: int = 5, compression_factor: float = 0.5)

Bases: object

TfClusters.

Manage a queue of clusters. To apply operations to the tree, all functions requires the tree object passed as parameter, and it’s later returned as part of the output. This is done such that the tree object is not stored in the class. And it’s later updated by the caller.

apply_rep_compression() None

apply_rep_compression.

Apply the compression factor to the representors.

best_closest(c_id: int, rep: Tensor | RaggedTensor, tree: tf_rtree, debug: bool = False) Tuple[int, float]

best_closest.

Compute the closest cluster to the given set of representors.

Parameters:
  • c_id (int) – c_id of the cluster

  • rep (Union[tf.Tensor, tf.RaggedTensor]) – representors to compute the closest cluster

  • tree (tf_rtree) – tree with all the clusters

  • debug (bool) – flag to enable debug print

Returns:

id of the closest cluster and the distance from the cluster

Return type:

Tuple[int, float]

property closest: Tensor

closest.

Return the tensor with all the closest clusters.

Returns:

tensor with all the closest clusters

Return type:

tf.Tensor

property clusters: RaggedTensor

clusters.

Return the ragged tensor with all the clusters.

Returns:

ragged tensor with all the clusters

Return type:

tf.RaggedTensor

compute_closest(tree: tf_rtree) None

compute_closest.

Compute the closest cluster for each cluster in the queue.

Parameters:

tree (tf_rtree) – tree with all the clusters

Return type:

None

compute_representors(c_id: int) None

recompute_representors.

Recompute the representors for the given cluster.

Parameters:

c_id (int) – id of the cluster to recompute the representors

Return type:

None

property distances: Tensor

distances.

Return the tensor with all the distances from the closest clusters.

Returns:

tensor with all the distances from the closest clusters

Return type:

tf.Tensor

get_idx(c_id: int | Tensor) int | Tensor

get_idx.

Get the index of the cluster given the id.

Parameters:

c_id (Union[int, tf.Tensor]) – if int the id of the cluster to get the index if a tensor then gather the indexes of the clusters

Returns:

index of the cluster, if c_id is int then the output is an int otherwise a tensor with the indexes.

Return type:

Union[int, tf.Tensor]

Raises:

ValueError – If the id is not in the range of the clusters

property ids: Tensor

ids.

Return the tensor with all the ids.

Returns:

tensor with all the ids for each cluster

Return type:

tf.Tensor

property mean: Tensor

mean.

Return the mean of all the points.

Returns:

mean of all the points

Return type:

tf.Tensor

merge(u_id: int, v_id: int) None

merge.

Merge two clusters given their indexes.

Parameters:
  • u_idx (int) – index of the first cluster to merge

  • v_idx (int) – index of the second cluster to merge

Return type:

None

property reps: RaggedTensor

reps.

Return the ragged tensor with all the representors.

Returns:

ragged tensor with all the representors for each cluster

Return type:

tf.RaggedTensor

property size: int

size.

Return the number of clusters with more than 0 points

Returns:

size of the queue

Return type:

int

update_closest(new_c_id: int, old_c_id: int, tree: tf_rtree) None

update_closest.

Function to update the closest data structure. The computation procceds in 2 steps:

  1. Identify all the clusters which have old_c_id as closest cluster and compute the new closest cluster for those object. the cluster with new_c_id falls into this category

  2. Compute the distance between each cluster and new_c_id and update the closest data structure only if the new distance is lower than the current one

Assumption:
  • The closest cluster of new_c_id is currently old_c_id

Parameters:
  • new_c_id (int) – id of the new cluster

  • old_c_id (int) – id of the old cluster

  • tree (tf_rtree) – tree with all the clusters

Return type:

None

src.model.layers.tf_cure.tf_cluster.tf_concat(points, slice_idx, delta) Tensor