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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build an AI Quiz Generator from Video with OpenAI API
In this LLM project, you will build a model to automate the transcription of video content and generate interactive quizzes using OpenAI’s Whisper and GPT-4o.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Build and Deploy an AI Resume Analyzer with OpenAI and Azure
In this AI Resume Analyzer project, you will learn to build and deploy AI resume analyzer that helps job seekers assess how effectively their resumes match job descriptions using OpenAI's language models and Azure's cloud infrastructure.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

OSZAR »