如何将仅一半的RNN输出馈送到张量流中的下一个RNN输出层?

时间:2019-12-02 23:59:59

标签: tensorflow neural-network recurrent-neural-network

我只想将奇数位置的RNN输出馈送到下一个RNN层。如何在张量流中实现这一目标?

我基本上想在下图中构建顶层,从而将序列大小减半。底层只是一个简单的RNN。

enter image description here

1 个答案:

答案 0 :(得分:0)

这是您需要的吗?

from tensorflow.keras import layers, models
import tensorflow.keras.backend as K

inp = layers.Input(shape=(10, 5))
out = layers.LSTM(50, return_sequences=True)(inp)
out = layers.Lambda(lambda x: tf.stack(tf.unstack(out, axis=1)[::2], axis=1))(out)
out = layers.LSTM(50)(out)
out = layers.Dense(20)(out)
m = models.Model(inputs=inp, outputs=out)
m.summary()

您将获得以下模型。您可以看到第二个LSTM仅从总共10个步骤中获得了5个时间步(即上一层的其他所有输出)

Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 10, 5)]           0         
_________________________________________________________________
lstm_2 (LSTM)                (None, 10, 50)            11200     
_________________________________________________________________
lambda_1 (Lambda)            (None, 5, 50)             0         
_________________________________________________________________
lstm_3 (LSTM)                (None, 50)                20200     
_________________________________________________________________
dense_1 (Dense)              (None, 20)                1020      
=================================================================
Total params: 32,420
Trainable params: 32,420
Non-trainable params: 0