如何在RNN中为Keras调整训练集,测试集和验证集?

时间:2019-05-05 16:08:54

标签: python keras neural-network cross-validation recurrent-neural-network

我正在处理时间序列的数据集,并通过重塑将其获取到RNN。

数据集包含40个时间步长dataset,我希望每1行包含1440列的行都被提取到RNN并接受训练,并在最后40次(1,1440)进入RNN进行学习。

X_train size: (40, 1, 1440)
X_test size: (40, 1, 1440)

问题是我想将数据集拆分为: 数据集验证集测试集,以便我可以在 timestep = t 之后进行预测并进行绘图输出。

img我已经读过@YSelf answerthe reason why,这很有用,但是很遗憾,由于数据结构的原因,我尚未成功设置。 我想提供我可以在实现脚本中进行调整的条件,以便让我说我有兴趣预测第35行之后的时间步长或直到结束的时间步长。我如何实现它以及如何展示它的情节?

我的代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from keras.layers import Dense , Activation , BatchNormalization
from keras.layers import LSTM,SimpleRNN
from keras.models import Sequential
from keras.optimizers import Adam, RMSprop

data_train = pd.read_csv("D:\Train.csv", header=None)

#select interested columns to predict 980 out of 1440 for prediction
j=0
index=[]
for i in range(1439):
    if j==2:
        j=0
        continue
    else:
        index.append(i)
        j+=1

Y_train = data_train[index]
Y_test  = data_test[index]
data_train = data_train.values
data_test  = data_test.values
X_train = data_train .reshape((data_train.shape[0], 1,data_train.shape[1]))
X_test  = data_test  .reshape((data_test.shape[0] , 1 ,data_test.shape[1]))


# create and fit the SimpleRNN model
model_RNN = Sequential()
model_RNN.add(SimpleRNN(units=1440, input_shape=(X_train.shape[1], X_train.shape[2]))) #in real data units=1440
model_RNN.add(Dense(960)) # in real data Dense(960)
model_RNN.add(BatchNormalization())
model_RNN.add(Activation('tanh'))
model_RNN.compile(loss='mean_squared_error', optimizer='adam')
hist_RNN=model_RNN.fit(X_train[:30, :], Y_train[30:, :], epochs =50, batch_size =20,validation_data=(X_train[:30, :], Y_train[30:, :]),verbose=1)

如果有人能解释问题的答案,我将不胜感激。预先感谢。

0 个答案:

没有答案