找回密码
 立即注册
查看: 310|回复: 0

keras安装与配置指南_Keras-快速指南

[复制链接]
发表于 2022-6-24 14:58 | 显示全部楼层 |阅读模式

[img=403px,256px]https://img-blog.csdnimg.cn/img_convert/d92968972ba1a7762b06acfe1a237f05.png[/img]


keras安装与配置指南
Keras-快速指南 (Keras - Quick Guide)

Keras-简介 (Keras - Introduction )

Deep learning is one of the major subfield of machine learning framework. Machine learning is the study of design of algorithms, inspired from the model of human brain. Deep learning is becoming more popular in data science fields like robotics, artificial intelligence(AI), audio & video recognition and image recognition. Artificial neural network is the core of deep learning methodologies. Deep learning is supported by various libraries such as Theano, TensorFlow, Caffe, Mxnet etc., Keras is one of the most powerful and easy to use python library, which is built on top of popular deep learning libraries like TensorFlow, Theano, etc., for creating deep learning models.
深度学习是机器学习框架的主要子领域之一。 机器学习是受人脑模型启发而进行的算法设计研究。 深度学习在机器人,人工智能(AI),音频和视频识别以及图像识别等数据科学领域正变得越来越流行。 人工神经网络是深度学习方法的核心。 深度学习受到Theano,TensorFlow,Caffe,Mxnet等各种库的支持,Keras是功能最强大且易于使用的python库之一,它基于流行的深度学习库如TensorFlow,Theano等构建。 ,用于创建深度学习模型。
Keras概述 (Overview of Keras)

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或Cognitive Toolkit(CNTK)等开源机器库之上运行。 Theano是用于快速数值计算任务的python库。 TensorFlow是用于创建神经网络和深度学习模型的最著名的符号数学库。 TensorFlow非常灵活,主要优势是分布式计算。 CNTK是Microsoft开发的深度学习框架。 它使用Python,C#,C ++或独立的机器学习工具包之类的库。 Theano和TensorFlow是非常强大的库,但是对于创建神经网络却很难理解。
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.
Keras基于最小的结构,它提供了一种干净,简便的方法来创建基于TensorFlow或Theano的深度学习模型。 Keras旨在快速定义深度学习模型。 好吧,Keras是深度学习应用程序的最佳选择。
特征 (Features)

Keras leverages various optimization techniques to make high level neural network API easier and more performant. It supports the following features ?
Keras利用各种优化技术来简化高级神经网络API,并提高其性能。 它支持以下功能-
    Consistent, simple and extensible API.
    一致,简单和可扩展的API。
    Minimal structure - easy to achieve the result without any frills.
    最小的结构-容易获得结果,没有任何多余的装饰。
    It supports multiple platforms and backends.
    它支持多个平台和后端。
    It is user friendly framework which runs on both CPU and GPU.
    它是用户友好的框架,可在CPU和GPU上运行。
    Highly scalability of computation.
    计算的高度可扩展性。
好处 (Benefits)

Keras is highly powerful and dynamic framework and comes up with the following advantages ?
Keras是一个功能强大且动态的框架,具有以下优点-
    Larger community support.
    更大的社区支持。
    Easy to test.
    易于测试。
    Keras neural networks are written in Python which makes things simpler.
    Keras神经网络是用Python编写的,这使事情变得更简单。
    Keras supports both convolution and recurrent networks.
    Keras支持卷积和循环网络。
    Deep learning models are discrete components, so that, you can combine into many ways.
    深度学习模型是离散的组件,因此,您可以将其组合成多种方式。

Keras-安装 (Keras - Installation)

This chapter explains about how to install Keras on your machine. Before moving to installation, let us go through the basic requirements of Keras.
本章介绍了如何在计算机上安装Keras。 在进行安装之前,让我们先了解Keras的基本要求。
先决条件 (Prerequisites)

You must satisfy the following requirements ?
您必须满足以下要求-
    Any kind of OS (Windows, Linux or Mac) 任何种类的操作系统(Windows,Linux或Mac) Python version 3.5 or higher. Python 3.5或更高版本。
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,
Keras是基于python的神经网络库,因此必须在您的计算机上安装python。 如果您的计算机上正确安装了python,请打开您的终端并输入python,您会看到如下所示的响应,
  1. Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18)
  2. [MSC v.1900 64 bit (AMD64)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>>
复制代码
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.
截至目前,最新版本为“ 3.7.2”。 如果未安装Python,请访问官方python链接-www.python.org并根据您的操作系统下载最新版本,然后立即将其安装在系统上。
Keras安装步骤 (Keras Installation Steps)

Keras installation is quite easy. Follow below steps to properly install Keras on your system.
Keras的安装非常简单。 请按照以下步骤在系统上正确安装Keras。
步骤1:创建虚拟环境 (Step 1: Create virtual environment)

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.
Virtualenv用于管理不同项目的Python软件包。 这将有助于避免破坏在其他环境中安装的软件包。 因此,始终建议在开发Python应用程序时使用虚拟环境。
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,
Linux或Mac OS用户,请转到您的项目根目录,然后键入以下命令以创建虚拟环境,
  1. python3 -m venv kerasenv
复制代码
After executing the above command, “kerasenv” directory is created with bin,lib and include folders in your installation location.
执行上述命令后,将使用bin,lib创建“ kerasenv”目录,并在您的安装位置包含文件夹
Windows
视窗
Windows user can use the below command,
Windows用户可以使用以下命令,
  1. py -m venv keras
复制代码
步骤2:激活环境 (Step 2: Activate the environment)

This step will configure python and pip executables in your shell path.
此步骤将在您的Shell路径中配置python和pip可执行文件。
Linux/Mac OS
Linux / Mac OS
Now we have created a virtual environment named “kerasvenv”. Move to the folder and type the below command,
现在,我们创建了一个名为“ kerasvenv”的虚拟环境。 移至该文件夹并输入以下命令,
  1. $ cd kerasvenv kerasvenv $ source bin/activate
复制代码
Windows
视窗
Windows users move inside the “kerasenv” folder and type the below command,
Windows用户进入“ kerasenv”文件夹,然后输入以下命令,
  1. .\env\Scripts\activate
复制代码
步骤3:Python库 (Step 3: Python libraries)

Keras depends on the following python libraries.
Keras依赖于以下python库。
    Numpy 脾气暴躁的 Pandas 大熊猫 Scikit-learn Scikit学习 Matplotlib Matplotlib Scipy 西皮 Seaborn 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
麻木
  1. pip install numpy
复制代码
you could see the following response,
您会看到以下回复,
  1. Collecting numpy
  2.    Downloading
  3. https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
  4.    numpy-3.1.1-cp36-cp36m-macosx_10_6_intel.
  5. macosx_10_9_intel.macosx_10_9_x86_64.
  6.    macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
  7.       |████████████████████████████████| 14.4MB 2.8MB/s
复制代码
pandas
大熊猫
  1. pip install pandas
复制代码
We could see the following response,
我们可以看到以下回应,
  1. Collecting pandas
  2.    Downloading
  3. https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
  4. pandas-3.1.1-cp36-cp36m-macosx_10_6_intel.
  5. macosx_10_9_intel.macosx_10_9_x86_64.
  6.    macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
  7.       |████████████████████████████████| 14.4MB 2.8MB/s
复制代码
matplotlib
matplotlib
  1. pip install matplotlib
复制代码
We could see the following response,
我们可以看到以下回应,
  1. Collecting matplotlib
  2.    Downloading
  3. https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
  4. matplotlib-3.1.1-cp36-cp36m-macosx_10_6_intel.
  5. macosx_10_9_intel.macosx_10_9_x86_64.
  6.    macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
  7.       |████████████████████████████████| 14.4MB 2.8MB/s
复制代码
scipy
科学的
  1. pip install scipy
复制代码
We could see the following response,
我们可以看到以下回应,
  1. Collecting scipy
  2.    Downloading
  3. https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8
  4. /scipy-3.1.1-cp36-cp36m-macosx_10_6_intel.
  5. macosx_10_9_intel.macosx_10_9_x86_64.
  6.    macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
  7.       |████████████████████████████████| 14.4MB 2.8MB/s
复制代码
scikit-learn
scikit学习
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 ?
这是一个开放源代码的机器学习库。 它用于分类,回归和聚类算法。 进行安装之前,需要满足以下条件:
    Python version 3.5 or higher Python 3.5或更高版本 NumPy version 1.11.0 or higher NumPy 1.11.0或更高版本 SciPy version 0.17.0 or higher SciPy版本0.17.0或更高 joblib 0.11 or higher. joblib 0.11或更高。
Now, we install scikit-learn using the below command ?
现在,我们使用以下命令安装scikit-learn-
  1. pip install -U scikit-learn
复制代码
Seaborn
Seaborn
Seaborn is an amazing library that allows you to easily visualize your data. Use the below command to install ?
Seaborn是一个了不起的库,可让您轻松地可视化数据。 使用以下命令安装-
  1. pip pip install seaborninstall -U scikit-learn
复制代码
You could see the message similar as specified below ?
您可能会看到类似以下内容的消息-
  1. Collecting seaborn
  2.    Downloading
  3. https://files.pythonhosted.org/packages/a8/76/220ba4420459d9c4c9c9587c6ce607bf56c25b3d3d2de62056efe482dadc
  4. /seaborn-0.9.0-py3-none-any.whl (208kB) 100%
  5.    |████████████████████████████████| 215kB 4.0MB/s
  6. Requirement already satisfied: numpy> = 1.9.3 in
  7. ./lib/python3.7/site-packages (from seaborn) (1.17.0)
  8. Collecting pandas> = 0.15.2 (from seaborn)
  9.    Downloading
  10. https://files.pythonhosted.org/packages/39/b7/441375a152f3f9929ff8bc2915218ff1a063a59d7137ae0546db616749f9/
  11. pandas-0.25.0-cp37-cp37m-macosx_10_9_x86_64.
  12. macosx_10_10_x86_64.whl (10.1MB) 100%
  13.    |████████████████████████████████| 10.1MB 1.8MB/s
  14. Requirement already satisfied: scipy>=0.14.0 in
  15. ./lib/python3.7/site-packages (from seaborn) (1.3.0)
  16. Collecting matplotlib> = 1.4.3 (from seaborn)
  17.    Downloading
  18. https://files.pythonhosted.org/packages/c3/8b/af9e0984f
  19. 5c0df06d3fab0bf396eb09cbf05f8452de4e9502b182f59c33b/
  20. matplotlib-3.1.1-cp37-cp37m-macosx_10_6_intel.
  21. macosx_10_9_intel.macosx_10_9_x86_64
  22. .macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) 100%
  23.    |████████████████████████████████| 14.4MB 1.4MB/s
  24. ......................................
  25. ......................................
  26. Successfully installed cycler-0.10.0 kiwisolver-1.1.0
  27. matplotlib-3.1.1 pandas-0.25.0 pyparsing-2.4.2
  28. python-dateutil-2.8.0 pytz-2019.2 seaborn-0.9.0
复制代码
使用Python安装Keras (Keras Installation Using Python)

As of now, we have completed basic requirements for the installtion of Kera. Now, install the Keras using same procedure as specified below ?
到目前为止,我们已经完成了安装Kera的基本要求。 现在,使用下面指定的相同步骤安装Keras-
  1. pip install keras
复制代码
退出虚拟环境 (Quit virtual environment)

After finishing all your changes in your project, then simply run the below command to quit the environment ?
完成项目中的所有更改后,只需运行以下命令即可退出环境-
  1. deactivate
复制代码
Python云 (Anaconda Cloud)

We believe that you have installed anaconda cloud on your machine. If anaconda is not installed, then visit the official link, www.anaconda.com/distribution and choose download based on your OS.
我们相信您已在计算机上安装了anaconda云。 如果未安装anaconda,请访问官方链接www.anaconda.com/distribution ,然后根据您的操作系统选择下载。
创建一个新的conda环境 (Create a new conda environment)

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 ?
启动anaconda提示,这将打开基本的Anaconda环境。 让我们创建一个新的conda环境。 此过程类似于virtualenv。 在conda终端中输入以下命令-
  1. conda create --name PythonCPU
复制代码
If you want, you can create and install modules using GPU also. In this tutorial, we follow CPU instructions.
如果需要,您还可以使用GPU创建和安装模块。 在本教程中,我们遵循CPU指令。
激活conda环境 (Activate conda environment)

To activate the environment, use the below command ?
要激活环境,请使用以下命令-
  1. activate PythonCPU
复制代码
安装间谍 (Install spyder)

Spyder is an IDE for executing python applications. Let us install this IDE in our conda environment using the below command ?
Spyder是用于执行python应用程序的IDE。 让我们使用以下命令在conda环境中安装此IDE-
  1. conda install spyder
复制代码
安装python库 (Install python libraries)

We have already known the python libraries numpy, pandas, etc., needed for keras. You can install all the modules by using the below syntax ?
我们已经知道keras需要的python库numpy,pandas等。 您可以使用以下语法安装所有模块-
Syntax
句法
  1. conda install -c anaconda <module-name>
复制代码
For example, you want to install pandas ?
例如,您要安装熊猫-
  1. conda install -c anaconda pandas
复制代码
Like the same method, try it yourself to install the remaining modules.
与相同的方法一样,请自己尝试安装其余模块。
安装Keras (Install Keras)

Now, everything looks good so you can start keras installation using the below command ?
现在,一切看起来都不错,因此您可以使用以下命令开始安装keras-
  1. conda install -c anaconda keras
复制代码
发射间谍 (Launch spyder)

Finally, launch spyder in your conda terminal using the below command ?
最后,使用以下命令在conda终端中启动spyder-
  1. spyder
复制代码
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-后端配置 (Keras - Backend Configuration)

This chapter explains Keras backend implementations TensorFlow and Theano in detail. Let us go through each implementation one by one.
本章详细说明Keras后端实现TensorFlow和Theano。 让我们逐一介绍每个实现。
TensorFlow (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.
TensorFlow是一个开源的机器学习库,用于Google开发的数值计算任务。 Keras是建立在TensorFlow或Theano之上的高级API。 我们已经知道如何使用pip安装TensorFlow。
If it is not installed, you can install using the below command ?
如果尚未安装,则可以使用以下命令进行安装-
  1. pip install TensorFlow
复制代码
Once we execute keras, we could see the configuration file is located at your home directory inside and go to .keras/keras.json.
执行keras后,我们可以看到配置文件位于您的主目录中,并转到.keras / keras.json。
keras.json (keras.json)
  1. {
  2.    "image_data_format": "channels_last",
  3.    "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow"
  4. }
复制代码
Here,
这里,
    image_data_format represent the data format.
    image_data_format表示数据格式。
    epsilon represents numeric constant. It is used to avoid DivideByZero error.
    epsilon表示数值常数。 用于避免DivideByZero错误。
    floatx represent the default data type float32. You can also change it to float16 or float64 using set_floatx() method.
    float x表示默认数据类型float32 。 您也可以使用set_floatx()方法将其更改为float16float64
    image_data_format represent the data format.
    image_data_format表示数据格式。
Suppose, if the file is not created then move to the location and create using the below steps ?
假设,如果未创建文件,则移动到该位置并使用以下步骤创建-
  1. > cd home
  2. > mkdir .keras
  3. > vi 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.
请记住,您应该指定.keras作为其文件夹名称,并将上述配置添加到keras.json文件中。 我们可以执行一些预定义的操作来了解后端功能。
茶野 (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 ?
Theano是一个开源深度学习库,可让您有效地评估多维数组。 我们可以使用以下命令轻松安装-
  1. pip install 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使用TensorFlow后端。 如果要将后端配置从TensorFlow更改为Theano,只需在keras.json文件中更改backend = theano。 它在下面描述-
keras.json (keras.json)
  1. {
  2.    "image_data_format": "channels_last",
  3.    "epsilon": 1e-07,
  4.    "floatx": "float32",
  5.    "backend": "theano"
  6. }
复制代码
Now save your file, restart your terminal and start keras, your backend will be changed.
现在保存文件,重新启动终端并启动keras,您的后端将被更改。
  1. >>> import keras as k
  2. using theano backend.
复制代码
Keras-深度学习概述 (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)

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.
深度学习最流行和最主要的方法是使用“人工神经网络”(ANN)。 它们的灵感来自人类大脑的模型,这是人体最复杂的器官。 人脑由超过900亿个称为“神经元”的微小细胞组成。 神经元通过称为“轴突”和“树突”的神经纤维相互连接。 轴突的主要作用是将信息从一个神经元传递到与其连接的另一神经元。
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.
同样,树突的主要作用是接收与之相连的另一个神经元的轴突传递的信息。 每个神经元处理少量信息,然后将结果传递给另一个神经元,此过程继续进行。 这是人脑处理大量信息(如语音,视觉等)并从中提取有用信息的基本方法。
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”.
基于此模型,心理学家Frank Rosenblatt于1958年发明了第一个人工神经网络。人工神经网络由类似于神经元的多个节点组成。 节点紧密互连,并组织成不同的隐藏层。 输入层接收输入数据,数据依次经过一个或多个隐藏层,最后输出层预测有关输入数据的有用信息。 例如,输入可以是图像,而输出可以是图像中标识的东西,例如“猫”。
A single neuron (called as perceptron in ANN) can be represented as below ?
单个神经元(在ANN中称为感知器)可以表示如下-



Here,
这里,
    Multiple input along with weight represents dendrites.
    多个输入以及重量代表树突。
    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.
    输入和激活功能的总和代表神经元。 Sum实际上表示所有输入的计算值,激活函数表示一个函数,它将Sum值修改为0、1或0到1。
    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)

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.
多层感知器是ANN的最简单形式。 它由一个输入层,一个或多个隐藏层以及最后一个输出层组成。 一层由感知器集合组成。 输入层基本上是输入数据的一个或多个特征。 每个隐藏层由一个或多个神经元组成,并处理特征的某些方面,并将处理后的信息发送到下一个隐藏层。 输出层进程从最后一个隐藏层接收数据,最后输出结果。



卷积神经网络(CNN) (Convolutional Neural Network (CNN))

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 ?
卷积神经网络是最流行的人工神经网络之一。 它广泛应用于图像和视频识别领域。 它基于数学概念卷积的概念。 它几乎与多层感知器相似,不同之处在于它在完全连接的隐藏神经元层之前包含一系列卷积层和池化层。 它具有三个重要的层-
    Convolution layer ? It is the primary building block and perform computational tasks based on convolution function.
    卷积层 -它是主要的构建块,并基于卷积函数执行计算任务。
    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.
    池层 -它位于卷积层旁边,用于通过删除不必要的信息来减小输入的大小,从而可以更快地执行计算。
    Fully connected layer ? It is arranged to next to series of convolution and pooling layer and classify input into various categories.
    完全连接的层 -安排在一系列卷积和池化层的旁边,并将输入分类为各种类别。
A simple CNN can be represented as below ?
一个简单的CNN可以表示如下-



Here,
这里,
    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)
    使用单个完全连接的层,并用于输出数据(例如,图像分类)
递归神经网络(RNN) (Recurrent Neural Network (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)对于解决其他ANN模型中的缺陷很有用。 好吧,大多数ANN都不记得以前的情况,而是学会根据训练中的情境做出决策。 同时,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.
该方法主要用于图像分类。 有时,我们可能需要展望未来以修正过去。 在这种情况下,双向RNN有助于从过去学习和预测未来。 例如,我们有多个输入的手写样本。 假设我们在一个输入中有混淆,然后我们需要再次检查其他输入以识别做出过去决定的正确上下文。
人工神经网络的工作流程 (Workflow of ANN)

Let us first understand the different phases of deep learning and then, learn how Keras helps in the process of deep learning.
首先让我们了解深度学习的不同阶段,然后了解Keras在深度学习过程中如何提供帮助。
收集所需数据 (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)

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.
分析数据并获得对数据的良好理解。 选择正确的ANN算法需要对数据有更好的了解。
选择一种算法(模型) (Choose an algorithm (model))

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.
选择一种最适合学习过程类型(例如图像分类,文本处理等)和可用输入数据的算法。 算法由Keras中的模型表示。 算法包括一层或多层。 ANN中的每个层都可以用Keras中的Keras层表示。
    Prepare data ? Process, filter and select only the required information from the data.
    准备数据 -处理,过滤和仅从数据中选择所需的信息。
    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.
    拆分数据 -将数据拆分为训练和测试数据集。 测试数据将用于评估算法/模型的预测(一旦机器学习),并交叉检查学习过程的效率。
    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.
    编译模型 -编译算法/模型,以便可以通过训练进一步学习,最后进行预测。 这一步需要我们选择损失函数和优化器。 损失函数和优化器用于学习阶段,以查找错误(与实际输出的偏差)并进行优化,以使错误最小化。
    Fit the model ? The actual learning process will be done in this phase using the training data set.
    拟合模型 -实际的学习过程将在此阶段使用训练数据集完成。
    Predict result for unknown value ? Predict the output for the unknown input data (other than existing training and test data)
    预测未知值的结果 -预测未知输入数据的输出(现有培训和测试数据除外)
    Evaluate model ? Evaluate the model by predicting the output for test data and cross-comparing the prediction with actual result of the test data.
    评估模型 -通过预测测试数据的输出并将预测与测试数据的实际结果进行交叉比较来评估模型
    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 ?
可以使用以下流程图表示以上步骤-




Keras-深度学习 (Keras - Deep learning)

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.
Keras提供了一个完整的框架来创建任何类型的神经网络。 Keras具有创新性并且非常易于学习。 它支持简单的神经网络到非常大和复杂的神经网络模型。 在本章中,让我们了解Keras框架的体系结构以及Keras如何帮助深度学习。
凯拉斯的体系结构 (Architecture of Keras)

Keras API can be divided into three main categories ?
Keras API可以分为三个主要类别-
    Model 模型 Layer 层 Core Modules 核心模块
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.
在Keras中,每个人工神经网络都由Keras模型表示。 反过来,每个Keras模型都是Keras层的组成 ,代表了ANN层,例如输入层,隐藏层,输出层,卷积层,池化层等。Keras模型和层访问Keras模块,用于激活函数,损失函数,正则化函数,等等,使用Keras模型,Keras Layer和Keras模块,可以以简单有效的方式表示任何ANN算法(CNN,RNN等)。
The following diagram depicts the relationship between model, layer and core modules ?
下图描述了模型,层和核心模块之间的关系-



Let us see the overview of Keras models, Keras layers and Keras modules.
让我们看一下Keras模型,Keras图层和Keras模块的概述。
模型 (Model)

Keras Models are of two types as mentioned below ?
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.
顺序模型 -顺序模型基本上是Keras层的线性组成。 顺序模型简单,最小,并且能够表示几乎所有可用的神经网络。
A simple sequential model is as follows ?
一个简单的顺序模型如下-
  1. from keras.models import Sequential
  2. from keras.layers import Dense, Activation
  3. model = Sequential()  
  4. model.add(Dense(512, activation = 'relu', input_shape = (784,)))
复制代码
Where,
哪里,
    Line 1 imports Sequential model from Keras models
    1号线从Keras模型导入顺序模型
    Line 2 imports Dense layer and Activation module
    第2行导入了密集层和激活模块
    Line 4 create a new sequential model using Sequential API
    第4行使用Sequential API创建新的顺序模型
    Line 5 adds a dense layer (Dense API) with relu activation (using Activation module) function.
    第5行添加了带有relu激活(使用Activation模块)功能的密集层(Dense API)。
Sequential model exposes Model class to create customized models as well. We can use sub-classing concept to create our own complex model.
顺序模型公开了Model类,以创建自定义模型。 我们可以使用子分类概念来创建我们自己的复杂模型。
Functional API ? Functional API is basically used to create complex models.
功能API-功能API基本上用于创建复杂的模型。
(Layer)

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,
Keras模型中的每个Keras层代表实际提出的神经网络模型中的对应层(输入层,隐藏层和输出层)。 Keras提供了许多预构建层,因此可以轻松创建任何复杂的神经网络。 下面指定了一些重要的Keras图层,
    Core Layers 核心层 Convolution Layers 卷积层 Pooling Layers 池层 Recurrent Layers 循环图层
A simple python code to represent a neural network model using sequential model is as follows ?
使用顺序模型表示神经网络模型的简单python代码如下-
  1. from keras.models import Sequential
  2. from keras.layers import Dense, Activation, Dropout model = Sequential()
  3. model.add(Dense(512, activation = 'relu', input_shape = (784,)))
  4. model.add(Dropout(0.2))
  5. model.add(Dense(512, activation = 'relu')) model.add(Dropout(0.2))
  6. model.add(Dense(num_classes, activation = 'softmax'))
复制代码
Where,
哪里,
    Line 1 imports Sequential model from Keras models
    1号线从Keras模型导入顺序模型
    Line 2 imports Dense layer and Activation module
    第2行导入了密集层和激活模块
    Line 4 create a new sequential model using Sequential API
    第4行使用Sequential API创建新的顺序模型
    Line 5 adds a dense layer (Dense API) with relu activation (using Activation module) function.
    第5行添加了带有relu激活(使用Activation模块)功能的密集层(Dense API)。
    Line 6 adds a dropout layer (Dropout API) to handle over-fitting.
    第6行添加了一个辍学层(Dropout API)来处理过度拟合。
    Line 7 adds another dense layer (Dense API) with relu activation (using Activation module) function.
    第7行添加了具有relu激活(使用Activation模块)功能的另一个密集层(Dense API)。
    Line 8 adds another dropout layer (Dropout API) to handle over-fitting.
    第8行添加了另一个辍学层(Dropout API)来处理过度拟合。
    Line 9 adds final dense layer (Dense API) with softmax activation (using Activation module) function.
    第9行添加了带有softmax激活(使用Activation模块)功能的最终密集层(Dense API)。
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.
Keras还提供了创建我们自己的自定义图层的选项。 可以通过对Keras.Layer类进行子类化来创建自定义图层,这类似于对Keras模型进行子类化。
核心模块 (Core Modules)

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 ?
Keras还提供了大量内置神经网络相关功能,以正确创建Keras模型和Keras层。 一些功能如下-
    Activations module ? Activation function is an important concept in ANN and activation modules provides many activation function like softmax, relu, etc.,
    激活模块 -激活功能是ANN中的重要概念,激活模块提供了许多激活功能,例如softmax,relu等,
    Loss module ? Loss module provides loss functions like mean_squared_error, mean_absolute_error, poisson, etc.,
    损失模块 -损失模块提供了诸如mean_squared_error,mean_absolute_error,泊松等损失函数,
    Optimizer module ? Optimizer module provides optimizer function like adam, sgd, etc.,
    优化器模块 -优化器模块提供诸如adam,sgd等的优化器功能,
    Regularizers ? Regularizer module provides functions like L1 regularizer, L2 regularizer, etc.,
    正则化器 -正则化器模块提供L1正则化器,L2正则化器等功能,
Let us learn Keras modules in detail in the upcoming chapter.
让我们在下一章中详细学习Keras模块。
Keras-模块 (Keras - Modules)

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.
如前所述,Keras模块包含预定义的类,函数和变量,它们对于深度学习算法很有用。 让我们学习本章中Keras提供的模块。
可用模块 (Available modules)

Let us first see the list of modules available in the Keras.
首先让我们看看Keras中可用的模块列表。
    Initializers ? Provides a list of initializers function. We can learn it in details in Keras layer chapter. during model creation phase of machine learning.
    初始化程序 -提供初始化程序列表。 我们可以在Keras 层一章中详细了解它。 在机器学习的模型创建阶段。
    Regularizers ? Provides a list of regularizers function. We can learn it in details in Keras Layers chapter.
    正则化器 -提供正则化器功能列表。 我们可以在Keras Layers一章中详细了解它。
    Constraints ? Provides a list of constraints function. We can learn it in details in Keras Layers chapter.
    约束 -提供约束功能列表。 我们可以在Keras Layers一章中详细了解它。
    Activations ? Provides a list of activator function. We can learn it in details in Keras Layers chapter.
    激活 -提供激活器功能列表。 我们可以在Keras Layers一章中详细了解它。
    Losses ? Provides a list of loss function. We can learn it in details in Model Training chapter.
    损失 -提供损失功能列表。 我们可以在“ 模型培训”一章中详细了解它。
    Metrics ? Provides a list of metrics function. We can learn it in details in Model Training chapter.
    指标 -提供指标列表功能。 我们可以在“ 模型培训”一章中详细了解它。
    Optimizers ? Provides a list of optimizer function. We can learn it in details in Model Training chapter.
    优化器 -提供优化器功能列表。 我们可以在“ 模型培训”一章中详细了解它。
    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.
    回调 -提供回调函数列表。 我们可以在训练过程中使用它来打印中间数据,并根据某些条件停止训练本身( EarlyStopping方法)。
    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.
    文本处理 -提供将文本转换为适用于机器学习的NumPy数组的功能。 我们可以在机器学习的数据准备阶段使用它。
    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.
    图像处理 -提供将图像转换为适合机器学习的NumPy数组的功能。 我们可以在机器学习的数据准备阶段使用它。
    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.
    顺序处理 -提供从给定的输入数据生成基于时间的数据的功能。 我们可以在机器学习的数据准备阶段使用它。
    Backend ? Provides function of the backend library like TensorFlow and Theano.
    后端 -提供TensorFlowTheano等后端库的功能。
    Utilities ? Provides lot of utility function useful in deep learning.
    实用程序 -提供许多在深度学习中有用的实用程序功能。
Let us see backend module and utils model in this chapter.
让我们在本章中了解后端模块和utils模型。
后端模块 (backend module)

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运行在TensorFlow后端之上。 如果需要,可以切换到Theano或CNTK等其他后端。 Defualt后端配置在.keras / keras.json文件下的根目录中定义。
Keras backend module can be imported using below code
可以使用以下代码导入Keras 后端模块
  1. >>> from keras import backend as k
复制代码
If we are using default backend TensorFlow, then the below function returns TensorFlow based information as specified below ?
如果我们使用默认后端TensorFlow ,则以下函数将返回基于TensorFlow的信息,如下所示-
  1. >>> k.backend()
  2. 'tensorflow'
  3. >>> k.epsilon()
  4. 1e-07
  5. >>> k.image_data_format()
  6. 'channels_last'
  7. >>> k.floatx()
  8. 'float32'
复制代码
Let us understand some of the significant backend functions used for data analysis in brief ?
让我们简要地了解一些用于数据分析的重要后端功能-
get_uid() (get_uid())

It is the identifier for the default graph. It is defined below ?
它是默认图形的标识符。 它定义如下-
  1. >>> k.get_uid(prefix='')
  2. 1
  3. >>> k.get_uid(prefix='') 2
复制代码
reset_uids (reset_uids)

It is used resets the uid value.
用于重置uid值。
  1. >>> k.reset_uids()
复制代码
Now, again execute the get_uid(). This will be reset and change again to 1.
现在,再次执行get_uid() 。 它将被重置并再次更改为1。
  1. >>> k.get_uid(prefix='')
  2. 1
复制代码
占位符 (placeholder)

It is used instantiates a placeholder tensor. Simple placeholder to hold 3-D shape is shown below ?
用于实例化一个占位符张量。 保持3D形状的简单占位符如下所示-
  1. >>> data = k.placeholder(shape = (1,3,3))
  2. >>> data
  3. <tf.Tensor 'Placeholder_9:0' shape = (1, 3, 3) dtype = float32>
  4. If you use int_shape(), it will show the shape.
  5. >>> k.int_shape(data) (1, 3, 3)
复制代码
(dot)

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和b是两个张量,而c是ab乘积的结果。 假设形状为(4,2),b形状为(2,3)。 定义如下
  1. >>> a = k.placeholder(shape = (4,2))
  2. >>> b = k.placeholder(shape = (2,3))
  3. >>> c = k.dot(a,b)
  4. >>> c
  5. <tf.Tensor 'MatMul_3:0' shape = (4, 3) dtype = float32>
  6. >>>
复制代码
那些 (ones)

It is used to initialize all as one value.
用于将所有值初始化为一个值。
  1. >>> res = k.ones(shape = (2,2))
  2. #print the value
  3. >>> k.eval(res)
  4. array([[1., 1.], [1., 1.]], dtype = float32)
复制代码
batch_dot (batch_dot)

It is used to perform the product of two data in batches. Input dimension must be 2 or higher. It is shown below ?
它用于批量执行两个数据的乘积。 输入尺寸必须为2或更大。 它显示如下-
  1. >>> a_batch = k.ones(shape = (2,3))
  2. >>> b_batch = k.ones(shape = (3,2))
  3. >>> c_batch = k.batch_dot(a_batch,b_batch)
  4. >>> c_batch
  5. <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.
它用于初始化变量。 让我们在此变量中执行简单的转置操作。
  1. >>> data = k.variable([[10,20,30,40],[50,60,70,80]])
  2. #variable initialized here
  3. >>> result = k.transpose(data)
  4. >>> print(result)
  5. Tensor("transpose_6:0", shape = (4, 2), dtype = float32)
  6. >>> print(k.eval(result))
  7.    [[10. 50.]
  8.    [20. 60.]
  9.    [30. 70.]
  10.    [40. 80.]]
复制代码
If you want to access from numpy ?
如果您想从numpy访问-
  1. >>> data = np.array([[10,20,30,40],[50,60,70,80]])
  2. >>> print(np.transpose(data))
  3.    [[10 50]
  4.    [20 60]
  5.    [30 70]
  6.    [40 80]]
  7. >>> res = k.variable(value = data)
  8. >>> print(res)
  9. <tf.Variable 'Variable_7:0' shape = (2, 4) dtype = float32_ref>
复制代码
is_sparse(张量) (is_sparse(tensor))

It is used to check whether the tensor is sparse or not.
用于检查张量是否稀疏。
  1. >>> a = k.placeholder((2, 2), sparse=True)
  2. >>> print(a) SparseTensor(indices =      
  3.    Tensor("Placeholder_8:0",
  4.    shape = (?, 2), dtype = int64),
  5. values = Tensor("Placeholder_7:0", shape = (?,),
  6. dtype = float32), dense_shape = Tensor("Const:0", shape = (2,), dtype = int64))
  7. >>> print(k.is_sparse(a)) True
复制代码
to_dense() (to_dense())

It is used to converts sparse into dense.
它用于将稀疏转换为密集。
  1. >>> b = k.to_dense(a)
  2. >>> print(b) Tensor("SparseToDense:0", shape = (2, 2), dtype = float32)
  3. >>> print(k.is_sparse(b)) False
复制代码
random_uniform_variable (random_uniform_variable)

It is used to initialize using uniform distribution concept.
它用于使用均匀分布概念进行初始化。
  1. k.random_uniform_variable(shape, mean, scale)
复制代码
Here,
这里,
    shape ? denotes the rows and columns in the format of tuples.
    形状 -表示在元组的格式的行和列。
    mean ? mean of uniform distribution.
    均值 -均匀分布的均值。
    scale ? standard deviation of uniform distribution.
    标度 -均匀分布的标准偏差。
Let us have a look at the below example usage ?
让我们看看下面的示例用法-
  1. >>> a = k.random_uniform_variable(shape = (2, 3), low=0, high = 1)
  2. >>> b = k. random_uniform_variable(shape = (3,2), low = 0, high = 1)
  3. >>> c = k.dot(a, b)
  4. >>> k.int_shape(c)
  5. (2, 2)
复制代码
utils模块 (utils module)

utils provides useful utilities function for deep learning. Some of the methods provided by the utils module is as follows ?
utils为深度学习提供了有用的实用程序功能。 utils模块提供的一些方法如下-
HDF5矩阵 (HDF5Matrix)

It is used to represent the input data in HDF5 format.
它用于以HDF5格式表示输入数据。
  1. from keras.utils import HDF5Matrix data = HDF5Matrix('data.hdf5', 'data')
复制代码
to_categorical (to_categorical)

It is used to convert class vector into binary class matrix.
它用于将类向量转换为二进制类矩阵。
  1. >>> from keras.utils import to_categorical
  2. >>> labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. >>> to_categorical(labels)
  4. array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
  5.    [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
  6.    [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
  7.    [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
  8.    [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
  9.    [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
  10.    [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
  11.    [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
  12.    [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
  13.    [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype = float32)
  14. >>> from keras.utils import normalize
  15. >>> normalize([1, 2, 3, 4, 5])
  16. array([[0.13483997, 0.26967994, 0.40451992, 0.53935989, 0.67419986]])
复制代码
print_summary (print_summary)

It is used to print the summary of the model.
它用于打印模型摘要。
  1. from keras.utils import print_summary print_summary(model)
复制代码
plot_model (plot_model)

It is used to create the model representation in dot format and save it to file.
它用于以点格式创建模型表示并将其保存到文件中。
  1. from keras.utils import plot_model
  2. plot_model(model,to_file = 'image.png')
复制代码
This plot_model will generate an image to understand the performance of model.
plot_model将生成一张图像以了解模型的性能。

Keras-图层 (Keras - Layers)

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.
如前所述,Keras图层是Keras模型的主要构建块。 每层接收输入信息,进行一些计算,最后输出转换后的信息。 一层的输出将流入下一层作为其输入。 让我们在本章中了解有关图层的完整详细信息。
介绍 (Introduction)

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层需要输入的形状(input_shape)来了解输入数据的结构, 初始化器设置每个输入的权重,最后通过激活器将输出转换为非线性。 在这两者之间,约束条件限制并指定了要生成的输入数据权重的范围,并且正则化程序将通过在优化过程中动态对权重施加惩罚来尝试优化图层(和模型)。
To summarise, Keras layer requires below minimum details to create a complete layer.
总而言之,Keras图层需要以下最低限度的细节才能创建完整的图层。
    Shape of the input data 输入数据的形状 Number of neurons / units in the layer 层中神经元/单位的数量 Initializers 初始化器 Regularizers 正则化器 Constraints 约束条件 Activations 激活方式
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.
让我们在下一章中了解基本概念。 在理解基本概念之前,让我们使用顺序模型API创建一个简单的Keras层,以了解Keras模型和层如何工作。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. from keras import regularizers
  5. from keras import constraints
  6. model = Sequential()
  7. model.add(Dense(32, input_shape=(16,), kernel_initializer = 'he_uniform',
  8.    kernel_regularizer = None, kernel_constraint = 'MaxNorm', activation = 'relu'))
  9. model.add(Dense(16, activation = 'relu'))
  10. model.add(Dense(8))
复制代码
where,
哪里,
    Line 1-5 imports the necessary modules.
    1-5行导入了必要的模块。
    Line 7 creates a new model using Sequential API.
    第7行使用Sequential API创建新模型。
    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.
    第9行创建一个新的Dense层并将其添加到模型中。 密集是Keras提供的入门级层,它接受神经元或单位数(32)作为其必需参数。 如果该层是第一层,那么我们还需要提供输入形状(16) 。 否则,上一层的输出将用作下一层的输入。 所有其他参数都是可选的。
      First parameter represents the number of units (neurons).
      第一个参数代表单位数(神经元)。
      input_shape represent the shape of input data.
      input_shape代表输入数据的形状。
      kernel_initializer represent initializer to be used. he_uniform function is set as value.
      kernel_initializer表示要使用的初始化程序。 he_uniform函数设置为值。
      kernel_regularizer represent regularizer to be used. None is set as value.
      kernel_regularizer表示要使用的正则化器。 没有设置任何值。
      kernel_constraint represent constraint to be used. MaxNorm function is set as value.
      kernel_constraint表示要使用的约束。 MaxNorm函数设置为值。
      activation represent activation to be used. relu function is set as value.
      激活表示要使用的激活。 将relu功能设置为值。
    Line 10 creates second Dense layer with 16 units and set relu as the activation function.
    第10行使用16个单位创建第二个Dense层,并将relu设置为激活功能。
    Line 11 creates final Dense layer with 8 units.
    第11行使用8个单位创建最终的密集层。
图层的基本概念 (Basic Concept of Layers)

Let us understand the basic concept of layer as well as how Keras supports each concept.
让我们了解层的基本概念以及Keras如何支持每个概念。
输入形状 (Input shape)

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.
在机器学习中,所有类型的输入数据(例如文本,图像或视频)都将首先转换为数字数组,然后输入算法。 输入数字可以是一维数组,二维数组(矩阵)或多维数组。 我们可以使用shape (整数元组)指定尺寸信息。 例如, (4,2)表示具有四行两列的矩阵。
  1. >>> import numpy as np
  2. >>> shape = (4, 2)
  3. >>> input = np.zeros(shape)
  4. >>> print(input)
  5. [
  6.    [0. 0.]
  7.    [0. 0.]
  8.    [0. 0.]
  9.    [0. 0.]
  10. ]
  11. >>>
复制代码
Similarly, (3,4,2) three dimensional matrix having three collections of 4x2 matrix (two rows and four columns).
类似地, (3,4,2)具有三个4x2矩阵集合(两行四列)的三维矩阵。
  1. >>> import numpy as np
  2. >>> shape = (3, 4, 2)
  3. >>> input = np.zeros(shape)
  4. >>> print(input)
  5. [
  6.    [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]
  7.    [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]
  8.    [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]
  9. ]
  10. >>>
复制代码
To create the first layer of the model (or input layer of the model), shape of the input data should be specified.
要创建模型的第一层(或模型的输入层),应指定输入数据的形状。
初始化器 (Initializers)

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 ?
在机器学习中,权重将分配给所有输入数据。 Initializers模块提供了不同的功能来设置这些初始权重。 Keras初始化器的一些功能如下-
零点 (Zeros)

Generates 0 for all input data.
为所有输入数据生成0
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.Zeros()
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_initializer = my_init))
复制代码
Where, kernel_initializer represent the initializer for kernel of the model.
其中, kernel_initializer表示模型内核的初始化程序。
那些 (Ones)

Generates 1 for all input data.
为所有输入数据生成1
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.Ones()
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
不变 (Constant)

Generates a constant value (say, 5) specified by the user for all input data.
生成用户为所有输入数据指定的常数值(例如5 )。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.Constant(value = 0) model.add(
  5.    Dense(512, activation = 'relu', input_shape = (784,), kernel_initializer = my_init)
  6. )
复制代码
where, value represent the constant value
其中, 代表常数
随机法线 (RandomNormal)

Generates value using normal distribution of input data.
使用输入数据的正态分布生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.RandomNormal(mean=0.0,
  5. stddev = 0.05, seed = None)
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_initializer = my_init))
复制代码
where,
哪里,
    mean represent the mean of the random values to generate
    平均值表示要生成的随机值的平均值
    stddev represent the standard deviation of the random values to generate
    stddev表示要生成的随机值的标准偏差
    seed represent the values to generate random number
    种子代表生成随机数的值
随机统一 (RandomUniform)

Generates value using uniform distribution of input data.
使用输入数据的均匀分布生成值。
  1. from keras import initializers
  2. my_init = initializers.RandomUniform(minval = -0.05, maxval = 0.05, seed = None)
  3. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  4.    kernel_initializer = my_init))
复制代码
where,
哪里,
    minval represent the lower bound of the random values to generate
    minval表示要生成的随机值的下限
    maxval represent the upper bound of the random values to generate
    maxval表示要生成的随机值的上限
截断法线 (TruncatedNormal)

Generates value using truncated normal distribution of input data.
使用输入数据的截断正态分布生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.TruncatedNormal(mean = 0.0, stddev = 0.05, seed = None
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
方差缩放 (VarianceScaling)

Generates value based on the input shape and output shape of the layer along with the specified scale.
根据图层的输入形状和输出形状以及指定的比例尺生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.VarianceScaling(
  5.    scale = 1.0, mode = 'fan_in', distribution = 'normal', seed = None)
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    skernel_initializer = my_init))
复制代码
where,
哪里,
    scale represent the scaling factor
    比例代表比例因子
    mode represent any one of fan_in, fan_out and fan_avg values
    模式代表fan_in,fan_outfan_avg值中的任何一个
    distribution represent either of normal or uniform
    分布代表正态分布均匀分布
方差缩放 (VarianceScaling)

It finds the stddev value for normal distribution using below formula and then find the weights using normal distribution,
它使用以下公式找到正态分布的stddev值,然后使用正态分布找到权重,
  1. stddev = sqrt(scale / n)
复制代码
where n represent,
n代表
    number of input units for mode = fan_in
    模式的输入单元数= fan_in
    number of out units for mode = fan_out
    模式的输出单元数= fan_out
    average number of input and output units for mode = fan_avg
    模式的平均输入和输出单元数= fan_avg
Similarly, it finds the limit for uniform distribution using below formula and then find the weights using uniform distribution,
同样,它使用以下公式找到均匀分布的极限 ,然后使用均匀分布找到权重,
  1. limit = sqrt(3 * scale / n)
复制代码
lecun_normal (lecun_normal)

Generates value using lecun normal distribution of input data.
使用lecun输入数据的正态分布生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.RandomUniform(minval = -0.05, maxval = 0.05, seed = None)
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
It finds the stddev using the below formula and then apply normal distribution
它使用以下公式找到stddev ,然后应用正态分布
  1. stddev = sqrt(1 / fan_in)
复制代码
where, fan_in represent the number of input units.
其中, fan_in代表输入单元的数量。
lecun_uniform (lecun_uniform)

Generates value using lecun uniform distribution of input data.
使用lecun输入数据的均匀分布来生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.lecun_uniform(seed = None)
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
It finds the limit using the below formula and then apply uniform distribution
它使用以下公式找到极限 ,然后应用均匀分布
  1. limit = sqrt(3 / fan_in)
复制代码
where,
哪里,
    fan_in represents the number of input units
    fan_in代表输入单元的数量
    fan_out represents the number of output units
    fan_out代表输出单元的数量
glorot_normal (glorot_normal)

Generates value using glorot normal distribution of input data.
使用输入数据的正态分布生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.glorot_normal(seed=None) model.add(
  5.    Dense(512, activation = 'relu', input_shape = (784,), kernel_initializer = my_init)
  6. )
复制代码
It finds the stddev using the below formula and then apply normal distribution
它使用以下公式找到stddev ,然后应用正态分布
  1. stddev = sqrt(2 / (fan_in + fan_out))
复制代码
where,
哪里,
    fan_in represents the number of input units
    fan_in代表输入单元的数量
    fan_out represents the number of output units
    fan_out代表输出单元的数量
glorot_uniform (glorot_uniform)

Generates value using glorot uniform distribution of input data.
使用输入数据的全局均匀分布来生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.glorot_uniform(seed = None)
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
It finds the limit using the below formula and then apply uniform distribution
它使用以下公式找到极限 ,然后应用均匀分布
  1. limit = sqrt(6 / (fan_in + fan_out))
复制代码
where,
哪里,
    fan_in represent the number of input units.
    fan_in代表输入单元的数量。
    fan_out represents the number of output units
    fan_out代表输出单元的数量
他正常 (he_normal)

Generates value using he normal distribution of input data.
使用输入数据的正态分布生成值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.RandomUniform(minval = -0.05, maxval = 0.05, seed = None)
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
It finds the stddev using the below formula and then apply normal distribution.
它使用以下公式找到stddev ,然后应用正态分布。
  1. stddev = sqrt(2 / fan_in)
复制代码
where, fan_in represent the number of input units.
其中, fan_in代表输入单元的数量。
he_uniform (he_uniform)

Generates value using he uniform distribution of input data.
使用输入数据的均匀分布来产生价值。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.he_normal(seed = None)
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
It finds the limit using the below formula and then apply uniform distribution.
它使用以下公式找到极限 ,然后应用均匀分布。
  1. limit = sqrt(6 / fan_in)
复制代码
where, fan_in represent the number of input units.
其中, fan_in代表输入单元的数量。
正交的 (Orthogonal)

Generates a random orthogonal matrix.
生成随机正交矩阵。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.Orthogonal(gain = 1.0, seed = None)
  5. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init))
复制代码
where, gain represent the multiplication factor of the matrix.
其中, 增益代表矩阵的乘法因子。
身分识别 (Identity)

Generates identity matrix.
生成单位矩阵。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.Identity(gain = 1.0) model.add(
  5.    Dense(512, activation = 'relu', input_shape = (784,), kernel_initializer = my_init)
  6. )
复制代码
约束条件 (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.
将权重限制为非负数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import initializers
  4. my_init = initializers.Identity(gain = 1.0) model.add(
  5.    Dense(512, activation = 'relu', input_shape = (784,),
  6.    kernel_initializer = my_init)
  7. )
复制代码
where, kernel_constraint represent the constraint to be used in the layer.
其中, kernel_constraint表示要在层中使用的约束。
单位范数 (UnitNorm)

Constrains weights to be unit norm.
将权重约束为单位范数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import constraints
  4. my_constrain = constraints.UnitNorm(axis = 0)
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_constraint = my_constrain))
复制代码
最大规范 (MaxNorm)

Constrains weight to norm less than or equals to the given value.
将权重限制为小于或等于给定值的范数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import constraints
  4. my_constrain = constraints.MaxNorm(max_value = 2, axis = 0)
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_constraint = my_constrain))
复制代码
where,
哪里,
    max_value represent the upper bound
    max_value代表上限
    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
    表示要应用约束的尺寸。 例如在形状(2,3,4)轴中0表示第一尺寸,1表示第二尺寸,2表示第三尺寸
最小最大范数 (MinMaxNorm)

Constrains weights to be norm between specified minimum and maximum values.
将权重限制在指定的最小值和最大值之间。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import constraints
  4. my_constrain = constraints.MinMaxNorm(min_value = 0.0, max_value = 1.0, rate = 1.0, axis = 0)
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_constraint = my_constrain))
复制代码
where, rate represent the rate at which the weight constrain is applied.
其中, rate表示施加重量约束的速率。
正则化器 (Regularizers)

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.
在机器学习中,正则化器用于优化阶段。 在优化过程中会对层参数施加一些惩罚。 Keras正则化模块提供以下功能来设置层上的惩罚。 正则化仅适用于每层。
L1正则器 (L1 Regularizer)

It provides L1 based regularization.
它提供基于L1的正则化。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import regularizers
  4. my_regularizer = regularizers.l1(0.)
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_regularizer = my_regularizer))
复制代码
where, kernel_regularizer represent the rate at which the weight constrain is applied.
其中, kernel_regularizer表示施加权重约束的速率。
L2调节器 (L2 Regularizer)

It provides L2 based regularization.
它提供基于L2的正则化。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import regularizers
  4. my_regularizer = regularizers.l2(0.)
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    kernel_regularizer = my_regularizer))
复制代码
L1和L2正则器 (L1 and L2 Regularizer)

It provides both L1 and L2 based regularization.
它提供了基于L1和L2的正则化。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. from keras import regularizers
  4. my_regularizer = regularizers.l2(0.)
  5. model = Sequential()
  6. model.add(Dense(512, activation = 'relu', input_shape = (784,),
  7.    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.
当您回想起单感知的概念时,感知器(神经元)的输出只是激活函数的结果,该函数接受所有输入的总和乘以其相应的权重加上总体偏差(如果有)。
  1. result = Activation(SUMOF(input * weight) + bias)
复制代码
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.
因此,激活函数在模型的成功学习中起着重要作用。 Keras在激活模块中提供了很多激活功能。 让我们学习该模块中所有可用的激活。
线性的 (linear)

Applies Linear function. Does nothing.
应用线性函数。 什么也没做。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'linear', input_shape = (784,)))
复制代码
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.
其中, 激活是指该层的激活功能。 可以简单地通过函数名称指定它,并且该层将使用相应的激活器。
lu (elu)

Applies Exponential linear unit.
应用指数线性单位。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'elu', input_shape = (784,)))
复制代码
selu (selu)

Applies Scaled exponential linear unit.
应用比例指数线性单位。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'selu', input_shape = (784,)))
复制代码
relu (relu)

Applies Rectified Linear Unit.
应用整流线性单位。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'relu', input_shape = (784,)))
复制代码
softmax (softmax)

Applies Softmax function.
应用Softmax功能。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'softmax', input_shape = (784,)))
复制代码
softplus (softplus)

Applies Softplus function.
应用Softplus功能。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'softplus', input_shape = (784,)))
复制代码
softsign (softsign)

Applies Softsign function.
应用软件签名功能。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'softsign', input_shape = (784,)))
复制代码
tanh (tanh)

Applies Hyperbolic tangent function.
应用双曲正切函数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'tanh', input_shape = (784,)))
复制代码
sigmod (sigmoid)

Applies Sigmoid function.
应用Sigmoid函数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'sigmoid', input_shape = (784,)))
复制代码
硬sigmod (hard_sigmoid)

Applies Hard Sigmoid function.
应用硬Sigmoid函数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'hard_sigmoid', input_shape = (784,)))
复制代码
指数的 (exponential)

Applies exponential function.
应用指数函数。
  1. from keras.models import Sequential
  2. from keras.layers import Activation, Dense
  3. model = Sequential()
  4. model.add(Dense(512, activation = 'exponential', input_shape = (784,)))
复制代码
Sr.NoLayers & Description
1Dense Layer
Dense layer is the regular deeply connected neural network layer.
2Dropout Layers
Dropout is one of the important concept in the machine learning.
3Flatten Layers
Flatten is used to flatten the input.
4Reshape Layers
Reshape is used to change the shape of the input.
5Permute Layers
Permute is also used to change the shape of the input using pattern.
6RepeatVector Layers
RepeatVector is used to repeat the input for set number, n of times.
7Lambda Layers
Lambda is used to transform the input data using an expression or function.
8Convolution Layers
Keras contains a lot of layers for creating Convolution based ANN, popularly called as Convolution Neural Network (CNN).
9Pooling Layer
It is used to perform max pooling operations on temporal data.
10Locally connected layer
Locally connected layers are similar to Conv1D layer but the difference is Conv1D layer weights are shared but here weights are unshared.
11Merge Layer
It is used to merge a list of inputs.
12Embedding Layer
It performs embedding operations in input layer.
序号 图层和说明
1个 致密层
密集层是规则的深度连接神经网络层。
2 辍学层
辍学是机器学习中的重要概念之一。
3 展平层
展平被用来压平的输入端。
4 重塑图层
重塑是用来改变所述输入的形状。
5 置换层
Permute还用于使用模式更改输入的形状。
6 RepeatVector图层
RepeatVector用于将输入重复设置次数n次。
7 Lambda图层
Lambda用于使用表达式或函数来转换输入数据。
8 卷积层
Keras包含用于创建基于卷积的ANN的许多层,通常被称为卷积神经网络(CNN)
9 池化层
它用于对时间数据执行最大池化操作。
10 本地连接层
本地连接的图层与Conv1D图层相似,但区别在于Conv1D图层的权重是共享的,但此处的权重是不共享的。
11 合并层
它用于合并输入列表。
12 嵌入层
它在输入层中执行嵌入操作。

Keras-定制层 (Keras - Customized Layer)

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允许创建我们自己的定制层。 一旦创建了新层,就可以在任何模型中使用它,而没有任何限制。 让我们在本章中学习如何创建新层。
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.
Keras提供了一个基础类,即Layer,可以对其进行子分类以创建我们自己的自定义层。 让我们创建一个简单的层,该层将根据正态分布查找权重,然后进行基本计算,以找到训练期间输入乘积及其权重的总和。
步骤1:导入必要的模块 (Step 1: Import the necessary module)

First, let us import the necessary modules ?
首先,让我们导入必要的模块-
  1. from keras import backend as K
  2. from keras.layers import Layer
复制代码
Here,
这里,
    backend is used to access the dot function.
    后端用于访问功能。
    Layer is the base class and we will be sub-classing it to create our layer
    图层是基类,我们将其子类化以创建图层
步骤2:定义图层类别 (Step 2: Define a layer class)

Let us create a new class, MyCustomLayer by sub-classing Layer class ?
让我们通过对Layer类进行子类化来创建一个新类MyCustomLayer-
  1. class MyCustomLayer(Layer):
  2.    ...
复制代码
步骤3:初始化图层类 (Step 3: Initialize the layer class)

Let us initialize our new class as specified below ?
让我们初始化我们的新类,如下所示:
  1. def __init__(self, output_dim, **kwargs):   
  2.    self.output_dim = output_dim
  3.    super(MyCustomLayer, self).__init__(**kwargs)
复制代码
Here,
这里,
    Line 2 sets the output dimension.
    第2行设置输出尺寸。
    Line 3 calls the base or super layer’s init function.
    第3行调用基础或超级层的init函数。
步骤4:实施构建方法 (Step 4: Implement build method)

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 ?
build是主要方法,其唯一目的是正确构建图层。 它可以做任何与图层内部工作有关的事情。 完成自定义功能后,我们可以调用基类构建功能。 我们的自定义构建函数如下-
  1. def build(self, input_shape):
  2.    self.kernel = self.add_weight(name = 'kernel',
  3.       shape = (input_shape[1], self.output_dim),
  4.       initializer = 'normal', trainable = True)
  5.    super(MyCustomLayer, self).build(input_shape)
复制代码
Here,
这里,
    Line 1 defines the build method with one argument, input_shape. Shape of the input data is referred by input_shape.
    第1行使用一个参数input_shape定义了构建方法。 输入数据的形状由input_shape引用。
    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.
    第2行创建对应于输入形状的权重并将其设置在内核中。 这是我们层的自定义功能。 它使用“普通”初始化程序创建权重。
    Line 6 calls the base class, build method.
    第6行调用基类build方法。
步骤5:实现调用方法 (Step 5: Implement call method)

call method does the exact working of the layer during training process.
call方法会在训练过程中精确执行图层的工作。
Our custom call method is as follows
我们的自定义调用方法如下
  1. def call(self, input_data):
  2.    return K.dot(input_data, self.kernel)
复制代码
Here,
这里,
    Line 1 defines the call method with one argument, input_data. input_data is the input data for our layer.
    第1行使用一个参数input_data定义了调用方法。 input_data是我们图层的输入数据。
    Line 2 return the dot product of the input data, input_data and our layer’s kernel, self.kernel
    第2行返回输入数据input_data与我们层的内核self.kernel的点积
步骤6:实现compute_output_shape方法 (Step 6: Implement compute_output_shape method)
  1. def compute_output_shape(self, input_shape): return (input_shape[0], self.output_dim)
复制代码
Here,
这里,
    Line 1 defines compute_output_shape method with one argument input_shape
    第1使用一个参数input_shape定义了compute_output_shape方法
    Line 2 computes the output shape using shape of input data and output dimension set while initializing the layer.
    第2行在初始化图层时使用输入数据的形状和设置的输出尺寸来计算输出形状。
Implementing the build, call and compute_output_shape completes the creating a customized layer. The final and complete code is as follows
实现build,callcompute_output_shape将完成创建自定义层。 最终的完整代码如下
  1. from keras import backend as K from keras.layers import Layer
  2. class MyCustomLayer(Layer):
  3.    def __init__(self, output_dim, **kwargs):
  4.       self.output_dim = output_dim
  5.       super(MyCustomLayer, self).__init__(**kwargs)
  6.    def build(self, input_shape): self.kernel =
  7.       self.add_weight(name = 'kernel',
  8.       shape = (input_shape[1], self.output_dim),
  9.       initializer = 'normal', trainable = True)
  10.       super(MyCustomLayer, self).build(input_shape) #
  11.       Be sure to call this at the end
  12.    def call(self, input_data): return K.dot(input_data, self.kernel)
  13.    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 ?
让我们使用下面指定的定制层创建一个简单的模型-
  1. from keras.models import Sequential
  2. from keras.layers import Dense
  3. model = Sequential()
  4. model.add(MyCustomLayer(32, input_shape = (16,)))
  5. model.add(Dense(8, activation = 'softmax')) model.summary()
复制代码
Here,
这里,
    Our MyCustomLayer is added to the model using 32 units and (16,) as input shape
    我们的MyCustomLayer使用32个单位和(16,)作为输入形状添加到模型中
Running the application will print the model summary as below ?
运行应用程序将打印模型摘要,如下所示:
  1. Model: "sequential_1"
  2. _________________________________________________________________
  3. Layer (type) Output Shape Param
  4. #================================================================
  5. my_custom_layer_1 (MyCustomL (None, 32) 512
  6. _________________________________________________________________
  7. dense_1 (Dense) (None, 8) 264
  8. =================================================================
  9. Total params: 776
  10. Trainable params: 776
  11. Non-trainable params: 0
  12. _________________________________________________________________
复制代码
Keras-型号 (Keras - Models)

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.
如前所述,Keras模型代表实际的神经网络模型。 Keras提供了两种创建模型的模式:简单易用的Sequential API以及更灵活,更高级的Functional API 。 现在让我们学习本章中同时使用顺序 API和功能 API创建模型的方法。
顺序的 (Sequential)

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.
顺序API的核心思想是简单地按顺序排列Keras层,因此,它称为顺序API 。 大部分的ANN还具有顺序排列的层,并且数据以给定的顺序从一层流到另一层,直到数据最终到达输出层。
A ANN model can be created by simply calling Sequential() API as specified below ?
可以通过简单地调用Sequential() API来创建ANN模型,如下所示:
  1. from keras.models import Sequential
  2. model = Sequential()
复制代码
添加图层 (Add layers)

To add a layer, simply create a layer using Keras layer API and then pass the layer through add() function as specified below ?
要添加层,只需使用Keras层API创建一个层,然后将该层通过add()函数传递,如下所示-
  1. from keras.models import Sequential
  2. model = Sequential()
  3. input_layer = Dense(32, input_shape=(8,)) model.add(input_layer)
  4. hidden_layer = Dense(64, activation='relu'); model.add(hidden_layer)
  5. output_layer = Dense(8)
  6. model.add(output_layer)
复制代码
Here, we have created one input layer, one hidden layer and one output layer.
在这里,我们创建了一个输入层,一个隐藏层和一个输出层。
访问模型 (Access the model)

Keras provides few methods to get the model information like layers, input data and output data. They are as follows ?
Keras提供了很少的方法来获取模型信息,例如图层,输入数据和输出数据。 它们如下-
    model.layers ? Returns all the layers of the model as list.
    model.layers-将模型的所有层作为列表返回。
  1. >>> layers = model.layers
  2. >>> layers
  3. [
  4.    <keras.layers.core.Dense object at 0x000002C8C888B8D0>,
  5.    <keras.layers.core.Dense object at 0x000002C8C888B7B8>
  6.    <keras.layers.core.Dense object at 0x 000002C8C888B898>
  7. ]
复制代码
    model.inputs ? Returns all the input tensors of the model as list.
    model.inputs-将模型的所有输入张量作为列表返回。
  1. >>> inputs = model.inputs
  2. >>> inputs
  3. [<tf.Tensor 'dense_13_input:0' shape=(?, 8) dtype=float32>]
复制代码
    model.outputs ? Returns all the output tensors of the model as list.
    model.outputs-将模型的所有输出张量作为列表返回。
  1. >>> outputs = model.outputs
  2. >>> outputs
  3. <tf.Tensor 'dense_15/BiasAdd:0' shape=(?, 8) dtype=float32>]
复制代码
    model.get_weights ? Returns all the weights as NumPy arrays.
    model.get_weights-将所有权重作为NumPy数组返回。
    model.set_weights(weight_numpy_array) ? Set the weights of the model.
    model.set_weights(weight_numpy_array) -设置模型的权重。
序列化模型 (Serialize the model)

Keras provides methods to serialize the model into object as well as json and load it again later. They are as follows ?
Keras提供了将模型序列化为对象以及json并在以后再次加载的方法。 它们如下-
    get_config() ? IReturns the model as an object.
    get_config() -I将模型作为对象返回。
  1. config = model.get_config()
复制代码
    from_config() ? It accept the model configuration object as argument and create the model accordingly.
    from_config() -接受模型配置对象作为参数并相应地创建模型。
  1. new_model = Sequential.from_config(config)
复制代码
    to_json() ? Returns the model as an json object.
    to_json() -将模型作为json对象返回。
  1. >>> json_string = model.to_json()
  2. >>> json_string '{"class_name": "Sequential", "config":
  3. {"name": "sequential_10", "layers":
  4. [{"class_name": "Dense", "config":
  5. {"name": "dense_13", "trainable": true, "batch_input_shape":
  6. [null, 8], "dtype": "float32", "units": 32, "activation": "linear",
  7. "use_bias": true, "kernel_initializer":
  8. {"class_name": "Vari anceScaling", "config":
  9. {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}},
  10. "bias_initializer": {"class_name": "Zeros", "conf
  11. ig": {}}, "kernel_regularizer": null, "bias_regularizer": null,
  12. "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}},
  13. {" class_name": "Dense", "config": {"name": "dense_14", "trainable": true,
  14. "dtype": "float32", "units": 64, "activation": "relu", "use_bias": true,
  15. "kern el_initializer": {"class_name": "VarianceScaling", "config":
  16. {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}},
  17. "bias_initia lizer": {"class_name": "Zeros",
  18. "config": {}}, "kernel_regularizer": null, "bias_regularizer": null,
  19. "activity_regularizer": null, "kernel_constraint" : null, "bias_constraint": null}},
  20. {"class_name": "Dense", "config": {"name": "dense_15", "trainable": true,
  21. "dtype": "float32", "units": 8, "activation": "linear", "use_bias": true,
  22. "kernel_initializer": {"class_name": "VarianceScaling", "config":
  23. {"scale": 1.0, "mode": "fan_avg", "distribution": " uniform", "seed": null}},
  24. "bias_initializer": {"class_name": "Zeros", "config": {}},
  25. "kernel_regularizer": null, "bias_regularizer": null, "activity_r egularizer":
  26. null, "kernel_constraint": null, "bias_constraint":
  27. null}}]}, "keras_version": "2.2.5", "backend": "tensorflow"}'
  28. >>>
复制代码
    model_from_json() ? Accepts json representation of the model and create a new model.
    model_from_json() -接受模型的json表示并创建一个新模型。
  1. from keras.models import model_from_json
  2. new_model = model_from_json(json_string)
复制代码
    to_yaml() ? Returns the model as a yaml string.
    to_yaml() -以yaml字符串形式返回模型。
  1. >>> yaml_string = model.to_yaml()
  2. >>> yaml_string 'backend: tensorflow\nclass_name:
  3. Sequential\nconfig:\n layers:\n - class_name: Dense\n config:\n
  4. activation: linear\n activity_regular izer: null\n batch_input_shape:
  5. !!python/tuple\n - null\n - 8\n bias_constraint: null\n bias_initializer:\n
  6. class_name : Zeros\n config: {}\n bias_regularizer: null\n dtype:
  7. float32\n kernel_constraint: null\n
  8. kernel_initializer:\n cla ss_name: VarianceScaling\n config:\n
  9. distribution: uniform\n mode: fan_avg\n
  10. scale: 1.0\n seed: null\n kernel_regularizer: null\n name: dense_13\n
  11. trainable: true\n units: 32\n
  12. use_bias: true\n - class_name: Dense\n config:\n activation: relu\n activity_regularizer: null\n
  13. bias_constraint: null\n bias_initializer:\n class_name: Zeros\n
  14. config : {}\n bias_regularizer: null\n dtype: float32\n
  15. kernel_constraint: null\n kernel_initializer:\n class_name: VarianceScalin g\n
  16. config:\n distribution: uniform\n mode: fan_avg\n scale: 1.0\n
  17. seed: null\n kernel_regularizer: nu ll\n name: dense_14\n trainable: true\n
  18. units: 64\n use_bias: true\n - class_name: Dense\n config:\n
  19. activation: linear\n activity_regularizer: null\n
  20. bias_constraint: null\n bias_initializer:\n
  21. class_name: Zeros\n config: {}\n bias_regu larizer: null\n
  22. dtype: float32\n kernel_constraint: null\n
  23. kernel_initializer:\n class_name: VarianceScaling\n config:\n
  24. distribution: uniform\n mode: fan_avg\n
  25. scale: 1.0\n seed: null\n kernel_regularizer: null\n name: dense _15\n
  26. trainable: true\n units: 8\n
  27. use_bias: true\n name: sequential_10\nkeras_version: 2.2.5\n'
  28. >>>
复制代码
    model_from_yaml() ? Accepts yaml representation of the model and create a new model.
    model_from_yaml() -接受模型的yaml表示并创建一个新模型。
  1. from keras.models import model_from_yaml
  2. new_model = model_from_yaml(yaml_string)
复制代码
总结模型 (Summarise the model)

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.
理解模型是正确使用模型进行训练和预测的非常重要的阶段。 Keras提供了一种简单的方法,即摘要以获取有关模型及其层的完整信息。
A summary of the model created in the previous section is as follows ?
上一节中创建的模型的摘要如下-
  1. >>> model.summary() Model: "sequential_10"
  2. _________________________________________________________________
  3. Layer (type) Output Shape Param
  4. #================================================================
  5. dense_13 (Dense) (None, 32) 288
  6. _________________________________________________________________
  7. dense_14 (Dense) (None, 64) 2112
  8. _________________________________________________________________
  9. dense_15 (Dense) (None, 8) 520
  10. =================================================================
  11. Total params: 2,920
  12. Trainable params: 2,920
  13. Non-trainable params: 0
  14. _________________________________________________________________
  15. >>>
复制代码
训练和预测模型 (Train and Predict the model)

Model provides function for training, evaluation and prediction process. They are as follows ?
模型为训练,评估和预测过程提供功能。 它们如下-
    compile ? Configure the learning process of the model
    编译 -配置模型的学习过程
    fit ? Train the model using the training data
    拟合 -使用训练数据训练模型
    evaluate ? Evaluate the model using the test data
    评估 -使用测试数据评估模型
    predict ? Predict the results for new input.
    预测 -预测新输入的结果。
功能性API (Functional 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.
顺序API用于逐层创建模型。 功能API是创建更复杂模型的替代方法。 在功能模型中,您可以定义共享图层的多个输入或输出。 首先,我们为模型创建一个实例,并连接到图层以访问模型的输入和输出。 本节简要介绍功能模型。
建立模型 (Create a model)

Import an input layer using the below module ?
使用以下模块导入输入层-
  1. >>> from keras.layers import Input
复制代码
Now, create an input layer specifying input dimension shape for the model using the below code ?
现在,使用以下代码创建一个输入层,指定模型的输入尺寸形状-
  1. >>> data = Input(shape=(2,3))
复制代码
Define layer for the input using the below module ?
使用以下模块为输入定义层-
  1. >>> from keras.layers import Dense
复制代码
Add Dense layer for the input using the below line of code ?
使用下面的代码行为输入添加密集层-
  1. >>> layer = Dense(2)(data)
  2. >>> print(layer)
  3. Tensor("dense_1/add:0", shape =(?, 2, 2), dtype = float32)
复制代码
Define model using the below module ?
使用以下模块定义模型-
  1. from keras.models import Model
复制代码
Create a model in functional way by specifying both input and output layer ?
通过指定输入和输出层以功能方式创建模型-
  1. model = Model(inputs = data, outputs = layer)
复制代码
The complete code to create a simple model is shown below ?
创建简单模型的完整代码如下所示-
  1. from keras.layers import Input
  2. from keras.models import Model
  3. from keras.layers import Dense
  4. data = Input(shape=(2,3))
  5. layer = Dense(2)(data) model =
  6. Model(inputs=data,outputs=layer) model.summary()
  7. _________________________________________________________________
  8. Layer (type)               Output Shape               Param #
  9. =================================================================
  10. input_2 (InputLayer)       (None, 2, 3)               0
  11. _________________________________________________________________
  12. dense_2 (Dense)            (None, 2, 2)               8
  13. =================================================================
  14. Total params: 8
  15. Trainable params: 8
  16. Non-trainable params: 0
  17. _________________________________________________________________
复制代码
Keras-模型编译 (Keras - Model Compilation)

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.
以前,我们研究了如何使用顺序和功能API创建模型的基础知识。 本章介绍如何编译模型。 编译是创建模型的最后一步。 编译完成后,我们可以进入训练阶段。
Let us learn few concepts required to better understand the compilation process.
让我们学习一些概念,以更好地理解编译过程。
失利 (Loss)

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在模型编译过程中需要损失函数。
Keras provides quite a few loss function in the losses module and they are as follows ?
Keras在损失模块中提供了很多损失函数,它们如下-
    mean_squared_error mean_squared_error mean_absolute_error mean_absolute_error mean_absolute_percentage_error mean_absolute_percentage_error mean_squared_logarithmic_error mean_squared_logarithmic_error squared_hinge squared_hinge hinge 合页 categorical_hinge categorical_hinge logcosh Logcosh huber_loss huber_loss categorical_crossentropy categorical_crossentropy sparse_categorical_crossentropy sparse_categorical_crossentropy binary_crossentropy 二元交叉熵 kullback_leibler_divergence kullback_leibler_divergence poisson 泊松 cosine_proximity cosine_proximity is_categorical_crossentropy is_categorical_crossentropy
All above loss function accepts two arguments ?
以上所有损失函数都接受两个参数-
    y_true ? true labels as tensors
    y_true -true标签为张量
    y_pred ? prediction with same shape as y_true
    y_pred -预测与相同形状y_true
Import the losses module before using loss function as specified below ?
在使用以下指定的损失函数之前,请导入损失模块-
  1. from keras import losses
复制代码
优化器 (Optimizer)

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:
在机器学习中, 优化是通过比较预测函数和损失函数来优化输入权重的重要过程。 Keras提供了许多优化器作为模块,这些优化器如下:
SGD ? Stochastic gradient descent optimizer.
SGD-随机梯度下降优化器。
  1. keras.optimizers.SGD(learning_rate = 0.01, momentum = 0.0, nesterov = False)
复制代码
RMSprop ? RMSProp optimizer.
RMSprop -RMSProp优化器。
  1. keras.optimizers.RMSprop(learning_rate = 0.001, rho = 0.9)
复制代码
Adagrad ? Adagrad optimizer.
Adagrad -Adagrad优化器。
  1. keras.optimizers.Adagrad(learning_rate = 0.01)
复制代码
Adadelta ? Adadelta optimizer.
Adadelta -Adadelta优化器。
  1. keras.optimizers.Adadelta(learning_rate = 1.0, rho = 0.95)
复制代码
Adam ? Adam optimizer.
Adam -Adam优化器。
  1. keras.optimizers.Adam(
  2.    learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, amsgrad = False
  3. )
复制代码
Adamax ? Adamax optimizer from Adam.
Adamax -Adam的Adamax优化器。
  1. keras.optimizers.Adamax(learning_rate = 0.002, beta_1 = 0.9, beta_2 = 0.999)
复制代码
Nadam ? Nesterov Adam optimizer.
Nadam -Nesterov Adam优化器。
  1. 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 ?
如下所示使用优化器之前,请导入优化器模块-
  1. from keras import optimizers
复制代码
指标 (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
在机器学习中, 指标用于评估模型的性能。 它类似于损失函数,但未在训练过程中使用。 Keras提供了很多度量作为模块,这些度量如下:
    accuracy 准确性 binary_accuracy binary_accuracy categorical_accuracy categorical_accuracy sparse_categorical_accuracy sparse_categorical_accuracy top_k_categorical_accuracy top_k_categorical_accuracy sparse_top_k_categorical_accuracy sparse_top_k_categorical_accuracy cosine_proximity cosine_proximity clone_metric clone_metric
Similar to loss function, metrics also accepts below two arguments ?
与损失函数类似,指标还接受以下两个参数-
    y_true ? true labels as tensors
    y_true -true标签为张量
    y_pred ? prediction with same shape as y_true
    y_pred -预测与相同形状y_true
Import the metrics module before using metrics as specified below ?
在使用指标之前,请导入指标模块,如下所示:
  1. from keras import metrics
复制代码
编译模型 (Compile the model)

Keras model provides a method, compile() to compile the model. The argument and default value of the compile() method is as follows
Keras模型提供了一种方法compile()来编译模型。 compile()方法的参数和默认值如下
  1. compile(
  2.    optimizer,
  3.    loss = None,
  4.    metrics = None,
  5.    loss_weights = None,
  6.    sample_weight_mode = None,
  7.    weighted_metrics = None,
  8.    target_tensors = None
  9. )
复制代码
The important arguments are as follows ?
重要的论据如下-
    loss function 损失函数 Optimizer 优化器 metrics 指标
A sample code to compile the mode is as follows ?
编译模式的示例代码如下-
  1. from keras import losses
  2. from keras import optimizers
  3. from keras import metrics
  4. model.compile(loss = 'mean_squared_error',  
  5.    optimizer = 'sgd', metrics = [metrics.categorical_accuracy])
复制代码
where,
哪里,
    loss function is set as mean_squared_error
    损失函数设置为mean_squared_error
    optimizer is set as sgd
    优化器设置为sgd
    metrics is set as metrics.categorical_accuracy
    指标设置为metrics.categorical_accuracy
模型训练 (Model Training)

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 ?
NumPy数组使用fit()训练模型。 该拟合函数的主要目的是用于评估训练模型。 这也可以用于绘制模型性能。 它具有以下语法-
  1. model.fit(X, y, epochs = , batch_size = )
复制代码
Here,
这里,
    X, y ? It is a tuple to evaluate your data.
    X,y-这是评估您的数据的元组。
    epochs ? no of times the model is needed to be evaluated during training.
    时期 -在训练期间无需评估模型的次数。
    batch_size ? training instances.
    batch_size-训练实例。
Let us take a simple example of numpy random data to use this concept.
让我们举一个简单的numpy随机数据示例来使用此概念。
建立资料 (Create data)

Let us create a random data using numpy for x and y with the help of below mentioned command ?
让我们借助下面提到的命令使用numpy为x和y创建随机数据-
  1. import numpy as np
  2. x_train = np.random.random((100,4,8))
  3. y_train = np.random.random((100,10))
复制代码
Now, create random validation data,
现在,创建随机验证数据,
  1. x_val = np.random.random((100,4,8))
  2. y_val = np.random.random((100,10))
复制代码
建立模型 (Create model)

Let us create simple sequential model ?
让我们创建简单的顺序模型-
  1. from keras.models import Sequential model = Sequential()
复制代码
添加图层 (Add layers)

Create layers to add model ?
创建图层以添加模型-
  1. from keras.layers import LSTM, Dense
  2. # add a sequence of vectors of dimension 16
  3. model.add(LSTM(16, return_sequences = True))
  4. model.add(Dense(10, activation = 'softmax'))
复制代码
编译模型 (compile model)

Now model is defined. You can compile using the below command ?
现在定义模型。 您可以使用以下命令进行编译-
  1. model.compile(
  2.    loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy']
  3. )
复制代码
套用fit() (Apply fit())

Now we apply fit() function to train our data ?
现在我们应用fit()函数来训练我们的数据-
  1. model.fit(x_train, y_train, batch_size = 32, epochs = 5, validation_data = (x_val, y_val))
复制代码
创建多层感知器ANN (Create a Multi-Layer Perceptron ANN)

We have learned to create, compile and train the Keras models.
我们已经学会了创建,编译和训练Keras模型。
Let us apply our learning and create a simple MPL based ANN.
让我们运用我们的学习知识,创建一个基于MPL的简单ANN。
数据集模块 (Dataset module)

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,
在创建模型之前,我们需要选择一个问题,需要收集所需的数据并将数据转换为NumPy数组。 收集数据后,我们可以准备模型并使用收集的数据进行训练。 数据收集是机器学习最困难的阶段之一。 Keras提供了一个特殊的模块,即数据集,用于下载在线机器学习数据以进行培训。 它从在线服务器获取数据,处理数据,并将数据作为训练和测试集返回。 让我们检查Keras数据集模块提供的数据。 模块中可用的数据如下,
    CIFAR10 small image classification CIFAR10小图像分类 CIFAR100 small image classification CIFAR100小图像分类 IMDB Movie reviews sentiment classification IMDB电影评论情感分类 Reuters newswire topics classification 路透社新闻分类 MNIST database of handwritten digits MNIST手写数字数据库 Fashion-MNIST database of fashion articles Fashion-MNIST时尚文章数据库 Boston housing price regression dataset 波士顿房屋价格回归数据集
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.
让我们使用手写数字 (或最低)的MNIST数据库作为输入。 minst是60,000张28x28灰度图像的集合。 它包含10位数字。 它还包含10,000个测试图像。
Below code can be used to load the dataset ?
以下代码可用于加载数据集-
  1. from keras.datasets import mnist
  2. (x_train, y_train), (x_test, y_test) = mnist.load_data()
复制代码
where
哪里
    Line 1 imports minst from the keras dataset module.
    第1从keras数据集模块导入minst
    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.
    第3行调用load_data函数,该函数将从在线服务器中获取数据并以2个元组的形式返回数据。第一个元组(x_train,y_train)表示形状为(number_sample,28,28)的训练数据,其数字标签为形状(number_samples,) 。 第二个元组(x_test,y_test)表示具有相同形状的测试数据。
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.
还可以使用类似的API来获取其他数据集,并且每个API都返回相似的数据,但数据的形状除外。 数据的形状取决于数据的类型。
建立模型 (Create a model)

Let us choose a simple multi-layer perceptron (MLP) as represented below and try to create the model using Keras.
让我们选择一个简单的多层感知器(MLP),如下所示,并尝试使用Keras创建模型。



The core features of the model are as follows ?
该模型的核心特征如下-
    Input layer consists of 784 values (28 x 28 = 784).
    输入层包含784个值(28 x 28 = 784)。
    First hidden layer, Dense consists of 512 neurons and ‘relu’ activation function.
    第一个隐藏层Dense由512个神经元和“ relu”激活功能组成。
    Second hidden layer, Dropout has 0.2 as its value.
    第二个隐藏层Dropout的值为0.2。
    Third hidden layer, again Dense consists of 512 neurons and ‘relu’ activation function.
    第三个隐藏层,又是Dense,由512个神经元和“ relu”激活功能组成。
    Fourth hidden layer, Dropout has 0.2 as its value.
    第四隐藏层, Dropout的值为0.2。
    Fifth and final layer consists of 10 neurons and ‘softmax’ activation function.
    第五层也是最后一层由10个神经元和“ softmax”激活功能组成。
    Use categorical_crossentropy as loss function.
    使用categorical_crossentropy作为损失函数。
    Use RMSprop() as Optimizer.
    使用RMSprop()作为优化程序。
    Use accuracy as metrics.
    使用准确性作为指标。
    Use 128 as batch size.
    使用128作为批处理大小。
    Use 20 as epochs.
    使用20作为时代。
Step 1 ? Import the modules
步骤1-导入模块
Let us import the necessary modules.
让我们导入必要的模块。
  1. import keras
  2. from keras.datasets import mnist
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout
  5. from keras.optimizers import RMSprop
  6. import numpy as np
复制代码
Step 2 ? Load data
第2步-加载数据
Let us import the mnist dataset.
让我们导入mnist数据集。
  1. (x_train, y_train), (x_test, y_test) = mnist.load_data()
复制代码
Step 3 ? Process the data
步骤3-处理数据
Let us change the dataset according to our model, so that it can be feed into our model.
让我们根据我们的模型更改数据集,以便可以将其输入到我们的模型中。
  1. x_train = x_train.reshape(60000, 784)
  2. x_test = x_test.reshape(10000, 784)
  3. x_train = x_train.astype('float32')
  4. x_test = x_test.astype('float32')
  5. x_train /= 255
  6. x_test /= 255
  7. y_train = keras.utils.to_categorical(y_train, 10)
  8. y_test = keras.utils.to_categorical(y_test, 10)
复制代码
Where
哪里
    reshape is used to reshape the input from (28, 28) tuple to (784, )
    reshape用于将输入从(28,28)元组调整为(784,)
    to_categorical is used to convert vector to binary matrix
    to_categorical用于将向量转换为二进制矩阵
Step 4 ? Create the model
步骤4-创建模型
Let us create the actual model.
让我们创建实际的模型。
  1. model = Sequential()
  2. model.add(Dense(512, activation = 'relu', input_shape = (784,)))
  3. model.add(Dropout(0.2))
  4. model.add(Dense(512, activation = 'relu'))
  5. model.add(Dropout(0.2))
  6. model.add(Dense(10, activation = 'softmax'))
复制代码
Step 5 ? Compile the model
第5步-编译模型
Let us compile the model using selected loss function, optimizer and metrics.
让我们使用选定的损失函数,优化器和指标来编译模型。
  1. model.compile(loss = 'categorical_crossentropy',     
  2.    optimizer = RMSprop(),
  3.    metrics = ['accuracy'])
复制代码
Step 6 ? Train the model
步骤6-训练模型
Let us train the model using fit() method.
让我们使用fit()方法训练模型。
  1. history = model.fit(
  2.    x_train, y_train,
  3.    batch_size = 128,
  4.    epochs = 20,
  5.    verbose = 1,
  6.    validation_data = (x_test, y_test)
  7. )
复制代码
最后的想法 (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.
我们已经创建了模型,加载了数据,并将数据训练到了模型中。 我们仍然需要评估模型并预测未知输入的输出,这将在下一章中学习。
  1. import keras
  2. from keras.datasets import mnist
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout
  5. from keras.optimizers import RMSprop
  6. import numpy as np
  7. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  8. x_train = x_train.reshape(60000, 784)
  9. x_test = x_test.reshape(10000, 784)
  10. x_train = x_train.astype('float32')
  11. x_test = x_test.astype('float32')
  12. x_train /= 255
  13. x_test /= 255
  14. y_train = keras.utils.to_categorical(y_train, 10)
  15. y_test = keras.utils.to_categorical(y_test, 10)
  16. model = Sequential()
  17. model.add(Dense(512, activation='relu', input_shape = (784,)))
  18. model.add(Dropout(0.2))
  19. model.add(Dense(512, activation = 'relu')) model.add(Dropout(0.2))
  20. model.add(Dense(10, activation = 'softmax'))
  21. model.compile(loss = 'categorical_crossentropy',
  22.    optimizer = RMSprop(),
  23.    metrics = ['accuracy'])
  24. history = model.fit(x_train, y_train,
  25.    batch_size = 128, epochs = 20, verbose = 1, validation_data = (x_test, y_test))
复制代码
Executing the application will give the below content as output ?
执行应用程序将给出以下内容作为输出-
  1. Train on 60000 samples, validate on 10000 samples Epoch 1/20
  2. 60000/60000 [==============================] - 7s 118us/step - loss: 0.2453
  3. - acc: 0.9236 - val_loss: 0.1004 - val_acc: 0.9675 Epoch 2/20
  4. 60000/60000 [==============================] - 7s 110us/step - loss: 0.1023
  5. - acc: 0.9693 - val_loss: 0.0797 - val_acc: 0.9761 Epoch 3/20
  6. 60000/60000 [==============================] - 7s 110us/step - loss: 0.0744
  7. - acc: 0.9770 - val_loss: 0.0727 - val_acc: 0.9791 Epoch 4/20
  8. 60000/60000 [==============================] - 7s 110us/step - loss: 0.0599
  9. - acc: 0.9823 - val_loss: 0.0704 - val_acc: 0.9801 Epoch 5/20
  10. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0504
  11. - acc: 0.9853 - val_loss: 0.0714 - val_acc: 0.9817 Epoch 6/20
  12. 60000/60000 [==============================] - 7s 111us/step - loss: 0.0438
  13. - acc: 0.9868 - val_loss: 0.0845 - val_acc: 0.9809 Epoch 7/20
  14. 60000/60000 [==============================] - 7s 114us/step - loss: 0.0391
  15. - acc: 0.9887 - val_loss: 0.0823 - val_acc: 0.9802 Epoch 8/20
  16. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0364
  17. - acc: 0.9892 - val_loss: 0.0818 - val_acc: 0.9830 Epoch 9/20
  18. 60000/60000 [==============================] - 7s 113us/step - loss: 0.0308
  19. - acc: 0.9905 - val_loss: 0.0833 - val_acc: 0.9829 Epoch 10/20
  20. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0289
  21. - acc: 0.9917 - val_loss: 0.0947 - val_acc: 0.9815 Epoch 11/20
  22. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0279
  23. - acc: 0.9921 - val_loss: 0.0818 - val_acc: 0.9831 Epoch 12/20
  24. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0260
  25. - acc: 0.9927 - val_loss: 0.0945 - val_acc: 0.9819 Epoch 13/20
  26. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0257
  27. - acc: 0.9931 - val_loss: 0.0952 - val_acc: 0.9836 Epoch 14/20
  28. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0229
  29. - acc: 0.9937 - val_loss: 0.0924 - val_acc: 0.9832 Epoch 15/20
  30. 60000/60000 [==============================] - 7s 115us/step - loss: 0.0235
  31. - acc: 0.9937 - val_loss: 0.1004 - val_acc: 0.9823 Epoch 16/20
  32. 60000/60000 [==============================] - 7s 113us/step - loss: 0.0214
  33. - acc: 0.9941 - val_loss: 0.0991 - val_acc: 0.9847 Epoch 17/20
  34. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0219
  35. - acc: 0.9943 - val_loss: 0.1044 - val_acc: 0.9837 Epoch 18/20
  36. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0190
  37. - acc: 0.9952 - val_loss: 0.1129 - val_acc: 0.9836 Epoch 19/20
  38. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0197
  39. - acc: 0.9953 - val_loss: 0.0981 - val_acc: 0.9841 Epoch 20/20
  40. 60000/60000 [==============================] - 7s 112us/step - loss: 0.0198
  41. - acc: 0.9950 - val_loss: 0.1215 - val_acc: 0.9828
复制代码
Keras-模型评估和模型预测 (Keras - Model Evaluation and Model Prediction)

This chapter deals with the model evaluation and model prediction in Keras.
本章介绍Keras中的模型评估和模型预测。
Let us begin by understanding the model evaluation.
让我们首先了解模型评估。
模型评估 (Model Evaluation)

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,
评估是在模型开发期间检查模型是否最适合给定问题和相应数据的过程。 Keras模型提供了评估模型的功能。 它有三个主要论点,
    Test data 测试数据 Test data label 测试数据标签 verbose - true or false 详细-正确或错误
Let us evaluate the model, which we created in the previous chapter using test data.
让我们评估在上一章中使用测试数据创建的模型。
  1. score = model.evaluate(x_test, y_test, verbose = 0)
  2. print('Test loss:', score[0])
  3. print('Test accuracy:', score[1])
复制代码
Executing the above code will output the below information.
执行以上代码将输出以下信息。
  1. 0
复制代码
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.
测试精度为98.28%。 我们创建了一个最佳模型来识别手写数字。 从积极的方面来看,我们仍然可以扩大模型的范围。
模型预测 (Model Prediction)

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,
预测是模型生成的最后一步,也是我们预期的结果。 Keras提供了一种预测得到训练模型预测的方法。 预测方法的特征如下:
  1. predict(
  2.    x,
  3.    batch_size = None,
  4.    verbose = 0,
  5.    steps = None,
  6.    callbacks = None,
  7.    max_queue_size = 10,
  8.    workers = 1,
  9.    use_multiprocessing = False
  10. )
复制代码
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.
在这里,除第一个参数(该参数引用未知的输入数据)外,所有参数都是可选的。 应该保持形状以获得正确的预测。
Let us do prediction for our MPL model created in previous chapter using below code ?
让我们使用以下代码对在上一章中创建的MPL模型进行预测-
  1. pred = model.predict(x_test)
  2. pred = np.argmax(pred, axis = 1)[:5]
  3. label = np.argmax(y_test,axis = 1)[:5]
  4. print(pred)
  5. print(label)
复制代码
Here,
这里,
    Line 1 call the predict function using test data.
    第1行使用测试数据调用预测函数。
    Line 2 gets the first five prediction
    第2行获得前五个预测
    Line 3 gets the first five labels of the test data.
    第3行获取测试数据的前五个标签。
    Line 5 - 6 prints the prediction and actual label.
    第5-6行打印了预测标签和实际标签。
The output of the above application is as follows ?
上述应用程序的输出如下-
  1. [7 2 1 0 4]
  2. [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-卷积神经网络 (Keras - Convolution Neural Network)

Let us modify the model from MPL to Convolution Neural Network (CNN) for our earlier digit identification problem.
让我们针对早期的数字识别问题,将模型从MPL修改为卷积神经网络(CNN)
CNN can be represented as below ?
CNN可以表示如下-



The core features of the model are as follows ?
该模型的核心特征如下-
    Input layer consists of (1, 8, 28) values.
    输入层由(1、8、28)个值组成。
    First layer, Conv2D consists of 32 filters and ‘relu’ activation function with kernel size, (3,3).
    第一层, Conv2D包含32个过滤器和内核大小为(3,3)的“ relu”激活功能。
    Second layer, Conv2D consists of 64 filters and ‘relu’ activation function with kernel size, (3,3).
    第二层, Conv2D包含64个过滤器和内核大小为(3,3)的“ relu”激活功能。
    Thrid layer, MaxPooling has pool size of (2, 2).
    阈值MaxPooling的池大小为(2,2)。
    Fifth layer, Flatten is used to flatten all its input into single dimension.
    第五层, 平化被用来压平其所有输入到单个维度。
    Sixth layer, Dense consists of 128 neurons and ‘relu’ activation function.
    第六层, 密集由128个神经元和“ relu”激活功能组成。
    Seventh layer, Dropout has 0.5 as its value.
    第七层, Dropout的值为0.5。
    Eighth and final layer consists of 10 neurons and ‘softmax’ activation function.
    第八层也是最后一层由10个神经元和“ softmax”激活功能组成。
    Use categorical_crossentropy as loss function.
    使用categorical_crossentropy作为损失函数。
    Use Adadelta() as Optimizer.
    使用Adadelta()作为优化程序。
    Use accuracy as metrics.
    使用准确性作为指标。
    Use 128 as batch size.
    使用128作为批处理大小。
    Use 20 as epochs.
    使用20作为时代。
Step 1 ? Import the modules
步骤1-导入模块
Let us import the necessary modules.
让我们导入必要的模块。
  1. import keras
  2. from keras.datasets import mnist
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout, Flatten
  5. from keras.layers import Conv2D, MaxPooling2D
  6. from keras import backend as K
  7. import numpy as np
复制代码
Step 2 ? Load data
第2步-加载数据
Let us import the mnist dataset.
让我们导入mnist数据集。
  1. (x_train, y_train), (x_test, y_test) = mnist.load_data()
复制代码
Step 3 ? Process the data
步骤3-处理数据
Let us change the dataset according to our model, so that it can be feed into our model.
让我们根据我们的模型更改数据集,以便可以将其输入到我们的模型中。
  1. img_rows, img_cols = 28, 28
  2. if K.image_data_format() == 'channels_first':
  3.    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
  4.    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
  5.    input_shape = (1, img_rows, img_cols)
  6. else:
  7.    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
  8.    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
  9.    input_shape = (img_rows, img_cols, 1)
  10.    
  11. x_train = x_train.astype('float32')
  12. x_test = x_test.astype('float32')
  13. x_train /= 255
  14. x_test /= 255
  15. y_train = keras.utils.to_categorical(y_train, 10)
  16. y_test = keras.utils.to_categorical(y_test, 10)
复制代码
The data processing is similar to MPL model except the shape of the input data and image format configuration.
除了输入数据的形状和图像格式配置外,数据处理与MPL模型相似。
Step 4 ? Create the model
步骤4-创建模型
Let us create tha actual model.
让我们创建实际模型。
  1. model = Sequential()
  2. model.add(Conv2D(32, kernel_size = (3, 3),  
  3.    activation = 'relu', input_shape = input_shape))
  4. model.add(Conv2D(64, (3, 3), activation = 'relu'))
  5. model.add(MaxPooling2D(pool_size = (2, 2)))
  6. model.add(Dropout(0.25)) model.add(Flatten())
  7. model.add(Dense(128, activation = 'relu'))
  8. model.add(Dropout(0.5))
  9. model.add(Dense(10, activation = 'softmax'))
复制代码
Step 5 ? Compile the model
第5步-编译模型
Let us compile the model using selected loss function, optimizer and metrics.
让我们使用选定的损失函数,优化器和指标来编译模型。
  1. model.compile(loss = keras.losses.categorical_crossentropy,
  2.    optimizer = keras.optimizers.Adadelta(), metrics = ['accuracy'])
复制代码
Step 6 ? Train the model
步骤6-训练模型
Let us train the model using fit() method.
让我们使用fit()方法训练模型。
  1. model.fit(
  2.    x_train, y_train,
  3.    batch_size = 128,
  4.    epochs = 12,
  5.    verbose = 1,
  6.    validation_data = (x_test, y_test)
  7. )
复制代码
Executing the application will output the below information ?
执行应用程序将输出以下信息-
  1. Train on 60000 samples, validate on 10000 samples Epoch 1/12
  2. 60000/60000 [==============================] - 84s 1ms/step - loss: 0.2687
  3. - acc: 0.9173 - val_loss: 0.0549 - val_acc: 0.9827 Epoch 2/12
  4. 60000/60000 [==============================] - 86s 1ms/step - loss: 0.0899
  5. - acc: 0.9737 - val_loss: 0.0452 - val_acc: 0.9845 Epoch 3/12
  6. 60000/60000 [==============================] - 83s 1ms/step - loss: 0.0666
  7. - acc: 0.9804 - val_loss: 0.0362 - val_acc: 0.9879 Epoch 4/12
  8. 60000/60000 [==============================] - 81s 1ms/step - loss: 0.0564
  9. - acc: 0.9830 - val_loss: 0.0336 - val_acc: 0.9890 Epoch 5/12
  10. 60000/60000 [==============================] - 86s 1ms/step - loss: 0.0472
  11. - acc: 0.9861 - val_loss: 0.0312 - val_acc: 0.9901 Epoch 6/12
  12. 60000/60000 [==============================] - 83s 1ms/step - loss: 0.0414
  13. - acc: 0.9877 - val_loss: 0.0306 - val_acc: 0.9902 Epoch 7/12
  14. 60000/60000 [==============================] - 89s 1ms/step - loss: 0.0375
  15. -acc: 0.9883 - val_loss: 0.0281 - val_acc: 0.9906 Epoch 8/12
  16. 60000/60000 [==============================] - 91s 2ms/step - loss: 0.0339
  17. - acc: 0.9893 - val_loss: 0.0280 - val_acc: 0.9912 Epoch 9/12
  18. 60000/60000 [==============================] - 89s 1ms/step - loss: 0.0325
  19. - acc: 0.9901 - val_loss: 0.0260 - val_acc: 0.9909 Epoch 10/12
  20. 60000/60000 [==============================] - 89s 1ms/step - loss: 0.0284
  21. - acc: 0.9910 - val_loss: 0.0250 - val_acc: 0.9919 Epoch 11/12
  22. 60000/60000 [==============================] - 86s 1ms/step - loss: 0.0287
  23. - acc: 0.9907 - val_loss: 0.0264 - val_acc: 0.9916 Epoch 12/12
  24. 60000/60000 [==============================] - 86s 1ms/step - loss: 0.0265
  25. - acc: 0.9920 - val_loss: 0.0249 - val_acc: 0.9922
复制代码
Step 7 ? Evaluate the model
步骤7-评估模型
Let us evaluate the model using test data.
让我们使用测试数据评估模型。
  1. score = model.evaluate(x_test, y_test, verbose = 0)
  2. print('Test loss:', score[0])
  3. print('Test accuracy:', score[1])
复制代码
Executing the above code will output the below information ?
执行上面的代码将输出以下信息-
  1. Test loss: 0.024936060590433316
  2. Test accuracy: 0.9922
复制代码
The test accuracy is 99.22%. We have created a best model to identify the handwriting digits.
测试精度为99.22%。 我们创建了一个最佳模型来识别手写数字。
Step 8 ? Predict
步骤8-预测
Finally, predict the digit from images as below ?
最后,从图像预测位数,如下所示:
  1. pred = model.predict(x_test)
  2. pred = np.argmax(pred, axis = 1)[:5]
  3. label = np.argmax(y_test,axis = 1)[:5]
  4. print(pred)
  5. print(label)
复制代码
The output of the above application is as follows ?
上述应用程序的输出如下-
  1. [7 2 1 0 4]
  2. [7 2 1 0 4]
复制代码
The output of both array is identical and it indicate our model correctly predicts the first five images.
两个数组的输出相同,这表明我们的模型正确预测了前五个图像。
Keras-使用MPL进行回归预测 (Keras - Regression Prediction using MPL)

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的简单ANN进行回归预测。 到目前为止,我们仅完成了基于分类的预测。 现在,我们将尝试通过分析先前(连续)的值及其影响因素来预测下一个可能的值。
The Regression MPL can be represented as below ?
回归MPL可以表示如下-



The core features of the model are as follows ?
该模型的核心特征如下-
    Input layer consists of (13,) values.
    输入层由(13,)个值组成。
    First layer, Dense consists of 64 units and ‘relu’ activation function with ‘normal’ kernel initializer.
    第一层, Dense由64个单元和带有“正常”内核初始化程序的“ relu”激活功能组成。
    Second layer, Dense consists of 64 units and ‘relu’ activation function.
    第二层, Dense由64个单元和“ relu”激活功能组成。
    Output layer, Dense consists of 1 unit.
    输出层, Dense由1个单元组成。
    Use mse as loss function.
    使用mse作为损失函数。
    Use RMSprop as Optimizer.
    使用RMSprop作为优化程序。
    Use accuracy as metrics.
    使用准确性作为指标。
    Use 128 as batch size.
    使用128作为批处理大小。
    Use 500 as epochs.
    使用500作为时代。
Step 1 ? Import the modules
步骤1-导入模块
Let us import the necessary modules.
让我们导入必要的模块。
  1. import keras
  2. from keras.datasets import boston_housing
  3. from keras.models import Sequential
  4. from keras.layers import Dense
  5. from keras.optimizers import RMSprop
  6. from keras.callbacks import EarlyStopping
  7. from sklearn import preprocessing
  8. from sklearn.preprocessing import scale
复制代码
Step 2 ? Load data
第2步-加载数据
Let us import the Boston housing dataset.
让我们导入波士顿住房数据集。
  1. (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
复制代码
Here,
这里,
boston_housing is a dataset provided by Keras. It represents a collection of housing information in Boston area, each having 13 features.
boston_housingKeras提供的数据集。 它代表了波士顿地区的住房信息收集,每个都有13个特征。
Step 3 ? Process the data
步骤3-处理数据
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 ?
让我们根据模型更改数据集,以便可以将其输入模型。 可以使用以下代码更改数据-
  1. x_train_scaled = preprocessing.scale(x_train)
  2. scaler = preprocessing.StandardScaler().fit(x_train)
  3. x_test_scaled = scaler.transform(x_test)
复制代码
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.
在这里,我们使用sklearn.preprocessing.scale函数对训练数据进行了标准化preprocessing.StandardScaler()。fit函数返回具有训练数据的标准化均值和标准偏差的标量,我们可以使用scalar.transform函数将其应用于测试数据。 这也将使用与训练数据相同的设置来标准化测试数据。
Step 4 ? Create the model
步骤4-创建模型
Let us create the actual model.
让我们创建实际的模型。
  1. model = Sequential()
  2. model.add(Dense(64, kernel_initializer = 'normal', activation = 'relu',
  3. input_shape = (13,)))
  4. model.add(Dense(64, activation = 'relu')) model.add(Dense(1))
复制代码
Step 5 ? Compile the model
第5步-编译模型
Let us compile the model using selected loss function, optimizer and metrics.
让我们使用选定的损失函数,优化器和指标来编译模型。
  1. model.compile(
  2.    loss = 'mse',
  3.    optimizer = RMSprop(),
  4.    metrics = ['mean_absolute_error']
  5. )
复制代码
Step 6 ? Train the model
步骤6-训练模型
Let us train the model using fit() method.
让我们使用fit()方法训练模型。
  1. history = model.fit(
  2.    x_train_scaled, y_train,   
  3.    batch_size=128,
  4.    epochs = 500,
  5.    verbose = 1,
  6.    validation_split = 0.2,
  7.    callbacks = [EarlyStopping(monitor = 'val_loss', patience = 20)]
  8. )
复制代码
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.
在这里,我们使用了回调函数EarlyStopping 。 此回调的目的是监视每个时期的损失值,并将其与以前的时期损失值进行比较,以找到训练方面的改进。 如果耐心时间没有改善,则整个过程将停止。
Executing the application will give the below information as output ?
执行应用程序将给出以下信息作为输出-
  1. Train on 323 samples, validate on 81 samples Epoch 1/500 2019-09-24 01:07:03.889046: I
  2. tensorflow/core/platform/cpu_feature_guard.cc:142]
  3. Your CPU supports instructions that this
  4. TensorFlow binary was not co mpiled to use: AVX2 323/323
  5. [==============================] - 0s 515us/step - loss: 562.3129
  6. - mean_absolute_error: 21.8575 - val_loss: 621.6523 - val_mean_absolute_erro
  7. r: 23.1730 Epoch 2/500
  8. 323/323 [==============================] - 0s 11us/step - loss: 545.1666
  9. - mean_absolute_error: 21.4887 - val_loss: 605.1341 - val_mean_absolute_error
  10. : 22.8293 Epoch 3/500
  11. 323/323 [==============================] - 0s 12us/step - loss: 528.9944
  12. - mean_absolute_error: 21.1328 - val_loss: 588.6594 - val_mean_absolute_error
  13. : 22.4799 Epoch 4/500
  14. 323/323 [==============================] - 0s 12us/step - loss: 512.2739
  15. - mean_absolute_error: 20.7658 - val_loss: 570.3772 - val_mean_absolute_error
  16. : 22.0853 Epoch 5/500
  17. 323/323 [==============================] - 0s 9us/step - loss: 493.9775
  18. - mean_absolute_error: 20.3506 - val_loss: 550.9548 - val_mean_absolute_error: 21.6547
  19. ..........
  20. ..........
  21. ..........
  22. Epoch 143/500
  23. 323/323 [==============================] - 0s 15us/step - loss: 8.1004
  24. - mean_absolute_error: 2.0002 - val_loss: 14.6286 - val_mean_absolute_error:
  25. 2. 5904 Epoch 144/500
  26. 323/323 [==============================] - 0s 19us/step - loss: 8.0300
  27. - mean_absolute_error: 1.9683 - val_loss: 14.5949 - val_mean_absolute_error:
  28. 2. 5843 Epoch 145/500
  29. 323/323 [==============================] - 0s 12us/step - loss: 7.8704
  30. - mean_absolute_error: 1.9313 - val_loss: 14.3770 - val_mean_absolute_error: 2. 4996
复制代码
Step 7 ? Evaluate the model
步骤7-评估模型
Let us evaluate the model using test data.
让我们使用测试数据评估模型。
  1. score = model.evaluate(x_test_scaled, y_test, verbose = 0)
  2. print('Test loss:', score[0])
  3. print('Test accuracy:', score[1])
复制代码
Executing the above code will output the below information ?
执行上面的代码将输出以下信息-
  1. Test loss: 21.928471583946077 Test accuracy: 2.9599233234629914
复制代码
Step 8 ? Predict
步骤8-预测
Finally, predict using test data as below ?
最后,使用以下测试数据进行预测-
  1. prediction = model.predict(x_test_scaled)
  2. print(prediction.flatten())
  3. print(y_test)
复制代码
The output of the above application is as follows ?
上述应用程序的输出如下-
  1. [ 7.5612316 17.583357 21.09344 31.859276 25.055613 18.673872 26.600405 22.403967 19.060272 22.264952
  2. 17.4191 17.00466 15.58924 41.624374 20.220217 18.985565 26.419338 19.837091 19.946192 36.43445
  3. 12.278508 16.330965 20.701359 14.345301 21.741161 25.050423 31.046402 27.738455 9.959419 20.93039
  4. 20.069063 14.518344 33.20235 24.735163 18.7274 9.148898 15.781284 18.556862 18.692865 26.045074
  5. 27.954073 28.106823 15.272034 40.879818 29.33896 23.714525 26.427515 16.483374 22.518442 22.425386
  6. 33.94826 18.831465 13.2501955 15.537227 34.639984 27.468002 13.474407 48.134598 34.39617
  7. 22.8503124.042334 17.747198 14.7837715 18.187277 23.655672 22.364983 13.858193 22.710032 14.371148
  8. 7.1272087 35.960033 28.247292 25.3014 14.477208 25.306196 17.891165 20.193708 23.585173 34.690193
  9. 12.200583 20.102983 38.45882 14.741723 14.408362 17.67158 18.418497 21.151712 21.157492 22.693687
  10. 29.809034 19.366991 20.072294 25.880817 40.814568 34.64087 19.43741 36.2591 50.73806 26.968863 43.91787
  11. 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
  12. 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
  13. 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
  14. 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. 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. ]
复制代码
The output of both array have around 10-30% difference and it indicate our model predicts with reasonable range.
两种阵列的输出相差约10-30%,这表明我们的模型在合理范围内进行了预测。
Keras-使用LSTM RNN的时间序列预测 (Keras - Time Series Prediction using 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.
在本章中,让我们编写一个简单的基于RSTM的长期短期记忆(LSTM)进行序列分析。 序列是一组值,其中每个值对应于特定的时间实例。 让我们考虑一个简单的阅读句子的例子。 阅读和理解句子包括按照给定的顺序阅读单词,并尝试在给定的上下文中理解每个单词及其含义,最后以积极或消极的态度理解句子。
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.
在此,将单词视为值,第一个值对应于第一个单词,第二个值对应于第二个单词等,并且将严格保持顺序。 序列分析经常用于自然语言处理中,以查找给定文本的情感分析。
Let us create a LSTM model to analyze the IMDB movie reviews and find its positive/negative sentiment.
让我们创建一个LSTM模型来分析IMDB电影评论并找到其正面/负面情绪。
The model for the sequence analysis can be represented as below ?
序列分析的模型可以表示如下-



The core features of the model are as follows ?
该模型的核心特征如下-
    Input layer using Embedding layer with 128 features.
    输入层使用具有128个要素的嵌入层。
    First layer, Dense consists of 128 units with normal dropout and recurrent dropout set to 0.2.
    第一层,密集(Dense)由128个单元组成,正常辍学和经常性辍学设置为0.2。
    Output layer, Dense consists of 1 unit and ‘sigmoid’ activation function.
    输出层Dense由1个单元和“ Sigmoid”激活功能组成。
    Use binary_crossentropy as loss function.
    使用binary_crossentropy作为损失函数。
    Use adam as Optimizer.
    使用亚当作为优化器。
    Use accuracy as metrics.
    使用准确性作为指标。
    Use 32 as batch size.
    使用32作为批处理大小。
    Use 15 as epochs.
    使用15作为时代。
    Use 80 as the maximum length of the word.
    使用80作为单词的最大长度。
    Use 2000 as the maximum number of word in a given sentence.
    使用2000作为给定句子中的最大单词数。
步骤1:导入模块 (Step 1: Import the modules)

Let us import the necessary modules.
让我们导入必要的模块。
  1. from keras.preprocessing import sequence
  2. from keras.models import Sequential
  3. from keras.layers import Dense, Embedding
  4. from keras.layers import LSTM
  5. from keras.datasets import imdb
复制代码
步骤2:载入资料 (Step 2: Load data)

Let us import the imdb dataset.
让我们导入imdb数据集。
  1. (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words = 2000)
复制代码
Here,
这里,
    imdb is a dataset provided by Keras. It represents a collection of movies and its reviews.
    imdb是Keras提供的数据集。 它代表了电影及其评论的集合。
    num_words represent the maximum number of words in the review.
    num_words表示评论中的最大单词数。
步骤3:处理资料 (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 ?
让我们根据我们的模型更改数据集,以便可以将其输入到我们的模型中。 可以使用以下代码更改数据-
  1. x_train = sequence.pad_sequences(x_train, maxlen=80)
  2. x_test = sequence.pad_sequences(x_test, maxlen=80)
复制代码
Here,
这里,
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.
sequence.pad_sequences将形状为(data)的输入数据列表转换为形状(data,timesteps)的 2D NumPy数组。 基本上,它将时间步概念添加到给定数据中。 它生成长度maxlen的时间步长。
步骤4:建立模型 (Step 4: Create the model)

Let us create the actual model.
让我们创建实际的模型。
  1. model = Sequential()
  2. model.add(Embedding(2000, 128))
  3. model.add(LSTM(128, dropout = 0.2, recurrent_dropout = 0.2))
  4. model.add(Dense(1, activation = 'sigmoid'))
复制代码
Here,
这里,
We have used Embedding layer as input layer and then added the LSTM layer. Finally, a Dense layer is used as output layer.
我们已经使用嵌入层作为输入层,然后添加了LSTM层。 最后, 密集层用作输出层。
步骤5:编译模型 (Step 5: Compile the model)

Let us compile the model using selected loss function, optimizer and metrics.
让我们使用选定的损失函数,优化器和指标来编译模型。
  1. model.compile(loss = 'binary_crossentropy',
  2.    optimizer = 'adam', metrics = ['accuracy'])
复制代码
步骤6:训练模型 (Step 6: Train the model)

LLet us train the model using fit() method.
让我们使用fit()方法训练模型。
  1. model.fit(
  2.    x_train, y_train,
  3.    batch_size = 32,
  4.    epochs = 15,
  5.    validation_data = (x_test, y_test)
  6. )
复制代码
Executing the application will output the below information ?
执行应用程序将输出以下信息-
  1. Epoch 1/15 2019-09-24 01:19:01.151247: I
  2. tensorflow/core/platform/cpu_feature_guard.cc:142]
  3. Your CPU supports instructions that this
  4. TensorFlow binary was not co mpiled to use: AVX2
  5. 25000/25000 [==============================] - 101s 4ms/step - loss: 0.4707
  6. - acc: 0.7716 - val_loss: 0.3769 - val_acc: 0.8349 Epoch 2/15
  7. 25000/25000 [==============================] - 95s 4ms/step - loss: 0.3058
  8. - acc: 0.8756 - val_loss: 0.3763 - val_acc: 0.8350 Epoch 3/15
  9. 25000/25000 [==============================] - 91s 4ms/step - loss: 0.2100
  10. - acc: 0.9178 - val_loss: 0.5065 - val_acc: 0.8110 Epoch 4/15
  11. 25000/25000 [==============================] - 90s 4ms/step - loss: 0.1394
  12. - acc: 0.9495 - val_loss: 0.6046 - val_acc: 0.8146 Epoch 5/15
  13. 25000/25000 [==============================] - 90s 4ms/step - loss: 0.0973
  14. - acc: 0.9652 - val_loss: 0.5969 - val_acc: 0.8147 Epoch 6/15
  15. 25000/25000 [==============================] - 98s 4ms/step - loss: 0.0759
  16. - acc: 0.9730 - val_loss: 0.6368 - val_acc: 0.8208 Epoch 7/15
  17. 25000/25000 [==============================] - 95s 4ms/step - loss: 0.0578
  18. - acc: 0.9811 - val_loss: 0.6657 - val_acc: 0.8184 Epoch 8/15
  19. 25000/25000 [==============================] - 97s 4ms/step - loss: 0.0448
  20. - acc: 0.9850 - val_loss: 0.7452 - val_acc: 0.8136 Epoch 9/15
  21. 25000/25000 [==============================] - 95s 4ms/step - loss: 0.0324
  22. - acc: 0.9894 - val_loss: 0.7616 - val_acc: 0.8162Epoch 10/15
  23. 25000/25000 [==============================] - 100s 4ms/step - loss: 0.0247
  24. - acc: 0.9922 - val_loss: 0.9654 - val_acc: 0.8148 Epoch 11/15
  25. 25000/25000 [==============================] - 99s 4ms/step - loss: 0.0169
  26. - acc: 0.9946 - val_loss: 1.0013 - val_acc: 0.8104 Epoch 12/15
  27. 25000/25000 [==============================] - 90s 4ms/step - loss: 0.0154
  28. - acc: 0.9948 - val_loss: 1.0316 - val_acc: 0.8100 Epoch 13/15
  29. 25000/25000 [==============================] - 89s 4ms/step - loss: 0.0113
  30. - acc: 0.9963 - val_loss: 1.1138 - val_acc: 0.8108 Epoch 14/15
  31. 25000/25000 [==============================] - 89s 4ms/step - loss: 0.0106
  32. - acc: 0.9971 - val_loss: 1.0538 - val_acc: 0.8102 Epoch 15/15
  33. 25000/25000 [==============================] - 89s 4ms/step - loss: 0.0090
  34. - acc: 0.9972 - val_loss: 1.1453 - val_acc: 0.8129
  35. 25000/25000 [==============================] - 10s 390us/step
复制代码
步骤7-评估模型 (Step 7 ? Evaluate the model)

Let us evaluate the model using test data.
让我们使用测试数据评估模型。
  1. score, acc = model.evaluate(x_test, y_test, batch_size = 32)
  2.    
  3. print('Test score:', score)
  4. print('Test accuracy:', acc)
复制代码
Executing the above code will output the below information ?
执行上面的代码将输出以下信息-
  1. Test score: 1.145306069601178
  2. Test accuracy: 0.81292
复制代码
Keras-应用 (Keras - Applications)

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.
Keras应用程序模块用于为深度神经网络提供预训练的模型。 Keras模型用于预测,特征提取和微调。 本章详细介绍了Keras应用程序。
预训练模型 (Pre-trained models)

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,
训练后的模型由架构和权重两部分组成。 模型权重是大文件,因此我们必须从ImageNet数据库下载并提取特征。 下面列出了一些流行的预训练模型,
    ResNet ResNet VGG16 VGG16 MobileNet 移动网 InceptionResNetV2 盗版ResNetV2 InceptionV3 盗梦空间V3
载入模型 (Loading a model)

Keras pre-trained models can be easily loaded as specified below ?
Keras预训练模型可以轻松加载,如下所示:
  1. import keras
  2. import numpy as np
  3. from keras.applications import vgg16, inception_v3, resnet50, mobilenet
  4. #Load the VGG model
  5. vgg_model = vgg16.VGG16(weights = 'imagenet')
  6. #Load the Inception_V3 model
  7. inception_model = inception_v3.InceptionV3(weights = 'imagenet')
  8. #Load the ResNet50 model
  9. resnet_model = resnet50.ResNet50(weights = 'imagenet')
  10. #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.
加载模型后,我们可以立即将其用于预测目的。 让我们在接下来的章节中检查每个预先训练的模型。
使用ResNet模型进行实时预测 (Real Time Prediction using ResNet Model)

ResNet is a pre-trained model. It is trained using ImageNet. ResNet model weights pre-trained on ImageNet. It has the following syntax ?
ResNet是一个预先训练的模型。 它使用ImageNet进行培训。 在ImageNet上预训练的ResNet模型权重。 它具有以下语法-
  1. keras.applications.resnet.ResNet50 (
  2.    include_top = True,
  3.    weights = 'imagenet',
  4.    input_tensor = None,
  5.    input_shape = None,
  6.    pooling = None,
  7.    classes = 1000
  8. )
复制代码
Here,
这里,
    include_top refers the fully-connected layer at the top of the network.
    include_top指网络顶部的完全连接层。
    weights refer pre-training on ImageNet.
    权重参考ImageNet上的预训练。
    input_tensor refers optional Keras tensor to use as image input for the model.
    input_tensor引用可选的Keras张量,以用作模型的图像输入。
    input_shape refers optional shape tuple. The default input size for this model is 224x224.
    input_shape表示可选的形状元组。 该模型的默认输入大小为224x224。
    classes refer optional number of classes to classify images.
    是指可选的若干类,以对图像进行分类。
Let us understand the model by writing a simple example ?
让我们通过写一个简单的例子来理解模型-
步骤1:导入模块 (Step 1: import the modules)

Let us load the necessary modules as specified below ?
让我们加载以下指定的必要模块-
  1. >>> import PIL
  2. >>> from keras.preprocessing.image import load_img
  3. >>> from keras.preprocessing.image import img_to_array
  4. >>> from keras.applications.imagenet_utils import decode_predictions
  5. >>> import matplotlib.pyplot as plt
  6. >>> import numpy as np
  7. >>> from keras.applications.resnet50 import ResNet50
  8. >>> from keras.applications import resnet50
复制代码
步骤2:选择输入 (Step 2: Select an input)

Let us choose an input image, Lotus as specified below ?
让我们选择一个输入图像, Lotus如下所示-
  1. >>> filename = 'banana.jpg'
  2. >>> ## load an image in PIL format
  3. >>> original = load_img(filename, target_size = (224, 224))
  4. >>> print('PIL image size',original.size)
  5. PIL image size (224, 224)
  6. >>> plt.imshow(original)
  7. <matplotlib.image.AxesImage object at 0x1304756d8>
  8. >>> plt.show()
复制代码
Here, we have loaded an image (banana.jpg) and displayed it.
在这里,我们加载了图像(banana.jpg)并显示了它。
步骤3:将图片转换成NumPy数组 (Step 3: Convert images into NumPy array)

Let us convert our input, Banana into NumPy array, so that it can be passed into the model for the purpose of prediction.
让我们将输入Banana转换为NumPy数组,以便可以将其传递到模型中以进行预测。
  1. >>> #convert the PIL image to a numpy array
  2. >>> numpy_image = img_to_array(original)
  3. >>> plt.imshow(np.uint8(numpy_image))
  4. <matplotlib.image.AxesImage object at 0x130475ac8>
  5. >>> print('numpy array size',numpy_image.shape)
  6. numpy array size (224, 224, 3)
  7. >>> # Convert the image / images into batch format
  8. >>> image_batch = np.expand_dims(numpy_image, axis = 0)
  9. >>> print('image batch size', image_batch.shape)
  10. image batch size (1, 224, 224, 3)
  11. >>>
复制代码
步骤4:模型预测 (Step 4: Model prediction)

Let us feed our input into the model to get the predictions
让我们将输入信息输入模型以获取预测
  1. >>> prepare the image for the resnet50 model >>>
  2. >>> processed_image = resnet50.preprocess_input(image_batch.copy())
  3. >>> # create resnet model
  4. >>>resnet_model = resnet50.ResNet50(weights = 'imagenet')
  5. >>> Downloavding data from https://github.com/fchollet/deep-learning-models/releas
  6. es/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
  7. 102858752/102853048 [==============================] - 33s 0us/step
  8. >>> # get the predicted probabilities for each class
  9. >>> predictions = resnet_model.predict(processed_image)
  10. >>> # convert the probabilities to class labels
  11. >>> label = decode_predictions(predictions)
  12. Downloading data from https://storage.googleapis.com/download.tensorflow.org/
  13. data/imagenet_class_index.json
  14. 40960/35363 [==================================] - 0s 0us/step
  15. >>> print(label)
复制代码
输出量 (Output)
  1. [
  2.    [
  3.       ('n07753592', 'banana', 0.99229723),
  4.       ('n03532672', 'hook', 0.0014551596),
  5.       ('n03970156', 'plunger', 0.0010738898),
  6.       ('n07753113', 'fig', 0.0009359837) ,
  7.       ('n03109150', 'corkscrew', 0.00028538404)
  8.    ]
  9. ]
复制代码
Here, the model predicted the images as banana correctly.
在此,模型正确地将图像预测为香蕉。

Keras-预训练模型 (Keras - Pre-Trained Models)

In this chapter, we will learn about the pre-trained models in Keras. Let us begin with VGG16.
在本章中,我们将学习Keras中的预训练模型。 让我们从VGG16开始。
VGG16 (VGG16)

VGG16 is another pre-trained model. It is also trained using ImageNet. The syntax to load the model is as follows ?
VGG16是另一种预训练模型。 它还使用ImageNet进行了培训。 加载模型的语法如下-
  1. keras.applications.vgg16.VGG16(
  2.    include_top = True,
  3.    weights = 'imagenet',
  4.    input_tensor = None,
  5.    input_shape = None,
  6.    pooling = None,
  7.    classes = 1000
  8. )
复制代码
The default input size for this model is 224x224.
该模型的默认输入大小为224x224。
MobileNetV2 (MobileNetV2)

MobileNetV2 is another pre-trained model. It is also trained uing ImageNet.
MobileNetV2是另一种预先训练的模型。 它还使用ImageNet进行了培训。
The syntax to load the model is as follows ?
加载模型的语法如下-
  1. keras.applications.mobilenet_v2.MobileNetV2 (
  2.    input_shape = None,
  3.    alpha = 1.0,
  4.    include_top = True,
  5.    weights = 'imagenet',
  6.    input_tensor = None,
  7.    pooling = None,
  8.    classes = 1000
  9. )
复制代码
Here,
这里,
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.
alpha控制网络的宽度。 如果该值小于1,则减少每层中的过滤器数量。 如果该值大于1,则增加每层中的过滤器数量。 如果alpha = 1,则在每一层使用纸张的默认过滤器数量。
The default input size for this model is 224x224.
该模型的默认输入大小为224x224
盗版ResNetV2 (InceptionResNetV2)

InceptionResNetV2 is another pre-trained model. It is also trained using ImageNet. The syntax to load the model is as follows ?
InceptionResNetV2是另一个预先训练的模型。 它还使用ImageNet进行了培训。 加载模型的语法如下-
  1. keras.applications.inception_resnet_v2.InceptionResNetV2 (
  2.    include_top = True,
  3.    weights = 'imagenet',
  4.    input_tensor = None,
  5.    input_shape = None,
  6.    pooling = None,
  7.    classes = 1000)
复制代码
This model and can be built both with ‘channels_first’ data format (channels, height, width) or ‘channels_last’ data format (height, width, channels).
此模型可以使用“ channels_first”数据格式(通道,高度,宽度)或“ channels_last”数据格式(高度,宽度,通道)构建。
The default input size for this model is 299x299.
该模型的默认输入大小为299x299
盗梦空间V3 (InceptionV3)

InceptionV3 is another pre-trained model. It is also trained uing ImageNet. The syntax to load the model is as follows ?
InceptionV3是另一个预先训练的模型。 它还使用ImageNet进行了培训。 加载模型的语法如下-
  1. keras.applications.inception_v3.InceptionV3 (
  2.    include_top = True,
  3.    weights = 'imagenet',
  4.    input_tensor = None,
  5.    input_shape = None,
  6.    pooling = None,
  7.    classes = 1000
  8. )
复制代码
Here,
这里,
The default input size for this model is 299x299.
该模型的默认输入大小为299x299
结论 (Conclusion)

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.
Keras是非常简单,可扩展且易于实现的神经网络API,可用于构建具有高级抽象的深度学习应用程序。 Keras是深层倾斜模型的最佳选择。
翻译自: https://www.tutorialspoint.com/keras/keras_quick_guide.htm
keras安装与配置指南



<div id="marketingBox" class="marketing-box"><div class="marketing-content">


CSDN 社区图书馆,开张营业!


深读计划,写书评领图书福利~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-26 07:47 , Processed in 0.132507 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表