Tensorflow Incompatible shapes: [16,48] vs. [0]

45 Views Asked by At

I have the following code portion for a siamese network

def build_sequential_model(input_shape, embedding_dim=16):
    model = Sequential()
    model.add(Input(shape=(input_shape,)))
    model.add(Dense(embedding_dim, activation='tanh'))    
    return model


sample_a = Input(shape=ftrSize)
    sample_b = Input(shape=ftrSize)

    feature_extractor = build_sequential_model(ftrSize, embedding_dim=embDim)

    feats_a = feature_extractor(sample_a)
    feats_b = feature_extractor(sample_b)
    # feature_extractor.summary()
    distance = Lambda(utils.euclidean_distance)([feats_a, feats_b])
    outputs = Dense(1, activation="sigmoid")(distance)
    model = Model(inputs=[sample_a, sample_b], outputs=outputs)
    model.compile(loss=utils.contrastive_loss, optimizer=Adam(learning_rate=0.0007),
                  metrics=["accuracy"])  # optimizer="adam", optimizer=Adam(lr=0.001)

    checkpoint_path = 'siamese/1D/db' + str(dBthresh) + '_' + 'emb' + str(embDim) + '_' + excludeName + '.ckpt'

    checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_loss', save_best_only=True, mode='min',
                                 save_weights_only=True,
                                 verbose=1,
                                 )
    callback_list = [checkpoint]#, early_stop, reduce_lr]

    history = model.fit(
        [pairs_train[:, 0], pairs_train[:, 1]], labels_train[:],
        validation_data=([pairs_val[:, 0], pairs_val[:, 1]], labels_val[:]),
        batch_size=16,
        epochs=20,
        callbacks=callback_list,
        # verbose=0,
        )

where ftrSize = 56 and embDim = 48

I have a loop at I try and test 10 times the same architecture on different sets of my data. At random times I get an error saying:

Incompatible shapes: [16,48] vs. [0]
     [[{{node model/lambda/sub}}]] [Op:__inference_test_function_182379]

pointing the sum_squared line in the euclidean distance function I am using as a Lambda layer

def euclidean_distance(vectors):
    feats_a, feats_b = vectors
    sum_squared = K.sum(K.square(feats_a - feats_b), axis=1,
                        keepdims=True)
    return K.sqrt(K.maximum(sum_squared, K.epsilon()))

shape [16,48] makes total sense since it's [batch size, embDim] but it seems that the second vector in euclidean_distance is of shape [0]

Upon checking on my test and train pair sets when this error occurs I cannot find erroneous or missing values. Data are numpy arrays

What that shape [0] could possibly mean? What else could be causing this error?

In similar questions usually the cause is a reshaping or inconsistent input image shapes but this is not in my case

I am using Python 3.11.4 tensorflow-macos==2.14.0 numpy==1.25.2

0

There are 0 best solutions below