Deep Learning With Keras 简明教程

Deep Learning with Keras - Compiling the Model

编译使用一个称为 compile 的单方法调用执行。

The compilation is performed using one single method call called compile.

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

compile 方法需要多个参数。loss 参数指定为类型 'categorical_crossentropy' 。metrics 参数设置为 'accuracy' ,最后我们使用 adam 优化器训练网络。此阶段的输出如下所示:

The compile method requires several parameters. The loss parameter is specified to have type 'categorical_crossentropy'. The metrics parameter is set to 'accuracy' and finally we use the adam optimizer for training the network. The output at this stage is shown below −

compile method

现在,我们可以将数据馈送到我们的网络中。

Now, we are ready to feed in the data to our network.

Loading Data

如前所述,我们将使用 Keras 提供的 mnist 数据集。当我们将数据加载到我们的系统中时,我们将把它分成训练数据和测试数据。通过按如下方式调用 load_data 方法来加载数据 −

As said earlier, we will use the mnist dataset provided by Keras. When we load the data into our system, we will split it in the training and test data. The data is loaded by calling the load_data method as follows −

(X_train, y_train), (X_test, y_test) = mnist.load_data()

此阶段的输出如下所示:

The output at this stage looks like the following −

loading data

现在,我们将学习加载数据集的结构。

Now, we shall learn the structure of the loaded dataset.

提供给我们的数据是尺寸为 28 x 28 像素的图形图像,每个图像都包含一个介于 0 和 9 之间的单独数字。我们将在控制台上显示前十张图片。执行此操作的代码如下:

The data that is provided to us are the graphic images of size 28 x 28 pixels, each containing a single digit between 0 and 9. We will display the first ten images on the console. The code for doing so is given below −

# printing first 10 images
for i in range(10):

plot.subplot(3,5,i+1)
plot.tight_layout()
plot.imshow(X_train[i], cmap='gray', interpolation='none')
plot.title("Digit: {}".format(y_train[i]))
plot.xticks([])
plot.yticks([])

在 10 个计数的迭代循环中,我们在每次迭代上创建一个子图,并在其中显示 X_train 矢量中的图像。我们给每张图像使用 y_train 矢量中的相应标题命名。请注意, y_train 矢量包含 X_train 矢量中相应图像的实际值。通过使用两个方法 xticksyticks 在没有参数的情况下调用,来移除 x 和 y 轴标记。运行代码时,您将看到以下输出:

In an iterative loop of 10 counts, we create a subplot on each iteration and show an image from X_train vector in it. We title each image from the corresponding y_train vector. Note that the y_train vector contains the actual values for the corresponding image in X_train vector. We remove the x and y axes markings by calling the two methods xticks and yticks with null argument. When you run the code, you would see the following output −

examining data points

接下来,我们准备数据将其馈送到我们的网络中。

Next, we will prepare data for feeding it into our network.