Explain how LSTM is used for Classification in keras

This recipe explains how LSTM is used for Classification in keras

Recipe Objective.

Explain how LSTM is used for Classification?

LSTM is mainly used for text classification so, we will take the example of it.

We will create a LSTM model for text classification

Learn to Build a Multi Class Image Classification Model in Python from Scratch

Step 1- Loading the text.

First we will load the text from our drive. pharma_train=pd.read_csv('/content/drive/My Drive/Python/pharma/train.csv') pharma_train

Step 2- Preprocessing of text.

MAX_WORDS = 10000 MAX_LENGTH = 150 # This is fixed. EMBEDDING_DIM = 100 tokenizer = Tokenizer(num_words=MAX_NB_WORDS, filters='!"#$%&()*+,-./:;<=>?@', lower=True) tokenizer.fit_on_texts(parma_train['job_discription'].values) word_index = tokenizer.word_index print('tokens' % len(word_index)) X = tokenizer.texts_to_sequences(df['job_discription'].values) X = pad_sequences(X, maxlen=MAX_LENGTH) Y = pd.get_dummies(pharma_train['job_type']).values

Step 3- Splitting the dataset

We will split the dataset into training and testing

X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.10, random_state = 42)

Step 4- Creating a LSTM model.

we will create a LSTM model and pass our dataset through it.

model = Sequential() model.add(Embedding(MAX_WORDS, EMBEDDING_DIM, input_length=X.shape[1])) model.add(SpatialDropout1D(0.2)) model.add(LSTM(50)) model.add(Dense(32, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(X_train, Y_train, epochs=10, batch_size=50,validation_split=0.1)

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.