Keras Deep Learning Cookbook - Rajdeep Dua - E-Book

Keras Deep Learning Cookbook E-Book

Rajdeep Dua

0,0
27,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 216

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Keras Deep Learning Cookbook
Over 30 recipes for implementing deep neural networks in Python

 

 

 

 

 

 

 

 

 

Rajdeep Dua
Manpreet Singh Ghotra

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Keras Deep Learning Cookbook

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Amey VarangaonkarAcquisition Editor:Karan JainContent Development Editor:Karan ThakkarTechnical Editor: Sagar SawantCopy Editor: Safis EditingProject Coordinator: Nidhi JoshiProofreader: Safis EditingIndexer:Pratik ShirodkarGraphics:Jisha ChirayilProduction Coordinator:Aparna Bhagat

First published: October 2018

Production reference: 1301018

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78862-175-5

www.packtpub.com

 
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

Packt.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. 

Contributors

About the authors

Rajdeep Dua has over 18 years experience in the cloud and big data space. He has taught Spark and big data at some of the most prestigious tech schools in India: IIIT Hyderabad, ISB, IIIT Delhi, and Pune College of Engineering. He currently leads the developer relations team at Salesforce India. He has also presented BigQuery and Google App Engine at the W3C conference in Hyderabad. He led the developer relations teams at Google, VMware, and Microsoft, and has spoken at hundreds of other conferences on the cloud. Some of the other references to his work can be seen at Your Story and on ACM digital library. His contributions to the open source community relate to Docker, Kubernetes, Android, OpenStack, and Cloud Foundry.

 

 

 

 

Manpreet Singh Ghotra has more than 15 years experience in software development for both enterprise and big data software. He is currently working at Salesforce on developing a machine learning platform/APIs using open source libraries and frameworks such as Keras, Apache Spark, and TensorFlow. He has worked on various machine learning systems, including sentiment analysis, spam detection, and anomaly detection. He was part of the machine learning group at one of the largest online retailers in the world, working on transit time calculations using Apache Mahout, and the R recommendation system, again using Apache Mahout. With a master's and postgraduate degree in machine learning, he has contributed to, and worked for, the machine learning community. 

About the reviewer

Sujit Pal works at Elsevier Labs, a research and development group within the Reed-Elsevier PLC Group. His interests are in information retrieval, distributed processing, ontology development, natural language processing, and machine learning, and codes in Python, Scala, and Java. He combines his skills in these areas in order to help build new features or feature improvements in different products across the company. He believes in lifelong learning and blogs about his experiences at sujitpal.blogspot.com.

 

 

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Keras Deep Learning Cookbook

Packt Upsell

Why subscribe?

Packt.com

Contributors

About the authors

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Keras Installation

Introduction

Installing Keras on Ubuntu 16.04

Getting ready

How to do it...

Installing miniconda

Installing numpy and scipy

Installing mkl

Installing TensorFlow

Installing Keras

Using the Theano backend with Keras

Installing Keras with Jupyter Notebook in a Docker image

Getting ready

How to do it...

Installing the Docker container 

Installing the Docker container with the host volume mapped

Installing Keras on Ubuntu 16.04 with GPU enabled

Getting ready

How to do it...

Installing cuda

Installing cudnn

Installing NVIDIA CUDA profiler tools interface development files

Installing the TensorFlow GPU version

Installing Keras

Working with Keras Datasets and Models

Introduction

CIFAR-10 dataset

How to do it...

CIFAR-100 dataset

How to do it...

Specifying the label mode

MNIST dataset

How to do it...

Load data from a CSV file

How to do it...

Models in Keras – getting started

Anatomy of a model

Types of models

Sequential models

How to do it...

Create a Sequential model

Compile the model

Train the model 

Evaluate the model

Predict using the model

Putting it all together

Model inspection internals

Model compilation internals

Initialize the loss

Model training

Output of the sample 

Shared layer models

Introduction – shared input layer

How to do it...

Concatenate function

Keras functional APIs

How to do it...

The output of the example

Keras functional APIs – linking the layers

How to do it...

Model class

Image classification using Keras functional APIs

How to do it...

Data Preprocessing, Optimization, and Visualization

Feature standardization of image data

Getting ready

How to do it...

Initializing ImageDataGenerator

Sequence padding

Getting ready

How to do it...

Pre-padding with default 0.0 padding

Post-padding

Padding with truncation

Padding with a non-default value

Model visualization

Getting ready

How to do it...

Code listing

Optimization 

Common code for samples

Optimization with stochastic gradient descent

Getting ready

How to do it...

Optimization with Adam

Getting ready

How to do it...

Optimization with AdaDelta

Getting ready

How to do it...

Adadelta optimizer

Optimization with RMSProp

Getting ready

How to do it...

Classification Using Different Keras Layers

Introduction

Classification for breast cancer

How to do it...

Data processing

Modeling

Full code listing 

Classification for spam detection

How to do it...

Data processing

Modeling

Full code listing

Implementing Convolutional Neural Networks

Introduction

Cervical cancer classification

Getting ready

How to do it…

Data processing

Modeling

Predictions

Digit recognition

Getting ready

How to do it…

Modeling

Generative Adversarial Networks

Introduction

GAN overview

Basic GAN

Getting ready

How to do it...

Building a generator 

Building a discriminator

Initialize the GAN instance

Training the GAN 

Output plots

Average metrics of the GAN

Boundary seeking GAN

Getting ready

How to do it...

Generator

Discriminator

Initializing the BGAN class

Boundary seeking loss

Train the BGAN

Output the plots

Iteration 0

Iteration 10000

Metrics of the BGAN model

Plotting the metrics 

DCGAN

Getting ready

How to do it...

Generator

Summary of the generator

Training the generator

Discriminator

Build the discriminator

Summary of the discriminator

Compile the discriminator

Combined model - generator and discriminator

Train the generator using feedback from a discriminator

Putting it all together

The output of the program

Average metrics of the model

Recurrent Neural Networks

Introduction

The need for RNNs

Simple RNNs for time series data

Getting ready

Loading the dataset

How to do it…

Instantiate a sequential model

LSTM networks for time series data

LSTM networks

LSTM memory example

Getting ready

How to do it...

Encoder

LSTM configuration and model

Train the model

Full code listing 

Time series forecasting with LSTM

Getting ready

 Load the dataset

How to do it…

Instantiate a sequential model

Observation

Sequence to sequence learning for the same length output with LSTM

Getting ready

How to do it…

Training data 

Model creation

Model fit and prediction

Natural Language Processing Using Keras Models

Introduction

Word embedding

Getting ready

How to do it...

Without embeddings

With embeddings

Sentiment analysis

Getting ready

How to do it…

Full code listing 

Text Summarization Using Keras Models

Introduction

Text summarization for reviews

How to do it…

Data processing

Encoder-decoder architecture

Training

See also

Reinforcement Learning

Introduction

The CartPole game with Keras

How to do it...

Implementing the DQN agent

The memory and remember

The replay function

The act function

Hyperparameters for the DQN

DQN agent class

Training the agent

Dueling DQN to play Cartpole 

Getting ready

DQN agent

init method

Setting the last layer of the network

Dueling policy

Init code base

BoltzmannQPolicy

Adjustment during training

Sequential memory

How to do it...

Plotting the training and testing results

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

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. This book covers installing and setting up Keras, while also demonstrating how you can perform deep learning with Keras in the TensorFlow, Apache MXNet, and CNTK backends.

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.

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.

What this book covers

Chapter 1, Keras Installation, covers various installation and setup procedures, as well as defining various Keras configurations.

Chapter 2, Working with Keras Datasets and Models, covers using various datasets, such as CIFAR10, CIFAR100, or MNIST, and many other datasets and models used for image classification. 

Chapter 3, Data Preprocessing, Optimization, and Visualization, covers various preprocessing and optimization techniques using Keras. The optimization techniques covered include TFOptimizer, AdaDelta, and many more.

Chapter 4, Classification Using Different Keras Layers, details various Keras layers, for example, recurrent layers, and convolutional layers. 

Chapter 5, Implementing Convolutional Neural Networks, teaches you convolutional neural network algorithms in detail, using the example of cervical cancer classification and the digit recognition dataset. 

Chapter 6, Generative Adversarial Networks, covers basic generative adversarial networks (GANs) and boundary-seeking GAN.

Chapter 7, Recurrent Neural Networks, covers the basics of recurrent neural networks in order to implement Keras based on historical datasets.

Chapter 8, Natural Language Processing Using Keras Models, covers the basics of NLP for word analysis and sentiment analysis using Keras.

Chapter 9, Text Summarization Using Keras Models, shows you how to use Keras models for text summarization when using the Amazon reviews dataset. 

Chapter 10, Reinforcement Learning, focuses on formulating and developing reinforcement learning models using Keras.

To get the most out of this book

Readers should have some basic knowledge of Keras and deep learning. 

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Keras-Deep-Learning-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781788621755_ColorImages.pdf.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

Keras Installation

In this chapter, we will cover the following recipes:

Installing Keras on Ubuntu 16.04

Installing Keras with Jupyter Notebook in a Docker image

Installing Keras on Ubuntu 16.04 with GPU enabled

Introduction

In this chapter, we look at how Keras can be installed on Ubuntu and CentOS. We will use Ubuntu 16.04, 64-bit (Canonical, Ubuntu, 16.04 LTS, and amd64 xenial image build on 2017-10-26) for the installation.

Installing Keras on Ubuntu 16.04

Before installing Keras, we have to install the Theano and TensorFlow packages and their dependencies. Since it is a fresh OS, make sure Python is installed. Let's look at the following section for Python installation.

Conda is an open source package management system and environment management system that runs on multiple OSes: Windows, macOS, and Linux. Conda installs, runs, and updates packages and their dependencies. Conda creates, saves, loads, and switches between environments on a local computer. It has been created for Python environments.

Getting ready

First you need to make sure you have a blank Ubuntu 16.04 OS locally or remotely available in the cloud and with root access. 

How to do it...

In the following sections, we take a at the installation of each component that needs to be done before we can go ahead with the installation of Keras.

Installing numpy and scipy

The numpy and scipy packages are prerequisites for Theano installation. The following versions are recommended:

NumPy >= 1.9.1 <= 1.12

SciPy >= 0.14 < 0.17.1: H

ighly recommended

 for sparse matrix and support for special functions in Theano, SciPy >=0.8 would do the work

BLAS installation (with Level 3 functionality) the recommended: MKL, this is free through

conda

with the 

mkl-service

package

Basic Linear Algebra Subprograms (BLAS) is a specification that defines a set of low-level routines for performing common linear algebra operations such as vector addition, scalar multiplication, dot products, linear combinations, and matrix multiplication. These are the de facto standard low-level routines for linear algebra libraries; the routines have bindings for both C and Fortran. Level 3 is referred to as matrix -to-matrix multiplications.

Execute the following command to install

numpy

and

scipy

. (Make sure

conda

is in your

PATH

):

conda install

numpy

conda install scipy

The output of the scipy installation is shown as follows. Notice that it installs libgfortran as part of the scipy installation:

Fetching package metadata ...........

Solving package specifications: .

Package plan for installation in environment /home/ubuntu/miniconda2:

The following new packages will also be installed:

libgfortran-ng: 7.2.0-h9f7466a_2

scipy: 1.0.0-py27hf5f0f52_0

Proceed ([y]/n)?

libgfortran-ng 100% |#############################################################| Time: 0:00:00 36.60 MB/s

scipy-1.0.0-py 100% |#############################################################| Time: 0:00:00 66.62 MB/s

Installing mkl

mkl

is a m

ath library for Intel and compatible processors. It is a part of

numpy

, but we want to make sure it is installed before we install Theano and TensorFlow:

conda install mkl

The output of the installation is given as follows. In our case, miniconda2 has already installed the latest version of mkl:

Fetching package metadata ...........

Solving package specifications: .

# All requested packages already installed.

# packages in environment at /home/ubuntu/miniconda2:

#

mkl 2018.0.1 h19d6760_4

Once all the prerequisites are installed, let's install TensorFlow.

Installing Keras

conda-forge is a GitHub entity with a repository of conda recipes.

Next, we will install Keras using

conda

 from

conda-forge

Execute the following command on the Terminal:

conda install -c conda-forge keras

The following listed output will confirm that Keras is installed:

Fetching package metadata .............

Solving package specifications: .

Package plan for installation in environment /home/ubuntu/miniconda2:

The following new packages will also be installed:

h5py: 2.7.1-py27_2 conda-forge

hdf5: 1.10.1-1 conda-forge

keras: 2.0.9-py27_0 conda-forge

libgfortran: 3.0.0-1

pyyaml: 3.12-py27_1 conda-forge

Proceed ([y]/n)? y

libgfortran-3. 100% |#############################################################| Time: 0:00:00 35.16 MB/s

hdf5-1.10.1-1. 100% |#############################################################| Time: 0:00:00 34.26 MB/s

pyyaml-3.12-py 100% |#############################################################| Time: 0:00:00 60.08 MB/s

h5py-2.7.1-py2 100% |#############################################################| Time: 0:00:00 58.54 MB/s

keras-2.0.9-py 100% |#############################################################| Time: 0:00:00 45.92 MB/s

Let's verify the Keras installation with the following code:

$ python

Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19)

Execute the following command to verify that Keras has been installed:

> from keras.models import Sequential

Using TensorFlow backend.

>>>

Notice that Keras is using the TensorFlow backend.

Using the Theano backend with Keras

Let's modify the default configuration and change TensorFlow to Theano as the backend of Keras. Modify the

keras.json

file:

vi .keras/keras.json

The default file has the following content:

{ "

image_data_format

":

"channels_last"

, "

epsilon

":

1e-07

, "

floatx

":

"float32"

, "

backend

":

"tensorflow"

}

The modified file will look like the following file. The 

"backend"

value has been changed to

"theano"

:

{ "

image_data_format

":

"channels_last"

, "

epsilon

":

1e-07

, "

floatx

":

"float32"

, "

backend

":

"theano"

}

Run the Python console and import

Sequential

from

keras.model

 u

sing the Theano backend:

$ python

Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19)

[GCC 7.2.0] on linux2

Type "help", "copyright", "credits" or "license" for more information.