Neuralnetworks Research Assignment
Neuralnetworks Research Assignment
No: 5
Sequencial Modeling-RNN
Aim: Using python program implement SimpleRNN using short and long
sequences dataset.
Data Description:
import tensorflow as tf
max_features = 10000
maxlen_short = 250
ag_news = load_dataset('ag_news')
# Separate into training data
texts = ag_news['train']['text']
labels = ag_news['train']['label']
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
import time
import pandas as pd
model = Sequential()
model.add(Embedding(vocab_size, 128,
input_length=input_length))
for i in range(num_layers):
model.add(Dropout(dropout_rate))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer, loss='binary_crossentropy',
metrics=['accuracy'])
return model
start_time = time.time()
model = build_rnn_model(input_length=input_length,
vocab_size=vocab_size,num_layers=config['num_layers'],num_units=co
nfig['num_units'],dropout_rate=config['dropout_rate'],optimizer=co
nfig['optimizer'])
print("Started Training")
print("Finished Taining")
end_time = time.time()
final_acc = history.history['val_accuracy'][-1]
final_loss = history.history['val_loss'][-1]
results_rnn_short = []
result.update(config)
results_rnn_short.append(result)
rnn_short_df = pd.DataFrame(results_rnn_short)
print(rnn_short_df)
import numpy as np
data = np.array(data)
labels = np.array(labels)
results_rnn_long = []
result.update(config)
results_rnn_long.append(result)
rnn_long_df = pd.DataFrame(results_rnn_long)
print(rnn_long_df)
Result:
Result Analysis:
The results show that for short sequences (IMDb), increasing the
number of RNN layers and units generally improves accuracy, with a
2-layer model achieving the best performance (76.18% accuracy),
while the 3-layer model with SGD struggles. For long sequences (AG
News), the models perform poorly, with accuracy consistently
around 8-10%, and a significant decrease in loss (suggesting in-
stability in training). This indicates that RNNs might not be
well-suited for handling long sequences, potentially due to van-
ishing gradient issues or the need for more sophisticated archi-
tectures like LSTMs.
Conclusion:
Thus, RNN using short and long sequences datasets were implemented
successfully, executed and verified.