【读书1】【2017】MATLAB与深度学习——示例:MNIST(5)

以下代码为MnistConv函数调用的Conv函数。

The following listing shows the functionConv, which the function MnistConv calls.

该函数根据输入图像和卷积滤波器矩阵,返回相应的特征映射。

This function takes the input image and theconvolution filter matrix and returns the feature maps.

这些代码位于Conv.m文件中。

This code is in the Conv.m file.

function y =Conv(x, W)

   [wrow,wcol, numFilters] = size(W);

   [xrow,xcol, ~ ] = size(x);

   yrow= xrow - wrow + 1;

   ycol= xcol - wcol + 1;

   y= zeros(yrow, ycol, numFilters);

   fork = 1:numFilters

          filter= W(:, :, k);

          filter= rot90(squeeze(filter), 2);

          y(:,:, k) = conv2(x, filter, 'valid');

   end

end

该代码使用MATLAB自带的二维卷积函数conv2实现卷积运算。

This code performs the convolutionoperation using conv2, a built-in two-dimensional convolution function ofMATLAB.

这里不再对函数Conv做进一步讲解。

Further details of the function Conv areomitted, as it is beyond the scope of this book.

函数MnistConv还调用了Pool函数,Pool函数的代码如下所示。

The function MnistConv also calls thefunction Pool, which is implemented in the following listing .

该函数对特征映射进行2×2均值池化处理后返回输出图像。

This function takes the feature map andreturns the image after the 2×2 mean pooling process.

Pool函数的代码位于Pool.m文件中。

This function is in the Pool.m file.

function y = Pool(x)

   %2x2 mean pooling

   [xrow,xcol, numFilters] = size(x);

   y= zeros(xrow/2, xcol/2, numFilters);

   fork = 1:numFilters

          filter= ones(2) / (2*2); % for mean

          image= conv2(x(:, :, k), filter, 'valid');

          y(:,:, k) = image(1:2:end, 1:2:end);

   end

end

这段代码比较有趣,它调用二维卷积函数conv2实现函数Conv的功能。

There is something interesting about thiscode; it calls the two-dimensional convolution function, conv2, just as thefunction Conv does.

这是因为池化过程是卷积运算的一种类型。

This is because the pooling process is atype of a convolution operation.

本例均值池化的实现是通过与以下滤波器的卷积完成的:

The mean pooling of this example isimplemented using the convolution operation with the following filter:

在这里插入图片描述

池化层的滤波器是预先定义的,而卷积层的滤波器是通过训练来确定的。

The filter of the pooling layer ispredefined, while that of the convolution layer is determined through training.

关于代码的更多细节已经超过本书讲解范围,请读者自行阅读理解。

The further details of the code are beyondthe scope of this book.

以下代码为TestMnistConv.m中的内容,它实现对函数MnistConv的测试。

The following listing shows theTestMnistConv.m file, which tests the function MnistConv.

loadMNISTImages和loadMNISTLabels函数下载地址:github.com/amaas/stanford_dl_ex/tree/master/common。

loadMNISTImages and loadMNISTLabelsfunctions are from github.com/amaas/stanford_dl_ex/tree/master/common.

该程序调用函数MnistConv,并对网络进行三次训练。

This program calls the function MnistConvand trains the network three times.

它向训练网络提供了2000个测试数据点,并计算出训练的准确性。

It provides the 2,000 test data points tothe trained network and displays its accuracy.

这个例子的测试运行大约需要2分钟30秒,获得了93%的准确度。(这里的运行时间与具体的电脑性能有关系,作者给出的时间只能作为参考)

The test run of this example yielded anaccuracy of 93% in 2 minutes and 30 seconds.

——本文译自Phil Kim所著的《Matlab Deep Learning》

更多精彩文章请关注微信号:在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42825609/article/details/84613862