[Python] In my Tensorflow model, why is the output of this concatenation the wrong shape? It...

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    After passing an input sequence to a 1D CNN, I'm trying to combine the output with some engineered features about the sequence. The input sequence is shape (29,1) and the input to the model is (Batch_size, 29, 1). The engineered input is shape (Batch_size, 2). After the CNN the sequence becomes (Batch_size, 29, 512) which I then flatten to (Batch_size, 14848). When I concatenate the two the shape becomes (Batch_size, 14850). Why do I get an error for the Dense layer x7 saying the input its receiving is shape (Batch_size, 1053)?

    cnn_output = Activation('relu')(x6)

    # --- Combining Branches ---
    cnn_flat = Flatten()(cnn_output) # flatten the CNN branch
    engineered_input = Input(shape=(num_engineered_features,), name='engineered_input')
    combined = Concatenate(axis=1)([cnn_flat, engineered_input])

    # --- Final Dense Layers ---
    x7 = Dense(64, activation='relu')(combined)


    I've confirmed these sizes by printing the sizes of the tensors between the layers.

    cnn_output = Activation('relu')(x6)

    # --- Combining Branches ---
    print(f"Shape of CNN Output before flattening: {cnn_output.shape}.")
    cnn_flat = Flatten()(cnn_output) # flatten the CNN branch
    print(f"Shape of CNN Output after flattening: {cnn_flat.shape}.")
    engineered_input = Input(shape=(num_engineered_features,), name='engineered_input')
    print(f"Shape of engineered_input: {engineered_input.shape}.")
    combined = Concatenate(axis=1)([cnn_flat, engineered_input])
    print(f"Shape of the concatenated tensor: {combined.shape}.")

    # --- Final Dense Layers ---
    x7 = Dense(64, activation='relu')(combined)


    Which outputs:

    Shape of CNN Output before flattening: (None, 29, 512).
    Shape of CNN Output after flattening: (None, 14848).
    Shape of engineered input: (None, 2).
    Shape of the concatenated tensor: (None, 14850).


    Before giving the following error: Input 0 of layer "dense_1" is incompatible with the layer: expected axis -1 of input shape to have value 14850, but received input with shape (None, 1053)

    I've also tried serving an individual batch to the model to ensure it's not a batch sizing issue and I still received this shape mismatch error.

    Continue reading...

Compartilhe esta Página