gpt4 book ai didi

python - 具有 'mse' 损失的 Keras 中具有余弦相似性的 Siamese 网络(更新)

转载 作者:行者123 更新时间:2023-12-04 09:35:49 26 4
gpt4 key购买 nike

我正在尝试在 Siamese 神经网络中使用基于余弦的相似性,以下是我的尝试
输入和标签

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)
余弦相似度
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)
连体模型
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 的输出
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]
我的模型工作正常,但我的问题是在余弦相似度之后,使用 mse 损失是拟合这个模型的正确方法吗?

最佳答案

它是 Model([left_input, right_input], L1_Distance)而不是 Model([left_input, left_input], L1_Distance)编辑:如果您是回归问题,那么 mse 可能是一个不错的选择。如果您的任务是分类问题,您可能必须更改它(binary_crossentropy ?)。还要注意你的最后一层计算距离,但在分类问题的情况下,它的输出必须被解释为概率分数

关于python - 具有 'mse' 损失的 Keras 中具有余弦相似性的 Siamese 网络(更新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62596035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com