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 |
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 |
30
|
top_k
|
(int, Optional)
|
In the relative representation space for embedding in |
50
|
alpha
|
(float, Optional)
|
Smoothing factor when running exponential smoothing
for updating the transform matrix |
0.5
|
subsample
|
(float, Optional)
|
For each iteration use a percentage subset of the
data to in |
0.33
|
random_seed
|
(int, Optional)
|
Use a random seed for reproducible results.
Used in all |
123
|
verbose
|
(bool, Optional)
|
If |
True
|
quadratic_assignment_kwargs
|
(dict, Optional)
|
Args to use in the |
{'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 |
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.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.
- Perform k-means clustering in each embedding space independently and obtain cluster centroids.
- Compute pairwise similarities in each space.
- 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.
- We construct pseudo-parallel pairs by sending each element in space
Ato the average of its k nearest neighbors from spaceB(based on similarity in relative space). - 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 |
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 |
{'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 |
50
|
subsample
|
(float, Optional)
|
For each iteration of refinement 1, use a subset of embeddings
in |
0.33
|
alpha
|
(float, Optional)
|
Smoothing factor when running exponential smoothing
for updating the transform matrix |
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 |
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 |