'My loss value is nan and accuracy: 0.0000e+00 on numerical data
I am working on the XAI model and implementing a simple model based on my data. While training, the loss, and accuracy are Nan and I am unable to find out the problem.
[XAI] https://github.com/EthicalML/xai/blob/master/examples/XAI%20Tabular%20Data%20Example%20Usage.ipynb
Below is my data, please do not include usage columns because these are prediction columns.
import xai.data
import pandas as pd
df = pd.read_csv('/content/heat_pipe.csv')
categorical_cols = ["Date", "Temperature(degree)", "Wind_speed(m/s)","Humidity(%)","day_of_the_week", "sarturday", "sunday", "public_holidays_(weekdays)","total_holidays"]
_ = xai.correlations(df, include_categorical=True, plot_type="matrix")
_ = xai.correlations(df, include_categorical=True)
proc_df = xai.normalize_numeric(df)
proc_df = xai.convert_categories(proc_df)
x = proc_df.drop("usage_(total)", axis=1)
y = proc_df["usage_(total)"]
x_train, y_train, x_test, y_test, train_idx, test_idx = \
xai.balanced_train_test_split(
x, y, "Date", "Temperature(degree)", "Wind_speed(m/s)","Humidity(%)", "day_of_the_week", "sarturday", "sunday", "public_holidays_(weekdays)","total_holidays",
min_per_group=300,
max_per_group=300,
categorical_cols=categorical_cols)
x_train_display = df[train_idx]
x_train_display
x_test_display = df[test_idx]
print("Total number of examples: ", x_test.shape[0])
df_test = x_test_display.copy()
df_test["usage_(total)"] = y_test
Model Code
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, mean_squared_error, roc_curve, auc
from tensorflow.keras.layers import Input, Dense, Flatten,Concatenate, concatenate, Dropout, Lambda, Embedding
from tensorflow.keras.models import Model, Sequential
def build_model(X):
input_els = []
encoded_els = []
dtypes = list(zip(X.dtypes.index, map(str, X.dtypes)))
for k,dtype in dtypes:
input_els.append(Input(shape=(1,)))
if dtype == "int8":
e = Flatten()(Embedding(X[k].max()+1, 1)(input_els[-1]))
else:
e = input_els[-1]
encoded_els.append(e)
encoded_els = concatenate(encoded_els)
layer1 = Dropout(0.5)(Dense(100, activation="relu")(encoded_els))
out = Dense(1, activation='sigmoid')(layer1)
# train model
model = Model(inputs=input_els, outputs=[out])
model.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['accuracy'])
return model
def f_in(X, m=None):
"""Preprocess input so it can be provided to a function"""
if m:
return [X.iloc[:m,i] for i in range(X.shape[1])]
else:
return [X.iloc[:,i] for i in range(X.shape[1])]
def f_out(probs, threshold=0.5):
"""Convert probabilities into classes"""
return list((probs >= threshold).astype(int).T[0])
model = build_model(x_train)
model.fit(f_in(x_train), y_train, epochs=50, batch_size=512)
Error
Epoch 1/50
1/1 [==============================] - 1s 713ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 2/50
1/1 [==============================] - 0s 12ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 3/50
1/1 [==============================] - 0s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 4/50
1/1 [==============================] - 0s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 5/50
1/1 [==============================] - 0s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 6/50
1/1 [==============================] - 0s 12ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 7/50
1/1 [==============================] - 0s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 8/50
1/1 [==============================] - 0s 12ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 9/50
1/1 [==============================] - 0s 13ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 10/50
1/1 [==============================] - 0s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 11/50
1/1 [==============================] - 0s 13ms/step - loss: nan - accuracy: 0.0000e+00
Solution 1:[1]
For me this looks like a problem with your loss function. Categorical_crossentropy is normaly used in multiclass prediction.
So to me this looks not like you have a classification problem but more like a regression problem. That's why you should use a loss for regression like mean_squared_error:
model.compile(optimizer="adam", loss='mean_squared_error', metrics=['accuracy'])
Another thing might be your activation function. Most of the time if you try to predict numerical values a linear activation function is used. A helpful read to choose the correct activation function might be this.
That means you would have to change your activation function of the output layer to linear
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fabian |

