Overfitting Issue

16 views
Skip to first unread message

Muhammad Noman Shafiq

unread,
Apr 9, 2024, 3:05:39 PMApr 9
to Keras-users
I am facing overfitting in this code i tried everything increase dataset of question answers in Urdu , use every normalization , use dropout but nothing works for me please some please help me

import tensorflow as tf
from tensorflow.keras.layers import Input, Embedding, LSTM, Dense, Concatenate, Activation, dot
from tensorflow.keras.models import Model

# Set random seed for NumPy
np.random.seed(42)

# Set random seed for TensorFlow
tf.random.set_seed(42)

# Encoder
encoder_inputs = Input(shape=(maxlen_questions,))
encoder_embedding = Embedding(VOCAB_SIZE, 300, mask_zero=True, weights=[embedding_matrix], trainable=False)(encoder_inputs)
encoder_lstm = LSTM(50, return_sequences=True, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding)

encoder_states = [state_h, state_c]

# Decoder
decoder_inputs = Input(shape=(maxlen_answers,))
decoder_embedding = Embedding(VOCAB_SIZE, 300, mask_zero=True, weights=[embedding_matrix], trainable=False)(decoder_inputs)
decoder_lstm = LSTM(50, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states)

# Dot product Attention
attention_layer = dot([decoder_outputs, encoder_outputs], axes=[2, 2])
attention_layer = Activation('softmax', name='attention_scores')(attention_layer)
attention_out = dot([attention_layer, encoder_outputs], axes=[2,1])

decoder_combined_context = Concatenate(axis=-1)([attention_out, decoder_outputs])

# Output layer
decoder_dense = Dense(VOCAB_SIZE, activation='softmax')
output = decoder_dense(decoder_combined_context)

# Model
model = Model([encoder_inputs, decoder_inputs], output)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

history = model.fit([encoder_input_data , decoder_input_data], decoder_output_data, batch_size=32, epochs=100,
                    validation_split=0.2).history



Reply all
Reply to author
Forward
0 new messages