How to add Bi directional LSTM layer to keras model?

How to add Bi directional LSTM layer to keras model?

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.        ]]

Relevant Projects

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

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.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.