Certainly! Here's an updated version of the code that includes example predictions and translations from the test set.
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Bidirectional, LSTM, Embedding, RepeatVector, Dense
# Constants
EMBEDDING_DIM = 100
HIDDEN_DIM = 128
MAX_SEQUENCE_LENGTH = 100
VOCAB_SIZE = 10000
BATCH_SIZE = 32
EPOCHS = 10
# Example data
train_X = ["I love apples", "She is a student"]
train_Y = ["أحب التفاح", "هي طالبة"]
val_X = ["He drinks coffee", "They are friends"]
val_Y = ["يشرب القهوة", "هم أصدقاء"]
test_X = ["Hello world", "How are you?"]
test_Y = ["مرحبا بالعالم", "كيف حالك؟"]
# Tokenize sentences and convert to numerical representations
tokenizer_x = tf.keras.preprocessing.text.Tokenizer(num_words=VOCAB_SIZE)
tokenizer_x.fit_on_texts(train_X)
train_X = tokenizer_x.texts_to_sequences(train_X)
train_X = pad_sequences(train_X, maxlen=MAX_SEQUENCE_LENGTH)
val_X = tokenizer_x.texts_to_sequences(val_X)
val_X = pad_sequences(val_X, maxlen=MAX_SEQUENCE_LENGTH)
test_X = tokenizer_x.texts_to_sequences(test_X)
test_X = pad_sequences(test_X, maxlen=MAX_SEQUENCE_LENGTH)
tokenizer_y = tf.keras.preprocessing.text.Tokenizer(num_words=VOCAB_SIZE, oov_token='<OOV>')
tokenizer_y.fit_on_texts(train_Y)
train_Y = tokenizer_y.texts_to_sequences(train_Y)
train_Y = pad_sequences(train_Y, maxlen=MAX_SEQUENCE_LENGTH)
val_Y = tokenizer_y.texts_to_sequences(val_Y)
val_Y = pad_sequences(val_Y, maxlen=MAX_SEQUENCE_LENGTH)
test_Y = tokenizer_y.texts_to_sequences(test_Y)
test_Y = pad_sequences(test_Y, maxlen=MAX_SEQUENCE_LENGTH)
# Define the model
model = tf.keras.Sequential([
Embedding(VOCAB_SIZE, EMBEDDING_DIM, input_length=MAX_SEQUENCE_LENGTH),
Bidirectional(LSTM(HIDDEN_DIM, return_sequences=True)),
LSTM(HIDDEN_DIM),
RepeatVector(MAX_SEQUENCE_LENGTH),
Bidirectional(LSTM(HIDDEN_DIM, return_sequences=True)),
Dense(VOCAB_SIZE, activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy'])
model.summary()
# Train the model
model.fit(train_X, np.expand_dims(train_Y, axis=-1),
validation_data=(val_X, np.expand_dims(val_Y, axis=-1)),
batch_size=BATCH_SIZE, epochs=EPOCHS)
# Evaluate on test data
_, acc = model.evaluate(test_X, np.expand_dims(test_Y, axis=-1))
print("Test accuracy:", acc)
# Example Predictions and Translations
test_predictions = model.predict(test_X)
for i in range(len(test_X)):
input_sentence = ' '.join([tokenizer_x.index_word[idx] for idx in test_X[i] if idx != 0])
target_translation = ' '.join([tokenizer_y.index_word[idx] for idx in test_Y[i] if idx != 0])
predicted_translation = []
for idx in np.argmax(test_predictions[i], axis=1):
if idx == 0:
break
predicted_translation.append(tokenizer_y.index_word.get(idx, '<OOV>'))
predicted_translation = ' '.join(predicted_translation)
print('\nInput Sentence:', input_sentence)
print('Target Translation:', target_translation)
print('Predicted Translation:', predicted_translation)
In this updated code, after training and evaluating the model on the test data, it predicts and displays example translations for each sentence in the test set. It converts the token indices back to words using the index_word dictionaries from the tokenizers.
Make sure to replace the example data (train_X, train_Y, etc.) with your actual train, validation, and test data. Also, adjust the constants (EMBEDDING_DIM, HIDDEN_DIM, MAX_SEQUENCE_LENGTH, VOCAB_SIZE, etc.) according to your specific requirements.