【问题标题】:keras LSTM poor predictionkeras LSTM 预测不佳
【发布时间】:2022-03-10 03:02:19
【问题描述】:

我正在尝试根据某个序列预测值(我有 5 个值,例如 1、2、3、4、5,并希望预测下一个值 - 6)。我正在为此使用 LSTM keras。

创建训练数据:

import numpy as np 
from keras.models import Sequential
from keras.layers import LSTM,Dense
a = [float(i) for i in range(1,100)]
a = np.array(a)

data_train = a[:int(len(a)*0.9)]
data_test = a[int(len(a)*0.9):]

x = 5
y = 1
z = 0

train_x = []
train_y = []
for i in data_train:
    t = data_train[z:x]
    r = data_train[x:x+y]
    if len(r) == 0:
        break
    else:
        train_x.append(t)
        train_y.append(r)
        z = z + 1
        x = x+1

train_x = np.array(train_x)
train_y = np.array(train_y)

x = 5
y = 1
z = 0

test_x = []
test_y = []
for i in data_test:
    t = data_test[z:x]
    r = data_test[x:x+y]
    if len(r) == 0:
        break
    else:
        test_x.append(t)
        test_y.append(r)
        z = z + 1
        x = x+1

test_x = np.array(test_x)
test_y = np.array(test_y)

print(train_x.shape,train_y.shape)
print(test_x.shape,test_y.shape)

将其转换成 LSTM 友好的形状:

train_x_1 = train_x.reshape(train_x.shape[0],len(train_x[0]),1)
train_y_1 = train_y.reshape(train_y.shape[0],1)
test_x_1 = test_x.reshape(test_x.shape[0],len(test_x[0]),1)
test_y_1 = test_y.reshape(test_y.shape[0],1)


print(train_x_1.shape, train_y_1.shape)
print(test_x_1.shape, test_y_1.shape)

构建和训练模型:

model = Sequential()
model.add(LSTM(32,return_sequences = False,input_shape=(trein_x_1.shape[1],1)))
model.add(Dense(1))

model.compile(loss='mse',  optimizer='adam', metrics=['accuracy'])
history = model.fit(train_x_1,
                    train_y_1,
                    epochs=20,
                    shuffle=False, 
                    batch_size=1, 
                    verbose=2, 
                    validation_data=(test_x_1,test_y_1))

但我得到了一个非常糟糕的结果,谁能解释我做错了什么。

pred = model.predict(test_x_1)
for i,a in enumerate(pred):
    print(pred[i],test_y_1[i])
[89.71895] [95.]
[89.87877] [96.]
[90.03465] [97.]
[90.18714] [98.]
[90.337006] [99.]

那么。

【问题讨论】:

  • 点评来源: 我在这里没有看到问题。请编辑您的帖子并添加一个。

标签: python tensorflow keras lstm


【解决方案1】:

您希望网络能够根据您用于训练的数据进行推断。神经网络are not good at this。您可以尝试规范化您的数据,以便不再进行推断,例如,使用相对值而不是绝对值。这当然会使这个例子变得非常琐碎。

【讨论】:

  • 谢谢。我通过 MinMaxScaler() 对数据进行了标准化。之后我得到了更好的预测,但仍然不是很好。 [94.866554] [95.] [95.829] [96.] [96.788605] [97.] [97.74528] [98.] [98.69899] [99.] 你可以看到每一个下一个预测都比上一个更糟糕。我该如何解决?
【解决方案2】:

请使用 x 的第一个推导(始终 delta x == 1 ),然后它可以工作。但这对于这个简单的公式当然没有真正的意义。尝试一些更复杂的东西,比如我的例子:

LSTM prediction of damped sin curve

来源:

https://www.kaggle.com/maciejbednarz/lstm-example

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多