训练期间Keras神经网络精度始终为0 [英] Keras Neural Network Accuracy is always 0 While Training

查看:566
本文介绍了训练期间Keras神经网络精度始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用keras神经网络进行简单的分类算法.目标是获取有关天气的3个数据点,并确定是否有野火.这是我用于训练模型的.csv数据集的图像(此图像只是最上面的几行,并不是全部): 野火天气数据集 如您所见,有4列,第四列是"1"表示"fire"或"0"表示"no fire".我希望算法预测1或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")

问题是,每当我运行此命令时,我的精度始终为"0.0000e + 00",并且训练结果如下:

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!

推荐答案

我意识到我先前的回答是极具误导性的,@ xdurch0和@Timbus Calin指出了这一点.这是经过编辑的答案.

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. 检查所有输入值是否有效.您的训练数据中是否有任何naninf值?

尝试使用其他激活功能. ReLU很好,但是很容易发生垂死的ReLu问题,神经网络基本上不学任何东西,因为它的权重没有更新.一种可能性是使用泄漏的ReLu或PReLU .

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.

请尝试使用渐变剪辑,这是一种用于解决渐变消失或爆炸的技术(在您的情况下可能会发生这种情况). Keras 允许用户为优化程序配置clipnorm clip value.

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.

SO上有报告类似问题的帖子,例如,您可能也会对此感兴趣.

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

这篇关于训练期间Keras神经网络精度始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆