Slightly different embeddings for pandas series using a sentence transformer
Hello,I found that the embeddings given by a sentence transformer  'distiluse-base-multilingual-cased-v1’is slightly different, depending on how you calculate them for a Pandas series.Specifically, the result is different if  I calculate encoding using series as an argument or if I use‘apply’method:import pandas as pdimport sentence transformer modelsentence_transformer_path=‘distiluse-base-multilingual-cased-v1’from sentence_transformers import SentenceTransformerencoder=SentenceTransformer(sentence_transformer_path).encodecreate dataframe with textss=[str(i**2) for i in range(10)]df=pd.DataFrame()df[‘num’]=sfind embedding 1st methodembed1=encoder(df[‘num’])compare embeddnigs for a given rowrow=1difference=(encoder(df.loc[row,‘num’])-embed1[row,:])find total length of the difference vectorprint('Method 1 difference: ',(sum(difference**2))**0.5)find embedding 2nd methodembed2=(df[‘num’]).apply(encoder)#compareembeddnigs for a given rowrow=1difference=(encoder(df.loc[row,‘num’])-embed2[row])find total length of the difference vectorprint('Method 2 difference: ',(sum(difference**2))**0.5)The difference seems to be minor but I wonder what is the reason. Also, I don’t know if it is specific to my machine or not.Thank you
2023-12-18T14:26:41Z
Dts1:import pandas as pdimport sentence transformer modelsentence_transformer_path=‘distiluse-base-multilingual-cased-v1’from sentence_transformers import SentenceTransformerencoder=SentenceTransformer(sentence_transformer_path).encodecreate dataframe with textss=[str(i**2) for i in range(10)]df=pd.DataFrame()df[“num”=sfind embedding 1st methodembed1=encoder(df[“num”])compare embeddings for a given rowrow=1difference=(encoder(df.loc[row,“num”])-embed1[row,:])find total length of the difference vectorprint('Method 1 difference: ',(sum(difference**2))**0.5)find embedding 2nd methodembed2=(df[“num”]).apply(encoder)#compareembeddings for a given rowrow=1difference=(encoder(df.loc[row,“num”])-embed2[row])find total length of the difference vectorprint('Method 2 difference: ',(sum(difference**2))**0.5)Forgot to include the output:Method 1 difference:  3.997695879885389e-07Method 2 difference:  0.0