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

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

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

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

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

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

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

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

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

artificial neural networks

在此,

Here,

  1. Multiple input along with weight represents dendrites.

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

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

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

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

Multi-Layer Perceptron

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

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

multi layer perceptron

Convolutional Neural Network (CNN)

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

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

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

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

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

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

A simple CNN can be represented as below −

cnn

在此,

Here,

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

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

Recurrent Neural Network (RNN)

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

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

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

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

Workflow of ANN

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

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

Collect required data

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

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

Analyze data

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

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

Choose an algorithm (model)

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

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

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

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

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

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

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

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

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

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

The above steps can be represented using below flow chart −

ann