使用 Keras 递归神经网络进行预测 - 准确度始终为 1.0

Predictions using a Keras Recurrent Neural Network - accuracy is always 1.0

TLDR:如何使用 Keras RNN 预测序列中的下一个值?


我有一个顺序值列表。我想将它们输入 RNN 以 预测 序列中的下一个值。

[ 0.43589744  0.44230769  0.49358974 ...,  0.71153846  0.70833333 0.69230769]

我正在使用 Keras 来执行此操作,并且可以获得一个损失减少但准确度始终为 1.0 的网络。这是错误的。 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,但都没有成功。以下是数据的格式。

# 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).

形状的输入

我也尝试过使用 Embedding 图层,但没有成功。 (这可能是错误的使用方式 - 我只看到它用于分类而不是预测)。

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 使用这个简单的 data/architecture 以便以后可以用它来解决更复杂的问题。

谢谢:)

我发布了一个 similar question on the Keras Github 页面并得到了很好的回答。


lukedeo表示,acc: 1.0000表示真实输出和预测输出都大于0.5,反之亦然。相反,我应该查看损失或 mse,以确定模型的准确性。这是因为我的网络是回归而不是 classifier/clusterer。

均方根误差是衡量准确度的好方法。 accuracy_percent = 1 - np.sqrt(mse)


fchollet(Keras 的创建者)详细阐述说“准确度与回归问题根本无关”。

在做分类问题时,根据目标(网络输出),通过将 class_mode 设置为 'categorical'model.comple(...) 中的 'binary' 可以使准确性相关。

尝试将 RMSProp 作为优化器

model.compile(loss="mean_squared_error", optimizer = 'rmsprop')