Program:
import numpy as np
import tensorflow as tf
from [Link] import layers, models
from [Link] import pad_sequences
from sklearn.model_selection import train_test_split
# Sample dataset (sentences and their corresponding POS tags)
data = [
(["I", "love", "coding"], ["PRON", "VERB", "NOUN"]),
(["Python", "is", "great"], ["PROPN", "VERB", "ADJ"]),
(["This", "is", "a", "test"], ["DET", "VERB", "DET", "NOUN"]),
(["Keras", "is", "fun"], ["PROPN", "VERB", "ADJ"]),
]
# Prepare the vocabulary
words = set()
tags = set()
for sentence, tag_sequence in data:
[Link](sentence)
[Link](tag_sequence)
word_to_index = {word: idx + 1 for idx, word in enumerate(sorted(words))}
tag_to_index = {tag: idx + 1 for idx, tag in enumerate(sorted(tags))}
# Prepare input and output sequences
X = [[word_to_index[word] for word in sentence] for sentence, _ in data]
y = [[tag_to_index[tag] for tag in tag_sequence] for _, tag_sequence in data]
# Pad sequences
max_len = max(max(len(seq) for seq in X), max(len(seq) for seq in y))
X = pad_sequences(X, maxlen=max_len, padding='post')
y = pad_sequences(y, maxlen=max_len, padding='post')
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Define the Seq2Seq model
input_dim = len(word_to_index) + 1
output_dim = len(tag_to_index) + 1
embedding_dim = 64
model = [Link]([
[Link](input_dim=input_dim, output_dim=embedding_dim,
input_length=max_len),
[Link](64, return_sequences=True),
[Link]([Link](output_dim, activation='softmax')),
])
[Link](loss='sparse_categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
# Train the model
y_train_reshaped = np.expand_dims(y_train, -1)
[Link](X_train, y_train_reshaped, batch_size=1, epochs=100)
# Evaluate the model
loss, accuracy = [Link](X_test, np.expand_dims(y_test, -1))
print(f'Test Accuracy: {accuracy:.4f}')
# Example prediction function
def predict_pos(sentence):
sequence = [word_to_index.get(word, 0) for word in sentence]
padded_sequence = pad_sequences([sequence], maxlen=max_len,
padding='post')
predictions = [Link](padded_sequence)
predicted_indices = [Link](predictions, axis=-1)[0]
return [list(tag_to_index.keys())[list(tag_to_index.values()).index(idx)] for
idx in predicted_indices if idx != 0]
# Test the prediction function
sample_sentence = ["Keras", "is", "fun"]
print(f"Sentence: '{' '.join(sample_sentence)}' - POS Tags:
{predict_pos(sample_sentence)}")
Output: