How to get a similarity vector from two vectors?

I want to make a classification model for 3 classes, i have 2 sentences for each observation, firstly i apply a cnn layer for each sentence and then i added dense layer.

inputs = Input(shape=(2,n_timesteps))
embedding_inputs = embedding_layer(inputs)

sentence1 = Lambda(lambda x: x[:,0,:,:])(embedding_inputs)
sentence2 = Lambda(lambda x: x[:,1,:,:])(embedding_inputs)

conv_sentence1 = Conv1D(filters=64, kernel_size=3, activation='relu',
input_shape=(n_timesteps,n_features))(sentence1)
conv_sentence2 = Conv1D(filters=64, kernel_size=3, activation='relu',
input_shape=(n_timesteps,n_features))(sentence2)

pooling_sentence1 = MaxPooling1D(pool_size=2)(conv_sentence1)
pooling_sentence2 = MaxPooling1D(pool_size=2)(conv_sentence2)

flat_sentence1 = Flatten()(pooling_sentence1)
flat_sentence2 = Flatten()(pooling_sentence2)

concat_senrences = concatenate([flat_sentence1,flat_sentence2])
dense_layer = dense(50)(concat_senrences)
dense_prediction = dense(3,activation='softmax')(dense_layer)


but i get an early overfetting, so i thought that the problem comes from "sentence 2", each observation has an unique "sentence 1", ,instead, "sentence 2" can be exists in several observations, in that case the neural network relies strongly on it, so i want to combine two sentences and apply an unique CNN layer, that's why i asked how to obtain a similarity vector.

thanks !!!

• If it is indeed a vector that you want then why not take the difference between the two vectors? A summation of the difference vector might not be a good idea though, since you do not know what each dimension in the vector stands for. – Atif Hassan Dec 19 '19 at 14:16
• thanks @AtifHassan, but i want a vector result from 2 vectors in order to use it as input in neural network(CNN) – joe_mind Dec 19 '19 at 14:39
• @joe_mind what you're asking is very uncommon, so you should probably explain what task you are trying to achieve and why you want to do it this way. This way people can tell you how the task itself is usually done. – Erwan Dec 19 '19 at 17:02

If I understand your question correctly, you have 2 sentences and you converted those sentences into 2 vectors, you want to know how those sentences are similar. If this is the case use cosine similarity between 2 sentences and cosine similarity is a scalar not a vector its the dot product of your 2 embedding vectors.

from numpy import dot
from numpy.linalg import norm

cos_sim = dot(a, b)/(norm(a)*norm(b))


where a and b are your vectors.

There is direct formula as well:

from sklearn.metrics.pairwise import cosine_similarity

cosine_similarity(vector1,vector2)

• thank you for your reply, In fact i want to get a new vector that represents the similarity, not a value as cosine. – joe_mind Dec 19 '19 at 12:17
• Similarity score is something which explains how similar two words are, lets say for example if you have a software engineer as one word and software architect as another word, the similarity lies around mentioning that in the vector space these two are close to each other. Its like your co-relation coefficient between 2 columns. So that means similarity can only be a scalar. – Saandeep Sreerambatla Dec 19 '19 at 12:36
• thanks, it is clear, but i want a vector result between two vectors in order to use it in neural network – joe_mind Dec 19 '19 at 14:37
• Can you tell me how you want to use it in a neural network? Any sample code please? – Saandeep Sreerambatla Dec 19 '19 at 15:39

If you have two vectors than You can get third similarity vector by subtracting the corresponding values. E.g VectorC =(VectorA - VectorB)

This will subtract the values. If the sum(VectorC) == 0 It means the VectorA and B are same. This is a way you get a similarity vector otherwise you will have a similarity in a single float value.

• thanks shaid hamdam, maybe it's a solution but I'm still looking for a credible solution. – joe_mind Dec 20 '19 at 13:54