Deep Learning With Keras 简明教程
Creating Deep Learning Model
我们的神经网络模型将包含一个线性层堆栈。要定义这样的模型,我们调用 Sequential 函数 -
Our neural network model will consist of a linear stack of layers. To define such a model, we call the Sequential function −
model = Sequential()
Input Layer
我们的网络中使用以下程序语句定义的输入层是第一层 −
We define the input layer, which is the first layer in our network using the following program statement −
model.add(Dense(512, input_shape=(784,)))
这将创建具有 512 个节点(神经元)和 784 个输入节点的层。这在下面的图中表示 −
This creates a layer with 512 nodes (neurons) with 784 input nodes. This is depicted in the figure below −
请注意,所有输入节点都完全连接到第 1 层,即每个输入节点都连接到第 1 层的所有 512 个节点。
Note that all the input nodes are fully connected to the Layer 1, that is each input node is connected to all 512 nodes of Layer 1.
接下来,我们需要添加第 1 层的输出激活函数。我们将使用 ReLU 作为我们的激活。使用以下程序语句添加激活函数 −
Next, we need to add the activation function for the output of Layer 1. We will use ReLU as our activation. The activation function is added using the following program statement −
model.add(Activation('relu'))
接下来,我们使用下面的语句添加 20% 的 Dropout。Dropout 是一种用于防止模型过拟合的技术。
Next, we add Dropout of 20% using the statement below. Dropout is a technique used to prevent model from overfitting.
model.add(Dropout(0.2))
此时,我们的输入层已完全定义。接下来,我们将添加一个隐藏层。
At this point, our input layer is fully defined. Next, we will add a hidden layer.
Hidden Layer
我们的隐藏层将包含 512 个节点。隐藏层的输入来自我们之前定义的输入层。所有节点都完全连接,如同之前的情况一样。隐藏层的输出将进入网络中的下一层,它将成为我们的最终输出层。我们将使用与前一层相同的 ReLU 激活,并且 Dropout 为 20%。在此处给出添加此层的代码 −
Our hidden layer will consist of 512 nodes. The input to the hidden layer comes from our previously defined input layer. All the nodes are fully connected as in the earlier case. The output of the hidden layer will go to the next layer in the network, which is going to be our final and output layer. We will use the same ReLU activation as for the previous layer and a dropout of 20%. The code for adding this layer is given here −
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
此时,网络可以可视化为以下内容 −
The network at this stage can be visualized as follows −
接下来,我们将向我们的网络添加最终层,即输出层。请注意,您可以使用与此处使用的类似代码添加任意数量的隐藏层。添加更多层会使网络难以训练;然而,在很多情况下(尽管不是全部)给出了更好的结果的明确优势。
Next, we will add the final layer to our network, which is the output layer. Note that you may add any number of hidden layers using the code similar to the one which you have used here. Adding more layers would make the network complex for training; however, giving a definite advantage of better results in many cases though not all.
Output Layer
输出层仅包含 10 个节点,因为我们希望将给定的图像分类为 10 个不同的数字。我们使用以下语句添加此层 −
The output layer consists of just 10 nodes as we want to classify the given images in 10 distinct digits. We add this layer, using the following statement −
model.add(Dense(10))
由于我们希望将输出分类为 10 个不同的单元,因此我们使用 softmax 激活。在 ReLU 的情况下,输出是二进制的。我们使用以下语句添加激活 −
As we want to classify the output in 10 distinct units, we use the softmax activation. In case of ReLU, the output is binary. We add the activation using the following statement −
model.add(Activation('softmax'))
此时,我们的网络可以如下图所示可视化 −
At this point, our network can be visualized as shown in the below diagram −
此时,我们的网络模型在软件中已完全定义。运行代码单元格,如果没有错误,您将在屏幕上收到一条确认消息,如下面的屏幕截图所示 −
At this point, our network model is fully defined in the software. Run the code cell and if there are no errors, you will get a confirmation message on the screen as shown in the screenshot below −
接下来,我们需要编译模型。
Next, we need to compile the model.