0% found this document useful (0 votes)
13 views3 pages

DL 6th Exp Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

DL 6th Exp Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program:

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.sequence 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:
words.update(sentence)
tags.update(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 = models.Sequential([
layers.Embedding(input_dim=input_dim, output_dim=embedding_dim,
input_length=max_len),
layers.LSTM(64, return_sequences=True),
layers.TimeDistributed(layers.Dense(output_dim, activation='softmax')),
])

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Train the model


y_train_reshaped = np.expand_dims(y_train, -1)
model.fit(X_train, y_train_reshaped, batch_size=1, epochs=100)

# Evaluate the model


loss, accuracy = model.evaluate(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 = model.predict(padded_sequence)
predicted_indices = np.argmax(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:

You might also like