【读书1】【2017】MATLAB与深度学习——Dropout(2)

下面是Dropout函数的具体实现细节。

Here are the details of the implementationof the function Dropout.

该函数的输入为输出向量和dropout比例,输出的返回值为dropout后的新向量。

It takes the output vector and dropoutratio and returns the new vector that will be multiplied to the output vector.

ym = Dropout(y, ratio)

其中y是输出向量,ratio是输出向量的丢弃比例。

where y is the output vector and ratio isthe ratio of the dropout of the output vector.

输出向量ym与输入向量y具有相同的维数。

The return vector ym of the functionDropout has the same dimensions as y.

向量ym中的零元素比例为ratio,剩下的为非零元素1/(1–ratio)。

ym contains zeros for as many elements asthe ratio and 1/(1–ratio) for the other elements.

考虑下面的例子:

Consider the following example:

y1 = rand(6, 1)

ym = Dropout(y1,0.5)

y1 = y1 .* ym

函数Dropout实现了dropout的功能。

The function Dropout implements thedropout.

执行该代码将显示如图5-7所示的结果。

Executing this code will display theresults shown in Figure 5-7.

在这里插入图片描述
图5-7 dropout 函数显示的结果The dropout function inaction

向量ym中的一半(三个元素)为0,其它三个元素为1/(1-0.5) = 2。

The vector ym has three elements: half(0.5) of the six elements of the vector y1, which are filled with zeroes, andthe others are filled with 1/(1-0.5), which equals 2.

ym乘以原始向量y1,经过乘积修改后的y1中元素以指定比例为零。

When this ym is multiplied to the originalvector y1, the revised y1 has zeros by the specified ratio.

也就是说,y1丢弃了指定比例的向量元素。

In other words, y1 drops out the specifiedportion of the elements.

其它元素乘以1/(1-ratio)的原因是为了补偿丢弃元素的输出损失。

The reason that we multiply the otherelement by 1/(1-ratio) is to compensate for the loss of output due to thedropped elements.

在前面的例子中,一旦向量y1的一半元素被丢弃,该层输出的幅度显著减小。

In the previous example, once half of thevector y1 has been dropped out, the magnitude of the layer’s outputsignificantly diminishes.

因此,幸存节点的输出应当进行适当放大。

Therefore, the outputs of the survivednodes are amplified by the proper proportion.

Dropout.m文件实现了Dropout函数的功能,即:

The function Dropout is implemented in theDropout.m file :

function ym =Dropout(y, ratio)

   [m, n] = size(y);

   ym= zeros(m, n);

   num= round(m*n*(1-ratio));

   idx= randperm(m*n, num);

   ym(idx)= 1 / (1-ratio);

end

这里的文字解释很长,但实现的代码是很简单的。

The explanation is long, but the codeitself is very simple.

上述代码实现向量ym的输出,ym的维数与y相同。

The code prepares the zero matrix ym, ofwhich the dimension is the same as that of y.

根据给定的丢弃比例ratio计算保留的元素个数num,并从ym中随机选定需要保留的元素。

It calculates the number of survivors, num,based on the given dropout ratio, ratio, and randomly selects the survivorsfrom ym.

具体的说,代码中是选定了ym元素的序号。

Specifically, it selects the indices of theelements of ym.

这种随机选定序号的操作是由randperm完成的。

This is done by the randperm portion of thecode.

在代码中确定出非零元素对应的序号,将数值1/(1-ratio)赋给这些选定的元素。

Now that the code has the indices of thenon-zero elements, put 1/(1-ratio) into those elements.

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

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

猜你喜欢

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