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

LSTM input shape for multivariate time series?

我知道这个问题被问过很多次,但我真的无法为我的案例解决这个输入形状问题。

My x_train shape == (5523000, 13) // (13 timeseries of length 5523000)

My y_train shape == (5523000, 1)

number of classes == 2

重塑 x_train 和 y_train:

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: Input 0 is incompatible with layer lstm_10: expected ndim=3, found ndim=4

我的问题是我在这里缺少什么?

PS: The idea of the project is that I have 13 signals coming from the 13 points of the human body, I want to use them to detect a certain type of diseases (an arousal). By using the LSTM, I want my model to locate the regions where I have that arousal based on these 13 signals.

The whole data is 993 patients, for each one I use 13 signals to detect the disorder regions.

如果您希望我将数据放入 3D 维度:

(500000 ,13, 993) # (nb_recods, nb_signals, nb_patient)

for each patient I have 500000 observations of 13 signals. nb_patient is 993

值得注意的是,500000 大小并不重要!因为我可以让患者进行更多或更少的观察。

更新:这是一位患者的样本数据。

这里是a chunk of my data first 2000 rows

您可以尝试如下修改:

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_train(这是 x_train[0] = 27615 的含义)和包含 5523000 个值的目标集 y_train。

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

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_sample:

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 https://keras.io/utils/)

y_train = np.array(target)
y_train = to_categorical(y_train, 2)

现在你有两个类别,我假设你提供的数据中有两个类别,所有目标值都是 0,所以我不知道你的目标可以取多少个可能的值。如果您的目标可以取 4 个可能的值,那么 to_categorical 函数中的类别数将为 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