我是深度学习和编写模型的新手,最终我想使用 lstm 和注意力机制按时间提取输入权重。我的数据是具有可变长度序列(多对一)的时间序列数据和分类问题。
我的输入形状是(None , number of features) =(None,11) 我使用了参差不齐的张量和keras框架,并且在尝试使模型工作时遇到了尺寸问题。
我不确定在构建模型之前如何查看每一层的形状,我不确定问题出在哪里。
这是我尝试使用的代码
#custom attention
class attention(Layer):
def __init__(self, return_sequences=True):
self.return_sequences = return_sequences
super(attention,self).__init__()
def build(self, input_shape):
self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
initializer="normal")
self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
initializer="zeros")
super(attention,self).build(input_shape)
def call(self, x):
e = K.tanh(K.dot(x,self.W)+self.b)
a = K.softmax(e, axis=1)
output = x*a
if self.return_sequences:
return output
return K.sum(output, axis=1)
#input
sequence_input = tf.keras.layers.Input(shape=(None,11), dtype=tf.float32,ragged=True)
rnn_cell_size =64
#define layers
lstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM
(rnn_cell_size,
dropout=0.3,
return_sequences=True,
return_state=True), name="bi_lstm_0")(sequence_input)
lstm, forward_h, forward_c, backward_h, backward_c = tf.keras.layers.Bidirectional (tf.keras.layers.LSTM
(rnn_cell_size,
dropout=0.2,
return_sequences=True,
return_state=True,
recurrent_activation='relu',
recurrent_initializer='glorot_uniform'))(lstm)
state_h = tf.keras.layers.Concatenate()([forward_h, backward_h])
state_c = tf.keras.layers.Concatenate()([forward_c, backward_c])
context_vector, attention_weights = attention(32)(lstm, state_h)
output = keras.layers.Dense(2, activation='sigmoid')(context_vector)
model = keras.Model(inputs=sequence_input, outputs=output)
错误
The mask that was passed in was [None, None, None, None, None] and cannot be applied to RaggedTensor inputs. Please make sure that there is no mask passed in by upstream layers.