Arturo Peguero
Arturo Peguero

Reputation: 25

Correcting NaN values/loss for ANN in tensorflow

I am running a churn model using tensorflow and running into a NaN loss. Reading around, I found that I probably had some NaN values in my data as was confirmed by print(np.any(np.isnan(X_test))).

I tried using

def standardize(train, test):
    mean = np.mean(train, axis=0)
    std = np.std(train, axis=0)+0.000001
    X_train = (train - mean) / std
    X_test = (test - mean) /std
    return X_train, X_test

But still coming up with NaN values.

Here's the full code if it helps:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf

dataset = pd.read_excel('CHURN DATA.xlsx')
X = dataset.iloc[:, 2:45].values
y = dataset.iloc[:, 45].values

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X[:, 1] = le.fit_transform(X[:,1])

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(),[0])], remainder = 'passthrough')
X = np.array(ct.fit_transform(X))

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units = 43, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 43, activation = 'relu'))
ann.add(tf.keras.layers.Dense(units = 1, activation = 'sigmoid'))
ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
ann.fit(X_train, y_train, batch_size = 256, epochs = 50)

Upvotes: 1

Views: 858

Answers (1)

Olasimbo
Olasimbo

Reputation: 1063

You havent replaced the nan values. And it’s likely that you have some inf and -inf values also in your data. You can replace both of them with 0

For dataframe

X.replace([np.inf, -np.inf], np.nan, inplace=True)
X = X.fillna(0)

or if your data is in a numpy array

X[np.isnan(X)] = 0

X[X == np.inf] = 0 
X[X == -np.inf] = 0

Upvotes: 1

Related Questions