Keras 简明教程

Keras - Quick Guide

Overview of Keras

Keras 运行于 Tensorflow、Theano 或 Cognitive Tookit (CNTK) 等开源机器库之上。Theano 是一个用于快速数值计算任务的 python 库。TensorFlow 是用于创建神经网络和深度学习模型的最著名的符号数学库。TensorFlow 非常灵活,最主要的优点是分布式计算。CNTK 是 Microsoft 开发的深度学习框架。它使用 Python、C#、C++ 等库或独立的机器学习工具包。Theano 和 TensorFlow 是非常强大的库,但对于创建神经网络来说很难理解。

Keras runs on top of open source machine libraries like TensorFlow, Theano or Cognitive Toolkit (CNTK). Theano is a python library used for fast numerical computation tasks. TensorFlow is the most famous symbolic math library used for creating neural networks and deep learning models. TensorFlow is very flexible and the primary benefit is distributed computing. CNTK is deep learning framework developed by Microsoft. It uses libraries such as Python, C#, C++ or standalone machine learning toolkits. Theano and TensorFlow are very powerful libraries but difficult to understand for creating neural networks.

Keras 基于极小的结构,提供了一种干净且简单的方式来创建基于 TensorFlow 或 Theano 的深度学习模型。Keras 旨在快速定义深度学习模型。好吧,Keras 是深度学习应用程序的最佳选择。

Keras is based on minimal structure that provides a clean and easy way to create deep learning models based on TensorFlow or Theano. Keras is designed to quickly define deep learning models. Well, Keras is an optimal choice for deep learning applications.

Features

Keras 采用各种优化技术,使高级神经网络 API 变得更加简单、高效。它支持以下功能:

Keras leverages various optimization techniques to make high level neural network API easier and more performant. It supports the following features −

  1. Consistent, simple and extensible API.

  2. Minimal structure - easy to achieve the result without any frills.

  3. It supports multiple platforms and backends.

  4. It is user friendly framework which runs on both CPU and GPU.

  5. Highly scalability of computation.

Benefits

Keras 是一个非常强大且动态的框架,具有以下优点:

Keras is highly powerful and dynamic framework and comes up with the following advantages −

  1. Larger community support.

  2. Easy to test.

  3. Keras neural networks are written in Python which makes things simpler.

  4. Keras supports both convolution and recurrent networks.

  5. Deep learning models are discrete components, so that, you can combine into many ways.

Keras - Installation

本章介绍了如何在机器上安装 Keras。在进入安装之前,让我们了解 Keras 的基本要求。

This chapter explains about how to install Keras on your machine. Before moving to installation, let us go through the basic requirements of Keras.

Prerequisites

您必须满足以下要求 −

You must satisfy the following requirements −

  1. Any kind of OS (Windows, Linux or Mac)

  2. Python version 3.5 or higher.

Python

Keras 是基于 Python 的神经网络库,因此必须在您的机器上安装 Python。如果已在您的机器上正确安装了 Python,请打开终端并输入 python,您会看到类似于下面指定的响应,

Keras is python based neural network library so python must be installed on your machine. If python is properly installed on your machine, then open your terminal and type python, you could see the response similar as specified below,

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18)
[MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

截至目前,最新版本是“3.7.2”。如果未安装 Python,请访问官方 Python 链接 - www.python.org 根据您的操作系统下载最新版本并立即在您的系统上安装。

As of now the latest version is ‘3.7.2’. If Python is not installed, then visit the official python link - www.python.org and download the latest version based on your OS and install it immediately on your system.

Keras Installation Steps

Keras 安装非常容易。按照以下步骤,可以正确安装系统中的 Keras。

Keras installation is quite easy. Follow below steps to properly install Keras on your system.

Step 1: Create virtual environment

Virtualenv 用于管理不同项目的 Python 包。这有助于避免破坏在其他环境中安装的包。因此,在开发 Python 应用程序时,始终建议使用虚拟环境。

Virtualenv is used to manage Python packages for different projects. This will be helpful to avoid breaking the packages installed in the other environments. So, it is always recommended to use a virtual environment while developing Python applications.

Linux/Mac OS

Linux/Mac OS

对于 Linux 或 mac OS 用户,转到项目根目录,然后键入以下命令以创建虚拟环境,

Linux or mac OS users, go to your project root directory and type the below command to create virtual environment,

python3 -m venv kerasenv

执行上述命令后,将在安装位置中使用 bin,lib and include folders 创建“kerasenv”目录。

After executing the above command, “kerasenv” directory is created with bin,lib and include folders in your installation location.

Windows

Windows

Windows 用户可以使用以下命令,

Windows user can use the below command,

py -m venv keras

Step 2: Activate the environment

此步骤将在 shell 路径中配置 python 和 pip 可执行文件。

This step will configure python and pip executables in your shell path.

Linux/Mac OS

Linux/Mac OS

现在,我们已经创建了一个名为“kerasvenv”的虚拟环境。进入该文件夹,然后键入以下命令,

Now we have created a virtual environment named “kerasvenv”. Move to the folder and type the below command,

$ cd kerasvenv kerasvenv $ source bin/activate

Windows

Windows

Windows 用户进入“kerasenv”文件夹并键入以下命令,

Windows users move inside the “kerasenv” folder and type the below command,

.\env\Scripts\activate

Step 3: Python libraries

Keras 依赖于以下 python 库。

Keras depends on the following python libraries.

  1. Numpy

  2. Pandas

  3. Scikit-learn

  4. Matplotlib

  5. Scipy

  6. Seaborn

希望您已在系统中安装了以上所有库。如果未安装这些库,请使用以下命令逐个安装。

Hopefully, you have installed all the above libraries on your system. If these libraries are not installed, then use the below command to install one by one.

numpy

numpy

pip install numpy

您可能会看到以下响应,

you could see the following response,

Collecting numpy
   Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
   numpy-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
   macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
      |████████████████████████████████| 14.4MB 2.8MB/s

pandas

pandas

pip install pandas

我们可以看到以下响应,

We could see the following response,

Collecting pandas
   Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
pandas-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
   macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
      |████████████████████████████████| 14.4MB 2.8MB/s

matplotlib

matplotlib

pip install matplotlib

我们可以看到以下响应,

We could see the following response,

Collecting matplotlib
   Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
matplotlib-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
   macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
      |████████████████████████████████| 14.4MB 2.8MB/s

scipy

scipy

pip install scipy

我们可以看到以下响应,

We could see the following response,

Collecting scipy
   Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8
/scipy-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
   macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
      |████████████████████████████████| 14.4MB 2.8MB/s

scikit-learn

scikit-learn

它是一个开源机器学习库。它用于分类、回归和聚类算法。在进行安装之前,它需要以下内容 -

It is an open source machine learning library. It is used for classification, regression and clustering algorithms. Before moving to the installation, it requires the following −

  1. Python version 3.5 or higher

  2. NumPy version 1.11.0 or higher

  3. SciPy version 0.17.0 or higher

  4. joblib 0.11 or higher.

现在,我们使用以下命令安装 scikit-learn -

Now, we install scikit-learn using the below command −

pip install -U scikit-learn

Seaborn

Seaborn

Seaborn 是一个神奇的库,它允许你轻松地可视化你的数据。使用以下命令进行安装 -

Seaborn is an amazing library that allows you to easily visualize your data. Use the below command to install −

pip install seaborn

你可能会看到如下所示的类似信息 -

You could see the message similar as specified below −

Collecting seaborn
   Downloading
https://files.pythonhosted.org/packages/a8/76/220ba4420459d9c4c9c9587c6ce607bf56c25b3d3d2de62056efe482dadc
/seaborn-0.9.0-py3-none-any.whl (208kB) 100%
   |████████████████████████████████| 215kB 4.0MB/s
Requirement already satisfied: numpy> = 1.9.3 in
./lib/python3.7/site-packages (from seaborn) (1.17.0)
Collecting pandas> = 0.15.2 (from seaborn)
   Downloading
https://files.pythonhosted.org/packages/39/b7/441375a152f3f9929ff8bc2915218ff1a063a59d7137ae0546db616749f9/
pandas-0.25.0-cp37-cp37m-macosx_10_9_x86_64.
macosx_10_10_x86_64.whl (10.1MB) 100%
   |████████████████████████████████| 10.1MB 1.8MB/s
Requirement already satisfied: scipy>=0.14.0 in
./lib/python3.7/site-packages (from seaborn) (1.3.0)
Collecting matplotlib> = 1.4.3 (from seaborn)
   Downloading
https://files.pythonhosted.org/packages/c3/8b/af9e0984f
5c0df06d3fab0bf396eb09cbf05f8452de4e9502b182f59c33b/
matplotlib-3.1.1-cp37-cp37m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64
.macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) 100%
   |████████████████████████████████| 14.4MB 1.4MB/s
......................................
......................................
Successfully installed cycler-0.10.0 kiwisolver-1.1.0
matplotlib-3.1.1 pandas-0.25.0 pyparsing-2.4.2
python-dateutil-2.8.0 pytz-2019.2 seaborn-0.9.0

Keras Installation Using Python

截至目前,我们已经完成了安装 Kera 的基本要求。现在,使用如下所示的相同过程安装 Keras -

As of now, we have completed basic requirements for the installtion of Kera. Now, install the Keras using same procedure as specified below −

pip install keras

Quit virtual environment

在完成项目中的所有更改后,只需运行以下命令即可退出环境 -

After finishing all your changes in your project, then simply run the below command to quit the environment −

deactivate

Anaconda Cloud

我们相信你已经在你的机器上安装了 anaconda cloud。如果未安装 anaconda,请访问官方链接, https://www.anaconda.com/download 并根据你的操作系统选择下载。

We believe that you have installed anaconda cloud on your machine. If anaconda is not installed, then visit the official link, https://www.anaconda.com/download and choose download based on your OS.

Create a new conda environment

启动 anaconda 提示,这将打开基础 Anaconda 环境。让我们创建一个新的 conda 环境。此过程类似于 virtualenv。在你的 conda 终端中键入以下命令 -

Launch anaconda prompt, this will open base Anaconda environment. Let us create a new conda environment. This process is similar to virtualenv. Type the below command in your conda terminal −

conda create --name PythonCPU

如果你愿意,你还可以使用 GPU 创建和安装模块。在本教程中,我们遵循 CPU 说明。

If you want, you can create and install modules using GPU also. In this tutorial, we follow CPU instructions.

Activate conda environment

要激活环境,请使用以下命令 -

To activate the environment, use the below command −

activate PythonCPU

Install spyder

Spyder 是用于执行 python 应用程序的 IDE。让我们使用以下命令在我们的 conda 环境中安装这个 IDE -

Spyder is an IDE for executing python applications. Let us install this IDE in our conda environment using the below command −

conda install spyder

Install python libraries

我们已经知道 keras 所需的 python 库 numpy、pandas 等。你可以使用以下语法安装所有模块 -

We have already known the python libraries numpy, pandas, etc., needed for keras. You can install all the modules by using the below syntax −

Syntax

Syntax

conda install -c anaconda <module-name>

例如,你想要安装 pandas -

For example, you want to install pandas −

conda install -c anaconda pandas

与相同的方法一样,尝试自己安装剩余的模块。

Like the same method, try it yourself to install the remaining modules.

Install Keras

现在,一切都看起来不错,所以你可以使用以下命令开始 keras 安装 -

Now, everything looks good so you can start keras installation using the below command −

conda install -c anaconda keras

Launch spyder

最后,使用以下命令在您的 conda 终端中启动 spyder −

Finally, launch spyder in your conda terminal using the below command −

spyder

为了确保一切都正确安装,导入所有模块,它会添加所有内容,如果出现任何问题,您将收到 module not found 错误消息。

To ensure everything was installed correctly, import all the modules, it will add everything and if anything went wrong, you will get module not found error message.

Keras - Backend Configuration

本章详细解释了Keras后端实施TensorFlow和Theano。让我们逐一了解每个实施。

This chapter explains Keras backend implementations TensorFlow and Theano in detail. Let us go through each implementation one by one.

TensorFlow

TensorFlow 是 Google 开发的用于数字计算任务的开源机器学习库。Keras 是建立在 TensorFlow 或 Theano 之上的一个高级别 API。我们已经知道如何使用 pip 安装 TensorFlow。

TensorFlow is an open source machine learning library used for numerical computational tasks developed by Google. Keras is a high level API built on top of TensorFlow or Theano. We know already how to install TensorFlow using pip.

如果没有安装,可以使用以下命令安装:

If it is not installed, you can install using the below command −

pip install TensorFlow

一旦执行 Keras,我们就能够看到配置文件位于你的主目录中,进入 .keras/keras.json。

Once we execute keras, we could see the configuration file is located at your home directory inside and go to .keras/keras.json.

keras.json

{
   "image_data_format": "channels_last",
   "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow"
}

在此,

Here,

  1. image_data_format represent the data format.

  2. epsilon represents numeric constant. It is used to avoid DivideByZero error.

  3. float*x represent the default data type *float32. You can also change it to float16 or float64 using set_floatx() method.

  4. image_data_format represent the data format.

假设如果没有创建这个文件,那么就移动到相应位置,然后使用以下步骤创建:

Suppose, if the file is not created then move to the location and create using the below steps −

> cd home
> mkdir .keras
> vi keras.json

记住,你应该将其文件夹名称指定为 .keras,并在 keras.json 文件中添加以上配置。我们可以执行一些预定义的操作以了解后端函数。

Remember, you should specify .keras as its folder name and add the above configuration inside keras.json file. We can perform some pre-defined operations to know backend functions.

Theano

Theano 是一个开源深度学习库,允许你高效地计算多维数组。我们可以使用以下命令轻松安装:

Theano is an open source deep learning library that allows you to evaluate multi-dimensional arrays effectively. We can easily install using the below command −

pip install theano

默认情况下,Keras 使用 TensorFlow 后端。如果你想把后端配置从 TensorFlow 更改为 Theano,只需在 keras.json 文件中将 backend = theano 更改一下即可。如下所述:

By default, keras uses TensorFlow backend. If you want to change backend configuration from TensorFlow to Theano, just change the backend = theano in keras.json file. It is described below −

keras.json

{
   "image_data_format": "channels_last",
   "epsilon": 1e-07,
   "floatx": "float32",
   "backend": "theano"
}

现在,保存你的文件,重启终端并启动 Keras,你的后端将会被更改。

Now save your file, restart your terminal and start keras, your backend will be changed.

>>> import keras as k
using theano backend.

Keras - Overview of Deep learning

深度学习是机器学习的一个不断发展中的子领域。深度学习涉及对输入进行逐层分析,每一层逐渐提取关于输入的高级信息。

Deep learning is an evolving subfield of machine learning. Deep learning involves analyzing the input in layer by layer manner, where each layer progressively extracts higher level information about the input.

让我们分析一张图像,以作为一种简单的情景。让我们假设你的输入图像被分成一个矩形的像素网格。现在,第一层对像素进行抽象。第二层理解图像中的边缘。下一层从边缘构建节点。然后,下一层将从节点中找到分支。最后,输出层将检测整个对象。在这里,特征提取过程从一层输出进入下一层输入。

Let us take a simple scenario of analyzing an image. Let us assume that your input image is divided up into a rectangular grid of pixels. Now, the first layer abstracts the pixels. The second layer understands the edges in the image. The Next layer constructs nodes from the edges. Then, the next would find branches from the nodes. Finally, the output layer will detect the full object. Here, the feature extraction process goes from the output of one layer into the input of the next subsequent layer.

通过使用这种方法,我们可以处理大量的特征,这使得深度学习成为一个非常强大的工具。深度学习算法也适用于非结构化数据分析。让我们在本章中学习深度学习的基础知识。

By using this approach, we can process huge amount of features, which makes deep learning a very powerful tool. Deep learning algorithms are also useful for the analysis of unstructured data. Let us go through the basics of deep learning in this chapter.

Artificial Neural Networks

深度学习最流行和最主要的方法是使用“人工神经网络”(ANN)。它们受到人类大脑模型的启发,而大脑是我们身体最复杂的器官。人脑由超过 900 亿个称为“神经元”的微小细胞组成。神经元通过称为“轴突”和“树突”的神经纤维相互连接。轴突的主要作用是将信息从一个神经元传送到另一个神经元,也就是与它相连的神经元。

The most popular and primary approach of deep learning is using “Artificial neural network” (ANN). They are inspired from the model of human brain, which is the most complex organ of our body. The human brain is made up of more than 90 billion tiny cells called “Neurons”. Neurons are inter-connected through nerve fiber called “axons” and “Dendrites”. The main role of axon is to transmit information from one neuron to another to which it is connected.

类似地,树突的主要作用是接收与之相连的另一个神经元的轴突所传输的信息。每个神经元处理少量信息,然后将结果传递给另一个神经元,这个过程不断进行。这就是我们人类大脑处理大量信息(如语言、视觉等)并从中提取有用信息的基本方法。

Similarly, the main role of dendrites is to receive the information being transmitted by the axons of another neuron to which it is connected. Each neuron processes a small information and then passes the result to another neuron and this process continues. This is the basic method used by our human brain to process huge about of information like speech, visual, etc., and extract useful information from it.

基于这个模型,第一个人工神经网络(ANN)是由心理学家 Frank Rosenblatt 于 1958 年发明的。ANN 由多个节点组成,类似于神经元。节点紧密相连,并组织成不同的隐藏层。输入层接收输入数据,数据依次经过一个或多个隐藏层,最后输出层预测一些关于输入数据的有用信息。例如,输入可能是一幅图像,而输出可能是图像中识别的物体,比如“猫”。

Based on this model, the first Artificial Neural Network (ANN) was invented by psychologist Frank Rosenblatt, in the year of 1958. ANNs are made up of multiple nodes which is similar to neurons. Nodes are tightly interconnected and organized into different hidden layers. The input layer receives the input data and the data goes through one or more hidden layers sequentially and finally the output layer predict something useful about the input data. For example, the input may be an image and the output may be the thing identified in the image, say a “Cat”.

一个单神经元(在 ANN 中被称为感知器)可以表示为以下形式:

A single neuron (called as perceptron in ANN) can be represented as below −

artificial neural networks

在此,

Here,

  1. Multiple input along with weight represents dendrites.

  2. Sum of input along with activation function represents neurons. Sum actually means computed value of all inputs and activation function represent a function, which modify the Sum value into 0, 1 or 0 to 1.

  3. Actual output represent axon and the output will be received by neuron in next layer.

让我们在此部分了解不同类型的人工神经网络。

Let us understand different types of artificial neural networks in this section.

Multi-Layer Perceptron

多层感知器是 ANN 最简单的形式。它由一个输入层、一个或多个隐藏层以及最终一个输出层组成。一层由感知器的集合组成。输入层基本上是输入数据的一个或多个特征。每个隐藏层都包含一个或多个神经元,处理特征的某些方面并将其处理后的信息发送到下一个隐藏层。输出层处理从最后一个隐藏层接收数据并最终输出结果。

Multi-Layer perceptron is the simplest form of ANN. It consists of a single input layer, one or more hidden layer and finally an output layer. A layer consists of a collection of perceptron. Input layer is basically one or more features of the input data. Every hidden layer consists of one or more neurons and process certain aspect of the feature and send the processed information into the next hidden layer. The output layer process receives the data from last hidden layer and finally output the result.

multi layer perceptron

Convolutional Neural Network (CNN)

卷积神经网络是其中一项最流行的 ANN。它广泛应用于图像和视频识别领域。它基于卷积的概念,这是一个数学概念。它几乎与多层感知器相似,只不过在完全连接的隐藏神经元层之前包含一系列卷积层和池化层。它有三个重要层数 −

Convolutional neural network is one of the most popular ANN. It is widely used in the fields of image and video recognition. It is based on the concept of convolution, a mathematical concept. It is almost similar to multi-layer perceptron except it contains series of convolution layer and pooling layer before the fully connected hidden neuron layer. It has three important layers −

  1. Convolution layer − It is the primary building block and perform computational tasks based on convolution function.

  2. Pooling layer − It is arranged next to convolution layer and is used to reduce the size of inputs by removing unnecessary information so computation can be performed faster.

  3. Fully connected layer − It is arranged to next to series of convolution and pooling layer and classify input into various categories.

一个简单的 CNN 可以表示如下 −

A simple CNN can be represented as below −

cnn

在此,

Here,

  1. 2 series of Convolution and pooling layer is used and it receives and process the input (e.g. image).

  2. A single fully connected layer is used and it is used to output the data (e.g. classification of image)

Recurrent Neural Network (RNN)

循环神经网络 (RNN) 对于解决其他 ANN 模型中的缺陷很有用。好,大多数 ANN 不会记住以前情况的步骤,而是学习根据训练中的上下文做出决策。与此同时,RNN 存储过去的信息,其所有决策都是基于从过去学到的内容做出的。

Recurrent Neural Networks (RNN) are useful to address the flaw in other ANN models. Well, Most of the ANN doesn’t remember the steps from previous situations and learned to make decisions based on context in training. Meanwhile, RNN stores the past information and all its decisions are taken from what it has learnt from the past.

此方法主要用于图像分类。有时,我们可能需要展望未来才能解决过去。在这种情况下,双向 RNN 有助于从过去学习并预测未来。例如,我们有来自多个输入的手写样本。假设我们对一个输入感到困惑,那么我们需要再次检查其他输入,以识别出从过去做出决定的正确上下文。

This approach is mainly useful in image classification. Sometimes, we may need to look into the future to fix the past. In this case bidirectional RNN is helpful to learn from the past and predict the future. For example, we have handwritten samples in multiple inputs. Suppose, we have confusion in one input then we need to check again other inputs to recognize the correct context which takes the decision from the past.

Workflow of ANN

让我们先了解深度学习的不同阶段,然后了解 Keras 如何帮助深度学习过程。

Let us first understand the different phases of deep learning and then, learn how Keras helps in the process of deep learning.

Collect required data

深度学习需要大量输入数据才能成功学习和预测结果。因此,首先尽可能多地收集数据。

Deep learning requires lot of input data to successfully learn and predict the result. So, first collect as much data as possible.

Analyze data

分析数据并很好地理解数据。选择正确的 ANN 算法需要更好地理解数据。

Analyze the data and acquire a good understanding of the data. The better understanding of the data is required to select the correct ANN algorithm.

Choose an algorithm (model)

选择最适合学习过程类型(例如图像分类、文本处理等)和可用输入数据的算法。算法由 Keras 中的 Model 表示。算法包括一个或多个图层。ANN 中的每一层都可以在 Keras 中由 Keras Layer 表示。

Choose an algorithm, which will best fit for the type of learning process (e.g image classification, text processing, etc.,) and the available input data. Algorithm is represented by Model in Keras. Algorithm includes one or more layers. Each layers in ANN can be represented by Keras Layer in Keras.

  1. Prepare data − Process, filter and select only the required information from the data.

  2. Split data − Split the data into training and test data set. Test data will be used to evaluate the prediction of the algorithm / Model (once the machine learn) and to cross check the efficiency of the learning process.

  3. Compile the model − Compile the algorithm / model, so that, it can be used further to learn by training and finally do to prediction. This step requires us to choose loss function and Optimizer. loss function and Optimizer are used in learning phase to find the error (deviation from actual output) and do optimization so that the error will be minimized.

  4. Fit the model − The actual learning process will be done in this phase using the training data set.

  5. Predict result for unknown value − Predict the output for the unknown input data (other than existing training and test data)

  6. Evaluate model − Evaluate the model by predicting the output for test data and cross-comparing the prediction with actual result of the test data.

  7. Freeze, Modify or choose new algorithm − Check whether the evaluation of the model is successful. If yes, save the algorithm for future prediction purpose. If not, then modify or choose new algorithm / model and finally, again train, predict and evaluate the model. Repeat the process until the best algorithm (model) is found.

上面的步骤可以使用以下流程图表示 -

The above steps can be represented using below flow chart −

ann

Keras - Deep learning

Keras 提供了一个完整的框架来创建任何类型的卷积神经网络。Keras 具有创新性,并且非常易于学习。它支持从简单神经网络到非常大和复杂的神经网络模型。在本章中,让我们了解 Keras 框架的架构以及 Keras 如何帮助进行深度学习。

Keras provides a complete framework to create any type of neural networks. Keras is innovative as well as very easy to learn. It supports simple neural network to very large and complex neural network model. Let us understand the architecture of Keras framework and how Keras helps in deep learning in this chapter.

Architecture of Keras

Keras API 可分为三大类 -

Keras API can be divided into three main categories −

  1. Model

  2. Layer

  3. Core Modules

在 Keras 中,每个 ANN 都由 Keras Models 表示。反过来,每个 Keras 模型都是 Keras Layers 的组成,并表示 ANN 层,如输入、隐藏层、输出层、卷积层、池化层等,Keras 模型和层访问 Keras modules 以获取激活函数、损失函数、正则化函数等。使用 Keras 模型、Keras 层和 Keras 模块,任何 ANN 算法(CNN、RNN 等)都可以用简单有效的方式表示。

In Keras, every ANN is represented by Keras Models. In turn, every Keras Model is composition of Keras Layers and represents ANN layers like input, hidden layer, output layers, convolution layer, pooling layer, etc., Keras model and layer access Keras modules for activation function, loss function, regularization function, etc., Using Keras model, Keras Layer, and Keras modules, any ANN algorithm (CNN, RNN, etc.,) can be represented in a simple and efficient manner.

下图描述了模型、层和核心模块之间的关系 -

The following diagram depicts the relationship between model, layer and core modules −

architecture of keras

让我们来看一下 Keras 模型、Keras 层和 Keras 模块的概述。

Let us see the overview of Keras models, Keras layers and Keras modules.

Model

Keras 模型有两种类型,如下所述 -

Keras Models are of two types as mentioned below −

Sequential Model - 顺序模型基本上是 Keras 层的线性组合。顺序模型简单、最小,并有能力表示几乎所有可用的神经网络。

Sequential Model − Sequential model is basically a linear composition of Keras Layers. Sequential model is easy, minimal as well as has the ability to represent nearly all available neural networks.

一个简单的顺序模型如下 -

A simple sequential model is as follows −

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,)))

其中,

Where,

  1. Line 1 imports Sequential model from Keras models

  2. Line 2 imports Dense layer and Activation module

  3. Line 4 create a new sequential model using Sequential API

  4. Line 5 adds a dense layer (Dense API) with relu activation (using Activation module) function.

Sequential 模型公开 Model 类以创建自定义模型。我们可以使用子类化概念来创建我们自己的复杂模型。

Sequential model exposes Model class to create customized models as well. We can use sub-classing concept to create our own complex model.

Functional API − Functional API 基本上可用于创建复杂模型。

Functional API − Functional API is basically used to create complex models.

Layer

Keras 模型中的每个 Keras 层代表实际提议的神经网络模型中的相应层(输入层、隐藏层和输出层)。Keras 提供了许多预构建的层,以便轻松创建任何复杂的神经网络。以下是其中一些重要的 Keras 层:

Each Keras layer in the Keras model represent the corresponding layer (input layer, hidden layer and output layer) in the actual proposed neural network model. Keras provides a lot of pre-build layers so that any complex neural network can be easily created. Some of the important Keras layers are specified below,

  1. Core Layers

  2. Convolution Layers

  3. Pooling Layers

  4. Recurrent Layers

一个用于表示使用 sequential 模型的神经网络模型的简单 python 代码如下所示 −

A simple python code to represent a neural network model using sequential model is as follows −

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout model = Sequential()

model.add(Dense(512, activation = 'relu', input_shape = (784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation = 'relu')) model.add(Dropout(0.2))
model.add(Dense(num_classes, activation = 'softmax'))

其中,

Where,

  1. Line 1 imports Sequential model from Keras models

  2. Line 2 imports Dense layer and Activation module

  3. Line 4 create a new sequential model using Sequential API

  4. Line 5 adds a dense layer (Dense API) with relu activation (using Activation module) function.

  5. Line 6 adds a dropout layer (Dropout API) to handle over-fitting.

  6. Line 7 adds another dense layer (Dense API) with relu activation (using Activation module) function.

  7. Line 8 adds another dropout layer (Dropout API) to handle over-fitting.

  8. Line 9 adds final dense layer (Dense API) with softmax activation (using Activation module) function.

Keras 还提供创建我们自己的自定义层。可以通过对 Keras.Layer 类进行子分类来创建自定义层,它类似于对 Keras 模型进行子分类。

Keras also provides options to create our own customized layers. Customized layer can be created by sub-classing the Keras.Layer class and it is similar to sub-classing Keras models.

Core Modules

Keras 还提供许多内置的神经网络相关函数来正确创建 Keras 模型和 Keras 层。以下是一些函数:

Keras also provides a lot of built-in neural network related functions to properly create the Keras model and Keras layers. Some of the function are as follows −

  1. Activations module − Activation function is an important concept in ANN and activation modules provides many activation function like softmax, relu, etc.,

  2. Loss module − Loss module provides loss functions like mean_squared_error, mean_absolute_error, poisson, etc.,

  3. Optimizer module − Optimizer module provides optimizer function like adam, sgd, etc.,

  4. Regularizers − Regularizer module provides functions like L1 regularizer, L2 regularizer, etc.,

让我们在后续章节中详细了解 Keras 模块。

Let us learn Keras modules in detail in the upcoming chapter.

Keras - Modules

正如我们先前了解的,Keras 模块包含预定义的类、函数和变量,它们对深度学习算法非常有用。让我们在本节中了解 Keras 提供的模块。

As we learned earlier, Keras modules contains pre-defined classes, functions and variables which are useful for deep learning algorithm. Let us learn the modules provided by Keras in this chapter.

Available modules

让我们首先查看 Keras 中可用模块的列表。

Let us first see the list of modules available in the Keras.

  1. Initializers − Provides a list of initializers function. We can learn it in details in Keras layer chapter. during model creation phase of machine learning.

  2. Regularizers − Provides a list of regularizers function. We can learn it in details in Keras Layers chapter.

  3. Constraints − Provides a list of constraints function. We can learn it in details in Keras Layers chapter.

  4. Activations − Provides a list of activator function. We can learn it in details in Keras Layers chapter.

  5. Losses − Provides a list of loss function. We can learn it in details in Model Training chapter.

  6. Metrics − Provides a list of metrics function. We can learn it in details in Model Training chapter.

  7. Optimizers − Provides a list of optimizer function. We can learn it in details in Model Training chapter.

  8. Callback − Provides a list of callback function. We can use it during the training process to print the intermediate data as well as to stop the training itself (EarlyStopping method) based on some condition.

  9. Text processing − Provides functions to convert text into NumPy array suitable for machine learning. We can use it in data preparation phase of machine learning.

  10. Image processing − Provides functions to convert images into NumPy array suitable for machine learning. We can use it in data preparation phase of machine learning.

  11. Sequence processing − Provides functions to generate time based data from the given input data. We can use it in data preparation phase of machine learning.

  12. Backend − Provides function of the backend library like TensorFlow and Theano.

  13. Utilities − Provides lot of utility function useful in deep learning.

让我们在本节中了解 backend 模块和 utils 模型。

Let us see backend module and utils model in this chapter.

backend module

backend module 用于 keras 后台操作。默认情况下,keras 在 TensorFlow 后台上运行。如果您愿意,您可以切换到其他后端,如 Theano 或 CNTK。Deufalt 后台配置在您的根目录下的 .keras/keras.json 文件中定义。

backend module is used for keras backend operations. By default, keras runs on top of TensorFlow backend. If you want, you can switch to other backends like Theano or CNTK. Defualt backend configuration is defined inside your root directory under .keras/keras.json file.

可以使用以下代码导入 Keras 后台模块

Keras backend module can be imported using below code

>>> from keras import backend as k

如果我们使用默认后端 TensorFlow,那么以下函数将返回如下指定的基于 TensorFlow 的信息 −

If we are using default backend TensorFlow, then the below function returns TensorFlow based information as specified below −

>>> k.backend()
'tensorflow'
>>> k.epsilon()
1e-07
>>> k.image_data_format()
'channels_last'
>>> k.floatx()
'float32'

让我们简要了解一些用于数据分析的重要后台函数 −

Let us understand some of the significant backend functions used for data analysis in brief −

get_uid()

它是默认图形的标识符。它在下面定义 −

It is the identifier for the default graph. It is defined below −

>>> k.get_uid(prefix='')
1
>>> k.get_uid(prefix='') 2

reset_uids

它用于重置 uid 值。

It is used resets the uid value.

>>> k.reset_uids()

现在,再次执行 get_uid()。这将被重置,并再次变为 1。

Now, again execute the get_uid(). This will be reset and change again to 1.

>>> k.get_uid(prefix='')
1

placeholder

用于实例化占位符张量。简单的占位符以保存 3D 形状的示例如下 −

It is used instantiates a placeholder tensor. Simple placeholder to hold 3-D shape is shown below −

>>> data = k.placeholder(shape = (1,3,3))
>>> data
<tf.Tensor 'Placeholder_9:0' shape = (1, 3, 3) dtype = float32>

If you use int_shape(), it will show the shape.

>>> k.int_shape(data) (1, 3, 3)

dot

它用于将两个张量相乘。考虑 a 和 b 是两个张量,而 c 将是 ab 乘法的结果。假定 a 的形状是 (4,2),而 b 的形状是 (2,3)。它在下面定义,

It is used to multiply two tensors. Consider a and b are two tensors and c will be the outcome of multiply of ab. Assume a shape is (4,2) and b shape is (2,3). It is defined below,

>>> a = k.placeholder(shape = (4,2))
>>> b = k.placeholder(shape = (2,3))
>>> c = k.dot(a,b)
>>> c
<tf.Tensor 'MatMul_3:0' shape = (4, 3) dtype = float32>
>>>

ones

它用于将所有内容初始化为 one 值。

It is used to initialize all as one value.

>>> res = k.ones(shape = (2,2))

#print the value

>>> k.eval(res)
array([[1., 1.], [1., 1.]], dtype = float32)

batch_dot

它用于对批次中的两个数据执行乘积。输入维度必须为 2 或更高。如下所示:

It is used to perform the product of two data in batches. Input dimension must be 2 or higher. It is shown below −

>>> a_batch = k.ones(shape = (2,3))
>>> b_batch = k.ones(shape = (3,2))
>>> c_batch = k.batch_dot(a_batch,b_batch)
>>> c_batch
<tf.Tensor 'ExpandDims:0' shape = (2, 1) dtype = float32>

variable

它用于初始化变量。让我们在这个变量中执行简单的转置操作。

It is used to initializes a variable. Let us perform simple transpose operation in this variable.

>>> data = k.variable([[10,20,30,40],[50,60,70,80]])
#variable initialized here
>>> result = k.transpose(data)
>>> print(result)
Tensor("transpose_6:0", shape = (4, 2), dtype = float32)
>>> print(k.eval(result))
   [[10. 50.]
   [20. 60.]
   [30. 70.]
   [40. 80.]]

如果你希望从 numpy 访问:

If you want to access from numpy −

>>> data = np.array([[10,20,30,40],[50,60,70,80]])

>>> print(np.transpose(data))
   [[10 50]
   [20 60]
   [30 70]
   [40 80]]

>>> res = k.variable(value = data)
>>> print(res)
<tf.Variable 'Variable_7:0' shape = (2, 4) dtype = float32_ref>

is_sparse(tensor)

它用于检查张量是否是稀疏张量。

It is used to check whether the tensor is sparse or not.

>>> a = k.placeholder((2, 2), sparse=True)

>>> print(a) SparseTensor(indices =
   Tensor("Placeholder_8:0",
   shape = (?, 2), dtype = int64),
values = Tensor("Placeholder_7:0", shape = (?,),
dtype = float32), dense_shape = Tensor("Const:0", shape = (2,), dtype = int64))

>>> print(k.is_sparse(a)) True

to_dense()

它用于将稀疏张量转换为稠密张量。

It is used to converts sparse into dense.

>>> b = k.to_dense(a)
>>> print(b) Tensor("SparseToDense:0", shape = (2, 2), dtype = float32)
>>> print(k.is_sparse(b)) False

random_uniform_variable

它用于在 uniform distribution 概念下初始化。

It is used to initialize using uniform distribution concept.

k.random_uniform_variable(shape, mean, scale)

在此,

Here,

  1. shape − denotes the rows and columns in the format of tuples.

  2. mean − mean of uniform distribution.

  3. scale − standard deviation of uniform distribution.

让我们看一下下面的示例用法:

Let us have a look at the below example usage −

>>> a = k.random_uniform_variable(shape = (2, 3), low=0, high = 1)
>>> b = k. random_uniform_variable(shape = (3,2), low = 0, high = 1)
>>> c = k.dot(a, b)
>>> k.int_shape(c)
(2, 2)

utils module

utils 提供了深度学习的有用实用程序函数。 utils 模块提供的一些方法如下:

utils provides useful utilities function for deep learning. Some of the methods provided by the utils module is as follows −

HDF5Matrix

它用于以 HDF5 格式表示输入数据。

It is used to represent the input data in HDF5 format.

from keras.utils import HDF5Matrix data = HDF5Matrix('data.hdf5', 'data')

to_categorical

它用于将类别向量转换为二进制类别矩阵。

It is used to convert class vector into binary class matrix.

>>> from keras.utils import to_categorical
>>> labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> to_categorical(labels)
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype = float32)
>>> from keras.utils import normalize
>>> normalize([1, 2, 3, 4, 5])
array([[0.13483997, 0.26967994, 0.40451992, 0.53935989, 0.67419986]])

print_summary

它用于打印模型摘要。

It is used to print the summary of the model.

from keras.utils import print_summary print_summary(model)

plot_model

它用于创建 dot 格式的模型表示形式并将其保存到文件中。

It is used to create the model representation in dot format and save it to file.

from keras.utils import plot_model
plot_model(model,to_file = 'image.png')

plot_model 将生成图像以了解模型的性能。

This plot_model will generate an image to understand the performance of model.

Keras - Layers

正如前面所学,Keras 层是 Keras 模型的主要构建块。每一层接收输入信息,进行一些计算,最后输出转换后的信息。一层输出将流入下一层作为其输入。让我们在此章节中了解有关层的完整详细信息。

As learned earlier, Keras layers are the primary building block of Keras models. Each layer receives input information, do some computation and finally output the transformed information. The output of one layer will flow into the next layer as its input. Let us learn complete details about layers in this chapter.

Introduction

一个 Keras 层需要 shape of the input (input_shape) 以便了解输入数据的结构,需要 initializer 设置每个输入的权重,最后需要激活器将输出转化为非线性。中间用约束限制输入数据权重的生成范围,而正则化函数会通过在优化过程中动态地对权重施加惩罚,来尝试优化层(以及模型)。

A Keras layer requires shape of the input (input_shape) to understand the structure of the input data, initializer to set the weight for each input and finally activators to transform the output to make it non-linear. In between, constraints restricts and specify the range in which the weight of input data to be generated and regularizer will try to optimize the layer (and the model) by dynamically applying the penalties on the weights during optimization process.

总而言之,Keras 层需要以下最低细节信息才能创建一个完整的层。

To summarise, Keras layer requires below minimum details to create a complete layer.

  1. Shape of the input data

  2. Number of neurons / units in the layer

  3. Initializers

  4. Regularizers

  5. Constraints

  6. Activations

让我们在下一章了解基本概念。在了解基本概念之前,让我们使用顺序模型 API 创建一个简单的 Keras 层,以便了解 Keras 模型和层的工作方式。

Let us understand the basic concept in the next chapter. Before understanding the basic concept, let us create a simple Keras layer using Sequential model API to get the idea of how Keras model and layer works.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers
from keras import regularizers
from keras import constraints

model = Sequential()

model.add(Dense(32, input_shape=(16,), kernel_initializer = 'he_uniform',
   kernel_regularizer = None, kernel_constraint = 'MaxNorm', activation = 'relu'))
model.add(Dense(16, activation = 'relu'))
model.add(Dense(8))

其中,

where,

  1. Line 1-5 imports the necessary modules.

  2. Line 7 creates a new model using Sequential API.

  3. Line 9 creates a new Dense layer and add it into the model. Dense is an entry level layer provided by Keras, which accepts the number of neurons or units (32) as its required parameter. If the layer is first layer, then we need to provide Input Shape, (16,) as well. Otherwise, the output of the previous layer will be used as input of the next layer. All other parameters are optional. First parameter represents the number of units (neurons). input_shape represent the shape of input data. kernel_initializer represent initializer to be used. he_uniform function is set as value. kernel_regularizer represent regularizer to be used. None is set as value. kernel_constraint represent constraint to be used. MaxNorm function is set as value. activation represent activation to be used. relu function is set as value.

  4. Line 10 creates second Dense layer with 16 units and set relu as the activation function.

  5. Line 11 creates final Dense layer with 8 units.

Basic Concept of Layers

让我们了解一下层的概念,以及 Keras 对每个概念的支持方式。

Let us understand the basic concept of layer as well as how Keras supports each concept.

Input shape

在机器学习中,文本、图像或视频等的各种输入数据将首先转换为数字数组,然后馈送到算法中。输入数字可能是单维数组、二维数组(矩阵)或多维数组。我们可以使用整数元组 shape 指定维度信息。例如, (4,2) 表示具有四行两列的矩阵。

In machine learning, all type of input data like text, images or videos will be first converted into array of numbers and then feed into the algorithm. Input numbers may be single dimensional array, two dimensional array (matrix) or multi-dimensional array. We can specify the dimensional information using shape, a tuple of integers. For example, (4,2) represent matrix with four rows and two columns.

>>> import numpy as np
>>> shape = (4, 2)
>>> input = np.zeros(shape)
>>> print(input)
[
   [0. 0.]
   [0. 0.]
   [0. 0.]
   [0. 0.]
]
>>>

类似地, (3,4,2) 是三维矩阵,具有三个 4x2 矩阵集合(两行和四列)。

Similarly, (3,4,2) three dimensional matrix having three collections of 4x2 matrix (two rows and four columns).

>>> import numpy as np
>>> shape = (3, 4, 2)
>>> input = np.zeros(shape)
>>> print(input)
[
   [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]
   [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]
   [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]
]
>>>

要创建模型的第一层(或模型的输入层),应指定输入数据形状。

To create the first layer of the model (or input layer of the model), shape of the input data should be specified.

Initializers

在机器学习中,所有输入数据都将分配权重。 Initializers 模块提供不同的函数来设置这些初始权重。其中一些 Keras Initializer 函数如下 −

In Machine Learning, weight will be assigned to all input data. Initializers module provides different functions to set these initial weight. Some of the Keras Initializer function are as follows −

Zeros

为所有输入数据生成 0

Generates 0 for all input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.Zeros()
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

其中, kernel_initializer 表示模型内核的初始化器。

Where, kernel_initializer represent the initializer for kernel of the model.

Ones

为所有输入数据生成 1

Generates 1 for all input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.Ones()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

Constant

为所有输入数据生成用户指定的常数值(例如 5 )。

Generates a constant value (say, 5) specified by the user for all input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.Constant(value = 0) model.add(
   Dense(512, activation = 'relu', input_shape = (784,), kernel_initializer = my_init)
)

value 表示常数値

where, value represent the constant value

RandomNormal

使用输入数据的正态分布生成数值。

Generates value using normal distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.RandomNormal(mean=0.0,
stddev = 0.05, seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

其中,

where,

  1. mean represent the mean of the random values to generate

  2. stddev represent the standard deviation of the random values to generate

  3. seed represent the values to generate random number

RandomUniform

使用输入数据的均匀分布生成数值。

Generates value using uniform distribution of input data.

from keras import initializers

my_init = initializers.RandomUniform(minval = -0.05, maxval = 0.05, seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

其中,

where,

  1. minval represent the lower bound of the random values to generate

  2. maxval represent the upper bound of the random values to generate

TruncatedNormal

使用输入数据的截尾正态分布生成数值。

Generates value using truncated normal distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.TruncatedNormal(mean = 0.0, stddev = 0.05, seed = None
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

VarianceScaling

基于该层输入形状和输出形状以及指定的缩放比例生成数值。

Generates value based on the input shape and output shape of the layer along with the specified scale.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.VarianceScaling(
   scale = 1.0, mode = 'fan_in', distribution = 'normal', seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   skernel_initializer = my_init))

其中,

where,

  1. scale represent the scaling factor

  2. mode represent any one of fan_in, fan_out and fan_avg values

  3. distribution represent either of normal or uniform

VarianceScaling

它使用以下公式查找正态分布的 stddev ,然后再使用正态分布查找权重,

It finds the stddev value for normal distribution using below formula and then find the weights using normal distribution,

stddev = sqrt(scale / n)

n 表示,

where n represent,

  1. number of input units for mode = fan_in

  2. number of out units for mode = fan_out

  3. average number of input and output units for mode = fan_avg

类似地,它使用以下公式查找均匀分布的限制,然后再使用均匀分布查找权重,

Similarly, it finds the limit for uniform distribution using below formula and then find the weights using uniform distribution,

limit = sqrt(3 * scale / n)

lecun_normal

利用输入数据的 lecun 正态分布来生成值。

Generates value using lecun normal distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.RandomUniform(minval = -0.05, maxval = 0.05, seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

它使用以下公式找到 stddev ,然后应用正态分布

It finds the stddev using the below formula and then apply normal distribution

stddev = sqrt(1 / fan_in)

其中, fan_in 表示输入单元的数量。

where, fan_in represent the number of input units.

lecun_uniform

利用输入数据的 lecun 均匀分布来生成值。

Generates value using lecun uniform distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.lecun_uniform(seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

它使用以下公式找到 limit ,然后应用均匀分布

It finds the limit using the below formula and then apply uniform distribution

limit = sqrt(3 / fan_in)

其中,

where,

  1. fan_in represents the number of input units

  2. fan_out represents the number of output units

glorot_normal

利用输入数据的 glorot 正态分布来生成值。

Generates value using glorot normal distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.glorot_normal(seed=None) model.add(
   Dense(512, activation = 'relu', input_shape = (784,), kernel_initializer = my_init)
)

它使用以下公式找到 stddev ,然后应用正态分布

It finds the stddev using the below formula and then apply normal distribution

stddev = sqrt(2 / (fan_in + fan_out))

其中,

where,

  1. fan_in represents the number of input units

  2. fan_out represents the number of output units

glorot_uniform

利用输入数据的 glorot 均匀分布来生成值。

Generates value using glorot uniform distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.glorot_uniform(seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

它使用以下公式找到 limit ,然后应用均匀分布

It finds the limit using the below formula and then apply uniform distribution

limit = sqrt(6 / (fan_in + fan_out))

其中,

where,

  1. fan_in represent the number of input units.

  2. fan_out represents the number of output units

he_normal

利用输入数据的 he 正态分布来生成值。

Generates value using he normal distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.RandomUniform(minval = -0.05, maxval = 0.05, seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

它使用以下公式找到 stddev,然后应用正态分布。

It finds the stddev using the below formula and then apply normal distribution.

stddev = sqrt(2 / fan_in)

其中, fan_in 表示输入单元的数量。

where, fan_in represent the number of input units.

he_uniform

利用输入数据的 he 均匀分布来生成值。

Generates value using he uniform distribution of input data.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.he_normal(seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

它使用以下公式找到 limit ,然后应用均匀分布。

It finds the limit using the below formula and then apply uniform distribution.

limit = sqrt(6 / fan_in)

其中, fan_in 表示输入单元的数量。

where, fan_in represent the number of input units.

Orthogonal

生成一个随机正交矩阵。

Generates a random orthogonal matrix.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.Orthogonal(gain = 1.0, seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init))

其中, gain 是矩阵的乘法因子。

where, gain represent the multiplication factor of the matrix.

Identity

生成单位矩阵。

Generates identity matrix.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.Identity(gain = 1.0) model.add(
   Dense(512, activation = 'relu', input_shape = (784,), kernel_initializer = my_init)
)

Constraints

在机器学习中,将在优化过程中对参数(权重)设置约束。<>Constraints 模块提供了不同的功能来设置该层上的约束。某些约束功能如下所示。

In machine learning, a constraint will be set on the parameter (weight) during optimization phase. <>Constraints module provides different functions to set the constraint on the layer. Some of the constraint functions are as follows.

NonNeg

将权重限制为非负数。

Constrains weights to be non-negative.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers

my_init = initializers.Identity(gain = 1.0) model.add(
   Dense(512, activation = 'relu', input_shape = (784,),
   kernel_initializer = my_init)
)

其中, kernel_constraint 表示层中要使用的约束。

where, kernel_constraint represent the constraint to be used in the layer.

UnitNorm

将权重限制为单位范数。

Constrains weights to be unit norm.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import constraints

my_constrain = constraints.UnitNorm(axis = 0)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_constraint = my_constrain))

MaxNorm

将权重限制为小于或等于给定值的范数。

Constrains weight to norm less than or equals to the given value.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import constraints

my_constrain = constraints.MaxNorm(max_value = 2, axis = 0)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_constraint = my_constrain))

其中,

where,

  1. max_value represent the upper bound

  2. axis represent the dimension in which the constraint to be applied. e.g. in Shape (2,3,4) axis 0 denotes first dimension, 1 denotes second dimension and 2 denotes third dimension

MinMaxNorm

将权重限制为指定最小值和最大值之间的范数。

Constrains weights to be norm between specified minimum and maximum values.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import constraints

my_constrain = constraints.MinMaxNorm(min_value = 0.0, max_value = 1.0, rate = 1.0, axis = 0)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_constraint = my_constrain))

其中, rate 表示应用权重约束的速率。

where, rate represent the rate at which the weight constrain is applied.

Regularizers

在机器学习中,正则化在优化阶段使用。在优化过程中对层参数应用一些惩罚。Keras 正则化模块提供以下函数来对层设置惩罚。正则化仅按层应用。

In machine learning, regularizers are used in the optimization phase. It applies some penalties on the layer parameter during optimization. Keras regularization module provides below functions to set penalties on the layer. Regularization applies per-layer basis only.

L1 Regularizer

它提供基于 L1 的正则化。

It provides L1 based regularization.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import regularizers

my_regularizer = regularizers.l1(0.)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_regularizer = my_regularizer))

其中, kernel_regularizer 表示应用权重约束的速率。

where, kernel_regularizer represent the rate at which the weight constrain is applied.

L2 Regularizer

它提供基于 L2 的正则化。

It provides L2 based regularization.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import regularizers

my_regularizer = regularizers.l2(0.)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_regularizer = my_regularizer))

L1 and L2 Regularizer

它提供基于 L1 和 L2 的正则化。

It provides both L1 and L2 based regularization.

from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import regularizers

my_regularizer = regularizers.l2(0.)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
   kernel_regularizer = my_regularizer))

Activations

在机器学习中,激活函数是一种特殊函数,用于发现特定的神经元是否被激活。基本上,激活函数对输入数据执行非线性变换,从而使神经元能够更好地学习。神经元的输出取决于激活函数。

In machine learning, activation function is a special function used to find whether a specific neuron is activated or not. Basically, the activation function does a nonlinear transformation of the input data and thus enable the neurons to learn better. Output of a neuron depends on the activation function.

当你回想单一感知的概念时,感知器(神经元)的输出仅仅是激活函数的结果,激活函数接受所有输入与其对应权重的总和乘以其结果加上整体偏差(如果可用的话)。

As you recall the concept of single perception, the output of a perceptron (neuron) is simply the result of the activation function, which accepts the summation of all input multiplied with its corresponding weight plus overall bias, if any available.

result = Activation(SUMOF(input * weight) + bias)

因此,激活函数在模型的成功学习中发挥着重要的作用。Keras 在 activations 模块中提供了很多激活函数。让我们了解模块中所有可用的激活函数。

So, activation function plays an important role in the successful learning of the model. Keras provides a lot of activation function in the activations module. Let us learn all the activations available in the module.

linear

应用线性函数。不进行任何操作。

Applies Linear function. Does nothing.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'linear', input_shape = (784,)))

其中, activation 引用层的激活函数。它可以通过函数名称指定,该层将使用相应的激活器。

Where, activation refers the activation function of the layer. It can be specified simply by the name of the function and the layer will use corresponding activators.

elu

应用指数线性单元。

Applies Exponential linear unit.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'elu', input_shape = (784,)))

selu

应用比例指数线性单元。

Applies Scaled exponential linear unit.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'selu', input_shape = (784,)))

relu

应用整流线性单元。

Applies Rectified Linear Unit.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,)))

softmax

应用 Softmax 函数。

Applies Softmax function.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'softmax', input_shape = (784,)))

softplus

应用 Softplus 函数。

Applies Softplus function.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'softplus', input_shape = (784,)))

softsign

应用 Softsign 函数。

Applies Softsign function.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'softsign', input_shape = (784,)))

tanh

应用双曲正切函数。

Applies Hyperbolic tangent function.

from keras.models import Sequential
from keras.layers import Activation, Dense
model = Sequential()
model.add(Dense(512, activation = 'tanh', input_shape = (784,)))

sigmoid

应用 Sigmoid 函数。

Applies Sigmoid function.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'sigmoid', input_shape = (784,)))

hard_sigmoid

应用 Hard Sigmoid 函数。

Applies Hard Sigmoid function.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'hard_sigmoid', input_shape = (784,)))

exponential

应用指数函数。

Applies exponential function.

from keras.models import Sequential
from keras.layers import Activation, Dense

model = Sequential()
model.add(Dense(512, activation = 'exponential', input_shape = (784,)))

Sr.No

Layers & Description

1

Dense Layer Dense layer is the regular deeply connected neural network layer.

2

Dropout Layers Dropout is one of the important concept in the machine learning.

3

Flatten Layers Flatten is used to flatten the input.

4

Reshape Layers Reshape is used to change the shape of the input.

5

Permute Layers Permute is also used to change the shape of the input using pattern.

6

RepeatVector Layers RepeatVector is used to repeat the input for set number, n of times.

7

Lambda Layers Lambda is used to transform the input data using an expression or function.

8

Convolution Layers Keras contains a lot of layers for creating Convolution based ANN, popularly called as Convolution Neural Network (CNN).

9

Pooling Layer It is used to perform max pooling operations on temporal data.

10

Locally connected layer Locally connected layers are similar to Conv1D layer but the difference is Conv1D layer weights are shared but here weights are unshared.

11

Merge Layer It is used to merge a list of inputs.

12

Embedding Layer It performs embedding operations in input layer.

Keras - Customized Layer

Keras 允许创建我们自己的自定义层。一旦创建一个新层,它就可以在任何模型中毫无限制地使用。让我们在这章中学习如何创建新层。

Keras allows to create our own customized layer. Once a new layer is created, it can be used in any model without any restriction. Let us learn how to create new layer in this chapter.

Keras 提供了一个基础 layer 类,Layer 可以对其进行子类化以创建我们自己的自定义层。让我们创建一个简单的层,它将根据正态分布找到权重,然后在训练期间对输入及其权重的乘积求和进行基本计算。

Keras provides a base layer class, Layer which can sub-classed to create our own customized layer. Let us create a simple layer which will find weight based on normal distribution and then do the basic computation of finding the summation of the product of input and its weight during training.

Step 1: Import the necessary module

首先,让我们导入必要的模块 −

First, let us import the necessary modules −

from keras import backend as K
from keras.layers import Layer

在此,

Here,

  1. backend is used to access the dot function.

  2. Layer is the base class and we will be sub-classing it to create our layer

Step 2: Define a layer class

让我们通过对 Layer class 进行子类化来创建一个新类 MyCustomLayer

Let us create a new class, MyCustomLayer by sub-classing Layer class

class MyCustomLayer(Layer):
   ...

Step 3: Initialize the layer class

让我们如下所示初始化我们的新类 −

Let us initialize our new class as specified below −

def __init__(self, output_dim, **kwargs):
   self.output_dim = output_dim
   super(MyCustomLayer, self).__init__(**kwargs)

在此,

Here,

  1. Line 2 sets the output dimension.

  2. Line 3 calls the base or super layer’s init function.

Step 4: Implement build method

build 是主要方法,其唯一目的是正确构建层。它可以执行与层内部工作相关的任何操作。完成自定义功能后,我们可以调用基础类 build 函数。我们的自定义 build 函数如下 −

build is the main method and its only purpose is to build the layer properly. It can do anything related to the inner working of the layer. Once the custom functionality is done, we can call the base class build function. Our custom build function is as follows −

def build(self, input_shape):
   self.kernel = self.add_weight(name = 'kernel',
      shape = (input_shape[1], self.output_dim),
      initializer = 'normal', trainable = True)
   super(MyCustomLayer, self).build(input_shape)

在此,

Here,

  1. Line 1 defines the build method with one argument, input_shape. Shape of the input data is referred by input_shape.

  2. Line 2 creates the weight corresponding to input shape and set it in the kernel. It is our custom functionality of the layer. It creates the weight using ‘normal’ initializer.

  3. Line 6 calls the base class, build method.

Step 5: Implement call method

call 方法在训练过程中执行层的精确工作。

call method does the exact working of the layer during training process.

我们的自定义 call 方法如下

Our custom call method is as follows

def call(self, input_data):
   return K.dot(input_data, self.kernel)

在此,

Here,

  1. Line 1 defines the call method with one argument, input_data. input_data is the input data for our layer.

  2. Line 2 return the dot product of the input data, input_data and our layer’s kernel, self.kernel

Step 6: Implement compute_output_shape method

def compute_output_shape(self, input_shape): return (input_shape[0], self.output_dim)

在此,

Here,

  1. Line 1 defines compute_output_shape method with one argument input_shape

  2. Line 2 computes the output shape using shape of input data and output dimension set while initializing the layer.

实现 build, callcompute_output_shape ,即完成了创建自定义层。最终的完整代码如下:

Implementing the build, call and compute_output_shape completes the creating a customized layer. The final and complete code is as follows

from keras import backend as K from keras.layers import Layer
class MyCustomLayer(Layer):
   def __init__(self, output_dim, **kwargs):
      self.output_dim = output_dim
      super(MyCustomLayer, self).__init__(**kwargs)
   def build(self, input_shape): self.kernel =
      self.add_weight(name = 'kernel',
      shape = (input_shape[1], self.output_dim),
      initializer = 'normal', trainable = True)
      super(MyCustomLayer, self).build(input_shape) #
      Be sure to call this at the end
   def call(self, input_data): return K.dot(input_data, self.kernel)
   def compute_output_shape(self, input_shape): return (input_shape[0], self.output_dim)

Using our customized layer

我们创建一个简单的模型,使用如下指定得自定义层:

Let us create a simple model using our customized layer as specified below −

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(MyCustomLayer(32, input_shape = (16,)))
model.add(Dense(8, activation = 'softmax')) model.summary()

在此,

Here,

  1. Our MyCustomLayer is added to the model using 32 units and (16,) as input shape

运行应用程序将打印模型摘要,如下所示:

Running the application will print the model summary as below −

Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param
#================================================================
my_custom_layer_1 (MyCustomL (None, 32) 512
_________________________________________________________________
dense_1 (Dense) (None, 8) 264
=================================================================
Total params: 776
Trainable params: 776
Non-trainable params: 0
_________________________________________________________________

Keras - Models

正如前面所学习的,Keras 模型表示实际神经网络模型。Keras 提供了两种创建模型的方法,简单易用的顺序 API 及更灵活、高级的功能 API。让我们在此章节中学习如何同时使用顺序 API 和功能 API 创建模型。

As learned earlier, Keras model represents the actual neural network model. Keras provides a two mode to create the model, simple and easy to use Sequential API as well as more flexible and advanced Functional API. Let us learn now to create model using both Sequential and Functional API in this chapter.

Sequential

Sequential API 的核心思想很简单,就是按顺序排列 Keras 层,因此,它被称为顺序 API。大多数 ANN 的层也按顺序排列,数据按照给定顺序从一个层流到另一个层,直至数据最终到达输出层。

The core idea of Sequential API is simply arranging the Keras layers in a sequential order and so, it is called Sequential API. Most of the ANN also has layers in sequential order and the data flows from one layer to another layer in the given order until the data finally reaches the output layer.

可以通过简单地调用 Sequential() API 来创建 ANN 模型,如下所示 −

A ANN model can be created by simply calling Sequential() API as specified below −

from keras.models import Sequential
model = Sequential()

Add layers

要添加一个层,只需使用 Keras 层 API 创建一个层,然后按照如下所示通过 add() 函数传递该层 −

To add a layer, simply create a layer using Keras layer API and then pass the layer through add() function as specified below −

from keras.models import Sequential

model = Sequential()
input_layer = Dense(32, input_shape=(8,)) model.add(input_layer)
hidden_layer = Dense(64, activation='relu'); model.add(hidden_layer)
output_layer = Dense(8)
model.add(output_layer)

在此,我们创建了一个输入层、一个隐藏层和一个输出层。

Here, we have created one input layer, one hidden layer and one output layer.

Access the model

Keras 提供了几种方法来获取模型信息,例如层、输入数据和输出数据。它们如下所示 −

Keras provides few methods to get the model information like layers, input data and output data. They are as follows −

  1. model.layers − Returns all the layers of the model as list.

>>> layers = model.layers
>>> layers
[
   <keras.layers.core.Dense object at 0x000002C8C888B8D0>,
   <keras.layers.core.Dense object at 0x000002C8C888B7B8>
   <keras.layers.core.Dense object at 0x 000002C8C888B898>
]
  1. model.inputs − Returns all the input tensors of the model as list.

>>> inputs = model.inputs
>>> inputs
[<tf.Tensor 'dense_13_input:0' shape=(?, 8) dtype=float32>]
  1. model.outputs − Returns all the output tensors of the model as list.

>>> outputs = model.outputs
>>> outputs
<tf.Tensor 'dense_15/BiasAdd:0' shape=(?, 8) dtype=float32>]
  1. model.get_weights − Returns all the weights as NumPy arrays.

  2. model.set_weights(weight_numpy_array) − Set the weights of the model.

Serialize the model

Keras 提供了一些方法,将模型序列化为对象以及 json,并稍后重新加载它。它们如下所示 −

Keras provides methods to serialize the model into object as well as json and load it again later. They are as follows −

  1. get_config() − IReturns the model as an object.

config = model.get_config()
  1. from_config() − It accept the model configuration object as argument and create the model accordingly.

new_model = Sequential.from_config(config)
  1. to_json() − Returns the model as an json object.

>>> json_string = model.to_json()
>>> json_string '{"class_name": "Sequential", "config":
{"name": "sequential_10", "layers":
[{"class_name": "Dense", "config":
{"name": "dense_13", "trainable": true, "batch_input_shape":
[null, 8], "dtype": "float32", "units": 32, "activation": "linear",
"use_bias": true, "kernel_initializer":
{"class_name": "Vari anceScaling", "config":
{"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}},
"bias_initializer": {"class_name": "Zeros", "conf
ig": {}}, "kernel_regularizer": null, "bias_regularizer": null,
"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}},
{" class_name": "Dense", "config": {"name": "dense_14", "trainable": true,
"dtype": "float32", "units": 64, "activation": "relu", "use_bias": true,
"kern el_initializer": {"class_name": "VarianceScaling", "config":
{"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}},
"bias_initia lizer": {"class_name": "Zeros",
"config": {}}, "kernel_regularizer": null, "bias_regularizer": null,
"activity_regularizer": null, "kernel_constraint" : null, "bias_constraint": null}},
{"class_name": "Dense", "config": {"name": "dense_15", "trainable": true,
"dtype": "float32", "units": 8, "activation": "linear", "use_bias": true,
"kernel_initializer": {"class_name": "VarianceScaling", "config":
{"scale": 1.0, "mode": "fan_avg", "distribution": " uniform", "seed": null}},
"bias_initializer": {"class_name": "Zeros", "config": {}},
"kernel_regularizer": null, "bias_regularizer": null, "activity_r egularizer":
null, "kernel_constraint": null, "bias_constraint":
null}}]}, "keras_version": "2.2.5", "backend": "tensorflow"}'
>>>
  1. model_from_json() − Accepts json representation of the model and create a new model.

from keras.models import model_from_json
new_model = model_from_json(json_string)
  1. to_yaml() − Returns the model as a yaml string.

>>> yaml_string = model.to_yaml()
>>> yaml_string 'backend: tensorflow\nclass_name:
Sequential\nconfig:\n layers:\n - class_name: Dense\n config:\n
activation: linear\n activity_regular izer: null\n batch_input_shape:
!!python/tuple\n - null\n - 8\n bias_constraint: null\n bias_initializer:\n
class_name : Zeros\n config: {}\n bias_regularizer: null\n dtype:
float32\n kernel_constraint: null\n
kernel_initializer:\n cla ss_name: VarianceScaling\n config:\n
distribution: uniform\n mode: fan_avg\n
scale: 1.0\n seed: null\n kernel_regularizer: null\n name: dense_13\n
trainable: true\n units: 32\n
use_bias: true\n - class_name: Dense\n config:\n activation: relu\n activity_regularizer: null\n
bias_constraint: null\n bias_initializer:\n class_name: Zeros\n
config : {}\n bias_regularizer: null\n dtype: float32\n
kernel_constraint: null\n kernel_initializer:\n class_name: VarianceScalin g\n
config:\n distribution: uniform\n mode: fan_avg\n scale: 1.0\n
seed: null\n kernel_regularizer: nu ll\n name: dense_14\n trainable: true\n
units: 64\n use_bias: true\n - class_name: Dense\n config:\n
activation: linear\n activity_regularizer: null\n
bias_constraint: null\n bias_initializer:\n
class_name: Zeros\n config: {}\n bias_regu larizer: null\n
dtype: float32\n kernel_constraint: null\n
kernel_initializer:\n class_name: VarianceScaling\n config:\n
distribution: uniform\n mode: fan_avg\n
scale: 1.0\n seed: null\n kernel_regularizer: null\n name: dense _15\n
trainable: true\n units: 8\n
use_bias: true\n name: sequential_10\nkeras_version: 2.2.5\n'
>>>
  1. model_from_yaml() − Accepts yaml representation of the model and create a new model.

from keras.models import model_from_yaml
new_model = model_from_yaml(yaml_string)

Summarise the model

了解模型对于正确应用其培训和预测非常重要。Keras提供了一个简单的方法摘要,以获取有关模型及其层的完整信息。

Understanding the model is very important phase to properly use it for training and prediction purposes. Keras provides a simple method, summary to get the full information about the model and its layers.

在上一部分中创建的模型的摘要如下:

A summary of the model created in the previous section is as follows −

>>> model.summary() Model: "sequential_10"
_________________________________________________________________
Layer (type) Output Shape Param
#================================================================
dense_13 (Dense) (None, 32) 288
_________________________________________________________________
dense_14 (Dense) (None, 64) 2112
_________________________________________________________________
dense_15 (Dense) (None, 8) 520
=================================================================
Total params: 2,920
Trainable params: 2,920
Non-trainable params: 0
_________________________________________________________________
>>>

Train and Predict the model

模型提供了用于训练、评估和预测过程的功能。如下所示:

Model provides function for training, evaluation and prediction process. They are as follows −

  1. compile − Configure the learning process of the model

  2. fit − Train the model using the training data

  3. evaluate − Evaluate the model using the test data

  4. predict − Predict the results for new input.

Functional API

顺序API用于逐层地创建模型。函数API是创建更复杂模型的另一种方法。函数模型,您可以定义多个输入或输出共享层。首先,我们为模型创建实例并连接到访问模型输入和输出的层。本章节简要解释了函数模型。

Sequential API is used to create models layer-by-layer. Functional API is an alternative approach of creating more complex models. Functional model, you can define multiple input or output that share layers. First, we create an instance for model and connecting to the layers to access input and output to the model. This section explains about functional model in brief.

Create a model

使用以下模块导入输入层:

Import an input layer using the below module −

>>> from keras.layers import Input

现在,使用以下代码,为模型指定输入尺寸形状创建输入层:

Now, create an input layer specifying input dimension shape for the model using the below code −

>>> data = Input(shape=(2,3))

使用以下模块定义输入层的层:

Define layer for the input using the below module −

>>> from keras.layers import Dense

使用以下代码行,为输入添加密集层:

Add Dense layer for the input using the below line of code −

>>> layer = Dense(2)(data)
>>> print(layer)
Tensor("dense_1/add:0", shape =(?, 2, 2), dtype = float32)

使用以下模块定义模型:

Define model using the below module −

from keras.models import Model

通过指定输入和输出层,以函数的方式创建模型:

Create a model in functional way by specifying both input and output layer −

model = Model(inputs = data, outputs = layer)

下面显示了创建简单模型的完整代码:

The complete code to create a simple model is shown below −

from keras.layers import Input
from keras.models import Model
from keras.layers import Dense

data = Input(shape=(2,3))
layer = Dense(2)(data) model =
Model(inputs=data,outputs=layer) model.summary()
_________________________________________________________________
Layer (type)               Output Shape               Param #
=================================================================
input_2 (InputLayer)       (None, 2, 3)               0
_________________________________________________________________
dense_2 (Dense)            (None, 2, 2)               8
=================================================================
Total params: 8
Trainable params: 8
Non-trainable params: 0
_________________________________________________________________

Keras - Model Compilation

我们之前已经学习了如何使用顺序 API 和函数 API 来创建模型的基础。本章将讲解如何编译模型。编译是创建模型的最后一步。编译完成后,我们就可以继续进入训练阶段了。

Previously, we studied the basics of how to create model using Sequential and Functional API. This chapter explains about how to compile the model. The compilation is the final step in creating a model. Once the compilation is done, we can move on to training phase.

让我们学习一些概念,以更好地了解编译过程。

Let us learn few concepts required to better understand the compilation process.

Loss

在机器学习中, Loss 函数用于找出学习过程中的错误或偏差。Keras 在模型编译过程中需要损失函数。

In machine learning, Loss function is used to find error or deviation in the learning process. Keras requires loss function during model compilation process.

Keras 在 losses 模块中提供了很多损失函数,如下所示:

Keras provides quite a few loss function in the losses module and they are as follows −

  1. mean_squared_error

  2. mean_absolute_error

  3. mean_absolute_percentage_error

  4. mean_squared_logarithmic_error

  5. squared_hinge

  6. hinge

  7. categorical_hinge

  8. logcosh

  9. huber_loss

  10. categorical_crossentropy

  11. sparse_categorical_crossentropy

  12. binary_crossentropy

  13. kullback_leibler_divergence

  14. poisson

  15. cosine_proximity

  16. is_categorical_crossentropy

所有以上损失函数接受两个参数:

All above loss function accepts two arguments −

  1. y_true − true labels as tensors

  2. y_pred − prediction with same shape as y_true

在使用损失函数之前导入损失模块,如下所示:

Import the losses module before using loss function as specified below −

from keras import losses

Optimizer

在机器学习中, Optimization 是一个通过比较预测与损失函数来优化输入权重的重要过程。Keras 提供了很多优化模块,即优化器,如下所示:

In machine learning, Optimization is an important process which optimize the input weights by comparing the prediction and the loss function. Keras provides quite a few optimizer as a module, optimizers and they are as follows:

SGD − 随机梯度下降优化器。

SGD − Stochastic gradient descent optimizer.

keras.optimizers.SGD(learning_rate = 0.01, momentum = 0.0, nesterov = False)

RMSprop − RMSProp 优化器。

RMSprop − RMSProp optimizer.

keras.optimizers.RMSprop(learning_rate = 0.001, rho = 0.9)

Adagrad − Adagrad 优化器。

Adagrad − Adagrad optimizer.

keras.optimizers.Adagrad(learning_rate = 0.01)

Adadelta − Adadelta 优化器。

Adadelta − Adadelta optimizer.

keras.optimizers.Adadelta(learning_rate = 1.0, rho = 0.95)

Adam − Adam 优化器。

Adam − Adam optimizer.

keras.optimizers.Adam(
   learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, amsgrad = False
)

Adamax − 来自 Adam 的 Adamax 优化器。

Adamax − Adamax optimizer from Adam.

keras.optimizers.Adamax(learning_rate = 0.002, beta_1 = 0.9, beta_2 = 0.999)

Nadam − Nesterov Adam 优化器。

Nadam − Nesterov Adam optimizer.

keras.optimizers.Nadam(learning_rate = 0.002, beta_1 = 0.9, beta_2 = 0.999)

在使用优化器之前导入优化器模块,如下所示:

Import the optimizers module before using optimizers as specified below −

from keras import optimizers

Metrics

在机器学习中, Metrics 用于评估你模型的性能。它与损失函数类似,但不用于训练过程中。Keras 提供了很多指标模块,即 metrics ,如下所示:

In machine learning, Metrics is used to evaluate the performance of your model. It is similar to loss function, but not used in training process. Keras provides quite a few metrics as a module, metrics and they are as follows

  1. accuracy

  2. binary_accuracy

  3. categorical_accuracy

  4. sparse_categorical_accuracy

  5. top_k_categorical_accuracy

  6. sparse_top_k_categorical_accuracy

  7. cosine_proximity

  8. clone_metric

同损失函数类似,指标也接受以下两个参数 −

Similar to loss function, metrics also accepts below two arguments −

  1. y_true − true labels as tensors

  2. y_pred − prediction with same shape as y_true

在使用指标之前,导入指标模块,如下所示 −

Import the metrics module before using metrics as specified below −

from keras import metrics

Compile the model

Keras 模型提供 compile() 方法来编译模型。 compile() 方法的参数和默认值为以下内容

Keras model provides a method, compile() to compile the model. The argument and default value of the compile() method is as follows

compile(
   optimizer,
   loss = None,
   metrics = None,
   loss_weights = None,
   sample_weight_mode = None,
   weighted_metrics = None,
   target_tensors = None
)

重要参数如下 −

The important arguments are as follows −

  1. loss function

  2. Optimizer

  3. metrics

编译模式的示例代码如下 −

A sample code to compile the mode is as follows −

from keras import losses
from keras import optimizers
from keras import metrics

model.compile(loss = 'mean_squared_error',
   optimizer = 'sgd', metrics = [metrics.categorical_accuracy])

其中,

where,

  1. loss function is set as mean_squared_error

  2. optimizer is set as sgd

  3. metrics is set as metrics.categorical_accuracy

Model Training

模型通过 fit() 使用 NumPy 数组进行训练。此拟合函数的主要目的是根据训练评估您的模型。它还可以用于绘制模型性能。它具有以下语法 −

Models are trained by NumPy arrays using fit(). The main purpose of this fit function is used to evaluate your model on training. This can be also used for graphing model performance. It has the following syntax −

model.fit(X, y, epochs = , batch_size = )

在此,

Here,

  1. X, y − It is a tuple to evaluate your data.

  2. epochs − no of times the model is needed to be evaluated during training.

  3. batch_size − training instances.

让我们以上述概念为例,使用 numpy 随机数据。

Let us take a simple example of numpy random data to use this concept.

Create data

让我们借助下面提到的命令使用 numpy 为 x 和 y 创建随机数据 −

Let us create a random data using numpy for x and y with the help of below mentioned command −

import numpy as np

x_train = np.random.random((100,4,8))
y_train = np.random.random((100,10))

现在,创建随机验证数据,

Now, create random validation data,

x_val = np.random.random((100,4,8))
y_val = np.random.random((100,10))

Create model

让我们创建简单的顺序模型 −

Let us create simple sequential model −

from keras.models import Sequential model = Sequential()

Add layers

创建图层以添加模型 −

Create layers to add model −

from keras.layers import LSTM, Dense

# add a sequence of vectors of dimension 16
model.add(LSTM(16, return_sequences = True))
model.add(Dense(10, activation = 'softmax'))

compile model

现在模型已定义。您可以使用以下命令进行编译 −

Now model is defined. You can compile using the below command −

model.compile(
   loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy']
)

Apply fit()

现在,我们应用 fit() 函数来训练我们的数据 −

Now we apply fit() function to train our data −

model.fit(x_train, y_train, batch_size = 32, epochs = 5, validation_data = (x_val, y_val))

Create a Multi-Layer Perceptron ANN

我们已经学会创建、编译和训练 Keras 模型。

We have learned to create, compile and train the Keras models.

让我们应用所学知识并创建一个基于 MPL 的简单神经网络。

Let us apply our learning and create a simple MPL based ANN.

Dataset module

在创建一个模型之前,我们需要选择一个问题,需要收集所需数据,并将数据转换为 NumPy 数组。一旦收集到数据,我们就可以准备模型并使用收集的数据对其进行训练。数据收集是机器学习中最困难的阶段之一。Keras 提供了一个特殊模块数据集,用于下载在线机器学习数据以用于训练目的。它从在线服务器获取数据,处理数据,并将数据作为训练和测试集返回。让我们检查 Keras 数据集模块提供的数据。此模块中提供的数据如下:

Before creating a model, we need to choose a problem, need to collect the required data and convert the data to NumPy array. Once data is collected, we can prepare the model and train it by using the collected data. Data collection is one of the most difficult phase of machine learning. Keras provides a special module, datasets to download the online machine learning data for training purposes. It fetches the data from online server, process the data and return the data as training and test set. Let us check the data provided by Keras dataset module. The data available in the module are as follows,

  1. CIFAR10 small image classification

  2. CIFAR100 small image classification

  3. IMDB Movie reviews sentiment classification

  4. Reuters newswire topics classification

  5. MNIST database of handwritten digits

  6. Fashion-MNIST database of fashion articles

  7. Boston housing price regression dataset

让我们使用 MNIST database of handwritten digits (或 minst)作为我们的输入。minst 是 60,000 个 28x28 灰度图像的集合。它包含 10 位数字。它还包含 10,000 个测试图像。

Let us use the MNIST database of handwritten digits (or minst) as our input. minst is a collection of 60,000, 28x28 grayscale images. It contains 10 digits. It also contains 10,000 test images.

可以使用以下代码加载数据集 −

Below code can be used to load the dataset −

from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

其中

where

  1. Line 1 imports minst from the keras dataset module.

  2. Line 3 calls the load_data function, which will fetch the data from online server and return the data as 2 tuples, First tuple, (x_train, y_train) represent the training data with shape, (number_sample, 28, 28) and its digit label with shape, (number_samples, ). Second tuple, (x_test, y_test) represent test data with same shape.

还可以使用类似的 API 获取其他数据集,每个 API 也返回类似的数据,但数据形状除外。数据的形状取决于数据类型。

Other dataset can also be fetched using similar API and every API returns similar data as well except the shape of the data. The shape of the data depends on the type of data.

Create a model

让我们选择下面表示的简单多层感知器 (MLP),并尝试使用 Keras 创建该模型。

Let us choose a simple multi-layer perceptron (MLP) as represented below and try to create the model using Keras.

create model

该模型的核心功能如下:

The core features of the model are as follows −

  1. Input layer consists of 784 values (28 x 28 = 784).

  2. First hidden layer, Dense consists of 512 neurons and ‘relu’ activation function.

  3. Second hidden layer, Dropout has 0.2 as its value.

  4. Third hidden layer, again Dense consists of 512 neurons and ‘relu’ activation function.

  5. Fourth hidden layer, Dropout has 0.2 as its value.

  6. Fifth and final layer consists of 10 neurons and ‘softmax’ activation function.

  7. Use categorical_crossentropy as loss function.

  8. Use RMSprop() as Optimizer.

  9. Use accuracy as metrics.

  10. Use 128 as batch size.

  11. Use 20 as epochs.

Step 1 − Import the modules

Step 1 − Import the modules

让我们导入必需的模块。

Let us import the necessary modules.

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
import numpy as np

Step 2 − Load data

Step 2 − Load data

让我们导入 mnist 数据集。

Let us import the mnist dataset.

(x_train, y_train), (x_test, y_test) = mnist.load_data()

Step 3 − Process the data

Step 3 − Process the data

让我们根据模型修改数据集,以便可以将其馈送至我们的模型。

Let us change the dataset according to our model, so that it can be feed into our model.

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

其中

Where

  1. reshape is used to reshape the input from (28, 28) tuple to (784, )

  2. to_categorical is used to convert vector to binary matrix

Step 4 − Create the model

Step 4 − Create the model

让我们创建实际模型。

Let us create the actual model.

model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation = 'softmax'))

Step 5 − Compile the model

Step 5 − Compile the model

让我们使用选定的损失函数、优化器和指标来编译模型。

Let us compile the model using selected loss function, optimizer and metrics.

model.compile(loss = 'categorical_crossentropy',
   optimizer = RMSprop(),
   metrics = ['accuracy'])

Step 6 − Train the model

Step 6 − Train the model

让我们使用 fit() 方法训练模型。

Let us train the model using fit() method.

history = model.fit(
   x_train, y_train,
   batch_size = 128,
   epochs = 20,
   verbose = 1,
   validation_data = (x_test, y_test)
)

Final thoughts

我们已创建该模型,加载了数据并且还将数据训练到了该模型。我们仍然需要评估模型并预测未知输入的输出,我们将在即将到来的章节中学习。

We have created the model, loaded the data and also trained the data to the model. We still need to evaluate the model and predict output for unknown input, which we learn in upcoming chapter.

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
import numpy as np

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape = (784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation = 'relu')) model.add(Dropout(0.2))
model.add(Dense(10, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy',
   optimizer = RMSprop(),
   metrics = ['accuracy'])

history = model.fit(x_train, y_train,
   batch_size = 128, epochs = 20, verbose = 1, validation_data = (x_test, y_test))

执行该应用程序将给出以下内容作为输出 −

Executing the application will give the below content as output −

Train on 60000 samples, validate on 10000 samples Epoch 1/20
60000/60000 [==============================] - 7s 118us/step - loss: 0.2453
- acc: 0.9236 - val_loss: 0.1004 - val_acc: 0.9675 Epoch 2/20
60000/60000 [==============================] - 7s 110us/step - loss: 0.1023
- acc: 0.9693 - val_loss: 0.0797 - val_acc: 0.9761 Epoch 3/20
60000/60000 [==============================] - 7s 110us/step - loss: 0.0744
- acc: 0.9770 - val_loss: 0.0727 - val_acc: 0.9791 Epoch 4/20
60000/60000 [==============================] - 7s 110us/step - loss: 0.0599
- acc: 0.9823 - val_loss: 0.0704 - val_acc: 0.9801 Epoch 5/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0504
- acc: 0.9853 - val_loss: 0.0714 - val_acc: 0.9817 Epoch 6/20
60000/60000 [==============================] - 7s 111us/step - loss: 0.0438
- acc: 0.9868 - val_loss: 0.0845 - val_acc: 0.9809 Epoch 7/20
60000/60000 [==============================] - 7s 114us/step - loss: 0.0391
- acc: 0.9887 - val_loss: 0.0823 - val_acc: 0.9802 Epoch 8/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0364
- acc: 0.9892 - val_loss: 0.0818 - val_acc: 0.9830 Epoch 9/20
60000/60000 [==============================] - 7s 113us/step - loss: 0.0308
- acc: 0.9905 - val_loss: 0.0833 - val_acc: 0.9829 Epoch 10/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0289
- acc: 0.9917 - val_loss: 0.0947 - val_acc: 0.9815 Epoch 11/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0279
- acc: 0.9921 - val_loss: 0.0818 - val_acc: 0.9831 Epoch 12/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0260
- acc: 0.9927 - val_loss: 0.0945 - val_acc: 0.9819 Epoch 13/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0257
- acc: 0.9931 - val_loss: 0.0952 - val_acc: 0.9836 Epoch 14/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0229
- acc: 0.9937 - val_loss: 0.0924 - val_acc: 0.9832 Epoch 15/20
60000/60000 [==============================] - 7s 115us/step - loss: 0.0235
- acc: 0.9937 - val_loss: 0.1004 - val_acc: 0.9823 Epoch 16/20
60000/60000 [==============================] - 7s 113us/step - loss: 0.0214
- acc: 0.9941 - val_loss: 0.0991 - val_acc: 0.9847 Epoch 17/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0219
- acc: 0.9943 - val_loss: 0.1044 - val_acc: 0.9837 Epoch 18/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0190
- acc: 0.9952 - val_loss: 0.1129 - val_acc: 0.9836 Epoch 19/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0197
- acc: 0.9953 - val_loss: 0.0981 - val_acc: 0.9841 Epoch 20/20
60000/60000 [==============================] - 7s 112us/step - loss: 0.0198
- acc: 0.9950 - val_loss: 0.1215 - val_acc: 0.9828

Keras - Model Evaluation and Model Prediction

本章介绍 Keras 中的模型评估和模型预测。

This chapter deals with the model evaluation and model prediction in Keras.

让我们从了解模型评估开始。

Let us begin by understanding the model evaluation.

Model Evaluation

评估是模型开发过程中的一个过程,用于检查模型是否最适合给定的问题和对应数据。Keras 模型提供了一个函数,evaluate,它对模型进行评估。它有三个主要参数:

Evaluation is a process during development of the model to check whether the model is best fit for the given problem and corresponding data. Keras model provides a function, evaluate which does the evaluation of the model. It has three main arguments,

  1. Test data

  2. Test data label

  3. verbose - true or false

让我们评估在上一章中使用测试数据创建的模型。

Let us evaluate the model, which we created in the previous chapter using test data.

score = model.evaluate(x_test, y_test, verbose = 0)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

执行上述代码将输出以下信息。

Executing the above code will output the below information.

0

测试准确率为 98.28%。我们创建了一个最佳模型来识别手写数字。从积极的一面来说,我们仍然可以改善我们的模型。

The test accuracy is 98.28%. We have created a best model to identify the handwriting digits. On the positive side, we can still scope to improve our model.

Model Prediction

Prediction 是最后一步,也是我们对模型生成的预期结果。Keras 提供了一个方法,predict,以获取训练模型的预测。predict 方法的特征如下:

Prediction is the final step and our expected outcome of the model generation. Keras provides a method, predict to get the prediction of the trained model. The signature of the predict method is as follows,

predict(
   x,
   batch_size = None,
   verbose = 0,
   steps = None,
   callbacks = None,
   max_queue_size = 10,
   workers = 1,
   use_multiprocessing = False
)

此处,除了第一个参数(引用未知输入数据)以外,所有参数都是可选的。应该保持形状以获得正确的预测。

Here, all arguments are optional except the first argument, which refers the unknown input data. The shape should be maintained to get the proper prediction.

让我们使用以下代码对在上一章中创建的 MPL 模型进行预测 −

Let us do prediction for our MPL model created in previous chapter using below code −

pred = model.predict(x_test)
pred = np.argmax(pred, axis = 1)[:5]
label = np.argmax(y_test,axis = 1)[:5]

print(pred)
print(label)

在此,

Here,

  1. Line 1 call the predict function using test data.

  2. Line 2 gets the first five prediction

  3. Line 3 gets the first five labels of the test data.

  4. Line 5 - 6 prints the prediction and actual label.

上述应用程序的输出如下 −

The output of the above application is as follows −

[7 2 1 0 4]
[7 2 1 0 4]

这两个数组的输出是相同的,这表明我们的模型正确预测了前五个图像。

The output of both array is identical and it indicate that our model predicts correctly the first five images.

Keras - Convolution Neural Network

让我们将模型从 MPL 修改为 Convolution Neural Network (CNN) 以解决我们先前的数字识别问题。

Let us modify the model from MPL to Convolution Neural Network (CNN) for our earlier digit identification problem.

CNN 可以表示如下 −

CNN can be represented as below −

convolution neural network

该模型的核心功能如下:

The core features of the model are as follows −

  1. Input layer consists of (1, 8, 28) values.

  2. First layer, Conv2D consists of 32 filters and ‘relu’ activation function with kernel size, (3,3).

  3. Second layer, Conv2D consists of 64 filters and ‘relu’ activation function with kernel size, (3,3).

  4. Thrid layer, MaxPooling has pool size of (2, 2).

  5. Fifth layer, Flatten is used to flatten all its input into single dimension.

  6. Sixth layer, Dense consists of 128 neurons and ‘relu’ activation function.

  7. Seventh layer, Dropout has 0.5 as its value.

  8. Eighth and final layer consists of 10 neurons and ‘softmax’ activation function.

  9. Use categorical_crossentropy as loss function.

  10. Use Adadelta() as Optimizer.

  11. Use accuracy as metrics.

  12. Use 128 as batch size.

  13. Use 20 as epochs.

Step 1 − Import the modules

Step 1 − Import the modules

让我们导入必需的模块。

Let us import the necessary modules.

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np

Step 2 − Load data

Step 2 − Load data

让我们导入 mnist 数据集。

Let us import the mnist dataset.

(x_train, y_train), (x_test, y_test) = mnist.load_data()

Step 3 − Process the data

Step 3 − Process the data

让我们根据模型修改数据集,以便可以将其馈送至我们的模型。

Let us change the dataset according to our model, so that it can be feed into our model.

img_rows, img_cols = 28, 28

if K.image_data_format() == 'channels_first':
   x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
   x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
   input_shape = (1, img_rows, img_cols)
else:
   x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
   x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
   input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

数据处理与 MPL 模型类似,除了输入数据形状和图像格式配置。

The data processing is similar to MPL model except the shape of the input data and image format configuration.

Step 4 − Create the model

Step 4 − Create the model

让我们创建实际模型。

Let us create tha actual model.

model = Sequential()
model.add(Conv2D(32, kernel_size = (3, 3),
   activation = 'relu', input_shape = input_shape))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25)) model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))

Step 5 − Compile the model

Step 5 − Compile the model

让我们使用选定的损失函数、优化器和指标来编译模型。

Let us compile the model using selected loss function, optimizer and metrics.

model.compile(loss = keras.losses.categorical_crossentropy,
   optimizer = keras.optimizers.Adadelta(), metrics = ['accuracy'])

Step 6 − Train the model

Step 6 − Train the model

让我们使用 fit() 方法训练模型。

Let us train the model using fit() method.

model.fit(
   x_train, y_train,
   batch_size = 128,
   epochs = 12,
   verbose = 1,
   validation_data = (x_test, y_test)
)

执行该应用程序将输出以下信息 −

Executing the application will output the below information −

Train on 60000 samples, validate on 10000 samples Epoch 1/12
60000/60000 [==============================] - 84s 1ms/step - loss: 0.2687
- acc: 0.9173 - val_loss: 0.0549 - val_acc: 0.9827 Epoch 2/12
60000/60000 [==============================] - 86s 1ms/step - loss: 0.0899
- acc: 0.9737 - val_loss: 0.0452 - val_acc: 0.9845 Epoch 3/12
60000/60000 [==============================] - 83s 1ms/step - loss: 0.0666
- acc: 0.9804 - val_loss: 0.0362 - val_acc: 0.9879 Epoch 4/12
60000/60000 [==============================] - 81s 1ms/step - loss: 0.0564
- acc: 0.9830 - val_loss: 0.0336 - val_acc: 0.9890 Epoch 5/12
60000/60000 [==============================] - 86s 1ms/step - loss: 0.0472
- acc: 0.9861 - val_loss: 0.0312 - val_acc: 0.9901 Epoch 6/12
60000/60000 [==============================] - 83s 1ms/step - loss: 0.0414
- acc: 0.9877 - val_loss: 0.0306 - val_acc: 0.9902 Epoch 7/12
60000/60000 [==============================] - 89s 1ms/step - loss: 0.0375
-acc: 0.9883 - val_loss: 0.0281 - val_acc: 0.9906 Epoch 8/12
60000/60000 [==============================] - 91s 2ms/step - loss: 0.0339
- acc: 0.9893 - val_loss: 0.0280 - val_acc: 0.9912 Epoch 9/12
60000/60000 [==============================] - 89s 1ms/step - loss: 0.0325
- acc: 0.9901 - val_loss: 0.0260 - val_acc: 0.9909 Epoch 10/12
60000/60000 [==============================] - 89s 1ms/step - loss: 0.0284
- acc: 0.9910 - val_loss: 0.0250 - val_acc: 0.9919 Epoch 11/12
60000/60000 [==============================] - 86s 1ms/step - loss: 0.0287
- acc: 0.9907 - val_loss: 0.0264 - val_acc: 0.9916 Epoch 12/12
60000/60000 [==============================] - 86s 1ms/step - loss: 0.0265
- acc: 0.9920 - val_loss: 0.0249 - val_acc: 0.9922

Step 7 − Evaluate the model

Step 7 − Evaluate the model

让我们使用测试数据评估模型。

Let us evaluate the model using test data.

score = model.evaluate(x_test, y_test, verbose = 0)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

执行以上代码将会输出以下信息−

Executing the above code will output the below information −

Test loss: 0.024936060590433316
Test accuracy: 0.9922

测试准确率为 99.22%。我们已创建用于识别手写数字的最佳模型。

The test accuracy is 99.22%. We have created a best model to identify the handwriting digits.

Step 8 − Predict

Step 8 − Predict

最后,从图像预测数字,如下所示 −

Finally, predict the digit from images as below −

pred = model.predict(x_test)
pred = np.argmax(pred, axis = 1)[:5]
label = np.argmax(y_test,axis = 1)[:5]

print(pred)
print(label)

上述应用程序的输出如下 −

The output of the above application is as follows −

[7 2 1 0 4]
[7 2 1 0 4]

两个数组的输出完全相同,表明我们的模型正确预测了前五个图像。

The output of both array is identical and it indicate our model correctly predicts the first five images.

Keras - Regression Prediction using MPL

在本章中,让我们编写一个简单的基于 MPL 的 ANN 来进行回归预测。到目前为止,我们只完成了基于分类的预测。现在,我们将尝试通过分析先前(连续)值及其影响因素来预测下一个可能的值。

In this chapter, let us write a simple MPL based ANN to do regression prediction. Till now, we have only done the classification based prediction. Now, we will try to predict the next possible value by analyzing the previous (continuous) values and its influencing factors.

回归 MPL 可以表示为以下内容:

The Regression MPL can be represented as below −

mpl

该模型的核心功能如下:

The core features of the model are as follows −

  1. Input layer consists of (13,) values.

  2. First layer, Dense consists of 64 units and ‘relu’ activation function with ‘normal’ kernel initializer.

  3. Second layer, Dense consists of 64 units and ‘relu’ activation function.

  4. Output layer, Dense consists of 1 unit.

  5. Use mse as loss function.

  6. Use RMSprop as Optimizer.

  7. Use accuracy as metrics.

  8. Use 128 as batch size.

  9. Use 500 as epochs.

Step 1 − Import the modules

Step 1 − Import the modules

让我们导入必需的模块。

Let us import the necessary modules.

import keras

from keras.datasets import boston_housing
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping
from sklearn import preprocessing
from sklearn.preprocessing import scale

Step 2 − Load data

Step 2 − Load data

让我们导入 Boston 住房数据集。

Let us import the Boston housing dataset.

(x_train, y_train), (x_test, y_test) = boston_housing.load_data()

在此,

Here,

boston_housing 是由Keras提供的用于数据集。它表示波士顿地区房价信息的一个集合,其中每条信息有13个特性。

boston_housing is a dataset provided by Keras. It represents a collection of housing information in Boston area, each having 13 features.

Step 3 − Process the data

Step 3 − Process the data

让我们根据我们的模型改变数据集,以便我们可以将它送入我们的模型。可以使用以下代码来改变数据−

Let us change the dataset according to our model, so that, we can feed into our model. The data can be changed using below code −

x_train_scaled = preprocessing.scale(x_train)
scaler = preprocessing.StandardScaler().fit(x_train)
x_test_scaled = scaler.transform(x_test)

在这里,我们已经使用 sklearn.preprocessing.scale 函数来规范化训练数据。 preprocessing.StandardScaler().fit 函数返回一个带有训练数据规范化均值和标准偏差的标量,我们可以使用 scalar.transform 函数将其应用于测试数据。这样会使用和训练数据中一样的设定来规范化测试数据。

Here, we have normalized the training data using sklearn.preprocessing.scale function. preprocessing.StandardScaler().fit function returns a scalar with the normalized mean and standard deviation of the training data, which we can apply to the test data using scalar.transform function. This will normalize the test data as well with the same setting as that of training data.

Step 4 − Create the model

Step 4 − Create the model

让我们创建实际模型。

Let us create the actual model.

model = Sequential()
model.add(Dense(64, kernel_initializer = 'normal', activation = 'relu',
input_shape = (13,)))
model.add(Dense(64, activation = 'relu')) model.add(Dense(1))

Step 5 − Compile the model

Step 5 − Compile the model

让我们使用选定的损失函数、优化器和指标来编译模型。

Let us compile the model using selected loss function, optimizer and metrics.

model.compile(
   loss = 'mse',
   optimizer = RMSprop(),
   metrics = ['mean_absolute_error']
)

Step 6 − Train the model

Step 6 − Train the model

让我们使用 fit() 方法训练模型。

Let us train the model using fit() method.

history = model.fit(
   x_train_scaled, y_train,
   batch_size=128,
   epochs = 500,
   verbose = 1,
   validation_split = 0.2,
   callbacks = [EarlyStopping(monitor = 'val_loss', patience = 20)]
)

在这里,我们使用了回调函数 EarlyStopping 。此回调函数的目的是监控每个时期中的损失值,并将其与前一个时期中的损失值进行对比,以寻找训练中的改进。如果 patience 次都没有改进,则会停止整个进程。

Here, we have used callback function, EarlyStopping. The purpose of this callback is to monitor the loss value during each epoch and compare it with previous epoch loss value to find the improvement in the training. If there is no improvement for the patience times, then the whole process will be stopped.

执行应用程序将给出如下的信息作为输出−

Executing the application will give the below information as output −

Train on 323 samples, validate on 81 samples Epoch 1/500 2019-09-24 01:07:03.889046: I
tensorflow/core/platform/cpu_feature_guard.cc:142]
Your CPU supports instructions that this
TensorFlow binary was not co mpiled to use: AVX2 323/323
[==============================] - 0s 515us/step - loss: 562.3129
- mean_absolute_error: 21.8575 - val_loss: 621.6523 - val_mean_absolute_erro
r: 23.1730 Epoch 2/500
323/323 [==============================] - 0s 11us/step - loss: 545.1666
- mean_absolute_error: 21.4887 - val_loss: 605.1341 - val_mean_absolute_error
: 22.8293 Epoch 3/500
323/323 [==============================] - 0s 12us/step - loss: 528.9944
- mean_absolute_error: 21.1328 - val_loss: 588.6594 - val_mean_absolute_error
: 22.4799 Epoch 4/500
323/323 [==============================] - 0s 12us/step - loss: 512.2739
- mean_absolute_error: 20.7658 - val_loss: 570.3772 - val_mean_absolute_error
: 22.0853 Epoch 5/500
323/323 [==============================] - 0s 9us/step - loss: 493.9775
- mean_absolute_error: 20.3506 - val_loss: 550.9548 - val_mean_absolute_error: 21.6547
..........
..........
..........
Epoch 143/500
323/323 [==============================] - 0s 15us/step - loss: 8.1004
- mean_absolute_error: 2.0002 - val_loss: 14.6286 - val_mean_absolute_error:
2. 5904 Epoch 144/500
323/323 [==============================] - 0s 19us/step - loss: 8.0300
- mean_absolute_error: 1.9683 - val_loss: 14.5949 - val_mean_absolute_error:
2. 5843 Epoch 145/500
323/323 [==============================] - 0s 12us/step - loss: 7.8704
- mean_absolute_error: 1.9313 - val_loss: 14.3770 - val_mean_absolute_error: 2. 4996

Step 7 − Evaluate the model

Step 7 − Evaluate the model

让我们使用测试数据评估模型。

Let us evaluate the model using test data.

score = model.evaluate(x_test_scaled, y_test, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

执行以上代码将会输出以下信息−

Executing the above code will output the below information −

Test loss: 21.928471583946077 Test accuracy: 2.9599233234629914

Step 7 − Evaluate the model

Step 7 − Evaluate the model

让我们使用测试数据评估模型。

Let us evaluate the model using test data.

score = model.evaluate(x_test_scaled, y_test, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

执行以上代码将会输出以下信息−

Executing the above code will output the below information −

Test loss: 21.928471583946077
Test accuracy: 2.9599233234629914

Step 8 − Predict

Step 8 − Predict

最后,如下预测使用测试数据−

Finally, predict using test data as below −

prediction = model.predict(x_test_scaled)
print(prediction.flatten())
print(y_test)

上述应用程序的输出如下 −

The output of the above application is as follows −

[ 7.5612316 17.583357 21.09344 31.859276 25.055613 18.673872 26.600405 22.403967 19.060272 22.264952
17.4191 17.00466 15.58924 41.624374 20.220217 18.985565 26.419338 19.837091 19.946192 36.43445
12.278508 16.330965 20.701359 14.345301 21.741161 25.050423 31.046402 27.738455 9.959419 20.93039
20.069063 14.518344 33.20235 24.735163 18.7274 9.148898 15.781284 18.556862 18.692865 26.045074
27.954073 28.106823 15.272034 40.879818 29.33896 23.714525 26.427515 16.483374 22.518442 22.425386
33.94826 18.831465 13.2501955 15.537227 34.639984 27.468002 13.474407 48.134598 34.39617
22.8503124.042334 17.747198 14.7837715 18.187277 23.655672 22.364983 13.858193 22.710032 14.371148
7.1272087 35.960033 28.247292 25.3014 14.477208 25.306196 17.891165 20.193708 23.585173 34.690193
12.200583 20.102983 38.45882 14.741723 14.408362 17.67158 18.418497 21.151712 21.157492 22.693687
29.809034 19.366991 20.072294 25.880817 40.814568 34.64087 19.43741 36.2591 50.73806 26.968863 43.91787
32.54908 20.248306 ] [ 7.2 18.8 19. 27. 22.2 24.5 31.2 22.9 20.5 23.2 18.6 14.5 17.8 50. 20.8 24.3 24.2
19.8 19.1 22.7 12. 10.2 20. 18.5 20.9 23. 27.5 30.1 9.5 22. 21.2 14.1 33.1 23.4 20.1 7.4 15.4 23.8 20.1
24.5 33. 28.4 14.1 46.7 32.5 29.6 28.4 19.8 20.2 25. 35.4 20.3 9.7 14.5 34.9 26.6 7.2 50. 32.4 21.6 29.8
13.1 27.5 21.2 23.1 21.9 13. 23.2 8.1 5.6 21.7 29.6 19.6 7. 26.4 18.9 20.9 28.1 35.4 10.2 24.3 43.1 17.6
15.4 16.2 27.1 21.4 21.5 22.4 25. 16.6 18.6 22. 42.8 35.1 21.5 36. 21.9 24.1 50. 26.7 25. ]

两个数组的输出大约有10-30%的差异,这表示我们的模型预测在合理范围内。

The output of both array have around 10-30% difference and it indicate our model predicts with reasonable range.

Keras - Time Series Prediction using LSTM RNN

在本章中,我们将编写一个基于简单长短期记忆 (LSTM) 的 RNN 来执行序列分析。序列是一组值,其中每个值对应于时间的一个特定实例。我们考虑一个简单的阅读句子示例。阅读理解一个句子包括按给定顺序阅读单词并尝试理解给定语境中的每个单词及其含义,最后以积极或消极的情感理解句子。

In this chapter, let us write a simple Long Short Term Memory (LSTM) based RNN to do sequence analysis. A sequence is a set of values where each value corresponds to a particular instance of time. Let us consider a simple example of reading a sentence. Reading and understanding a sentence involves reading the word in the given order and trying to understand each word and its meaning in the given context and finally understanding the sentence in a positive or negative sentiment.

在此处,单词被视为值,并且第一个值对应于第一个单词,第二个值对应于第二个单词,等等,并严格保持顺序。 Sequence Analysis 在自然语言处理中经常用于寻找给定文本的情感分析。

Here, the words are considered as values, and first value corresponds to first word, second value corresponds to second word, etc., and the order will be strictly maintained. Sequence Analysis is used frequently in natural language processing to find the sentiment analysis of the given text.

让我们创建一个 LSTM 模型来分析 IMDB 电影评论并找出其正面/负面情绪。

Let us create a LSTM model to analyze the IMDB movie reviews and find its positive/negative sentiment.

用于序列分析的模型可以表示为以下内容 −

The model for the sequence analysis can be represented as below −

sequence analysis

该模型的核心功能如下:

The core features of the model are as follows −

  1. Input layer using Embedding layer with 128 features.

  2. First layer, Dense consists of 128 units with normal dropout and recurrent dropout set to 0.2.

  3. Output layer, Dense consists of 1 unit and ‘sigmoid’ activation function.

  4. Use binary_crossentropy as loss function.

  5. Use adam as Optimizer.

  6. Use accuracy as metrics.

  7. Use 32 as batch size.

  8. Use 15 as epochs.

  9. Use 80 as the maximum length of the word.

  10. Use 2000 as the maximum number of word in a given sentence.

Step 1: Import the modules

让我们导入必需的模块。

Let us import the necessary modules.

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM
from keras.datasets import imdb

Step 2: Load data

我们导入 imdb 数据集。

Let us import the imdb dataset.

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words = 2000)

在此,

Here,

  1. imdb is a dataset provided by Keras. It represents a collection of movies and its reviews.

  2. num_words represent the maximum number of words in the review.

Step 3: Process the data

让我们根据模型更改数据集,以便可以将其输入到模型中。可以使用以下代码更改数据 −

Let us change the dataset according to our model, so that it can be fed into our model. The data can be changed using the below code −

x_train = sequence.pad_sequences(x_train, maxlen=80)
x_test = sequence.pad_sequences(x_test, maxlen=80)

在此,

Here,

sequence.pad_sequences 将形状为 (data) 的输入数据列表转换为形状为 (data, timesteps) 的 2D NumPy 数组。它基本上将时间步概念添加到给定数据中。它生成长度为 maxlen 的时间步。

sequence.pad_sequences convert the list of input data with shape, (data) into 2D NumPy array of shape (data, timesteps). Basically, it adds timesteps concept into the given data. It generates the timesteps of length, maxlen.

Step 4: Create the model

让我们创建实际模型。

Let us create the actual model.

model = Sequential()
model.add(Embedding(2000, 128))
model.add(LSTM(128, dropout = 0.2, recurrent_dropout = 0.2))
model.add(Dense(1, activation = 'sigmoid'))

在此,

Here,

我们使用 Embedding layer 作为输入层,然后添加 LSTM 层。最后,使用 Dense layer 作为输出层。

We have used Embedding layer as input layer and then added the LSTM layer. Finally, a Dense layer is used as output layer.

Step 5: Compile the model

让我们使用选定的损失函数、优化器和指标来编译模型。

Let us compile the model using selected loss function, optimizer and metrics.

model.compile(loss = 'binary_crossentropy',
   optimizer = 'adam', metrics = ['accuracy'])

Step 6: Train the model

让我们使用 fit() 方法训练模型。

LLet us train the model using fit() method.

model.fit(
   x_train, y_train,
   batch_size = 32,
   epochs = 15,
   validation_data = (x_test, y_test)
)

执行该应用程序将输出以下信息 −

Executing the application will output the below information −

Epoch 1/15 2019-09-24 01:19:01.151247: I
tensorflow/core/platform/cpu_feature_guard.cc:142]
Your CPU supports instructions that this
TensorFlow binary was not co mpiled to use: AVX2
25000/25000 [==============================] - 101s 4ms/step - loss: 0.4707
- acc: 0.7716 - val_loss: 0.3769 - val_acc: 0.8349 Epoch 2/15
25000/25000 [==============================] - 95s 4ms/step - loss: 0.3058
- acc: 0.8756 - val_loss: 0.3763 - val_acc: 0.8350 Epoch 3/15
25000/25000 [==============================] - 91s 4ms/step - loss: 0.2100
- acc: 0.9178 - val_loss: 0.5065 - val_acc: 0.8110 Epoch 4/15
25000/25000 [==============================] - 90s 4ms/step - loss: 0.1394
- acc: 0.9495 - val_loss: 0.6046 - val_acc: 0.8146 Epoch 5/15
25000/25000 [==============================] - 90s 4ms/step - loss: 0.0973
- acc: 0.9652 - val_loss: 0.5969 - val_acc: 0.8147 Epoch 6/15
25000/25000 [==============================] - 98s 4ms/step - loss: 0.0759
- acc: 0.9730 - val_loss: 0.6368 - val_acc: 0.8208 Epoch 7/15
25000/25000 [==============================] - 95s 4ms/step - loss: 0.0578
- acc: 0.9811 - val_loss: 0.6657 - val_acc: 0.8184 Epoch 8/15
25000/25000 [==============================] - 97s 4ms/step - loss: 0.0448
- acc: 0.9850 - val_loss: 0.7452 - val_acc: 0.8136 Epoch 9/15
25000/25000 [==============================] - 95s 4ms/step - loss: 0.0324
- acc: 0.9894 - val_loss: 0.7616 - val_acc: 0.8162Epoch 10/15
25000/25000 [==============================] - 100s 4ms/step - loss: 0.0247
- acc: 0.9922 - val_loss: 0.9654 - val_acc: 0.8148 Epoch 11/15
25000/25000 [==============================] - 99s 4ms/step - loss: 0.0169
- acc: 0.9946 - val_loss: 1.0013 - val_acc: 0.8104 Epoch 12/15
25000/25000 [==============================] - 90s 4ms/step - loss: 0.0154
- acc: 0.9948 - val_loss: 1.0316 - val_acc: 0.8100 Epoch 13/15
25000/25000 [==============================] - 89s 4ms/step - loss: 0.0113
- acc: 0.9963 - val_loss: 1.1138 - val_acc: 0.8108 Epoch 14/15
25000/25000 [==============================] - 89s 4ms/step - loss: 0.0106
- acc: 0.9971 - val_loss: 1.0538 - val_acc: 0.8102 Epoch 15/15
25000/25000 [==============================] - 89s 4ms/step - loss: 0.0090
- acc: 0.9972 - val_loss: 1.1453 - val_acc: 0.8129
25000/25000 [==============================] - 10s 390us/step

Step 7 − Evaluate the model

让我们使用测试数据评估模型。

Let us evaluate the model using test data.

score, acc = model.evaluate(x_test, y_test, batch_size = 32)

print('Test score:', score)
print('Test accuracy:', acc)

执行以上代码将会输出以下信息−

Executing the above code will output the below information −

Test score: 1.145306069601178
Test accuracy: 0.81292

Keras - Applications

Keras 应用程序模块用于为深度神经网络提供预训练模型。Keras 模型用于预测、特征提取和微调。本章详细解释了 Keras 应用程序。

Keras applications module is used to provide pre-trained model for deep neural networks. Keras models are used for prediction, feature extraction and fine tuning. This chapter explains about Keras applications in detail.

Pre-trained models

训练好的模型由两个部分组成,模型架构和模型权重。模型权重是大型文件,因此我们必须从 ImageNet 数据库下载和提取特征。下面列出了一些流行的预训练模型:

Trained model consists of two parts model Architecture and model Weights. Model weights are large file so we have to download and extract the feature from ImageNet database. Some of the popular pre-trained models are listed below,

  1. ResNet

  2. VGG16

  3. MobileNet

  4. InceptionResNetV2

  5. InceptionV3

Loading a model

Keras 预训练模型可以轻松加载,如下所示:

Keras pre-trained models can be easily loaded as specified below −

import keras
import numpy as np

from keras.applications import vgg16, inception_v3, resnet50, mobilenet

#Load the VGG model
vgg_model = vgg16.VGG16(weights = 'imagenet')

#Load the Inception_V3 model
inception_model = inception_v3.InceptionV3(weights = 'imagenet')

#Load the ResNet50 model
resnet_model = resnet50.ResNet50(weights = 'imagenet')

#Load the MobileNet model mobilenet_model = mobilenet.MobileNet(weights = 'imagenet')

一旦加载了该模型,我们就可以立即将其用于预测目的。让我们在即将到来的章节中查看每个预训练模型。

Once the model is loaded, we can immediately use it for prediction purpose. Let us check each pre-trained model in the upcoming chapters.

Real Time Prediction using ResNet Model

ResNet 是一个预训练模型,它使用 ImageNet 进行了训练。在 ImageNet 上预训练了 ResNet 模型权重。它的语法如下所示:

ResNet is a pre-trained model. It is trained using ImageNet. ResNet model weights pre-trained on ImageNet. It has the following syntax −

keras.applications.resnet.ResNet50 (
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None,
   classes = 1000
)

在此,

Here,

  1. include_top refers the fully-connected layer at the top of the network.

  2. weights refer pre-training on ImageNet.

  3. input_tensor refers optional Keras tensor to use as image input for the model.

  4. input_shape refers optional shape tuple. The default input size for this model is 224x224.

  5. classes refer optional number of classes to classify images.

让我们通过写一个简单的示例来了解该模型:

Let us understand the model by writing a simple example −

Step 1: import the modules

让我们加载必要的模块,如下所示:

Let us load the necessary modules as specified below −

>>> import PIL
>>> from keras.preprocessing.image import load_img
>>> from keras.preprocessing.image import img_to_array
>>> from keras.applications.imagenet_utils import decode_predictions
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from keras.applications.resnet50 import ResNet50
>>> from keras.applications import resnet50

Step 2: Select an input

让我们选择一个输入图像, Lotus 如下所示:

Let us choose an input image, Lotus as specified below −

>>> filename = 'banana.jpg'
>>> ## load an image in PIL format
>>> original = load_img(filename, target_size = (224, 224))
>>> print('PIL image size',original.size)
PIL image size (224, 224)
>>> plt.imshow(original)
<matplotlib.image.AxesImage object at 0x1304756d8>
>>> plt.show()

在这里,我们加载了一张图像 (banana.jpg) 并显示了它。

Here, we have loaded an image (banana.jpg) and displayed it.

Step 3: Convert images into NumPy array

让我们将我们的输入 Banana 转换成 NumPy 数组,以便将其传递到模型中以进行预测。

Let us convert our input, Banana into NumPy array, so that it can be passed into the model for the purpose of prediction.

>>> #convert the PIL image to a numpy array
>>> numpy_image = img_to_array(original)

>>> plt.imshow(np.uint8(numpy_image))
<matplotlib.image.AxesImage object at 0x130475ac8>

>>> print('numpy array size',numpy_image.shape)
numpy array size (224, 224, 3)

>>> # Convert the image / images into batch format
>>> image_batch = np.expand_dims(numpy_image, axis = 0)

>>> print('image batch size', image_batch.shape)
image batch size (1, 224, 224, 3)
>>>

Step 4: Model prediction

让我们将我们的输入输入模型以获取预测

Let us feed our input into the model to get the predictions

>>> prepare the image for the resnet50 model >>>
>>> processed_image = resnet50.preprocess_input(image_batch.copy())

>>> # create resnet model
>>>resnet_model = resnet50.ResNet50(weights = 'imagenet')
>>> Downloavding data from https://github.com/fchollet/deep-learning-models/releas
es/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
102858752/102853048 [==============================] - 33s 0us/step

>>> # get the predicted probabilities for each class
>>> predictions = resnet_model.predict(processed_image)

>>> # convert the probabilities to class labels
>>> label = decode_predictions(predictions)
Downloading data from https://storage.googleapis.com/download.tensorflow.org/
data/imagenet_class_index.json
40960/35363 [==================================] - 0s 0us/step

>>> print(label)

Output

[
   [
      ('n07753592', 'banana', 0.99229723),
      ('n03532672', 'hook', 0.0014551596),
      ('n03970156', 'plunger', 0.0010738898),
      ('n07753113', 'fig', 0.0009359837) ,
      ('n03109150', 'corkscrew', 0.00028538404)
   ]
]

在这里,该模型正确地预测了香蕉图像。

Here, the model predicted the images as banana correctly.

Keras - Pre-Trained Models

在本章中,我们将学习 Keras 中的预训练模型。让我们从 VGG16 开始。

In this chapter, we will learn about the pre-trained models in Keras. Let us begin with VGG16.

VGG16

VGG16 是另一个预训练模型。它也是使用 ImageNet 训练的。加载模型的语法如下:

VGG16 is another pre-trained model. It is also trained using ImageNet. The syntax to load the model is as follows −

keras.applications.vgg16.VGG16(
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None,
   classes = 1000
)

该模型的默认输入大小为 224x224。

The default input size for this model is 224x224.

MobileNetV2

MobileNetV2 是另一个预训练模型。它也是使用 ImageNet 训练的。

MobileNetV2 is another pre-trained model. It is also trained uing ImageNet.

加载模型的语法如下:

The syntax to load the model is as follows −

keras.applications.mobilenet_v2.MobileNetV2 (
   input_shape = None,
   alpha = 1.0,
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   pooling = None,
   classes = 1000
)

在此,

Here,

alpha 控制网络的宽度。如果值低于 1,则减少每一层中的滤波器数量。如果值高于 1,则增加每一层中的滤波器数量。如果 alpha = 1,则每一层都使用来自论文的默认滤波器数量。

alpha controls the width of the network. If the value is below 1, decreases the number of filters in each layer. If the value is above 1, increases the number of filters in each layer. If alpha = 1, default number of filters from the paper are used at each layer.

该模型的默认输入大小为 224x224

The default input size for this model is 224x224.

InceptionResNetV2

InceptionResNetV2 是另一个预训练模型。它也是使用 ImageNet 训练的。加载模型的语法如下:

InceptionResNetV2 is another pre-trained model. It is also trained using ImageNet. The syntax to load the model is as follows −

keras.applications.inception_resnet_v2.InceptionResNetV2 (
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None,
   classes = 1000)

此模型可以采用“channels_first”数据格式(通道数、高度、宽度)或“channels_last”数据格式(高度、宽度、通道数)构建。

This model and can be built both with ‘channels_first’ data format (channels, height, width) or ‘channels_last’ data format (height, width, channels).

此模型的默认输入大小为 299x299

The default input size for this model is 299x299.

InceptionV3

InceptionV3 是另一个预训练模型。它也是使用 ImageNet 训练的。加载模型的语法如下所示 −

InceptionV3 is another pre-trained model. It is also trained uing ImageNet. The syntax to load the model is as follows −

keras.applications.inception_v3.InceptionV3 (
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None,
   classes = 1000
)

在此,

Here,

此模型的默认输入大小为 299x299

The default input size for this model is 299x299.

Conclusion

Keras 是非常简单、可扩展且易于实现的神经网络 API,可用于构建具有高级抽象的高级学习应用程序。Keras 是深度学习模型的最佳选择。

Keras is very simple, extensible and easy to implement neural network API, which can be used to build deep learning applications with high level abstraction. Keras is an optimal choice for deep leaning models.