【问题标题】:Keras LSTM dense layer multidimensional inputKeras LSTM 密集层多维输入
【发布时间】:2018-02-22 17:22:08
【问题描述】:

我正在尝试创建一个 keras LSTM 来预测时间序列。我的 x_train 的形状像 3000,15,10(示例,时间步长,特征),y_train 像 3000,15,1,我正在尝试构建多对多模型(每个序列 10 个输入特征产生 1 个输出/序列)。

我使用的代码是这样的:

model = Sequential()

model.add(LSTM(
    10,
    input_shape=(15, 10),
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    100,
    return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(
        X_train, y_train,
        batch_size=512, nb_epoch=1, validation_split=0.05)

但是,我在使用时无法拟合模型:

model.add(Dense(1, activation='linear'))
>> Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (3000, 15, 1)

或以这种方式格式化时:

model.add(Dense(1))
model.add(Activation("linear"))
>> Error when checking model target: expected activation_1 to have 2 dimensions, but got array with shape (3000, 15, 1)

在添加密集层之前,我已经尝试过压平模型(model.add(Flatten())),但这只是给了我ValueError: Input 0 is incompatible with layer flatten_1: expected ndim >= 3, found ndim=2。这让我很困惑,因为我认为我的数据实际上是 3 维的,不是吗?

代码来源于https://github.com/Vict0rSch/deep_learning/tree/master/keras/recurrent

【问题讨论】:

  • 既然是多对多,为什么要设置return_sequences=False?尝试在第二个LSTM 中将其设置为True
  • 嗨,Marcin,我将其更改为 True,但仍然出现相同的错误。
  • 你能更新你的代码 sn-p 吗?您使用哪个版本的keras?您是否 100% 确定这是同一条消息?
  • 更新了 sn-p,我正在使用 1.2.2(使用 Python 2.7.5)。错误是Error when checking model target: expected activation_1 to have 2 dimensions, but got array with shape (3000, 15, 1),我使用的是model.add(Dense(1)) model.add(Activation("linear")) 格式。
  • 现在我明白了。您使用的是相对较旧的keras 版本。试试:model.add(TimeDistributed(Dense(1))).

标签: python multidimensional-array keras lstm


【解决方案1】:

如果是keras < 2.0:您需要使用TimeDistributed 包装器才能将其逐个元素地应用于序列。

keras >= 2.0 的情况下:Dense 默认情况下按元素应用层。

【讨论】:

    【解决方案2】:

    由于您更新了您的 keras 版本并且您的错误消息已更改,这就是在我的机器上工作的内容 (Keras 2.0.x)

    这行得通:

    model = Sequential()
    
    model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM( 100, return_sequences=True))
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='linear'))
    

    这也有效:

    model = Sequential()
    
    model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM( 100, return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(1,return_sequences=True, activation='linear'))
    

    测试:

    x = np.ones((3000,15,10))
    y = np.ones((3000,15,1))
    

    编译和训练:

    model.compile(optimizer='adam',loss='mse')
    model.fit(x,y,epochs=4,verbose=2)
    

    【讨论】:

    • 嗨丹尼尔,不幸的是,这给了我以下错误:an input and an output are associated with the same recurrent state and should have the same type but have type 'TensorType(float32, col)' and 'TensorType(float32, matrix)' respectively. 然而,即使model.add(Dense(1, activation="linear")) 编译并输出预期的形状,结果似乎是错误的 - 我只得到大约 -1 到 2 的值. 它应该输出 ~ -5 和 ~ 20 - 25 之间的值
    • 您使用的是旧版本的 keras,对吗?这个答案可能只适合 keras 2。
    • 你在这个 LSTM 之前是否使用过任何TimeDistributed? (LSTM,至少在 keras 2 中,不应该使用TimeDistributed,它已经是一个时间层)。
    • 我现在使用的是当前版本和Dense。代码就像原来的问题一样。
    • 好的....我不确定该错误的原因是什么。我从来没见过。我已经在我的答案上发布了我测试过的两个选项,并且在我的机器上可以正常工作。
    猜你喜欢
    • 2017-07-25
    • 2019-02-18
    • 2019-01-21
    • 2016-03-24
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多