繁体   English   中英

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

[英]LSTM input shape for multivariate time series?

我知道这个问题已经问过很多次了,但是我确实无法解决我的情况下的这种输入形状问题。

我的x_train shape ==(5523000,13)//(13个时间序列的长度5523000)

我的y_train形状==(5523000,1)

类数== 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:输入0与层lstm_10不兼容:预期ndim = 3,找到的ndim = 4

我的问题是我在这里想念的是什么?

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

整个数据为993位患者,每位患者我都使用13个信号来检测疾病区域。

如果要我将数据放入3D尺寸中:

(500000 ,13, 993) 500000,13,993 (500000 ,13, 993) #(nb_recods,nb_signals,nb_ Patient)

对于每个患者,我对13个信号有500000次观察。 nb_ Patient是993

值得注意的是,500000大小无关紧要! 因为我可以让患者得到更多或更少的观察结果。

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

这是我的数据前2000行的一大块

您可以尝试以下修改:

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”表示什么,因此,我将为您提供一个有效的代码,让我们看看是否可以使用它或可以对其进行修改以使代码正常工作。 输入数据的大小和目标必须匹配。 输入x_train的行数为27615(x_train [0] = 27615的含义),目标集y_train的值为5523000。

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

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)

您正在使用分类损失,因此必须将目标更改为分类(检查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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM