Search code examples
pythontensorflowmachine-learningneural-networktensorflow2.0

Tensorflow binary crossentropy always predicts the same value


I'm new to the general stack overflow community and especially new to the Tensorflow library (1 week as of now). So I'm positive I am missing something here, just can't quite figure it out myself.

I have done a few practices utilizing Tensorflow's example dataset and a few miscellaneous tutorials. I found myself attempting to apply one of the tutorial's code to my own dataset, this dataset is a csv and the model should return it's prediction of the "Last Result" column. This turned out to be a bit much because I quickly encountered a problem, the "predict" function always predicts identical values for every data entry.

[[0.6335701] [0.6335701] ... [0.6335701] [0.6335701]]

import pandas as pd
from sklearn.model_selection import train_test_split
from keras.models import Sequential, load_model
from keras.layers import Dense
from sklearn.metrics import accuracy_score

df = pd.read_csv('*.csv')
x = pd.get_dummies(df.drop(['Last Result'], axis=1))
y = df['Last Result'].apply(lambda X: 1 if X == 'Registered' else 0)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.2)
x_train.head()
print("x train: \n", x_train)
y_train.head()
print("y train: \n", y_train)

# Below is code to create a new TensorFlow Model. If you need to call a saved model, see 'load model' below.

model = Sequential()
model.add(Dense(units=32, activation='relu', input_dim=len(x_train.columns)))
model.add(Dense(units=32, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=100, batch_size=128)

# model.save('*.keras')

# This is code to recall a previously saved TensorFlow Model

# model = load_model('*.keras')

y_hat = model.predict(x_test)
# y_hat = [0 if val < 0.5 else 1 for val in y_hat]
print(y_hat)

# print(accuracy_score(y_test, y_hat))

I have tried other suggestions from similar posts to no avail. My best understanding of the issue is that the model is not learning anything and is instead just guessing all one value because one of the expected values is true ~60% of the time, thus the ~.60 predictions.

Any help on the matter would mean a great deal in assisting my understanding of the library. Thanks in advance and thank you for your time.


Solution

  • You need to remove the pd.get_dummies function call. Just directly assign

    x = df.drop(['Last Result'], axis=1)
    

    Calling pd.get_dummies converts the samples into 0/1 data: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.get_dummies.html.

    Can you give more information about the nature of your dataset? This would help in understanding what you are trying to do.