Skip to content

CentroidMiniVec2Vec

Bases: MiniVec2VecBase

__init__()

Constructor for Mini-Vec2Vec using the centroids only method. (Option 2 in the paper)

fit(A, B, n_clusters=20, n_runs=30, top_k=50, alpha=0.5, subsample=0.33, random_seed=123, verbose=True, quadratic_assignment_kwargs={'method': '2opt', 'options': {'maximize': True}})

Create the optimal matrix W of linear transforms for mapping embeddings from embedding space A to embedding space B, using relative representations.

This provides an easier to use but less configurable implementation of the paper. If you want a more configurable implementation I would recommend you run match_anchors, refinement_1, and refinement_2 separately.

Parameters:

Name Type Description Default
A array

Embeddings from the embedding space you want to transform from.

required
B array

Embeddings from the embedding space you want to transform to.

required
n_clusters (int, Optional)

Number of clusters for k-means for finding centroids for both the matching_anchors and refinement_2 steps Default is 20

20
n_runs (int, Optional)

Number of iterations to run to get a varied set of centers that represent the embedding spaces. Used in both matching_anchors and refinement_1. Default is 30

30
top_k (int, Optional)

In the relative representation space for embedding in A and B, find the top k nearest neighbors from relative space from B for each A. Only used in refinement_1. (A and B share the same representation space)

50
alpha (float, Optional)

Smoothing factor when running exponential smoothing for updating the transform matrix W. Must be 0 < alpha < 1. Used in both refinement_1 and refinement_2. Default is 0.5

0.5
subsample (float, Optional)

For each iteration use a percentage subset of the data to in A and B, to get the centers. Must be 0 < subsample <= 1. Set subsample=1 to use all the data. Used in all matching_anchors, refinement_1, and refinement_2. Default is 0.33

0.33
random_seed (int, Optional)

Use a random seed for reproducible results. Used in all matching_anchors, refinement_1, and refinement_2. Default is 123

123
verbose (bool, Optional)

If False it will hide all the progress bars Used in matching_anchors and refinement_1. Default is True

True
quadratic_assignment_kwargs (dict, Optional)

Args to use in the from scipy.optimize.quadratic_assignment during the alignment of clusters. Default is {'method':"2opt", 'options':{"maximize": True},}

{'method': '2opt', 'options': {'maximize': True}}

Returns:

Name Type Description
self CentroidMiniVec2Vec

Fitted MiniVec2Vec object

fit_transform(A, B, n_clusters=20, n_runs=30, top_k=50, alpha=0.5, subsample=0.33, random_seed=123, verbose=True)

Create the optimal matrix W of linear transforms for mapping embeddings from embedding space A to embedding space B. Then apply it to the embeddings from A. Same as running .fit().transform(X).

Parameters:

Name Type Description Default
A array

Embeddings from the embedding space you want to transform from.

required
B array

Embeddings from the embedding space you want to transform to.

required
n_clusters (int, Optional)

Number of clusters for k-means for finding centroids Default is 20

20
n_runs (int, Optional)

Number of iterations to run to get a varied set of centers that represent the embedding spaces. Default is 30

30
top_k (int, Optional)

In the relative representation space for embedding in A and B, find the top k nearest neighbors from relative space from B for each A. (A and B share the same representation space) Default is 50

50
subsample (float or None, Optional)

For each iteration use a percentage subset of the data to in A and B, to get the centers. Must be 0 < subsample <= 1. Set subsample=1 to use all the data. Default is 0.33

0.33
random_seed (int, Optional)

Use a random seed for reproducible results. Default is 123

123
verbose (bool, Optional)

If False it will hide all the progress bars Default is True

True

Returns:

Name Type Description
self

Fitted MiniVec2Vec object

match_anchors(A, B, n_clusters=20, n_runs=30, subsample=0.33, random_seed=123, verbose=True, quadratic_assignment_kwargs={'method': '2opt', 'options': {'maximize': True}})

Find an approximate matching between embedding in A and B using centroids.

  1. Perform k-means clustering in each embedding space independently and obtain cluster centroids.
  2. Compute pairwise similarities in each space.
  3. Find the optimal matching between the cluster centroids by solving the Quadratic Assignment Problem (QAP), which finds a permutation that aligns the similarity matrices optimally.
  4. We construct pseudo-parallel pairs by sending each element in space A to the average of its k nearest neighbors from space B (based on similarity in relative space).
  5. Optimal orthogonal transformation is obtained by Procrustes analysis

Parameters:

Name Type Description Default
A array

Embeddings from the embedding space you want to transform from.

required
B array

Embeddings from the embedding space you want to transform to.

required
n_clusters (int, Optional)

Number of clusters for k-means for finding centroids Default is 20

20
n_runs (int, Optional)

Number of iterations to run to get a varied set of centers that represent the embedding spaces. Default is 30

30
subsample (float or None, Optional)

For each iteration use a percentage subset of the data to in A and B, to get the centers. Must be 0 < subsample <= 1. Set subsample=1 to use all the data. Default is 0.33

0.33
random_seed (int, Optional)

Use a random seed for reproducible results. Default is 123

123
verbose (bool, Optional)

If False it will hide all the progress bars Default is True

True
quadratic_assignment_kwargs (dict, Optional)

Args to use in the from scipy.optimize.quadratic_assignment during the alignment of clusters. Default is {'method':"2opt", 'options':{"maximize": True},}

{'method': '2opt', 'options': {'maximize': True}}

Returns:

Name Type Description
self CentroidMiniVec2Vec

MiniVec2Vec object after match anchors have been applied

refinement_1(A, B, n_runs=30, top_k=50, subsample=0.33, alpha=0.5, random_seed=123, verbose=True)

Refinement 1: Iterative Closest Point Average

Transforming embed-dings from space A to B using W, averaging their nearest neighbors in space B, and obtaining a new orthogonal transformation with Procrustes analysis.

Parameters:

Name Type Description Default
A array

Embeddings from the embedding space you want to transform from.

required
B array

Embeddings from the embedding space you want to transform to.

required
n_runs (int, Optional)

Number of iterations to run to get a varied set of centers that represent the embedding spaces. Default is 30

30
top_k (int, Optional)

In the relative representation space for embedding in A and B, find the top k nearest neighbors from relative space from B for each A. (A and B share the same representation space)

50
subsample (float, Optional)

For each iteration of refinement 1, use a subset of embeddings in A. To reduce overhead and reduce overfitting. Default is 0.33

0.33
alpha (float, Optional)

Smoothing factor when running exponential smoothing for updating the transform matrix W. Must be 0 < alpha < 1. Default is 0.5

0.5
random_seed (int, Optional)

Use a random seed for reproducible results. Default is 123

123
verbose (bool, Optional)

If False it will hide all the progress bars Default is True

True

Returns:

Name Type Description
self

MiniVec2Vec object after refinement 1 have been applied

refinement_2(A, B, n_clusters=50, alpha=0.5, random_seed=123)

Refinement 2: Cluter-Based Alignment Correction

Improve the large-scale matching between the spaces, by clustering the embeddings in space A. We then apply W to the cluster centroids and cluster the B embeddings, where the clustering algorithm is initialized with the transformed A centroids as the initial centroids. The transformed clusters should be close to a set of clusters in the new space, therefore expect the clustering algorithm to make only minor adjustments, correcting biases in the transformation and moving the centroids to their “right positions” in space B.

Parameters:

Name Type Description Default
A array

Embeddings from the embedding space you want to transform from.

required
B array

Embeddings from the embedding space you want to transform to.

required
n_clusters (int, Optional)

Number of clusters for k-means for finding centroids Default is 20

50
alpha (float, Optional)

Smoothing factor when running exponential smoothing for updating the transform matrix W. Must be 0 < alpha < 1. Default is 0.5

0.5
random_seed (int, Optional)

Use a random seed for reproducible results. Default is 123

123

Returns:

Name Type Description
self

MiniVec2Vec object after refinement 2 have been applied

transform(X)

Transform the input embeddings X.

Parameters:

Name Type Description Default
X array

Set of embeddings to transform

required

Returns:

Name Type Description
array

Transformed embeddings X