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

下面的程序清单显示了BackpropMmt.m文件,它实现了带有动量的反向传播算法。

The following listing shows theBackpropMmt.m file, which implements the back-propagation algorithm with themomentum.

BackpropMmt函数的工作方式与前面的示例相同,它根据输入权重和训练数据返回调整后的权重。

The BackpropMmt function operates in thesame manner as that of the previous example; it takes the weights and trainingdata and returns the adjusted weights.

此列表中使用的变量与BackpropXOR函数中定义的变量完全相同。

This listing employs the same variables asdefined in the BackpropXOR function.

[W1 W2] = BackpropMmt(W1, W2, X, D)
function [W1, W2] = BackpropMmt(W1, W2, X, D)

alpha = 0.9;
beta = 0.9;
mmt1 = zeros(size(W1));
mmt2 = zeros(size(W2));
N = 4;
for k = 1:N

x = X(k, ?’;
d = D(k);
v1 = W1x;
y1 = Sigmoid(v1);
v = W2
y1;
y = Sigmoid(v);
e = d - y;
delta = y.(1-y).e;
e1 = W2’delta;
delta1 = y1.
(1-y1).e1;
dW1 = alpha
delta1
x’;
mmt1 = dW1 + beta
mmt1;
W1 = W1 + mmt1;
dW2 = alphadeltay1’;
mmt2 = dW2 + beta*mmt2;
W2 = W2 + mmt2;

end
end

当训练学习开始时,代码将动量mmt1和mmt2初始化为零。

The code initializes the momentums, mmt1and mmt2, as zeros when it starts the learning process.

修改权重调整公式,以反映动量的变化为:

The weight adjustment formula is modifiedto reflect the momentum as:

dW1 = alphadelta1x’;

mmt1 = dW1 + beta*mmt1;

W1 = W1 + mmt1;

下面的程序清单显示了TestBackpropMmt.m文件的内容,该程序对函数BackpropMmt进行测试。

The following program listing shows theTestBackpropMmt.m file, which tests the function BackpropMmt.

该程序调用BackpropMmt函数,并对神经网络进行10000次训练。

This program calls the BackpropMmt functionand trains the neural network 10,000 times.

训练数据被馈入到神经网络,并将输出显示在计算机屏幕上。

The training data is fed to the neuralnetwork and the output is shown on the screen.

通过将神经网络输出与训练数据的正确输出进行比较来验证网络训练的性能。

The performance of the training is verifiedby comparing the output to the correct output of the training data.

由于该代码与前面的示例几乎相同,因此省略了进一步的解释。

As this code is almost identical to that ofthe previous example, further explanation is omitted.

clear all

X = [ 0 0 1;

0 1 1;

1 0 1;

1 1 1;

];

D = [ 0 1 1 0];

W1 = 2*rand(4, 3) - 1;

W2 = 2*rand(1, 4) - 1;

for epoch = 1:10000 % train

[W1 W2] =BackpropMmt(W1, W2, X, D);

end

N = 4; % inference

for k = 1:N

x = X(k, ?’;

v1 = W1*x;

y1 =Sigmoid(v1);

v = W2*y1;

y = Sigmoid(v)

end

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值