I tried to reproduce your code as some of the components, e.g. the euclidean_distance function, were missing. The following seems to work well on my system:
import tensorflow as tf
import keras.backend as K
def euclidean_distance(x):
return tf.expand_dims(K.sqrt(K.sum(K.square(x[0] - x[1]), axis=-1)), axis=1)
model_input = tf.keras.layers.Input((40, 1))
x = tf.keras.layers.Conv1D(8, 64, activation="relu", padding='same', kernel_regularizer=tf.keras.regularizers.L1L2(l1=1e-5, l2=1e-4),)(model_input)
x = tf.keras.layers.Conv1D(8, 128, activation="relu", padding='same', kernel_regularizer=tf.keras.regularizers.L1L2(l1=1e-5, l2=1e-4),)(x)
x = tf.keras.layers.AveragePooling1D(pool_size= 2, padding='same')(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(100, activation="relu")(x)
embedding_network = tf.keras.Model(model_input, x)
input_1 = tf.keras.layers.Input((40, 1))
input_2 = tf.keras.layers.Input((40, 1))
cnn_1 = embedding_network(input_1)
cnn_2 = embedding_network(input_2)
merge_layer_1 = tf.keras.layers.Lambda(euclidean_distance)([cnn_1, cnn_2])
output_layer = tf.keras.layers.Dense(1, activation="sigmoid")(merge_layer_1)
siamese = tf.keras.Model(inputs=[input_1, input_2], outputs=output_layer)
embedding_network.summary()
Output:
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 40, 1)] 0
conv1d (Conv1D) (None, 40, 8) 520
conv1d_1 (Conv1D) (None, 40, 8) 8200
average_pooling1d (AverageP (None, 20, 8) 0
ooling1D)
flatten (Flatten) (None, 160) 0
dense (Dense) (None, 100) 16100
=================================================================
Total params: 24,820
Trainable params: 24,820
Non-trainable params: 0
_________________________________________________________________
And, similarly
siamese.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_2 (InputLayer) [(None, 40, 1)] 0 []
input_3 (InputLayer) [(None, 40, 1)] 0 []
model (Functional) (None, 100) 24820 ['input_2[0][0]',
'input_3[0][0]']
lambda (Lambda) (None, 1) 0 ['model[0][0]',
'model[1][0]']
dense_1 (Dense) (None, 1) 2 ['lambda[0][0]']
==================================================================================================
Total params: 24,822
Trainable params: 24,822
Non-trainable params: 0
__________________________________________________________________________________________________
And, a simple test of the model:
batch_size=5
inp1=tf.random.uniform((batch_size, 40, 1))
inp2=tf.random.uniform((batch_size, 40, 1))
siamese([inp1, inp2])
Output:
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[0.5550248 ],
[0.55784535],
[0.54480696],
[0.54240334],
[0.54322207]], dtype=float32)>