Python – Conjoined networks in the Keras symmetry problem

Conjoined networks in the Keras symmetry problem… here is a solution to the problem.

Conjoined networks in the Keras symmetry problem

I’m trying to implement a twin network that is Keras for text similarity, but my network doesn’t seem to be symmetrical. When I test, it gives a different similarity score for A, B than B, A. How do I fix it? I’ve also tried dot mode, and all dense layers. My code is,

def contrastive(y_true, y_pred):
    margin = 1
    return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))

def network():

encoder = Sequential()
    encoder.add(Embedding(27, 15, input_length=15))
    encoder.add(LSTM(15))
    encoder.add(Dense(15))

return encoder

l_twin = network()
r_twin = network()

merged = Merge([l_twin, r_twin], mode='cos', dot_axes=1)

siamese = Sequential()
siamese.add(merged)
siamese.add(Dense(1, activation="sigmoid"))
siamese.compile(loss=contrastive, optimizer='adam', metrics=['accuracy'])
history = siamese.fit([l_encodings, r_encodings], target, epochs=5, batch_size=50)
siamese.save("siamese.h5")
siamese.save_weights("siamese_weights.h5")

Solution

In the example you created, you are creating different networks network(), so they will be independent.

As Yu-Yang says, check original example

First, they create the layer only once
# Network definition
base_network = create_base_network(input_shape)

They then applied the network to two different inputs:

input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)

# because we re-use the same instance `base_network`,
# the weights of the network
# will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)

You should try to correct your code by using a functional API instead of a sequential API.

Related Problems and Solutions