简体   繁体   English

在 Keras 中具有余弦相似性且具有“mse”损失的连体网络(更新)

[英]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连体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基于 SoftMax 的 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?我的 Model 工作正常,但我的问题是在余弦相似度之后,使用 mse 损失是拟合这个 model 的正确方法吗?

it is Model([left_input, right_input], L1_Distance) and not Model([left_input, left_input], L1_Distance)它是Model([left_input, right_input], L1_Distance)而不是Model([left_input, left_input], L1_Distance)

EDIT: if your is a regression problem the mse can be a good choice.编辑:如果您是回归问题,则 mse 可能是一个不错的选择。 if your task is a classification problem probably you have to change it (binary_crossentropy?).如果您的任务是分类问题,您可能必须更改它(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还要注意你的最后一层计算距离,但在分类问题的情况下,它的 output 必须被解释为概率分数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM