简体   繁体   中英

how do I mask the input of lstm in tf.keras

I am building a hybrid model (RNN on top of CNN) and I want to mask the input, the problem is
that mask_zero is not supported by conv layers. I have tried to do masking and pass it to lstm like this:

inputs = tf.keras.layers.Input(shape=(100,))
     mask = tf.keras.layers.Masking().compute_mask(inputs) 
     embedding = tf.keras.layers.Embedding(self.preprocess["max_features"]+1, 300, input_length=100,
                 weights=[self.preprocess["matrix"]], trainable=True)(inputs) 
     lstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(200,recurrent_dropout=0.2, dropout=0.2,return_sequences=True))(embedding,mask=mask)
     conv = tf.keras.layers.Conv1D(filters=200, kernel_size=3, padding='same', activation='relu')(lstm)

the 0 ind of matrix is vector of zeros.

I am getting the follwoing error from the lstm layer: IndexError: list assignment index out of range

Have you tried checking the Docs for Tensorflow ? Go to this link I think it will help you.
In the above example, they add mask_zero=True

embedding = layers.Embedding(input_dim=5000, output_dim=16, mask_zero=True)
masked_output = embedding(padded_inputs)

print(masked_output._keras_mask)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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