python-3.xtensorflowkerasneural-networkrelu

Neural Network Using ReLU Activation Function


I am trying to use a neural network to predict the price of houses. Here is what the top of the dataset looks like:

    Price   Beds    SqFt    Built   Garage  FullBaths   HalfBaths   LotSqFt
    485000  3       2336    2004    2       2.0          1.0        2178.0
    430000  4       2106    2005    2       2.0          1.0        2178.0
    445000  3       1410    1999    1       2.0          0.0        3049.0

...

I am trying to use the ReLU activation function, but my accuracy is zero even after 100 epochs. Am I missing something here?

X = dataset[:,1:8] #predictor variables
Y = dataset[:,0] #sell price

#Normalize data
from sklearn import preprocessing
X_scale = min_max_scaler.fit_transform(X)
X_scale

#Split Data
from sklearn.model_selection import train_test_split
X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)
X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)
print(X_train.shape, X_val.shape, X_test.shape, Y_train.shape, Y_val.shape, Y_test.shape)
from keras.models import Sequential
from keras.layers import Dense

model = Sequential(
    Dense(32, activation='relu', input_shape=(7,)))

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

hist = model.fit(X_train, Y_train,
          batch_size=32, epochs=100,
          validation_data=(X_val, Y_val))

model.evaluate(X_test, Y_test)[1]
## Output: 3/3 [==============================] - 0s 3ms/step - loss: -5698781.5000 - accuracy: 0.0000e+00

Solution

  • Your accuracy is 0 because you forgot to add an output layer, so your loss is not computed properly. In addition to this, accuracy is not a relevant metric since you are doing regression and not classification.

    You need to modify your model like this:

    model = Sequential(
        Dense(32, activation='relu', input_shape=(7,)),
        Dense(1, activation='linear'))
    

    Also, in your model.compile() you have to modify your loss to be "mse" instead of "binary_crossentropy", since you are doing regression and not classification.

    model.compile(optimizer='sgd',
                  loss='mse',
                  metrics=['mean_squared_error'])