Keras 简明教程
Keras - Deep learning
Keras 提供了一个完整的框架来创建任何类型的卷积神经网络。Keras 具有创新性,并且非常易于学习。它支持从简单神经网络到非常大和复杂的神经网络模型。在本章中,让我们了解 Keras 框架的架构以及 Keras 如何帮助进行深度学习。
Keras provides a complete framework to create any type of neural networks. Keras is innovative as well as very easy to learn. It supports simple neural network to very large and complex neural network model. Let us understand the architecture of Keras framework and how Keras helps in deep learning in this chapter.
Architecture of Keras
Keras API 可分为三大类 -
Keras API can be divided into three main categories −
-
Model
-
Layer
-
Core Modules
在 Keras 中,每个 ANN 都由 Keras Models 表示。反过来,每个 Keras 模型都是 Keras Layers 的组成,并表示 ANN 层,如输入、隐藏层、输出层、卷积层、池化层等,Keras 模型和层访问 Keras modules 以获取激活函数、损失函数、正则化函数等。使用 Keras 模型、Keras 层和 Keras 模块,任何 ANN 算法(CNN、RNN 等)都可以用简单有效的方式表示。
In Keras, every ANN is represented by Keras Models. In turn, every Keras Model is composition of Keras Layers and represents ANN layers like input, hidden layer, output layers, convolution layer, pooling layer, etc., Keras model and layer access Keras modules for activation function, loss function, regularization function, etc., Using Keras model, Keras Layer, and Keras modules, any ANN algorithm (CNN, RNN, etc.,) can be represented in a simple and efficient manner.
下图描述了模型、层和核心模块之间的关系 -
The following diagram depicts the relationship between model, layer and core modules −
让我们来看一下 Keras 模型、Keras 层和 Keras 模块的概述。
Let us see the overview of Keras models, Keras layers and Keras modules.
Model
Keras 模型有两种类型,如下所述 -
Keras Models are of two types as mentioned below −
Sequential Model - 顺序模型基本上是 Keras 层的线性组合。顺序模型简单、最小,并有能力表示几乎所有可用的神经网络。
Sequential Model − Sequential model is basically a linear composition of Keras Layers. Sequential model is easy, minimal as well as has the ability to represent nearly all available neural networks.
一个简单的顺序模型如下 -
A simple sequential model is as follows −
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,)))
其中,
Where,
-
Line 1 imports Sequential model from Keras models
-
Line 2 imports Dense layer and Activation module
-
Line 4 create a new sequential model using Sequential API
-
Line 5 adds a dense layer (Dense API) with relu activation (using Activation module) function.
Sequential 模型公开 Model 类以创建自定义模型。我们可以使用子类化概念来创建我们自己的复杂模型。
Sequential model exposes Model class to create customized models as well. We can use sub-classing concept to create our own complex model.
Functional API − Functional API 基本上可用于创建复杂模型。
Functional API − Functional API is basically used to create complex models.
Layer
Keras 模型中的每个 Keras 层代表实际提议的神经网络模型中的相应层(输入层、隐藏层和输出层)。Keras 提供了许多预构建的层,以便轻松创建任何复杂的神经网络。以下是其中一些重要的 Keras 层:
Each Keras layer in the Keras model represent the corresponding layer (input layer, hidden layer and output layer) in the actual proposed neural network model. Keras provides a lot of pre-build layers so that any complex neural network can be easily created. Some of the important Keras layers are specified below,
-
Core Layers
-
Convolution Layers
-
Pooling Layers
-
Recurrent Layers
一个用于表示使用 sequential 模型的神经网络模型的简单 python 代码如下所示 −
A simple python code to represent a neural network model using sequential model is as follows −
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation = 'relu')) model.add(Dropout(0.2))
model.add(Dense(num_classes, activation = 'softmax'))
其中,
Where,
-
Line 1 imports Sequential model from Keras models
-
Line 2 imports Dense layer and Activation module
-
Line 4 create a new sequential model using Sequential API
-
Line 5 adds a dense layer (Dense API) with relu activation (using Activation module) function.
-
Line 6 adds a dropout layer (Dropout API) to handle over-fitting.
-
Line 7 adds another dense layer (Dense API) with relu activation (using Activation module) function.
-
Line 8 adds another dropout layer (Dropout API) to handle over-fitting.
-
Line 9 adds final dense layer (Dense API) with softmax activation (using Activation module) function.
Keras 还提供创建我们自己的自定义层。可以通过对 Keras.Layer 类进行子分类来创建自定义层,它类似于对 Keras 模型进行子分类。
Keras also provides options to create our own customized layers. Customized layer can be created by sub-classing the Keras.Layer class and it is similar to sub-classing Keras models.
Core Modules
Keras 还提供许多内置的神经网络相关函数来正确创建 Keras 模型和 Keras 层。以下是一些函数:
Keras also provides a lot of built-in neural network related functions to properly create the Keras model and Keras layers. Some of the function are as follows −
-
Activations module − Activation function is an important concept in ANN and activation modules provides many activation function like softmax, relu, etc.,
-
Loss module − Loss module provides loss functions like mean_squared_error, mean_absolute_error, poisson, etc.,
-
Optimizer module − Optimizer module provides optimizer function like adam, sgd, etc.,
-
Regularizers − Regularizer module provides functions like L1 regularizer, L2 regularizer, etc.,
让我们在后续章节中详细了解 Keras 模块。
Let us learn Keras modules in detail in the upcoming chapter.