[Python] Why does my Keras model show 4 parameters instead of 2?

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm training a simple Keras model with one Dense layer, but when I call model.summary(), it shows 4 parameters instead of 2.

    import tensorflow as tf
    import numpy as np

    # Generate dummy data
    X_train = np.random.rand(40) # Shape (40,)
    y_train = np.random.rand(40)

    # Define a simple model
    model = tf.keras.Sequential([
    tf.keras.layers.Dense(1)
    ])

    # Compile the model
    model.compile(loss=tf.keras.losses.mae,
    optimizer=tf.keras.optimizers.SGD(),
    metrics=["mae"])

    # Train the model
    model.fit(tf.expand_dims(X_train, axis=-1), y_train, epochs=100)

    # Check model summary
    model.summary()


    Since Dense(1) should have 1 weight + 1 bias, I expected 2 parameters, not 4.

    Why does passing a 1D input (shape=(40,)) cause Keras to use 4 parameters instead of 2? Is Keras automatically handling the input shape in a way that duplicates the parameters?

    this is the result:

    [​IMG]

    Continue reading...

Compartilhe esta Página