[Python] Why does my SimpleRNN model in Sequential API show '?' for output shape and zero...

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024.

  1. Stack

    Stack Membro Participativo

    I'm building a SimpleRNN model with an Embedding layer in Keras and encountering an issue when using the Sequential API. The model summary shows the output shape as ? and the number of trainable parameters as 0. However, when I build the same model using the Functional API, everything works as expected.

    from keras.models import Sequential
    from keras.layers import Embedding, SimpleRNN, Dense

    model = Sequential([
    Embedding(input_dim=10000, output_dim=50, input_length=100),
    SimpleRNN(32),
    Dense(1, activation='sigmoid')
    ])

    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    model.summary()


    I get the following output:

    Output shape: ?
    Trainable parameters: 0(unbuilt)


    However, when I use the Functional API, like this:

    from keras.models import Model
    from keras.layers import Input, Embedding, SimpleRNN, Dense

    inputs = Input(shape=(100,))
    x = Embedding(input_dim=10000, output_dim=50)(inputs)
    x = SimpleRNN(32)(x)
    outputs = Dense(1, activation='sigmoid')(x)

    model = Model(inputs, outputs)
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    model.summary()


    The model works fine with the correct output shape and trainable parameters.

    Could anyone explain why this is happening with the Sequential API and how I can resolve this issue? Am I missing something in the configuration for Sequential?

    Continue reading...

Compartilhe esta Página