使用Keras递归神经网络进行预测-精度始终为1.0 [英] Predictions using a Keras Recurrent Neural Network - accuracy is always 1.0
问题描述
TLDR:如何使用Keras RNN预测序列中的下一个值?
TLDR: How do I use a Keras RNN to predict the next value in a sequence?
我有一个顺序值列表.我想将它们输入RNN以预测序列中的下一个值.
I have a list of sequential values. I want to feed them into a RNN to predict the next value in the sequence.
[ 0.43589744 0.44230769 0.49358974 ..., 0.71153846 0.70833333 0.69230769]
我正在使用Keras进行此操作,可以使网络的损耗减少,但精度始终为1.0.这是错误的. y_tests != model.predict(x_tests)
.
I'm using Keras to do this and can get a network with a decreasing loss but the accuracy is consistently 1.0. This is wrong. y_tests != model.predict(x_tests)
.
Epoch 0
1517/1517 [==============================] - 0s - loss: 0.0726 - acc: 1.0000 - val_loss: 0.0636 - val_acc: 1.0000
Epoch 1
1517/1517 [==============================] - 0s - loss: 0.0720 - acc: 1.0000 - val_loss: 0.0629 - val_acc: 1.0000
...
这是我的网络.
model = Sequential()
model.add(SimpleRNN(1, 100))
model.add(Dense(100, 1, activation = "sigmoid"))
model.compile(loss="mean_squared_error", optimizer = "sgd")
我尝试过SimpleRNN,GRU和LSTM,但是没有运气.这是数据的格式.
I have tried a SimpleRNN, GRU and LSTM but have had no luck. Here is how the data is formatted.
# Current value
y_train = [[ 0.60576923] [ 0.64102564] [ 0.66025641] ..., [ 0.71153846] [ 0.70833333] [ 0.69230769]]
# Previous 10 values
x_train_10 = [
[[ 0.65064103] [ 0.66346154] [ 0.66346154] ..., [ 0.72115385] [ 0.72435897] [ 0.71153846]] ...,
[[ 0.66346154] [ 0.66346154] [ 0.67628205] ..., [ 0.72435897] [ 0.71153846] [ 0.70833333]]
]
# Previous value
x_train_1 = [[ 0.58333333] [ 0.60576923] [ 0.64102564] ..., [ 0.72435897] [ 0.71153846] [ 0.70833333]]
# So here are the shapes...
y_train.shape = (1895, 1)
x_train_10.shape = (1895, 10, 1)
x_train_1.shape = (1895, 1)
x_train_10
中的每个元素都是前10个值的列表.我按照Keras的文档格式进行格式化,以使循环图层采用形状为(nb_samples, timesteps, input_dim)
的输入.
Each element in x_train_10
is a list of the previous 10 values. I formatted it like this to follow Keras's documentation that recurrent layers take inputs of shape (nb_samples, timesteps, input_dim)
.
我也尝试使用没有运气的Embedding
层. (这可能是错误的使用方式-我只看到它用于分类而不是预测).
I have also tried using an Embedding
layer with no luck. (This may be the wrong way to use it - I've only seen it used in classification not prediction).
model = Sequential()
model.add(Embedding(1, 30))
model.add(LSTM(30, 100))
...
pad_sequences
也不起作用.
x_train_1 = sequence.pad_sequences(x_train_1, maxlen = None, dtype = "float32")
我想让RNN处理这种简单的数据/架构,以便以后将其用于更复杂的问题.
I want to get the RNN working with this simple data/architecture so I can use it for more complex problems later.
谢谢:)
推荐答案
我在Keras Github上发布了一个类似的问题页面,并得到了很好的答案.
I posted a similar question on the Keras Github page and got a good answer.
lukedeo 说,acc: 1.0000
表示真实输出和预测输出均大于0.5,反之亦然.相反,我应该查看损失或mse
,以确定模型的准确性.这是因为我的网络是一个回归,而不是分类器/集群.
lukedeo said that acc: 1.0000
means that both the true output and the predicted output are greater than 0.5 or vice versa. Instead, I should look at loss, or mse
, to determine the accuracy of the model. This is because my network is a regression not a classifier/clusterer.
均方根误差是准确性的良好度量. accuracy_percent = 1 - np.sqrt(mse)
Root mean squared error is a good measure of accuracy. accuracy_percent = 1 - np.sqrt(mse)
fchollet (Keras的创建者)详细地说,"精度与回归问题根本不相关."
fchollet (the Keras creator) elaborated by saying that "accuracy is not relevant at all for a regression problem."
在进行分类问题时,可以根据目标(网络输出)将class_mode
设置为'categorical'
或model.comple(...)
中的'binary'
来提高准确性.
When doing a classification problem, accuracy can be made relevant by setting class_mode
to 'categorical'
or 'binary'
in model.comple(...)
depending on the target (network output).
这篇关于使用Keras递归神经网络进行预测-精度始终为1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!