简体   繁体   English

如何在Keras中声明多个输入LSTM模型?

[英]How to declare multiple inputs LSTM model in Keras?

I have a Keras' code of declaring LSTM. 我有一个声明LSTM的Keras代码。 But I noticed Container class has already been removed in the latest version. 但是我注意到最新版本中已经删除了Container类。 https://keras.io/layers/containers/ https://keras.io/layers/containers/

How do I declare multiple inputs for LSTM in the latest format? 如何以最新格式声明LSTM的多个输入? I want to concatenate all inputs for the LSTM inputs. 我想连接LSTM输入的所有输入。

Although I noticed a similar post, what I want to do is declaration of the model. 虽然我注意到了类似的帖子,但我想要做的是声明模型。 How to work with multiple inputs for LSTM in Keras? 如何在Keras中使用LSTM的多个输入?

  • keras 1.2.2 keras 1.2.2
  • Python 3.5.2 (Anaconda 4.1.1, 64bit) Python 3.5.2(Anaconda 4.1.1,64bit)

``` ```

g = Graph()
g.add_input(
    name='i1',
    input_shape=(None, i1_size)
)
g.add_input(
    name='i2',
    input_shape=(None, i2_size)
)
g.add_node(
    LSTM(
        n_hidden,
        return_sequences=True,
        activation='tanh'
    ),
    name='h1',
    inputs=[
        'i1',
        'i2'
    ]
)

``` ```

Oh, May I just set input_shape as (i1_size+i2_size) like below? 哦,我可以将input_shape设置为(i1_size + i2_size),如下所示吗?

model = Sequential()
model.add(LSTM(n_hidden, input_shape=(None, i1_size+i2_size), activation='tanh', return_sequences=True))

You asked: 您询问:

Oh, May I just set input_shape as (i1_size+i2_size) like below? 哦,我可以将input_shape设置为(i1_size + i2_size),如下所示吗?

 model = Sequential() model.add(LSTM(n_hidden, input_shape=(None, i1_size+i2_size), activation='tanh', return_sequences=True)) 

Yes, Jef. 是的,杰夫。 Just keep in mind that your None in (None, i1_size+i2_size) is the number of RNN time steps/input_length and there are caveats to when you can skip defining it. 请记住,你的无(无,i1_size + i2_size)是RNN时间步数/ input_length的数量,当你可以跳过定义它时有一些警告。 Please see the description for input_length at https://keras.io/layers/recurrent/ for details. 有关详细信息,请参阅https://keras.io/layers/recurrent/上的 input_length说明。

And just FYI input_shape=(None, i1_size+i2_size) can also be written as input_dim=i1_size+i2_size (assuming you don't include input_length ). 只有FYI input_shape=(None, i1_size+i2_size)也可以写成input_dim=i1_size+i2_size (假设你不包含input_length )。

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

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