概述
下面的程序清单显示了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 = W2y1;
y = Sigmoid(v);
e = d - y;
delta = y.(1-y).e;
e1 = W2’delta;
delta1 = y1.(1-y1).e1;
dW1 = alphadelta1x’;
mmt1 = dW1 + betammt1;
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》
更多精彩文章请关注微信号:
最后
以上就是欢呼心锁为你收集整理的【读书1】【2017】MATLAB与深度学习——动量(2)的全部内容,希望文章能够帮你解决【读书1】【2017】MATLAB与深度学习——动量(2)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复