0
$\begingroup$

I have a multi-input Keras model combining a text input and a numeric input. Both inputs are passed through Dense layers followed by Embedding layers, then concatenated and pooled using GlobalMaxPooling1D. Despite thorough debugging of layer dimensions at each step, I still encounter an error when feeding data into the model.

Here is the model definition:

def model_builder(hp, vocab_size, max_token_size):
    # Hiperparametre tanımları
    hp_text_units = hp.Int('text_units', min_value=64, max_value=512, step=32)
    hp_numeric_units = hp.Int('numeric_units', min_value=32, max_value=256, step=32)
    hp_dropout = hp.Float('dropout', min_value=0.1, max_value=0.5, step=0.1)
    hp_learning_rate = hp.Float('learning_rate', min_value=1e-4, max_value=1e-2, sampling='log')

    # Girdi tanımlamaları
    text_input = tf.keras.Input(shape=(max_token_size,), dtype="int32", name="text_input")
    numeric_input = tf.keras.Input(shape=(26,), name="numeric_input")
    # Debugging Embedding Layer Output
    temp_model = tf.keras.Model(inputs=text_input, outputs=text_input)
    print("Text input Shape (Static):", temp_model.output_shape)

    temp_model = tf.keras.Model(inputs=numeric_input, outputs=numeric_input)
    print("Numeric input Shape (Static):", temp_model.output_shape)

    text_dense = tf.keras.layers.Dense(224, activation='relu')(text_input)

    # Debugging Text Dense Layer Output
    temp_model = tf.keras.Model(inputs=text_input, outputs=text_dense)
    print("Text Dense Layer Shape (Static):", temp_model.output_shape)

    # Embedding katmanı
    embedding_layer = tf.keras.layers.Embedding(
        input_dim=224,#vocab_size,
        output_dim=224,  # Embedding boyutu sabit
        #input_length=max_token_size
    )(text_dense)

    # Debugging Embedding Layer Output
    temp_model = tf.keras.Model(inputs=text_input, outputs=embedding_layer)
    print("Embedding Layer Shape (Static):", temp_model.output_shape)

    # Text Pipeline
    text_dense1 = tf.keras.layers.Dense(224, activation='relu')(embedding_layer)

    # Debugging Text Dense Layer Output
    temp_model = tf.keras.Model(inputs=text_input, outputs=text_dense1)
    print("Text Dense Layer 1 Shape (Static):", temp_model.output_shape)

    text_batch_normalization = tf.keras.layers.BatchNormalization()(text_dense1)

    # Debugging Text Dense Layer Output
    temp_model = tf.keras.Model(inputs=text_input, outputs=text_batch_normalization)
    print("Text Batch Normalization Shape (Static):", temp_model.output_shape)

    text_dropout = tf.keras.layers.Dropout(hp_dropout)(text_batch_normalization)

    temp_model = tf.keras.Model(inputs=text_input, outputs=text_dropout)
    print("Text Dropout Shape (Static):", temp_model.output_shape)

    # Numeric Pipeline
    numeric_input_reshaped = tf.keras.layers.Reshape((26,))(numeric_input)
    temp_model = tf.keras.Model(inputs=numeric_input, outputs=numeric_input_reshaped)
    print("Numeric Reshape Layer Shape (Static):", temp_model.output_shape)

    numeric_dense = tf.keras.layers.Dense(224, activation='relu')(numeric_input_reshaped)

    # Debugging Numeric Dense Layer Output
    temp_model = tf.keras.Model(inputs=numeric_input, outputs=numeric_dense)
    print("Numeric Dense Layer Shape (Static):", temp_model.output_shape)

    #flattened_numeric_input = tf.keras.layers.Flatten()(numeric_input)
    embedding_layer_numeric = tf.keras.layers.Embedding(
        input_dim=224,
        output_dim=224,  # Embedding boyutu sabit
        #input_length=max_token_size
    )(numeric_dense)

    # Debugging Embedding Layer Output
    temp_model = tf.keras.Model(inputs=text_input, outputs=embedding_layer_numeric)
    print("Embedding Layer Numeric Shape (Static):", temp_model.output_shape)

    numeric_dense1 = tf.keras.layers.Dense(224, activation='relu')(embedding_layer_numeric)

    # Debugging Numeric Dense Layer Output
    temp_model = tf.keras.Model(inputs=numeric_input, outputs=numeric_dense1)
    print("Numeric Dense Layer 1 Shape (Static):", temp_model.output_shape)

    numeric_batch_normalization = tf.keras.layers.BatchNormalization()(numeric_dense1)
    temp_model = tf.keras.Model(inputs=numeric_input, outputs=numeric_batch_normalization)
    print("Numeric Batch Normalization Shape (Static):", temp_model.output_shape)

    numeric_dropout = tf.keras.layers.Dropout(hp_dropout)(numeric_batch_normalization)
    temp_model = tf.keras.Model(inputs=numeric_input, outputs=numeric_dropout)
    print("Numeric Dropout Shape (Static):", temp_model.output_shape)

    # Birleştirme
    combined = tf.keras.layers.Concatenate()([text_dropout, numeric_dropout])

    # Debugging Concatenated Layer Output
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined)
    print("Concatenated Layer Shape (Static):", temp_model.output_shape)

    # Global Max Pooling ile boyut düşürme
    combined_pooled = tf.keras.layers.GlobalMaxPooling1D()(combined)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_pooled)
    print("Concatenated GlobalMaxPooling Shape (Static):", temp_model.output_shape)

    # Combined Pipeline
    combined_dense1 = tf.keras.layers.Dense(256, activation='relu')(combined_pooled)

    # Debugging Combined Dense Layer 1 Output
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_dense1)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_dense1)
    print("Combined Dense Layer 1 Shape (Static):", temp_model.output_shape)

    combined_batch_normalization = tf.keras.layers.BatchNormalization()(combined_dense1)
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_batch_normalization)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_batch_normalization)
    print("Combined Batch Normalization Shape (Static):", temp_model.output_shape)

    combined_dropout = tf.keras.layers.Dropout(hp_dropout)(combined_batch_normalization)
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_dropout)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_dropout)
    print("Combined Dropout Shape (Static):", temp_model.output_shape)

    combined_dense2 = tf.keras.layers.Dense(128, activation='relu')(combined_dropout)

    # Debugging Combined Dense Layer 2 Output
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_dense2)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_dense2)
    print("Combined Dense Layer 2 Shape (Static):", temp_model.output_shape)

    combined_batch_normalization1 = tf.keras.layers.BatchNormalization()(combined_dense2)
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_batch_normalization1)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_batch_normalization1)
    print("Combined Batch Normalization 1 Shape (Static):", temp_model.output_shape)

    combined_dropout1= tf.keras.layers.Dropout(hp_dropout)(combined_batch_normalization1)
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_dropout1)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_dropout1)
    print("Combined Dropout 1 Shape (Static):", temp_model.output_shape)

    combined_dense3 = tf.keras.layers.Dense(64, activation='relu')(combined_dropout1)

    # Debugging Combined Dense Layer 3 Output
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_dense3)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_dense3)
    print("Combined Dense Layer 3 Shape (Static):", temp_model.output_shape)

    combined_dropout2 = tf.keras.layers.Dropout(hp_dropout)(combined_dense3)
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=combined_dropout2)
    temp_model = tf.keras.Model(inputs=[combined], outputs=combined_dropout2)
    print("Combined Dropout2 Shape (Static):", temp_model.output_shape)

    # Çıkış Katmanı
    output = tf.keras.layers.Dense(1, activation='sigmoid')(combined_dropout2)

    # Debugging Output Layer Shape
    #temp_model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=output)
    temp_model = tf.keras.Model(inputs=[combined], outputs=output)

    print("Output Layer Shape (Static):", temp_model.output_shape)

    # Model Derleme
    model = tf.keras.Model(inputs=[text_input, numeric_input], outputs=output)
    model.compile(
        optimizer=tf.keras.optimizers.Adam(learning_rate=hp_learning_rate),
        loss='binary_crossentropy',
        metrics=['accuracy']
    )

    return model

Error Logs:

Despite verifying dimensions throughout the model using debug print statements, I get the following error when the model starts training:

ValueError: Exception encountered when calling Functional.call(). Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 1149, but received input with shape (None, 26)

Debug Logs: Here are the layer dimensions printed during model building:

Text input Shape (Static): (None, 1149) Text Dense Layer Shape (Static): (None, 224) Embedding Layer Shape (Static): (None, 224, 224) Text Dense Layer 1 Shape (Static): (None, 224, 224) Text Batch Normalization Shape (Static): (None, 224, 224) Text Dropout Shape (Static): (None, 224, 224) Numeric input Shape (Static): (None, 26) Numeric Reshape Layer Shape (Static): (None, 26) Numeric Dense Layer Shape (Static): (None, 224) Embedding Layer Numeric Shape (Static): (None, 224, 224) Numeric Dense Layer 1 Shape (Static): (None, 224, 224) Numeric Batch Normalization Shape (Static): (None, 224, 224) Numeric Dropout Shape (Static): (None, 224, 224) Concatenated Layer Shape (Static): (None, 224, 448) Concatenated GlobalMaxPooling Shape (Static): (None, 448) Combined Dense Layer 1 Shape (Static): (None, 256) Combined Batch Normalization Shape (Static): (None, 256) Combined Dropout Shape (Static): (None, 256) Combined Dense Layer 2 Shape (Static): (None, 128) Combined Batch Normalization 1 Shape (Static): (None, 128) Combined Dropout 1 Shape (Static): (None, 128) Combined Dense Layer 3 Shape (Static): (None, 64) Combined Dropout2 Shape (Static): (None, 64) Output Layer Shape (Static): (None, 1)

$\endgroup$
3
  • $\begingroup$ Hey. Programming questions/issues are off-topic here, even if your program is an AI program. We focus on theoretical, social and philosophical aspects of AI. See ai.stackexchange.com/help/on-topic. $\endgroup$ Commented Dec 18, 2024 at 11:41
  • $\begingroup$ This does not make sense. There are many keras and/or tensorflow questions in this platform hence you have the specific dedicated tags for them. This is not just programming this is about how to conduct a multi-input deep learning model!! $\endgroup$ Commented Dec 18, 2024 at 13:48
  • $\begingroup$ When this site was conceived, there were already other sites that covered programming questions, like Stack Overflow. Our focus was to be a science site and avoid overlapping too much with other sites, so that we had a distinct purpose. You may be interested also in the site Data Science SE, but you're very welcome to ask theoretical AI questions here! If you think we could change now our scope to also cover programming issues related to AI, you could create a discussion here: ai.meta.stackexchange.com/questions/ask. $\endgroup$ Commented Dec 18, 2024 at 17:12

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.