DL 6th Exp Program
DL 6th Exp 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"]),
]
# 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')
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'])
Output: