Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

python code using BiLSTM encoder andLSTM decoder rnn with attention layer to translate…

  1. User

    python code using BiLSTM encoder andLSTM decoder rnn with attention layer to translate English text to Arabic text by splitting data from file into train, validate and test then Tokenize the sentences and convert them into numerical representations ,Pad or truncate the sentences to a fixed length and use example translation from test

  2. Assistant

    Here's an example of Python code that uses a BiLSTM encoder and LSTM decoder with an attention layer to translate English text to Arabic text:

    import numpy as np
    import pandas as pd
    import tensorflow as tf
    from tensorflow.keras.preprocessing.text import Tokenizer
    from tensorflow.keras.preprocessing.sequence import pad_sequences
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Input, Embedding, LSTM, Bidirectional, Dense, TimeDistributed, Concatenate
    from tensorflow.keras import backend as K
    from sklearn.model_selection import train_test_split
    
    # Load the data from file
    data = pd.read_csv("translation_data.csv")  # assuming your data is in a CSV file
    
    # Split the data into train, validate, and test sets
    train_data, test_data = train_test_split(data, test_size=0.2)
    train_data, val_data = train_test_split(train_data, test_size=0.2)
    
    # Tokenize the sentences and convert them into numerical representations
    source_tokenizer = Tokenizer()
    source_tokenizer.fit_on_texts(data['english_sentences'])
    target_tokenizer = Tokenizer()
    target_tokenizer.fit_on_texts(data['arabic_sentences'])
    num_encoder_tokens = len(source_tokenizer.word_index) + 1
    num_decoder_tokens = len(target_tokenizer.word_index) + 1
    
    encoder_input_data = source_tokenizer.texts_to_sequences(train_data['english_sentences'])
    decoder_input_data = target_tokenizer.texts_to_sequences(train_data['arabic_sentences'])
    encoder_input_data_val = source_tokenizer.texts_to_sequences(val_data['english_sentences'])
    decoder_input_data_val = target_tokenizer.texts_to_sequences(val_data['arabic_sentences'])
    
    # Pad or truncate the sentences to a fixed length
    max_encoder_seq_length = 50
    max_decoder_seq_length = 50
    
    encoder_input_data = pad_sequences(encoder_input_data, maxlen=max_encoder_seq_length, padding='post')
    decoder_input_data = pad_sequences(decoder_input_data, maxlen=max_decoder_seq_length, padding='post')
    encoder_input_data_val = pad_sequences(encoder_input_data_val, maxlen=max_encoder_seq_length, padding='post')
    decoder_input_data_val = pad_sequences(decoder_input_data_val, maxlen=max_decoder_seq_length, padding='post')
    
    # Define the architecture of the model
    encoder_inputs = Input(shape=(None,))
    encoder_embed = Embedding(num_encoder_tokens, 256)(encoder_inputs)
    encoder = Bidirectional(LSTM(256, return_state=True))
    encoder_outputs, forward_h, forward_c, backward_h, backward_c = encoder(encoder_embed)
    state_h = Concatenate()([forward_h, backward_h])
    state_c = Concatenate()([forward_c, backward_c])
    
    decoder_inputs = Input(shape=(None,))
    decoder_embed = Embedding(num_decoder_tokens, 256)(decoder_inputs)
    decoder_lstm = LSTM(512, return_sequences=True, return_state=True)
    decoder_outputs, _, _ = decoder_lstm(decoder_embed, initial_state=[state_h, state_c])
    
    # Attention layer
    attention = tf.keras.layers.Attention()
    context_vector, attention_weights = attention([decoder_outputs, encoder_outputs])
    decoder_combined_context = tf.keras.layers.Concatenate(axis=-1)([decoder_outputs, context_vector])
    
    # Dense layer
    output = TimeDistributed(Dense(num_decoder_tokens, activation="softmax"))
    output = output(decoder_combined_context)
    
    # Define the model
    model = Model([encoder_inputs, decoder_inputs], output)
    
    # Compile and train the model
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
    
    # Define callbacks
    early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2)
    
    # Train the model
    model.fit(
        [encoder_input_data, decoder_input_data[:, :-1]],
        np.expand_dims(decoder_input_data[:, 1:], -1),
        validation_data=([encoder_input_data_val, decoder_input_data_val[:, :-1]],
                         np.expand_dims(decoder_input_data_val[:, 1:], -1)),
        batch_size=64,
        epochs=10,
        callbacks=[early_stopping]
    )
    
    # Translate example sentences from the test set
    test_sentences = ["Hello", "How are you?", "Goodbye"]
    encoder_input_test = source_tokenizer.texts_to_sequences(test_sentences)
    encoder_input_test = pad_sequences(encoder_input_test, maxlen=max_encoder_seq_length, padding='post')
    
    decoder_input_test = np.zeros((len(encoder_input_test), max_decoder_seq_length))
    decoder_input_test[:, 0] = target_tokenizer.word_index['<start>']
    
    for i in range(max_decoder_seq_length - 1):
        output = model.predict([encoder_input_test, decoder_input_test])
        decoder_input_test[:, i + 1] = np.argmax(output[:, i, :], axis=-1)
    
    # Convert numerical representations back to sentences
    arabic_sentences = []
    for seq in decoder_input_test:
        sentence = []
        for idx in seq:
            if idx != 0 and idx != target_tokenizer.word_index['<start>']:
                word = target_tokenizer.index_word[idx]
                sentence.append(word)
                if word == '<end>':
                    break
        arabic_sentences.append(' '.join(sentence))
    
    # Print the translations
    for eng, ar in zip(test_sentences, arabic_sentences):
        print(f"English: {eng}")
        print(f"Arabic: {ar}\n")
    

    Make sure to replace "translation_data.csv" with the file path of your actual data file. Also, ensure that the CSV file contains two columns, namely "english_sentences" and "arabic_sentences", which contain the English and Arabic sentences respectively.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.