Keras Deep Learning Cookbook
eBook - ePub

Keras Deep Learning Cookbook

Over 30 recipes for implementing deep neural networks in Python

  1. 252 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Keras Deep Learning Cookbook

Over 30 recipes for implementing deep neural networks in Python

About this book

Leverage the power of deep learning and Keras to develop smarter and more efficient data models

Key Features

  • Understand different neural networks and their implementation using Keras
  • Explore recipes for training and fine-tuning your neural network models
  • Put your deep learning knowledge to practice with real-world use-cases, tips, and tricks

Book Description

Keras has quickly emerged as a popular deep learning library. Written in Python, it allows you to train convolutional as well as recurrent neural networks with speed and accuracy.

The Keras Deep Learning Cookbook shows you how to tackle different problems encountered while training efficient deep learning models, with the help of the popular Keras library. Starting with installing and setting up Keras, the book demonstrates how you can perform deep learning with Keras in the TensorFlow. From loading data to fitting and evaluating your model for optimal performance, you will work through a step-by-step process to tackle every possible problem faced while training deep models. You will implement convolutional and recurrent neural networks, adversarial networks, and more with the help of this handy guide. In addition to this, you will learn how to train these models for real-world image and language processing tasks.

By the end of this book, you will have a practical, hands-on understanding of how you can leverage the power of Python and Keras to perform effective deep learning

What you will learn

  • Install and configure Keras in TensorFlow
  • Master neural network programming using the Keras library
  • Understand the different Keras layers
  • Use Keras to implement simple feed-forward neural networks, CNNs and RNNs
  • Work with various datasets and models used for image and text classification
  • Develop text summarization and reinforcement learning models using Keras

Who this book is for

Keras Deep Learning Cookbook is for you if you are a data scientist or machine learning expert who wants to find practical solutions to common problems encountered while training deep learning models. A basic understanding of Python and some experience in machine learning and neural networks is required for this book.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Recurrent Neural Networks

In this chapter, we will cover the following recipes:
  • Simple RNNs for time series data
  • LSTM networks for time series data
  • LSTM memory example time series forecasting with LSTM
  • Sequence to sequence learning for the same length output with LSTM

Introduction

In this chapter, we will learn various recipes on how to create recurrent neural networks (RNNs) using Keras. First, we will understand the need for RNN. We will start with the simple RNNs followed by long short-term memory (LSTM) RNNs (these networks remember the state over a long period of time because of special gates in the cell).

The need for RNNs

Traditional neural networks cannot remember their past interactions, and that is a significant shortcoming. RNNs address this issue. They are networks with loops in them, allowing information to persist. RNNs have loops. In the next diagram, a chunk of the neural network, A, looks at some input, xt, and outputs a value, ht. A loop in the network allows information to be passed from one step of the network to the next.
This diagram shows what a neural network looks like:

Simple RNNs for time series data

In this recipe, we will learn how to use a simple RNN implementation of Keras to predict sales based on a historical dataset.
RNNs are a class of artificial neural network where connections between nodes of the network form a directed graph along a sequence. This topology allows it to exhibit dynamic temporal behavior for input of the time sequence type. Unlike feedforward neural networks, RNNs can use their internal state (also called memory) to process sequences of inputs. This makes them suitable for tasks such as unsegmented, connected handwriting recognition or speech recognition.
A simple RNN is implemented as part of the keras.layers.SimpleRNN class as follows:
keras.layers.SimpleRNN(units, activation='tanh', 
use_bias=True,
kernel_initializer='glorot_uniform',
recurrent_initializer='orthogonal',
bias_initializer='zeros',
kernel_regularizer=None,
recurrent_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
recurrent_constraint=None,
bias_constraint=None,
dropout=0.0,
recurrent_dropout=0.0,
return_sequences=False,
return_state=False,
go_backwards=False,
stateful=False,
unroll=False)
A simple RNN is a fully-connected RNN where the output is to be fed back to the input. We will be using a simple RNN for time series prediction.

Getting ready

The dataset is in this file: sales-of-shampoo-over-a-three-ye.csv. It has two columns, the first for months and the second for sales figures for each month, as follows:
"Month","Sales of shampoo over a three year period"
"1-01",266.0
"1-02",145.9
"1-03",183.1
"1-04",119.3
"1-05",180.3
"1-06",168.5
"1-07",231.8
First, we need to import the relevant classes, as follows:
from pandas import read_csv
from matplotlib import pyplot
from pandas import datetime

Loading the dataset

  1. We define a parser to convert YY to YYYY, shown as follow:
def parser(x):
return datetime.strptime('200' + x, '%Y-%m')
  1. Next, call the read_csv function of pandas to load a .csv file into a pandas DataFrame.
Notice the data parser being used is the function defined previously.
  1. The next read_csv function is called in the next code:
series = read_csv('sales-of-shampoo-over-a-three-ye.csv', header=0, parse_dates=[0], index_col=0, 
squeeze=True, date_parser=parser)
  1. Once the series is loaded, let's summarize the first few rows:
print(series.head())
The output of the preceding code is as follows:
Month
2001-01-01 266.0
2001-02-01 145.9
2001-03-01 183.1
2001-04-01 119.3
2001-05-01 180.3
  1. Next, let's print the line plot using the pyplot library:
series.plot()
pyplot.show()
The...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Keras Installation
  7. Working with Keras Datasets and Models
  8. Data Preprocessing, Optimization, and Visualization
  9. Classification Using Different Keras Layers
  10. Implementing Convolutional Neural Networks
  11. Generative Adversarial Networks
  12. Recurrent Neural Networks
  13. Natural Language Processing Using Keras Models
  14. Text Summarization Using Keras Models
  15. Reinforcement Learning
  16. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Keras Deep Learning Cookbook by Rajdeep Dua, Manpreet Singh Ghotra in PDF and/or ePUB format, as well as other popular books in Computer Science & Artificial Intelligence (AI) & Semantics. We have over one million books available in our catalogue for you to explore.