Advanced Python Project Next Alphabet or Word Prediction using LSTM














































Advanced Python Project Next Alphabet or Word Prediction using LSTM




Hello, Rishabh here, this time I bring to you:

Continuing the series - 'Simple Python Project'. These are simple projects with which beginners can start with. This series will cover beginner python, intermediate and advanced python, machine learning and later deep learning.

Comments recommending other to-do python projects are supremely recommended.

Anyways, let's crack on with it!


Next Alphabet or Word Prediction using LSTM



In [20]:
# LSTM with Variable Length Input Sequences to One Character Output
import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.utils import np_utils
from keras.preprocessing.sequence import pad_sequences
In [21]:
# fix random seed for reproducibility
numpy.random.seed(7)
In [22]:
# define the raw dataset
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
In [23]:
# create mapping of characters to integers (0-25) and the reverse
char_to_int = dict((c, i) for i, c in enumerate(alphabet))
int_to_char = dict((i, c) for i, c in enumerate(alphabet))
In [24]:
# prepare the dataset of input to output pairs encoded as integers
num_inputs = 1000
max_len = 5
dataX = []
dataY = []
for i in range(num_inputs):
start = numpy.random.randint(len(alphabet)-2)
end = numpy.random.randint(start, min(start+max_len,len(alphabet)-1))
sequence_in = alphabet[start:end+1]
sequence_out = alphabet[end + 1]
dataX.append([char_to_int[char] for char in sequence_in])
dataY.append(char_to_int[sequence_out])
seq += sequence_in, '->', sequence_out
print(seq[:150])
('QR', '->', 'S', 'BCDEF', '->', 'G', 'PQRS', '->', 'T', 'BC', '->', 'D', 'TUVW', '->', 'X', 'DEFGH', '->', 'I', 'TU', '->', 'V', 'B', '->', 'C', 'VW', '->', 'X', 'MN', '->', 'O', 'CDEFG', '->', 'H', 'DEFG', '->', 'H', 'QRST', '->', 'U', 'RS', '->', 'T', 'OPQ', '->', 'R', 'KLMNO', '->', 'P', 'GHIJ', '->', 'K', 'STUV', '->', 'W', 'OPQRS', '->', 'T', 'L', '->', 'M', 'GHIJ', '->', 'K', 'JKL', '->', 'M', 'AB', '->', 'C', 'OP', '->', 'Q', 'IJ', '->', 'K', 'ABC', '->', 'D', 'HIJK', '->', 'L', 'IJKLM', '->', 'N', 'TUVWX', '->', 'Y', 'R', '->', 'S', 'X', '->', 'Y', 'IJKLM', '->', 'N', 'S', '->', 'T', 'PQ', '->', 'R', 'DEF', '->', 'G', 'KLMNO', '->', 'P', 'NOP', '->', 'Q', 'EF', '->', 'G', 'M', '->', 'N', 'KLMN', '->', 'O', 'NO', '->', 'P', 'ST', '->', 'U', 'C', '->', 'D', 'QR', '->', 'S', 'D', '->', 'E', 'KLM', '->', 'N', 'LMN', '->', 'O', 'X', '->', 'Y', 'STU', '->', 'V', 'WX', '->', 'Y')
In [25]:
# convert list of lists to array and pad sequences if needed
X = pad_sequences(dataX, maxlen=max_len, dtype='float32')
# reshape X to be [samples, time steps, features]
X = numpy.reshape(X, (X.shape[0], max_len, 1))
# normalize
X = X / float(len(alphabet))
# one hot encode the output variable
y = np_utils.to_categorical(dataY)
# create and fit the model
In [39]:
batch_size = 50
model = Sequential()
model.add(LSTM(32, input_shape=(X.shape[1], 1)))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=500, batch_size=batch_size, verbose=0)
Out[39]:
<keras.callbacks.callbacks.History at 0x1f4c3398e48>
In [40]:
# summarize performance of the model
scores = model.evaluate(X, y, verbose=0)
print("Model Accuracy: %.2f%%" % (scores[1]*100))
Model Accuracy: 88.50%
In [41]:
# demonstrate some model predictions
for i in range(20):
pattern_index = numpy.random.randint(len(dataX))
pattern = dataX[pattern_index]
x = pad_sequences([pattern], maxlen=max_len, dtype='float32')
x = numpy.reshape(x, (1, max_len, 1))
x = x / float(len(alphabet))
prediction = model.predict(x, verbose=0)
index = numpy.argmax(prediction)
result = int_to_char[index]
seq_in = [int_to_char[value] for value in pattern]
print(seq_in, "->", result)
['I'] -> J
['G', 'H', 'I', 'J', 'K'] -> L
['U', 'V', 'W', 'X'] -> Y
['T', 'U', 'V', 'W', 'X'] -> Y
['T', 'U', 'V'] -> W
['V', 'W', 'X', 'Y'] -> Z
['J', 'K'] -> L
['H', 'I', 'J', 'K'] -> L
['W', 'X'] -> Y
['K', 'L', 'M', 'N', 'O'] -> P
['C', 'D'] -> E
['M'] -> N
['V', 'W', 'X'] -> Y
['N'] -> N
['T', 'U', 'V', 'W'] -> X
['Q'] -> R
['I', 'J', 'K'] -> L
['I'] -> J
['J', 'K', 'L', 'M', 'N'] -> O
['M'] -> N
In [ ]:

---------------------------------------------

End.

Please comment below any questions or article requests.
Like the articles and Follow me to get notified when I post another article.

Cheers!



More Articles of Rishabh Karmakar:

Name Views Likes
Basic Date and Time in Python 167 1
Using Defaultdict in Python 56 1
Floor Division in Python 208 1
Python time sleep function 77 1
Immutable Objects in Python 78 1
Mutable Objects in Python 81 1
Python Escape Sequences 70 1
Escape Sequence in Python 0 0
Python Bytearray 66 1
Plotting Histogram in Python 141 1
Using Voluptuous in Python 177 1
TRAINING MACHINE LEARNING MODEL WITH TENSORFLOW CLOUD 260 1
Pandas - ModuleNotFoundError - No Module Named Pandas and Functions Explained 5533 5
OpenCV - ModuleNotFoundError - No Module Named cv2 and Algorithms Explained 344 5
OpenCV - ModuleNotFoundError - No Module Named 274 9
Simple Python Project Mario Shooter Game 389 4
Web crawling using Python and Scrapy 267 6
CNN vs ANN vs RNN 295 6
Deep Learning - Activation and Loss Function 249 6
CNN vs. RNN vs. ANN 42 10
Advanced Python Projects Predicting and Forecasting Stock Market Prices 564 6
Intermediate Python Projects Tensorflow Live Object Detection 331 7
Intermediate Python Projects Optical Character and Text Recognizer from Live Images 336 7
Advanced Python Projects Analysing Music Trends and Recommendations 469 7
Advanced Python Projects Python Chatbot using NLTK and Keras 760 7
Advanced Python Projects CIFAR-10 Image Classification using Deep Learning 554 9
Advanced Python Projects Image Caption Generator using CNN and LSTM 454 10
Advanced Python Project Breast Cancer Classification using SVR 373 10
Advanced Python Project Smiling Face Detector using CNN 445 10
Advanced Python Project Handwritten Digit Recognizer 509 11
Intermediate Python Project Speed Typing in Python 456 10
Advanced Python Project Next Alphabet or Word Prediction using LSTM 366 12
Intermediate Python Project Find most similar word using Word2Vec 288 10
Advanced Python Projects IMDB Movie Review Sentiment Analysis 261 10
Intermediate Python Project Detection of Real or Fake News 282 10
Advanced Python Project Credit Card Fraud Detection 264 10
Intermediate Python Project Snake Game Pygame 326 10
Simple Python Projects Make a multi-colored rainbow pattern in Python 400 10
Advanced Project Analysis with Sentiment Classification using Bidirectional Recurrent Neural Network 244 10
Advanced Project Forest Fire Prediction using SVR Random Forest and Deep NN 381 10
Advanced Project Sentiment and WordCloud Analysis of Online Reviews 338 12
Python - Synthetic Data Generator for Machine Learning and Artificial Intelligence 263 9
Advanced Project Deep Dream AI 271 10
Python - Symbolic regression classification generator 271 9
Generate Polynomial Functions and Random Function Generator - Python 290 10
Intermediate Project Sentiment and WordCloud Analysis of Women E-Commerce 288 10
Intermediate Project Iris Data Classification 226 10
Intermediate Project Titanic Classification using Decision Tree 248 10
Machine Learning - Decision Tree in Python 298 9
Python - All about lambda functions 251 9
Machine Learning - Polynomial Regression in Python 278 9
Machine Learning - Multiple Linear Regression in Python 260 11
Machine Learning - Simple Linear Regression in Python 245 9
Implement all 2D and 3D types plots in Python 236 9
Implement 1D, 2D and 3D CNN in Python 311 9
Simple Python Projects Multiplayer Tic-Tac-Toe 274 11
Problem - Validating Credit Card Numbers Hackerrank 280 9
Problem - Linear Algebra using Numpy Hackerrank 715 9
Problem - Basic Spell Checker Hackerrank 215 9
Implement Binary Tree in Python 277 9
Implement Graph in Python 262 9
Python for Data Science 213 9
Simple Python Projects Select Region of Interest - OpenCV 324 10
Simple Python Projects Code to mask white pixels in a coloured image - OpenCV 336 10
Simple Python Projects Code to mask white pixels in a gray scale image - OpenCV 298 10
Simple Python Projects Convert colour image to gray scale and apply cartoon effects - OpenCV 348 10
Advanced Project - Automatic Facial Recognition Based Attendance System 721 9
Simple Python Projects Live Talking Counting Clock - Pyttsx3, Tkinter 264 10
Python Simple and Fast Text to Speech 228 9
Simple Python Projects Simple Zodiac Sign Teller 233 10
Simple Python Projects Simple Fortune Teller Game 276 10
Simple Python Projects Simple Guessing Game 390 10
Implementing Multi-line Strings 202 9
Save and Load Machine Learning Models in Python with scikit-learn 401 9
Use of Ternary operator for conditional assignment. 351 9
Chaining comparison operators 216 9
Implement Circular Singly Linked List 241 9
Implementing Command Line Arguments 356 9
Solve Knapsack Problem Using Dynamic Programming 390 10

Comments