Skip to content

Instantly share code, notes, and snippets.

@sdcubber
Last active October 8, 2018 16:45
Show Gist options
  • Save sdcubber/8cdeec2e89fc1101fd1fa1cf9f24c0b1 to your computer and use it in GitHub Desktop.
Save sdcubber/8cdeec2e89fc1101fd1fa1cf9f24c0b1 to your computer and use it in GitHub Desktop.
# Siamese network: two input sequences through same embedding layer
sequence_one = layers.Input((10, )) # Input of arbitrary shape
sequence_two = layers.Input((10, ))
embedding_layer = layers.Embedding(input_dim=1000,
output_dim=128, input_length=10)
embedded_seq_1 = embedding_layer(sequence_one)
embedded_seq_2 = embedding_layer(sequence_two)
import tensorflow as tf
averaging_layer = layers.Lambda(lambda x: tf.reduce_mean(x, axis=1), name='averaging')
avg_emb_1 = averaging_layer(embedded_seq_1)
avg_emb_2 = averaging_layer(embedded_seq_2)
dot_product = layers.Dot(axes=-1)([avg_emb_1, avg_emb_2])
model = models.Model(inputs=[sequence_one, sequence_two], outputs=dot_product)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment