0

I'm making a simple classification algo with a keras neural network. The goal is to take 3 data points on weather and decide whether or not there's a wildfire. Here's an image of the .csv dataset that I'm using to train the model(this image is only the top few lines and isn't the entire thing ): wildfire weather dataset As you can see, there are 4 columns with the fourth being either a "1" which means "fire", or a "0" which means "no fire". I want the algo to predict either a 1 or a 0. This is the code that I wrote:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import csv


#THIS IS USED TO TRAIN THE MODEL
# Importing the dataset
dataset = pd.read_csv('Fire_Weather.csv')
dataset.head()

X=dataset.iloc[:,0:3]
Y=dataset.iloc[:,3]

X.head()
obj=StandardScaler()
X=obj.fit_transform(X)

X_train,X_test,y_train,y_test=train_test_split(X, Y, test_size=0.25)


print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)


classifier = Sequential()

    # Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 
                                                      'relu', input_dim = 3))
   # classifier.add(Dropout(p = 0.1))

   # Adding the second hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation 
                                                                   = 'relu'))
   # classifier.add(Dropout(p = 0.1))

   # Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation 
                                                               = 'sigmoid'))

       # Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics 
                                                          = ['accuracy'])

classifier.fit(X_train, y_train, batch_size = 3, epochs = 10)
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
print(y_pred)

classifier.save("weather_model.h5")

The problem is that whenever I run this, my accuracy is always "0.0000e+00" and my training output looks like this:

    Epoch 1/10
2146/2146 [==============================] - 2s 758us/step - loss: nan - accuracy: 0.0238
Epoch 2/10
2146/2146 [==============================] - 1s 625us/step - loss: nan - accuracy: 0.0000e+00
Epoch 3/10
2146/2146 [==============================] - 1s 604us/step - loss: nan - accuracy: 0.0000e+00
Epoch 4/10
2146/2146 [==============================] - 1s 609us/step - loss: nan - accuracy: 0.0000e+00
Epoch 5/10
2146/2146 [==============================] - 1s 624us/step - loss: nan - accuracy: 0.0000e+00
Epoch 6/10
2146/2146 [==============================] - 1s 633us/step - loss: nan - accuracy: 0.0000e+00
Epoch 7/10
2146/2146 [==============================] - 1s 481us/step - loss: nan - accuracy: 0.0000e+00
Epoch 8/10
2146/2146 [==============================] - 1s 476us/step - loss: nan - accuracy: 0.0000e+00
Epoch 9/10
2146/2146 [==============================] - 1s 474us/step - loss: nan - accuracy: 0.0000e+00
Epoch 10/10
2146/2146 [==============================] - 1s 474us/step - loss: nan - accuracy: 0.0000e+00

Does anyone know why this is happening and what I could do to my code to fix this? Thank You!

gm gm
  • 17
  • 3
  • Hi, have you already checked, if your data contains `NaN` or `inf` values? keras/tensorflow doesn't like that at all! I'd suggest you to convert all data to `float32`, make sure no such values are included and apply a standard-normalizer (e.g. the one of sklearn). – jottbe Feb 23 '20 at 21:52
  • can you print out a few rows of X and Y for me? – Nicolas Gervais Feb 23 '20 at 23:09
  • Please paste some data from csv files in order to see whats going on – Prany Feb 24 '20 at 12:32
  • Please Provide a dataset other wise I will not able to predict what things wrong? because dataset which i have by using that it work correct ? Your code is correct. and check weather it does not nan or string value – Welcome_back Feb 25 '20 at 08:09

1 Answers1

0

EDIT: I realized that my earlier response was highly misleading, which was thankfully pointed out by @xdurch0 and @Timbus Calin. Here is an edited answer.

  1. Check that all your input values are valid. Are there any nan or inf values in your training data?

  2. Try using different activation functions. ReLU is good, but it is prone to what is known as the dying ReLu problem, where the neural network basically learns nothing since no updates are made to its weight. One possibility is to use Leaky ReLu or PReLU.

  3. Try using gradient clipping, which is a technique used to tackle vanishing or exploding gradients (which is likely what is happening in your case). Keras allows users to configure clipnorm clip value for optimizers.

There are posts on SO that report similar problems, such as this one, which might also be of interest to you.

Jake Tae
  • 1,681
  • 1
  • 8
  • 11
  • 1
    This is not correct. Keras is "smart" enough to convert the predictions to 0/1 (by thresholding at 0.5) and do the comparisons that way (assuming the labels were supplied correctly). The issue is most likely stems from the same problem that results in a loss of `nan` (whatever that may be). I.e. the model outputs are either `nan` or maybe `inf`. – xdurch0 Feb 24 '20 at 09:23
  • xdurch0 is correct. Per your logic, almost all the time we will be having 0% accuracy, only in the cases where the model is 100% sure that a data point belongs to a class or not. – Timbus Calin Feb 24 '20 at 09:35
  • @xdurch0 Thanks for pointing it out. My earlier response was written on the fly and admittedly poorly thought out. I've edited the answer after more thinking. Thanks again. – Jake Tae Feb 24 '20 at 10:55
  • Thanks everyone for helping! I went through my dataset again and realized that I had some `NaN` values. After deleting those, my algo started training as supposed to and now works perfectly. – gm gm Feb 25 '20 at 00:51