【问题标题】:LSTM input shape for multivariate time series?多元时间序列的 LSTM 输入形状?
【发布时间】:2019-04-22 06:26:48
【问题描述】:

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

我的 x_train 形状 == (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) # (nb_recods, nb_signals, nb_patient)

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

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

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

这里是a chunk of my data first 2000 rows

【问题讨论】:

  • 您的输入数据到底是什么?你为什么要像x_train= x_train.values.reshape(27615,200,13) 这样重塑你的数据。请提供更多背景信息,最好提供一些 x_trainy_train 示例(仅 2 或 3 个)。

标签: python machine-learning neural-network data-mining lstm


【解决方案1】:

好的,我对您的代码做了一些更改。首先,我现在仍然不知道你试图重塑数据的“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)

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

【讨论】:

    【解决方案2】:

    您可以尝试以下一些修改:

    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    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多