function [nn, L] = nntrain(nn, train_x, train_y, opts)
m = size(train_x, 1);
batchsize = opts.batchsize;
numepochs = opts.numepochs;
numbatches = floor(m / batchsize);
L = zeros(numepochs*numbatches,1);
n = 1;
for k = 1 : numepochs
tic;
kk = randperm(m);
for j = 1 : numbatches
batch_x = train_x(kk((j - 1) * batchsize + 1 : j * batchsize), :);
batch_y = train_y(kk((j - 1) * batchsize + 1 : j * batchsize), :);
nn = nnff(nn, batch_x, batch_y);
nn = nnbp(nn);
nn = nngrads(nn);
L(n) = nn.loss;
n = n + 1;
end
t = toc;
nn = nnff(nn, train_x, train_y);
str_perf = sprintf('; Full-batch train err = %f', nn.loss);
disp(['NN train: epoch ' num2str(k) '/' num2str(opts.numepochs) '. Took ' num2str(t) ' seconds' '. Mini-batch mean squared error on training set is ' num2str(mean(L((n-numbatches):(n-1)))) str_perf]);
end
end
function nn = nnff(nn, x, y)
n = nn.n;
m = size(x, 1);
x = [ones(m,1) x];
nn.a{1} = x;
%feedforward pass
for i = 2 : n-1
nn.a{i} = sigm(nn.a{i - 1} * nn.W{i - 1}');
%Add the bias term
nn.a{i} = [ones(m,1) nn.a{i}];
end
nn.a{n} = sigm(nn.a{n - 1} * nn.W{n - 1}');
%error and loss
nn.error = y - nn.a{n};
nn.loss = 1/2 * sum(sum(nn.error .^ 2)) / m;
function X = sigm(P)
X = 1./(1+exp(-P));
end
end
function nn = nnbp(nn)
n = nn.n;
d{n} = - nn.error .* (nn.a{n} .* (1 - nn.a{n}));
for i = (n - 1) : -1 : 2
% Derivative of the activation function
d_act = nn.a{i} .* (1 - nn.a{i});
% Backpropagate first derivatives
if i+1==n % in this case in d{n} there is not the bias term to be removed
d{i} = (d{i + 1} * nn.W{i}) .* d_act; % Bishop (5.56)
else % in this case in d{i} the bias term has to be removed
d{i} = (d{i + 1}(:,2:end) * nn.W{i}) .* d_act;
end
end
for i = 1 : (n - 1)
if i+1==n
nn.dW{i} = (d{i + 1}' * nn.a{i}) / size(d{i + 1}, 1);
else
nn.dW{i} = (d{i + 1}(:,2:end)' * nn.a{i}) / size(d{i + 1}, 1);
end
end
end
function nn = nngrads(nn)
for i = 1 : (nn.n - 1)
dW = nn.dW{i};
dW = nn.learning_rate * dW;
if(nn.momentum>0)
nn.vW{i} = nn.momentum*nn.vW{i} + dW;
dW = nn.vW{i};
end
nn.W{i} = nn.W{i} - dW;
end
end
mozun2020
- 粉丝: 1w+
- 资源: 131
最新资源
- 阿尔法平台选择有批注(1-11).docx
- TA-Lib-0.4.28-cp311-cp311-win-amd64.whl
- 玄铁e907-r1s1用户手册-occ
- 阿尔法平台填空自测.pdf
- 匠芯创D13x芯片用户手册
- 阿尔法填空答案填空.pdf
- 匠芯创D13x硬件设计手册
- 阿尔法实验汇总.docx
- 匠芯创D13x数据手册
- 2024PPt资源02
- 手机拆螺丝机sw16可编辑全套技术资料100%好用.zip
- RISC-V 手册 中文版
- 四季除草机sw16可编辑全套技术资料100%好用.zip
- 水面垃圾自动收集装置sw18全套技术资料100%好用.zip
- 提砂机(砂水分离)sw18全套技术资料100%好用.zip
- 四柱油压机sw18可编辑全套技术资料100%好用.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0