[英]Keras LSTM input dimension setting with attention class
我正在嘗試使用 keras 訓練 LSTM model 但我認為我在這里出了點問題。
我有一個錯誤
TypeError: __init__() takes 2 positional arguments but 3 were given
代碼在以下鏈接中:
https://androidkt.com/text-classification-using-attention-mechanism-in-keras/
當我嘗試使用注意 class 應用代碼時,首先它告訴我注意沒有定義,然后我使用注意“在大寫字母中表示注意”,然后它給了我錯誤。
注意class如下:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Attention(tf.keras.Model):
def __init__(self, units):
super(Attention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
def call(self, features, hidden):
hidden_with_time_axis = tf.expand_dims(hidden, 1)
score = tf.nn.tanh(self.W1(features) + self.W2(hidden_with_time_axis))
attention_weights = tf.nn.softmax(self.V(score), axis=1)
context_vector = attention_weights * features
context_vector = tf.reduce_sum(context_vector, axis=1)
return context_vector, attention_weights
The rest of the code in the link I mentioned before.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
lstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM
(rnn_cell_size,
dropout=0.3,
return_sequences=True,
return_state=True,
recurrent_activation='relu',
recurrent_initializer='glorot_uniform'), name="bi_lstm_0")(embedded_sequences)
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 = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])
**context_vector, attention_weights = Attention(lstm, state_h)**
output = keras.layers.Dense(1, activation='sigmoid')(context_vector)
model = keras.Model(inputs=sequence_input, outputs=output)
# summarize layers
print(model.summary())
對不起大家,我找到了答案:
context_vector, attention_weights = Attention(32)(lstm, state_h)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.