Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Natural Language Processing using Python/RNN for Sequence Generation.ipynb
3074 views
Kernel: Python 3 (ipykernel)

image.png

A Simple Recurrent Neural Network (RNN) is a fundamental type of sequence model designed for handling time series and sequential data. It has a looping mechanism that allows information to persist across different time steps, making it useful for tasks like sequence prediction, language modeling, and time-series forecasting.

  1. Understanding Simple RNN Architecture A Simple RNN consists of:

  • Input Layer: Takes in sequential data where each input has timesteps and features.

  • Hidden Layer(s) (Recurrent Layer): Uses activation functions (like ReLU or tanh) to process sequences.

  • Each hidden unit receives input from both: %7B91351614-AD37-4AB7-86A1-A3E736FBF11B%7D.png

f(x) =max(0,x)

x[1,2,3,4] y=[1,0,1,0]

Seq() layer1.(nodes=4,hidden=8,) layer2.(acivation function ="relu")

layer.ouput(sigmoid =binary classification, softmax = multiclassification, regression="tanh")

epoch (batch=2,epoch=20,accuracy="accuracy")

RNN to generate a Alphabet sequence

import numpy as np import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import SimpleRNN, Dense, Embedding from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.preprocessing.text import Tokenizer # Generate alphabetical sequence data sequences = ["abcde", "bcdef", "cdefg", "defgh", "efghi", "fghij"] tokenizer = Tokenizer(char_level=True) tokenizer.fit_on_texts(sequences) encoded_sequences = tokenizer.texts_to_sequences(sequences)
encoded_sequences
[[9, 7, 5, 3, 1], [7, 5, 3, 1, 2], [5, 3, 1, 2, 4], [3, 1, 2, 4, 6], [1, 2, 4, 6, 8], [2, 4, 6, 8, 10]]
# Prepare input-output pairs for training X = [seq[:-1] for seq in encoded_sequences] # Input sequence y = [seq[1:] for seq in encoded_sequences] # Shifted output sequence # Padding sequences to equal length max_length = max(len(seq) for seq in X) X = pad_sequences(X, maxlen=max_length, padding='pre') y = pad_sequences(y, maxlen=max_length, padding='pre')
X
array([[9, 7, 5, 3], [7, 5, 3, 1], [5, 3, 1, 2], [3, 1, 2, 4], [1, 2, 4, 6], [2, 4, 6, 8]])
y
array([[ 7, 5, 3, 1], [ 5, 3, 1, 2], [ 3, 1, 2, 4], [ 1, 2, 4, 6], [ 2, 4, 6, 8], [ 4, 6, 8, 10]])
# Convert y to categorical vocab_size = len(tokenizer.word_index) + 1 y = tf.keras.utils.to_categorical(y, num_classes=vocab_size) y
array([[[0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], [[0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]], [[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]], [[0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]], [[0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]])
# Define Simple RNN model for alphabetical sequence generation def build_rnn_model(): model = Sequential() model.add(Embedding(input_dim=vocab_size, output_dim=8, input_length=max_length)) model.add(SimpleRNN(64, activation='relu', return_sequences=True)) model.add(Dense(vocab_size, activation='softmax')) # Output layer for sequence prediction model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return model # Train and evaluate RNN model def train_and_evaluate(): model = build_rnn_model() model.fit(X, y, epochs=100, batch_size=1, verbose=1) loss, acc = model.evaluate(X, y, verbose=0) print(f"Simple RNN Model Accuracy: {acc:.4f}") # Run Simple RNN model print("\nTraining Simple RNN model for alphabetical sequence generation...") train_and_evaluate()
Training Simple RNN model for alphabetical sequence generation... Epoch 1/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 6s 12ms/step - accuracy: 0.1804 - loss: 2.3938 Epoch 2/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.5196 - loss: 2.3742 Epoch 3/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.5577 - loss: 2.3524 Epoch 4/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 12ms/step - accuracy: 0.4137 - loss: 2.3423 Epoch 5/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.4732 - loss: 2.3138 Epoch 6/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.4213 - loss: 2.2856 Epoch 7/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.5923 - loss: 2.2179 Epoch 8/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.5394 - loss: 2.1619 Epoch 9/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.5565 - loss: 2.1071 Epoch 10/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.4554 - loss: 2.0548 Epoch 11/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.5530 - loss: 1.9071 Epoch 12/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.7208 - loss: 1.7851 Epoch 13/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.7571 - loss: 1.5884 Epoch 14/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.7958 - loss: 1.5078 Epoch 15/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.8232 - loss: 1.3052 Epoch 16/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.7006 - loss: 1.2477 Epoch 17/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.6968 - loss: 1.1969 Epoch 18/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.8343 - loss: 1.0045 Epoch 19/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.8673 - loss: 0.8923 Epoch 20/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.8435 - loss: 0.8944 Epoch 21/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.8435 - loss: 0.8104 Epoch 22/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.7732 - loss: 0.9113 Epoch 23/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.8565 - loss: 0.7029 Epoch 24/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.8429 - loss: 0.6171 Epoch 25/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.7690 - loss: 0.7177 Epoch 26/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.8655 - loss: 0.6304 Epoch 27/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.8863 - loss: 0.6077 Epoch 28/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.7732 - loss: 0.6745 Epoch 29/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.8827 - loss: 0.5007 Epoch 30/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.9065 - loss: 0.6600 Epoch 31/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9720 - loss: 0.5021 Epoch 32/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.9596 - loss: 0.4589 Epoch 33/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9065 - loss: 0.5453 Epoch 34/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9601 - loss: 0.4455 Epoch 35/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9065 - loss: 0.5060 Epoch 36/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9881 - loss: 0.3144 Epoch 37/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9810 - loss: 0.3450 Epoch 38/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9720 - loss: 0.3519 Epoch 39/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.9881 - loss: 0.3003 Epoch 40/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9881 - loss: 0.2758 Epoch 41/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9720 - loss: 0.3005 Epoch 42/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9148 - loss: 0.3769 Epoch 43/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9720 - loss: 0.2876 Epoch 44/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9881 - loss: 0.2474 Epoch 45/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9810 - loss: 0.2800 Epoch 46/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9148 - loss: 0.3165 Epoch 47/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9750 - loss: 0.2356 Epoch 48/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9881 - loss: 0.2102 Epoch 49/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2151 Epoch 50/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2530 Epoch 51/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2261 Epoch 52/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2521 Epoch 53/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2448 Epoch 54/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1818 Epoch 55/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.1702 Epoch 56/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.2016 Epoch 57/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1421 Epoch 58/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1875 Epoch 59/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1944 Epoch 60/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 1.0000 - loss: 0.1447 Epoch 61/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.2030 Epoch 62/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1969 Epoch 63/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1492 Epoch 64/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1283 Epoch 65/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1451 Epoch 66/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1898 Epoch 67/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1069 Epoch 68/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1336 Epoch 69/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.11828 Epoch 70/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.1376 Epoch 71/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.1213 Epoch 72/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1351 Epoch 73/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0970 Epoch 74/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.1088 Epoch 75/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0934 Epoch 76/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0916 Epoch 77/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0993 Epoch 78/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0940 Epoch 79/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0908 Epoch 80/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0820 Epoch 81/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.1117 Epoch 82/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0812 Epoch 83/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0717 Epoch 84/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0740 Epoch 85/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 1.0000 - loss: 0.0864 Epoch 86/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0844 Epoch 87/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0559 Epoch 88/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0628 Epoch 89/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0648 Epoch 90/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0671 Epoch 91/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0603 Epoch 92/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0506 Epoch 93/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0501 Epoch 94/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0572 Epoch 95/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0424 Epoch 96/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0563 Epoch 97/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0514 Epoch 98/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0433 Epoch 99/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0392 Epoch 100/100 6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0521 WARNING:tensorflow:6 out of the last 37 calls to <function TensorFlowTrainer.make_test_function.<locals>.one_step_on_iterator at 0x0000024CD8355940> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. Simple RNN Model Accuracy: 1.0000
import numpy as np import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import SimpleRNN, Dense, Embedding from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.preprocessing.text import Tokenizer # Generate alphabetical sequence data sequences = ["abc", "bcd", "cde", "def", "efg", "fgh", "ghi", "hij"] tokenizer = Tokenizer(char_level=True) tokenizer.fit_on_texts(sequences) vocab_size = len(tokenizer.word_index) + 1 encoded_sequences = tokenizer.texts_to_sequences(sequences) # Prepare input-output pairs for training X = [seq[:-1] for seq in encoded_sequences] # Input sequence y = [seq[-1] for seq in encoded_sequences] # Next character as output # Padding sequences to equal length max_length = max(len(seq) for seq in X) X = pad_sequences(X, maxlen=max_length, padding='pre') y = np.array(y) # Define Simple RNN model for next alphabet prediction def build_rnn_model(): model = Sequential() model.add(Embedding(input_dim=vocab_size, output_dim=8, input_length=max_length)) model.add(SimpleRNN(64, activation='relu')) model.add(Dense(vocab_size, activation='softmax')) # Output layer for next character prediction model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) return model # Train and evaluate RNN model def train_and_evaluate(): model = build_rnn_model() model.fit(X, y, epochs=100, batch_size=1, verbose=1) loss, acc = model.evaluate(X, y, verbose=0) print(f"Simple RNN Model Accuracy: {acc:.4f}") return model # Function to predict next alphabet def predict_next_char(model, input_seq): input_seq_encoded = tokenizer.texts_to_sequences([input_seq]) input_seq_padded = pad_sequences(input_seq_encoded, maxlen=max_length, padding='pre') predicted_index = np.argmax(model.predict(input_seq_padded), axis=-1) for char, index in tokenizer.word_index.items(): if index == predicted_index: return char return "?" # Run Simple RNN model print("\nTraining Simple RNN model for next alphabet prediction...") trained_model = train_and_evaluate() # Test the model on new inputs new_inputs = ["cde", "def", "efg"] for inp in new_inputs: predicted_char = predict_next_char(trained_model, inp) print(f"Input: {inp} -> Predicted Next Alphabet: {predicted_char}")
Training Simple RNN model for next alphabet prediction... Epoch 1/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 5s 8ms/step - accuracy: 0.2048 - loss: 2.3977 Epoch 2/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.2352 - loss: 2.3856 Epoch 3/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.4598 - loss: 2.3710 Epoch 4/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.5349 - loss: 2.3563 Epoch 5/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.8498 - loss: 2.3283 Epoch 6/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 2.3114 Epoch 7/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 2.2855 Epoch 8/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 2.2607 Epoch 9/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 2.2158 Epoch 10/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 2.1879 Epoch 11/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 2.1030 Epoch 12/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 2.0220 Epoch 13/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 1.9924 Epoch 14/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 1.8852 Epoch 15/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 1.7671 Epoch 16/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 1.6771 Epoch 17/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 1.5378 Epoch 18/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 1.4446 Epoch 19/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 1.2798 Epoch 20/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 1.2361 Epoch 21/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 1.1699 Epoch 22/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.9608 Epoch 23/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.8718 Epoch 24/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.6729 Epoch 25/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.6388 Epoch 26/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.4811 Epoch 27/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.4397 Epoch 28/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.4422 Epoch 29/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.3586 Epoch 30/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2844 Epoch 31/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.2523 Epoch 32/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.2278 Epoch 33/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.1842 Epoch 34/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1647 Epoch 35/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.1200 Epoch 36/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1183 Epoch 37/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.1085 Epoch 38/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.1000 Epoch 39/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0815 Epoch 40/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0755 Epoch 41/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0667 Epoch 42/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0625 Epoch 43/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0532 Epoch 44/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0534 Epoch 45/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0473 Epoch 46/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0458 Epoch 47/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0437 Epoch 48/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0344 Epoch 49/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0336 Epoch 50/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0323 Epoch 51/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0277 Epoch 52/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0255 Epoch 53/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0260 Epoch 54/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0241 Epoch 55/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0233 Epoch 56/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0237 Epoch 57/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0199 Epoch 58/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0187 Epoch 59/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0208 Epoch 60/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0184 Epoch 61/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0158 Epoch 62/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0166 Epoch 63/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0157 Epoch 64/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0147 Epoch 65/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0137 Epoch 66/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0131 Epoch 67/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0138 Epoch 68/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0120 Epoch 69/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.01159 Epoch 70/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0115 Epoch 71/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0115 Epoch 72/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0108 Epoch 73/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0101 Epoch 74/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0097 Epoch 75/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0089 Epoch 76/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0094 Epoch 77/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0090 Epoch 78/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0096 Epoch 79/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0084 Epoch 80/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 1.0000 - loss: 0.0082 Epoch 81/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0080 Epoch 82/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0068 Epoch 83/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0072 Epoch 84/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0067 Epoch 85/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0069 Epoch 86/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0067 Epoch 87/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0067 Epoch 88/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0063 Epoch 89/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0064 Epoch 90/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0057 Epoch 91/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0059 Epoch 92/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0055 Epoch 93/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0059 Epoch 94/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0052 Epoch 95/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 1.0000 - loss: 0.0052 Epoch 96/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0053 Epoch 97/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0047 Epoch 98/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0047 Epoch 99/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0047 Epoch 100/100 8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 1.0000 - loss: 0.0046 Simple RNN Model Accuracy: 1.0000 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 445ms/step Input: cde -> Predicted Next Alphabet: f 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 68ms/step Input: def -> Predicted Next Alphabet: g 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 83ms/step Input: efg -> Predicted Next Alphabet: h

3. Limitations of Simple RNN

  • While Simple RNNs are useful, they suffer from:

  • Vanishing Gradient Problem: As sequences become long, earlier time step information gets lost due to repeated multiplication of small gradients.

  • Limited Memory: Struggles to capture long-term dependencies in sequences.

  • To overcome these issues, LSTMs (Long Short-Term Memory) and GRUs (Gated Recurrent Units) were introduced, which use gating mechanisms to retain long-term dependencies more effectively.

What is LSTM (Long Short-Term Memory)?

Long Short-Term Memory (LSTM) is a type of recurrent neural network (RNN) specifically designed to handle long-term dependencies in sequence data. It was introduced to overcome the vanishing gradient problem, which makes standard RNNs struggle to retain information over long sequences.

  • LSTM is a powerful improvement over simple RNNs.

  • It effectively learns long-term dependencies and avoids vanishing gradients.

  • Works well for sequence generation, NLP, speech processing, and time-series forecasting.