【问题标题】:LSTM/RNN Predicting cosine from sineLSTM/RNN 从正弦预测余弦
【发布时间】:2023-03-28 18:24:01
【问题描述】:

我正在尝试编写一个简单的 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)

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

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

我想知道可能出了什么问题。

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

【问题讨论】:

    标签: python tensorflow keras lstm tflearn


    【解决方案1】:

    回答我自己的问题。这是一个增加 epoch 数量和调整批量大小的问题。例如,这里是#epochs = 200,batch size = 10 的预测。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2016-06-16
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多