1

I am trying to get the embedding for a siamese network written in keras and I keep having the issue below. Does anyone know how to solve this issue?

Here is the network:

input = layers.Input((40, 1))
x = layers.Conv1D(8, 64, activation="relu", padding='same', kernel_regularizer=regularizers.L1L2(l1=1e-5, l2=1e-4),)(input)
x = layers.Conv1D(8, 128, activation="relu", padding='same', kernel_regularizer=regularizers.L1L2(l1=1e-5, l2=1e-4),)(x)
x = layers.AveragePooling1D(pool_size= 2, padding='same')(x)
x = layers.Flatten()(x)

x = layers.Dense(100, activation="relu")(x)
embedding_network = keras.Model(input, x)

input_1 = layers.Input((40, 1))
input_2 = layers.Input((40, 1))
 
cnn_1 = embedding_network(input_1)
cnn_2 = embedding_network(input_2)



merge_layer_1 = layers.Lambda(euclidean_distance)([cnn_1, cnn_2])


output_layer = layers.Dense(1, activation="sigmoid")(merge_layer_1)
siamese = keras.Model(inputs=[input_1, input_2], outputs=output_layer)

Here is the what it is done to get the embedding:

get_layer_output = tf.keras.backend.function([siamese.layers[0].input],[siamese.layers[-2].output])

Here is the error:

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 40, 1), dtype=tf.float32, name='input_3'), name='input_3', description="created by layer 'input_3'") at layer "model". The following previous layers were accessed without issue: ['model']

1 Answer 1

1

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)>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.