如何重塑RNN / LSTM模型的数据集?

时间:2018-05-17 20:37:20

标签: python-3.x keras deep-learning classification rnn

我正在尝试为二进制分类0或1

构建RNN / LSTM模型

我的数据集样本(患者编号,磨时/秒的时间,X Y和Z的归一化,峰度,偏度,俯仰,滚动和偏航,标签)。

1,15,-0.248010047716,0.00378335508419,-0.0152548459993,-86.3738760481,0.872322164158,-3.51314800063,0

1,31,-0.248010047716,0.00378335508419,-0.0152548459993,-86.3738760481,0.872322164158,-3.51314800063,0

1,46,-0.267422664673,0.0051143782875,-0.0191247001961,-85.7662354031,1.0928406847,-4.08015176908,0

1,62,-0.267422664673,0.0051143782875,-0.0191247001961,-85.7662354031,1.0928406847,-4.08015176908,0 

这是我的代码

import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Bidirectional
from keras.preprocessing import sequence
# fix random seed for reproducibility
np.random.seed(7)

train = np.loadtxt("featwithsignalsTRAIN.txt", delimiter=",")
test = np.loadtxt("featwithsignalsTEST.txt", delimiter=",")

x_train = train[:,[2,3,4,5,6,7]]
x_test = test[:,[2,3,4,5,6,7]]
y_train = train[:,8]
y_test = test[:,8]

# create the model
model = Sequential()
model.add(LSTM(20, dropout=0.2, input_dim=6))
model.add(Dense(4, activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs = 2)

我正在尝试重塑数据集,以便能够将其提供给RNN / LSTM模型。

因为它给了我以下错误

  

检查输入时出错:预期lstm_1_input有3个维度,   但得到了阵形(1415684,6)

谁能帮助我吗? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要的是TimeseriesGenerator,它将您的数据转换为固定窗口大小的序列。目前您正在传递整个数据集(...,6),如果您使用滑动窗口,请说大小为10(根据您的数据,这可能不一定对应于10毫秒),生成器将提供形状输入(......,10,6)这是LSTM所期望的。 LSTM将处理10个时间步,即该窗口中的数据点,模型将进行预测。