0

I wanna use eight features to predict a target feature, and while I am using keras, I got accuracy to be zeros all the time. I am new to machine learning, and I am quite confused.

Have tried different activation, I thought this could be a regression problem so I used 'linear' as the last activation function, and it turns out that the accuracy is still zero

from sklearn import preprocessing
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
import pandas as pd


# Step 2 - Load our data
zeolite_13X_error = pd.read_csv("zeolite_13X_error.csv", delimiter=",")
dataset = zeolite_13X_error.values
X = dataset[:, 0:8]
Y = dataset[:, 10]  # Purity
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
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_text, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)

# Building and training first NN
model = Sequential([
    Dense(32, activation='relu', input_shape=(8,)),
    Dense(32, activation='relu'),
    Dense(1, activation='linear'),
])
model.compile(optimizer='sgd',
              loss='binary_crossentropy',
              metrics=['accuracy'])
hist = model.fit(X_train, Y_train,
                 batch_size=32, epochs=10,
                 validation_data=(X_val, Y_val))
desertnaut
  • 57,590
  • 26
  • 140
  • 166
yulin wang
  • 11
  • 1
  • 3
  • what's the number of features you're trying to predict? If it's only two, you can use Sigmoid in the output layer to help you predict your values between two categories (0 or 1) – Celso Wellington Oct 07 '19 at 21:16
  • I am only trying to predict one target actually, based on eight input features. I was originally using sigoid, but the accuracy is still 0 all the time. – yulin wang Oct 07 '19 at 21:20
  • Accuracy only makes sense for classification, and this looks like a regression problem, so you cannot use accuracy for this. – Dr. Snoopy Oct 08 '19 at 08:27

2 Answers2

1

If you decide to treat this as a regression problem, then

  1. Your loss should be mean_squared_error, or some other loss appropriate for regression, but not binary_crossentropy, which is appropriate for binary classification only, and
  2. Accuracy is meaningless - it is meaningful only for classification settings; in regression settings, we normally use the loss itself for performance evaluation - see own answer in What function defines accuracy in Keras when the loss is mean squared error (MSE)? for more.

If you decide to tackle this as a classification problem, you should change the activation of your last layer to sigmoid.

In any case, the combination you show here - loss='binary_crossentropy' and activation='linear' for the single-node last layer - is meaningless.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

Check the output of your model to check the values. The model is predicting probabilities, instead of binary 0/1 decision which i believe is your case as you are using accuracy as a metric. If the model is predicting probabilities then convert them into 0 or 1 by rounding them based on threshold (of your choice i.e. if prediction > 0.5 then 1 else 0).

Also increase the number of epochs. Also use sigmoid activation in the output layer.

secretive
  • 2,032
  • 7
  • 16