我对keras很陌生我已经构建了一个简单的网络来尝试:
import numpy as np;
from keras.models import Sequential;
from keras.layers import Dense,Activation;
data= np.genfromtxt("./kerastests/mydata.csv", delimiter=';')
x_target=data[:,29]
x_training=np.delete(data,6,axis=1)
x_training=np.delete(x_training,28,axis=1)
model=Sequential()
model.add(Dense(20,activation='relu', input_dim=x_training.shape[1]))
model.add(Dense(10,activation='relu'))
model.add(Dense(1));
model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])
model.fit(x_training, x_target)
从我的源数据中我已经删除了2列,如您所见。一个是以字符串格式出现日期的列(在它旁边的数据集中,我有一个列用于另一个月的另一个列,另一个用于一年,因此我不需要该列)而另一列是我用作的列模型的目标)。
当我训练这个模型时,我得到了这个输出:
32/816 [>.............................] - ETA: 23s - loss: 13541942.0000 - acc: 0.0000e+00
800/816 [============================>.] - ETA: 0s - loss: 11575466.0400 - acc: 0.0000e+00
816/816 [==============================] - 1s - loss: 11536905.2353 - acc: 0.0000e+00
Epoch 2/10
32/816 [>.............................] - ETA: 0s - loss: 6794785.0000 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5381360.4314 - acc: 0.0000e+00
Epoch 3/10
32/816 [>.............................] - ETA: 0s - loss: 6235184.0000 - acc: 0.0000e+00
800/816 [============================>.] - ETA: 0s - loss: 5199512.8700 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5192977.4216 - acc: 0.0000e+00
Epoch 4/10
32/816 [>.............................] - ETA: 0s - loss: 4680165.5000 - acc: 0.0000e+00
736/816 [==========================>...] - ETA: 0s - loss: 5050110.3043 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5168771.5490 - acc: 0.0000e+00
Epoch 5/10
32/816 [>.............................] - ETA: 0s - loss: 5932391.0000 - acc: 0.0000e+00
768/816 [===========================>..] - ETA: 0s - loss: 5198882.9167 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5159585.9020 - acc: 0.0000e+00
Epoch 6/10
32/816 [>.............................] - ETA: 0s - loss: 4488318.0000 - acc: 0.0000e+00
768/816 [===========================>..] - ETA: 0s - loss: 5144843.8333 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5151492.1765 - acc: 0.0000e+00
Epoch 7/10
32/816 [>.............................] - ETA: 0s - loss: 6920405.0000 - acc: 0.0000e+00
800/816 [============================>.] - ETA: 0s - loss: 5139358.5000 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5169839.2941 - acc: 0.0000e+00
Epoch 8/10
32/816 [>.............................] - ETA: 0s - loss: 3973038.7500 - acc: 0.0000e+00
672/816 [=======================>......] - ETA: 0s - loss: 5183285.3690 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5141417.0000 - acc: 0.0000e+00
Epoch 9/10
32/816 [>.............................] - ETA: 0s - loss: 4969548.5000 - acc: 0.0000e+00
768/816 [===========================>..] - ETA: 0s - loss: 5126550.1667 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5136524.5098 - acc: 0.0000e+00
Epoch 10/10
32/816 [>.............................] - ETA: 0s - loss: 6334703.5000 - acc: 0.0000e+00
768/816 [===========================>..] - ETA: 0s - loss: 5197778.8229 - acc: 0.0000e+00
816/816 [==============================] - 0s - loss: 5141391.2059 - acc: 0.0000e+00
为什么会这样?我的数据是定时系列。我知道,对于定时系列,人们不会使用密集神经元,但它只是一个测试。真正让我感到沮丧的是,准确度总是为0.而对于其他测试,我甚至会失败:达到“NAN”值。
有人可以帮忙吗?
干杯。
答案 0 :(得分:25)
您的模型似乎与回归模型相对应,原因如下:
您正在使用linear
(默认值)作为输出图层中的激活函数(以及之前图层中的relu
)。
您的损失是loss='mean_squared_error'
。
但是,您使用的指标 - metrics=['accuracy']
对应于分类问题。如果要进行回归,请删除metrics=['accuracy']
。也就是说,使用
model.compile(optimizer='adam',loss='mean_squared_error')
以下是回归和分类的keras指标列表(取自this blog post):
Keras回归指标
•均方误差:mean_squared_error,MSE或mse
•平均绝对误差:mean_absolute_error,MAE,mae
•平均绝对百分比误差:mean_absolute_percentage_error,MAPE, mape
•Cosine Proximity:cosine_proximity,cosine
Keras分类指标
•二进制准确度:binary_accuracy,acc
•分类准确度:categorical_accuracy,acc
•稀疏分类准确度:sparse_categorical_accuracy
•Top k分类准确度:top_k_categorical_accuracy(需要你 指定一个k参数)
•Sparse Top k分类准确度:sparse_top_k_categorical_accuracy (要求您指定k参数)
答案 1 :(得分:2)
尝试这个。
在尝试从kaggle解决泰坦尼克号问题时,我忘记填充数据框中的缺失数据,因为缺失数据中填充了“ nan”。
模型抛出类似的输出
#------------------------------------------------------
Epoch 1/50
891/891 [==============================] - 3s 3ms/step - loss: 9.8239 - acc: 0.0000e+00
Epoch 2/50
891/891 [==============================] - 1s 2ms/step - loss: 9.8231 - acc: 0.0000e+00
Epoch 3/50
891/891 [==============================] - 1s 1ms/step - loss: 9.8231 - acc: 0.0000e+00
Epoch 4/50
891/891 [==============================] - 1s 1ms/step - loss: 9.8231 - acc: 0.0000e+00
Epoch 5/50
891/891 [==============================] - 1s 1ms/step - loss: 9.8231 - acc: 0.0000e+00
#------------------------------------------------------
在将数据输入模型之前,请确保已准备好数据。
就我而言,我必须进行以下更改
+++++++++++++++++++++++++++++++++++
dataset[['Age']] = dataset[['Age']].fillna(value=dataset[['Age']].mean())
dataset[['Fare']] = dataset[['Fare']].fillna(value=dataset[['Fare']].mean())
dataset[['Embarked']] = dataset[['Embarked']].fillna(value=dataset['Embarked'].value_counts().idxmax())
答案 2 :(得分:1)
我想指出一件非常重要的事情,但不幸的是它被忽略了:mean_squared_error
并不是无效分类功能的无效损失函数。
cross_entropy 的数学性质与 mean_squared_error 的假设(在本评论中将不作赘述)一起使后者不合适或在分类问题的训练方面,比 cross_entropy 更糟糕。
答案 3 :(得分:0)
添加以下内容以获得指标:
history = model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mean_squared_error'])
# OR
history = model.compile(optimizer='adam', loss='mean_absolute_error', metrics=['mean_absolute_error'])
history.history.keys()
history.history