Siamese Network with Cosine Similarity in Keras with ‘mse’ loss (Updated)

I am trying to use the cosine based similarity in the Siamese Neural Network, Following is my try

Inputs and Labels

EXAMPLES=10000
FEATURES=30
LEFT=np.random.random((EXAMPLES,FEATURES))
RIGHT=np.random.random((EXAMPLES,FEATURES))
LABELS=[]
for i in range(EXAMPLES):
    LABELS.append(np.random.randint(0,2))
LABELS=np.asarray(LABELS)

Cosine Similarity

def cosine_distance(vecs):
    #I'm not sure about this function too
    y_true, y_pred = vecs
    y_true = K.l2_normalize(y_true, axis=-1)
    y_pred = K.l2_normalize(y_pred, axis=-1)
    return K.mean(1 - K.sum((y_true * y_pred), axis=-1))

def cosine_dist_output_shape(shapes):
    shape1, shape2 = shapes
    print((shape1[0], 1))
    return (shape1[0], 1)

Siamese Model

inputShape=Input(shape=(FEATURES,))
left_input = Input(shape=(FEATURES,))
right_input = Input(shape=(FEATURES,))
    
model = Sequential()
model.add(Dense(20, activation='relu', input_shape=(30,)))
model.add(BatchNormalization())
model.add(Dense(10, activation='relu'))
    
   
    
encoded_l = model(left_input)
encoded_r = model(right_input)

L1_Distance = Lambda(cosine_distance, output_shape=cosine_dist_output_shape)([encoded_l, encoded_r])
siamese_net = Model([left_input, right_input], L1_Distance)
siamese_net.summary()

    
siamese_net.compile(loss="mse",optimizer=Adam(lr=0.0001))
siamese_net.fit(x=[LEFT,RIGHT],y=LABELS,batch_size=64,epochs=100)

SoftMax Based Output

model = Sequential()
model.add(Dense(20, activation='relu', input_shape=(30,)))
model.add(BatchNormalization())
model.add(Dense(10, activation='relu'))

#model.add(Dense(30, activation='relu'))

   
    
encoded_l = model(left_input)
encoded_r = model(right_input)


L1_Layer = Lambda(cosine_distance, output_shape=cosine_dist_output_shape)([encoded_l, encoded_r])
L1_Diatance = L1_layer([encoded_l, encoded_r])
prediction = Dense(2,activation='softmax')(L1_Diatance)

siamese_net = Model([left_input, right_input], prediction)
siamese_net.compile(loss="binary_crossentropy",optimizer=Adam(lr=0.001))
siamese_net.summary()

Model: "model_26"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_126 (InputLayer)          (None, 30)           0                                            
__________________________________________________________________________________________________
input_127 (InputLayer)          (None, 30)           0                                            
__________________________________________________________________________________________________
sequential_42 (Sequential)      (None, 10)           910         input_126[0][0]                  
                                                                 input_127[0][0]                  
__________________________________________________________________________________________________
lambda_19 (Lambda)              multiple             0           sequential_42[1][0]              
                                                                 sequential_42[2][0]              
__________________________________________________________________________________________________
dense_133 (Dense)               (None, 2)            22          lambda_19[9][0]  

My Model is working fine , but my question is after the cosine similarity , using of mse loss is a correct way of fitting this model?

Answer

it is Model([left_input, right_input], L1_Distance) and not Model([left_input, left_input], L1_Distance)

EDIT: if your is a regression problem the mse can be a good choice. if your task is a classification problem probably you have to change it (binary_crossentropy ?). pay attention also that your last layer computes a distance but in case of classification problem its output must be interpreted as a probability score

Leave a Reply

Your email address will not be published. Required fields are marked *