为什么训练时我的Keras模型的精度始终为0?
keras
neural-network
tensorflow
18
0

我对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)

如您所见,我从源数据中删除了两列。一个是带有字符串格式日期的列(在数据集中,除此以外,我有一天的列,另外一个月的列,以及年的列,因此我不需要该列)和另一列是我用作模型目标的列)。

当我训练这个模型时,我得到以下输出:

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    

为什么会这样呢?我的数据是一个时间序列。我知道对于时间序列,人们通常不使用Dense神经元,但这只是一个测试。真正困扰我的是精度始终为0。在其他测试中,我什至失败了:达到“ NAN”值。

有人可以帮忙吗?

参考资料:
Stack Overflow
收藏
评论
共 1 个回答
高赞 时间 活跃

您的模型似乎与回归模型相对应,原因如下:

  • 您在输出层(以及之前的层中的relu )中使用linear (默认值)作为激活函数。

  • 您的损失为loss='mean_squared_error'

但是,您使用的metrics=['accuracy']metrics=['accuracy']对应于分类问题。如果要进行回归,请删除metrics=['accuracy'] 。即使用

model.compile(optimizer='adam',loss='mean_squared_error')

以下是用于回归和分类的keras度量的列表(摘自此博客文章 ):

Keras回归指标

•均方误差:mean_squared_error,MSE或mse

•平均绝对错误:mean_absolute_error,MAE,mae

•平均绝对百分比错误:mean_absolute_percentage_error,MAPE,mape

•余弦接近度:cosine_proximity,余弦

Keras分类指标

•二进制精度:binary_accuracy,acc

•分类精度:categorical_accuracy,acc

•稀疏分类准确性:sparse_categorical_accuracy

•Top k分类准确度:top_k_categorical_accuracy(要求您指定ak参数)

•Sparse Top k分类准确度:sparse_top_k_categorical_accuracy(要求您指定ak参数)

收藏
评论
新手导航
  • 社区规范
  • 提出问题
  • 进行投票
  • 个人资料
  • 优化问题
  • 回答问题