Python 多元时间序列的LSTM输入形状?

Python 多元时间序列的LSTM输入形状?,python,machine-learning,neural-network,data-mining,lstm,Python,Machine Learning,Neural Network,Data Mining,Lstm,我知道这个问题被问了很多次,但我真的不能为我的案例解决这个输入形状问题 我的x_列车形状==(5523000,13)/(长度为5523000的13个时间序列) 我的y_火车形状==(5523000,1) 类别数==2 要重塑x_序列和y_序列,请执行以下操作: x_train= x_train.values.reshape(27615,200,13) # 5523000/200 = 27615 y_train= y_train.values.reshape((5523000,1)) # I

我知道这个问题被问了很多次,但我真的不能为我的案例解决这个输入形状问题

我的x_列车形状==(5523000,13)/(长度为5523000的13个时间序列)

我的y_火车形状==(5523000,1)

类别数==2

要重塑x_序列和y_序列,请执行以下操作:

x_train= x_train.values.reshape(27615,200,13)  # 5523000/200 = 27615
y_train= y_train.values.reshape((5523000,1))   # I know I have a problem here but I dont know how to fix it
这是我的lstm网络:

def lstm_baseline(x_train, y_train):
    batch_size=200
    model = Sequential()
    model.add(LSTM(batch_size, input_shape=(27615,200,13),
                   activation='relu', return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(128, activation='relu'))
    model.add(Dropout(0.1))

    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(1, activation='softmax'))

    model.compile(
        loss='categorical_crossentropy',
        optimizer='rmsprop',
        metrics=['accuracy'])

    model.fit(x_train,y_train, epochs= 15)

    return model
无论何时运行代码,都会出现以下错误:

ValueError:输入0与层lstm_10不兼容:应为 ndim=3,发现ndim=4

我的问题是我在这里遗漏了什么

PS:这个项目的想法是,我有13个信号来自人体的13个点,我想用它们来检测某种类型的疾病(唤醒)。通过使用LSTM,我希望我的模型能够根据这13个信号来定位我的觉醒区域

整个数据是993名患者,每个患者我使用13个信号来检测疾病区域

如果要我将数据放入三维尺寸:

(500000,13993)
#(nb#U记录,nb#U信号,nb#U患者)

对于每个患者,我对13个信号进行了500000次观察。 NBU患者是993

值得注意的是,50万的尺寸并不重要!因为我可以让患者进行更多或更少的观察

更新:以下是一名患者的样本数据


这里是

您可以尝试下面这样的修改:

x_train = x_train.reshape(1999, 1, 13)
# double-check dimensions
x_train.shape

def lstm_baseline(x_train, y_train, batch_size):
    model = Sequential()
    model.add(LSTM(batch_size, input_shape=(None, 13),
                   activation='relu', return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(128, activation='relu'))
    model.add(Dropout(0.1))

    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(1, activation='softmax'))

    model.compile(
        loss='binary_crossentropy',
        optimizer='adam',
        metrics=['accuracy'])

    return model    

好的,我对你的代码做了一些修改。首先,我现在仍然不知道你试图重塑数据时的“200”意味着什么,所以我将给你一个工作代码,让我们看看你是否可以使用它,或者你是否可以修改它使你的代码工作。输入数据和目标的大小必须匹配。不能有一个包含27615行的输入x_列(这是x_列[0]=27615的意思)和一个包含5523000个值的目标集y_列

我从您为本例提供的数据示例中选取了前两行:

x_sample = [[-17,  -7, -7,  0, -5, -18, 73, 9, -282, 28550, 67],
            [-21, -16, -7, -6, -8,  15, 60, 6, -239, 28550, 94]]

y_sample = [0, 0]
让我们重塑x_样本的形状:

x_train = np.array(example)

#Here x_train.shape = (2,11), we want to reshape it to (2,11,1) to
#fit the network's input dimension
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
您使用的是分类损失,因此您必须将目标更改为分类损失(chek)

现在您有两个类别,我假设两个类别,如您提供的数据中的所有目标值为0,因此我不知道您的目标可以接受多少可能的值。如果您的目标可以接受4个可能的值,则to_分类函数中的类别数将为4。最后一个密集层的每个输出将代表一个类别和该输出的值,以及您的输入属于该类别的概率

现在,我们只需稍微修改一下您的LSTM模型:

def lstm_baseline(x_train, y_train):
   batch_size = 200
   model = Sequential()
   #We are gonna change input shape for input_dim
   model.add(LSTM(batch_size, input_dim=1,
                  activation='relu', return_sequences=True))
   model.add(Dropout(0.2))

   model.add(LSTM(128, activation='relu'))
   model.add(Dropout(0.1))

   model.add(Dense(32, activation='relu'))
   model.add(Dropout(0.2))

   #We are gonna set the number of outputs to 2, to match with the
   #number of categories
   model.add(Dense(2, activation='softmax'))

   model.compile(
       loss='categorical_crossentropy',
       optimizer='rmsprop',
       metrics=['accuracy'])

   model.fit(x_train, y_train, epochs=15)

return model

您的输入数据到底是什么?为什么要像这样重塑数据
x\u train=x\u train.values.reformate(27615200,13)
。请提供更多的上下文,理想情况下,提供一些x_-train和y_-train示例(仅2或3个)。
def lstm_baseline(x_train, y_train):
   batch_size = 200
   model = Sequential()
   #We are gonna change input shape for input_dim
   model.add(LSTM(batch_size, input_dim=1,
                  activation='relu', return_sequences=True))
   model.add(Dropout(0.2))

   model.add(LSTM(128, activation='relu'))
   model.add(Dropout(0.1))

   model.add(Dense(32, activation='relu'))
   model.add(Dropout(0.2))

   #We are gonna set the number of outputs to 2, to match with the
   #number of categories
   model.add(Dense(2, activation='softmax'))

   model.compile(
       loss='categorical_crossentropy',
       optimizer='rmsprop',
       metrics=['accuracy'])

   model.fit(x_train, y_train, epochs=15)

return model