How to add Bi directional LSTM layer to keras model?

This recipe helps you add Bi directional LSTM layer to keras model

Recipe Objective

Adding Bi directional LSTM layer to keras model

Bi-directional LSTMs is an extension of LSTM, can improve the working of the model on sequence classification problems.

Step 1- Importing Libraries

from keras.layers import Bidirectional from tensorflow import keras from keras.models import Sequential from keras.layers import LSTM from keras.layers import Activation, Dense import numpy as np

Step 2- Create a neural network model.

adding a Bidirectional layer.

model = Sequential() model.add(Bidirectional(LSTM(32, return_sequences=True), input_shape=(5, 10))) model.add(Bidirectional(LSTM(32))) model.add(Activation('relu')) model.compile(loss='categorical_crossentropy', optimizer='Adam')

Step-3 Create a sample model and make prediction from it.

y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape((1,1,10)) # make and show prediction print(model.predict(y))

[[0.02718286 0.02762156 0.02089063 0.01052572 0.06378494 0.01932984
  0.         0.         0.06944765 0.01507997 0.         0.
  0.05792578 0.04587546 0.         0.01098552 0.04473235 0.06602424
  0.         0.         0.         0.01608978 0.         0.
  0.03304935 0.         0.05253785 0.04292118 0.10207159 0.07474144
  0.         0.01693208 0.06157123 0.02414081 0.05233147 0.03505142
  0.03542253 0.01108169 0.01113066 0.         0.         0.05232322
  0.         0.10277228 0.02966982 0.         0.         0.
  0.03759805 0.         0.         0.01015551 0.08046164 0.
  0.         0.00290035 0.         0.02540161 0.         0.
  0.05021353 0.         0.03642806 0.        ]]

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

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

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

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.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.