Keras LSTM致密层多维输入 [英] Keras LSTM dense layer multidimensional input

查看:565
本文介绍了Keras LSTM致密层多维输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a keras LSTM to predict time series. My x_train is shaped like 3000,15,10 (Examples, Timesteps, Features), y_train like 3000,15,1 and I'm trying to build a many to many model (10 input features per sequence make 1 output / sequence).

我正在使用的代码是这样:

The code I'm using is this:

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)

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

However, I can't fit the model when using :

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)

或以这种方式格式化时:

or when formatting it this way:

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维的,不是吗?

I already tried flattening the model ( model.add(Flatten()) ) before adding the dense layer but that just gives me ValueError: Input 0 is incompatible with layer flatten_1: expected ndim >= 3, found ndim=2. This confuses me because I think my data actually is 3 dimensional, isn't it?

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

推荐答案

对于keras < 2.0:您需要使用

In case of keras < 2.0: you need to use TimeDistributed wrapper in order to apply it element-wise to a sequence.

对于keras >= 2.0:默认情况下,Dense层是逐元素应用的.

In case of keras >= 2.0: Dense layer is applied element-wise by default.

这篇关于Keras LSTM致密层多维输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆