Problems with input_shape using Keras

Asked

Viewed 47 times

0

Hello,

I’m trying to make my first ANN code to predict the price of homes. I have separated the datasets into training and testing, however, I am finding some problems when compiling my code below:

    x_train =  train[col]
    y_train = train['price']
    x_test = test[col]
    y_test = test['price']  
    model = Sequential([
        Dense(32, activation='relu', input_dim=(11,)), 
        Dense(32, activation='relu'), 
        Dense(1, activation='sigmoid')
    ])
    model.compile(
        optimizer='sgd',
        loss='binary_crossentropy',
        metrics= ['accuracy']
    )
    hist = model.fit(
        x_train,
        x_train,
        batch_size=32, 
        epochs=100,
        validation_data=(x_test, y_test)
    )

Which returns the second error:

 ValueError: Error when checking target: expected dense_31 to have shape (1,) but got array with shape (11,)

Imagem do dataset

  • Because x_train is repeated in model.fit()?

1 answer

0

The problem is in model.fit, as the target variable is expected to have the same format as defined in Sequential().

So when does

hist = model.fit(
        x_train,
        x_train,
        batch_size=32, 
        epochs=100,
        validation_data=(x_test, y_test)
    )

you put x_train as target, generating the error.

The right thing would be

hist = model.fit(
        x_train,
        y_train,
        batch_size=32, 
        epochs=100,
        validation_data=(x_test, y_test)
    )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.