thushv89 / attention_keras

Keras Layer implementation of Attention for Sequential models
https://towardsdatascience.com/light-on-math-ml-attention-with-keras-dc8dbc1fad39
MIT License
443 stars 266 forks source link

ValueError: The first argument to `Layer.call` must always be passed. #47

Open SuryaVikram opened 3 years ago

SuryaVikram commented 3 years ago

Hello, thank you for sharing this.

I am getting this error when am trying to run this in Colab "ValueError: The first argument to Layer.call must always be passed."

This is my model code: from attention import AttentionLayer

from keras import backend as K K.clear_session() latent_dim = 100 embedding_dim=100

Encoder

encoder_inputs = Input(shape=(max_len_text,)) enc_emb = Embedding(x_voc_size, latent_dim,trainable=True)(encoder_inputs)

LSTM 1

encoder_lstm1 = LSTM(latent_dim,return_sequences=True,return_state=True) encoder_output1, state_h1, state_c1 = encoder_lstm1(enc_emb)

LSTM 2

encoder_lstm2 = LSTM(latent_dim,return_sequences=True,return_state=True) encoder_output2, state_h2, state_c2 = encoder_lstm2(encoder_output1)

LSTM 3

encoder_lstm3=LSTM(latent_dim, return_state=True, return_sequences=True) encoder_outputs, state_h, state_c= encoder_lstm3(encoder_output2)

Set up the decoder.

decoder_inputs = Input(shape=(None,)) dec_emb_layer = Embedding(y_voc_size, latent_dim,trainable=True) dec_emb = dec_emb_layer(decoder_inputs)

LSTM using encoder_states as initial state

decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs,decoder_fwd_state, decoder_back_state = decoder_lstm(dec_emb,initial_state=[state_h, state_c])

Attention Layer

attn_layer = AttentionLayer(name='attention_layer') attn_out, attn_states = attn_layer()([encoder_outputs, decoder_outputs])

Concat attention output and decoder LSTM output

decoder_concat_input = Concatenate(axis=-1, name='concat_layer')([decoder_outputs, attn_out])

Dense layer

decoder_dense = TimeDistributed(Dense(y_voc_size, activation='softmax')) decoder_outputs = decoder_dense(decoder_outputs)

Define the model

model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.summary()


Please advice if I am missing something, thank you

Diamondcast commented 2 years ago

attn_layer() - Should you have () here?