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 Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

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.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

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.