我正在使用keras神经网络进行简单分类。目标是获取3个天气数据点,并确定是否有野火。这是我用来训练模型的.csv数据集的图像(此图像只是最上面的几行,并不是全部): wildfire weather dataset 如您所见,有4列,第四列是“ 1”表示“ fire”或“ 0”表示“ no fire”。我希望算法预测1或0。这是我编写的代码:
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”,并且训练结果如下:
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
有人知道为什么会这样吗,我能对我的代码做些什么来解决这个问题? 谢谢!
答案 0 :(得分:0)
编辑:我意识到我以前的回答非常容易引起误解,@ xdurch0和@Timbus Calin指出了这一点。这是经过编辑的答案。
检查所有输入值是否有效。您的训练数据中是否有任何nan
或inf
值?
尝试使用其他激活功能。 ReLU
很好,但是它很容易出现dying ReLu problem的问题,其中神经网络基本上不学任何东西,因为它的权重没有更新。一种可能性是使用Leaky ReLu or PReLU。
请尝试使用渐变剪切,这是一种用于消除消失或爆炸的渐变的技术(在您的情况下可能会发生这种情况)。 Keras允许用户为优化器配置clipnorm
clip value
。
SO上有一些帖子报告了类似的问题,例如this one,您可能也会对此感兴趣。