首页 文章

LSTM / RNN从正弦预测余弦

提问于
浏览
1

我正在尝试编写一个简单的LSTM / RNN . 给定 sine 输入,我可以预测 cosine 信号吗?

运行我的代码时,我可以准确地预测 sine 的下一个值给定历史 sine 值;但鉴于历史 sine 值,我无法准确预测 cosine 的下一个值 .

我大量借用了following code example,用于预测字母表中的下一个字符 .

由于我使用的是LSTM / RNN,因此我定义了与输出数据点对应的序列输入数据的 windows (长度 seq_length ) .

例如,

输入序列 - >输出序列[0.,0.00314198,0.00628393,0.00942582,0.01256761] - > 1.0

在上面的示例序列中, sin(0) 为0,然后我们为接下来的4个点设置 sine 值 . 这些值具有关联的 cos(0) .

相应的代码,

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

time_points = np.linspace(0, 8*np.pi, 8000)

seq_length = 5
dataX = []
dataY = []
for i in range(0, len(time_points) - seq_length, 1):
    seq_in = np.sin(time_points)[i:i + seq_length]
    seq_out = np.cos(time_points)[i]
    dataX.append([seq_in])
    dataY.append(seq_out)

X = np.reshape(dataX, (len(dataX), seq_length, 1)) #numpy as np
y = np.reshape(dataY, (len(dataY), 1))

LSTM Keras代码

model = Sequential()
model.add(LSTM(16, input_shape=(X.shape[1], X.shape[2])))


model.add(Dense(y.shape[1], activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X[:6000], y[:6000], epochs=20, batch_size=10, verbose=2, validation_split=0.3)

下图显示了当我们尝试从顺序正弦数据中学习余弦时的预测和基本事实 .

enter image description here

但是,如果我们要使用顺序正弦数据学习正弦(即具有 seq_out = np.sin(time_points)[i] ),则预测是准确的,如下所示 .

enter image description here

我想知道会出现什么问题 .

或者,我怎样才能获得更准确的预测?

1 回答

  • 0

    回答我自己的问题 . 这是一个增加时代数量和摆弄批量大小的问题 . 例如,这里是#epochs = 200的预测,批量大小= 10 .

    enter image description here

相关问题