RNN的两个输入

时间:2017-05-11 04:28:22

标签: keras

如果我想将两个网络的输入放入Keras的RNN中,我该如何做到这一点?例如,假设我有两个RNN AB,其输出进入RNN C

1 个答案:

答案 0 :(得分:1)

需要使用keras.layers.merge.concatenate。请参阅以下示例:

def build_rnn(x_train, y_train, in_len):
    epochs = 100
    batch_size = 300
    hidden_units = 256

    vec_dims = 1
    in_shape = (in_len, vec_dims)

    inputs = [Input(shape=in_shape, name="input_a"), Input(shape=in_shape, name="input_b")]
    merge_outs = []

    for inp in inputs:
        # stack a few RNNs
        net = SimpleRNN(hidden_units, return_sequences=True)(inp)
        merge_outs.append(SimpleRNN(hidden_units, return_sequences=True)(net))

    merged = Concatenate(axis=-1)(merge_outs)
    merged = SimpleRNN(hidden_units, input_shape=(in_len, 2*vec_dims, ), return_sequences=False,
                       name="pre_out")(merged)
    output = Dense(vec_dims, input_shape=(vec_dims,), name='output')(merged)

    model = Model(inputs=inputs, outputs=[output])
    return model