tensorflow.Keras.LSTM 系列预测以向下的方式重复

问题描述

你好,

我一直在开发一个程序,该程序可以预测金融时间序列的移动平均线。 我目前遇到的问题是,重复向下系列。我已经寻找过类似的问题,但过了一段时间我开始放弃。我的情节是这样的:

Plot of the prediction repeating

首先我会告诉你我认为问题可能出在哪里。 我写了一个函数,它接受一个数组(又名系列),并按百分比缩放(1 是起始数字。[1,0.9,1.5,...] x>1 是胜利,x

class Prepare_Data:
# This Class is for preparing the Data into an acceptable range for the KI to learn
def __init__(self):
    pass            # requires no input

def get_data_percentage(x_data,y_data=None):
    # The first fuction is to scale the data into a percentage. 1 is no loss
    # x>1 is win,x<1 is loss
    # The function can take two data,since the first one is a series and the second the output
    start_data = x_data[0]
    new_y_data = []
    new_x_data = []
    try:
        new_y_data.append(y_data / start_data)
    except:
        pass
    for data_point in x_data:
        new_x_data.append(data_point/start_data)
    return np.array(new_x_data),np.array(new_y_data),start_data

函数还应该对 y_train 进行缩放,并且应该使用标量来取回原始数据。 然后我加载训练数据并使用函数对其进行缩放:

  X,Y = tool.Sequences(data=MA,steps=30)

# Prepare_Data.get_data_percentage only return the percentage
# The input is two dimensional and function want one input
scalars = []
x_train,y_train = [],[]
for i in range(0,len(X)):
    temp_X,temp_Y,scalar = Prepare_Data.get_data_percentage(X[i],Y[i])
    x_train.append(temp_X)
    y_train.append(temp_Y)
    scalars.append(scalar)
x_train,y_train = np.array(x_train),np.array(y_train)

我的模特在这里

def train(x_train,y_train,epochs=4):

model = Sequential()
model.add(LSTM(180,activation="relu",return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(180,activation="sigmoid"))
model.add(Dropout(0.2))
model.add(Dense(1,activation="sigmoid"))
model.compile(optimizer=Adam(learning_rate=0.0001),loss="mean_squared_error",metrics=["mae"])

history = model.fit(x_train,epochs=epochs,verbose=2,validation_split=0.2)

plt.plot(history.history["mae"],label="accuracy")
plt.plot(history.history["loss"],label="loss")
plt.legend()
plt.show()


model.save("second_model")

我对模型进行了一些修改,可能看起来很奇怪,但我认为问题不在模型中

Epoch 4/4 126/126 - 4s - loss: 4.0923e-05 - mae: 0.0044 - val_loss: 4.3546e-05 - val_mae: 0.0048

训练后我训练数据并将其添加到数组中。我相信我将它添加到数组中的方式是主要问题,因为我使用上面的函数来缩小它左右。但我不知道为什么会这样

n_steps = 30
x_test = np.linspace(0,len(MA),len(MA))

y_test,test_scalar = MA[:n_steps],[]
y_test = np.array(y_test)
ground_truth = MA
plt.plot(x_test[:n_steps],y_test,label="Beginning")

model = load_model("second_model")

for i in range(0,len(x_test)-n_steps):
    net_input,nothing,temp_scalar = Prepare_Data.get_data_percentage(y_test[i:i + n_steps])
    net_input = net_input.reshape((1,n_steps,1))
    y = model.predict(net_input,verbose=0)
    # print(y)
    y_test = np.append(y_test,y*temp_scalar)

plt.plot(x_test,ground_truth,label="ground truth")
plt.plot(x_test,label="prediction")

plt.legend()
plt.show()
plt.savefig("plot")

如果我错了,请纠正我:

  1. 输入应该是 x_train,y_train = [[数据系列],..],[输出] len(x_train) == len(y_train)
  2. 我将 x_train、y_train 拆分为:data[i:i+steps]、data[i+steps]

我已经尝试过处理模型和更大的批量。我认为使用更大的序列并不能解决这个问题。如果你能找到我还没有看到的东西,那就太好了。 对不起,如果我的代码不清楚。我是自学的,不知道好的代码的标准是什么。提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)