import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, Dense, Dropout, RepeatVector, TimeDistributed
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping
# Sample data placeholders (replace with actual data loading)
X_train = tf.random.uniform((1000, 100, 300)) # 1000 samples, 100 timesteps, 300 features
Y_train = tf.random.uniform((1000, 20, 300)) # summaries
X_val = tf.random.uniform((200, 100, 300))
Y_val = tf.random.uniform((200, 20, 300))
# Model with dropout added
inputs = Input(shape=(100, 300))
lstm1 = LSTM(256)(inputs)
drop1 = Dropout(0.3)(lstm1)
repeat = RepeatVector(20)(drop1)
lstm2 = LSTM(256, return_sequences=True)(repeat)
drop2 = Dropout(0.3)(lstm2)
outputs = TimeDistributed(Dense(300, activation='softmax'))(drop2)
model = Model(inputs, outputs)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005), loss='categorical_crossentropy')
# Early stopping callback
early_stop = EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
# Train model
model.fit(X_train, Y_train, epochs=20, batch_size=32, validation_data=(X_val, Y_val), callbacks=[early_stop])