0
$\begingroup$

I have two lists of sentences

A=["Astring1", "Astring2",...,"AstringN"] 
B=["Bstring1", "Bstring2",...,"BstringN"]

I used an embedding model such as BERT to get the vectorized embeddings of all my strings in each list

EmbeddingA=["EmbeddingAstring1", "EmbeddingAstring2",...,"EmbeddingAstringN"] 
EmbeddingB=["EmbeddingBstring1", "EmbeddingBstring2",...,"EmbeddingBstringN"]

Is it possible to calculate the correlation between my two lists EmbeddingA and EmbeddingB ?

(My final goal would be to get a correlation matrix showing the corrélations between many lists A, B, C, D...)

$\endgroup$

2 Answers 2

0
$\begingroup$

Not enough rep to comment, but it's super easy! Assuming the embeddings in both lists are the same shape (I think BERT does [1, 12, 728]), you can stack the embeddings into 2D Matrices. Each row of the matrix should correspond to an embedding for each sentence. The matrices should each be of shape (N, D) where N is the number of sentences and D is the embedding dimension. You can then use your correlation method of choice after transposing the matrices. The reason we transpose is because correlation is row-wise, and the matrices are currently in (N, D) as stated earlier. We need a shape of (D, N), or (feature, sentence) as opposed to (sentence, feature).

import numpy as np # if you haven't already
import pandas as pd # if you haven't already
matrixA = np.array(EmbeddingA)  # Shape (N, D)
matrixB = np.array(EmbeddingB)  # Shape (N, D)

correlation_matrix = np.corrcoef(matrixA.T, matrixB.T)
correlation_df = pd.DataFrame(correlation_matrix)
print(correlation_df)
$\endgroup$
1
  • $\begingroup$ I'm sorry I explained my problem very badly. Let's say I have many lists of sentences A,B,C,D,E... And I want to make a correlation matrix which will calculate the correlations between every lists, so I would like cor(EmbeddingA, EmbeddingB)= a scalar Is it possible ? $\endgroup$
    – Leon
    Commented Sep 25 at 17:59
0
$\begingroup$

A simple correlation function can be applied on the embeddings. Pearson correlation is considered by default.

import numpy as np

correlation_matrix = np.corrcoef(embeddings)

Correlation can be visualised further by a heatmap

import seaborn as sns

sns.heatmap(correlation_matrix, annot=True)
$\endgroup$
1
  • $\begingroup$ I'm sorry I explained my problem very badly. Let's say I have many lists of sentences A,B,C,D,E... And I want to make a correlation matrix which will calculate the correlations between every lists, so I would like cor(EmbeddingA, EmbeddingB)= a scalar Is it possible $\endgroup$
    – Leon
    Commented Sep 25 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.