我正在尝试为二进制分类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)
但是它给了我以下错误
检查输入时出错:预期lstm_1_input有3个维度,但是有阵列形状(1415684,6)
答案 0 :(得分:0)
LSTM图层采用3维输入,对应于(batch_size,timesteps,features)。在您的情况下,您只有一个二维输入,即(batch_size,features)。
LSTM层适用于序列格式(句子,股票价格......)。您需要重新整形数据,以便以这种方式使用它。更具体地说,您需要重塑您的数据以使每位患者拥有一条线(或者您可以选择每位患者拥有多个序列,但我们现在要求每位患者使用一条线),并且每条线都需要包含多个阵列,每个阵列对应于对患者的观察。