Keras similarity calculation. Enumerating distance between two tensors, which indicates as lists

22 views Asked by At

I follow the Nicholas Renotte tutorial "Build a Deep Facial Recognition App"(Python). But at the part 4 I faced a problem, here is the code:

Siamese L1 Distance class

class L1Dist(Layer):

    # Init method - inheritance
    def __init__(self, **kwargs):
        super().__init__()

    # Magic happens here - similarity calculation
    def call(self, input_embedding, validation_embedding):
        return tf.math.abs(input_embedding - validation_embedding)

TypeError: unsupported operand type(s) for -: 'list' and 'list'

In the video all is fine, but in my case function can't do the substraction (input_embedding - validation_embedding)

Arguments received by L1Dist.call():
args=(['<KerasTensor shape=(None, 4096), dtype=float32, sparse=False, name=keras_tensor_18>'], ['<KerasTensor shape=(None, 4096), dtype=float32, sparse=False, name=keras_tensor_19>'])

Tried to modify:

def call(self, input_embedding, validation_embedding):
        input_embedding = tf.convert_to_tensor(input_embedding)
        validation_embedding = tf.convert_to_tensor(validation_embedding)
        input_embedding = tf.squeeze(input_embedding, axis=0)  # Remove potential first dimension
        validation_embedding = tf.squeeze(validation_embedding, axis=0)
        return tf.math.abs(input_embedding - validation_embedding)

But failed

line 108, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: TypeError: object of type 'KerasTensor' has no len()

Tried tf.keras.layers.Subtract()([input_embedding, validation_embedding]) But AttributeError: Exception encountered when calling Subtract.call().

'list' object has no attribute 'shape'

With keras.ops.subtract(input_embedding, validation_embedding) faced: ValueError(f"Invalid dtype: {dtype}") ValueError: Invalid dtype: list

0

There are 0 answers