卷积神经网络代码简单备注
我看的是mycnn的代码
1 主函数example_LeNet5_SDLM_training:
close all force
clear all force% load('vehicleFEFace32.mat');
classes_to_select = [1,2,3,4,5,6,7,8,9,10]; % character classes
train_samples_per_class = 200; % how many samples per class should be used for training
test_samples_per_class = 200; % how many samples per class should be used for testing
inp_rows = 32; % number of rows in training images
inp_cols = inp_rows; % number of columns in training images
n_iterations = 35; % number of iterations of training algorithm
recom_Hes_it = 15; % recompute diagonal Hessian every recom_Hes_it iterations
show_perf_it = 1; % show performance plots every show_perf_it iterations [inf - don't show]
use_LeCun_eta = true; % use the eta sequence, proposed by LeCun
resize_samples = false; % resize or pad the input images flag
samples_coeff = 1; % rescaling coefficient for the input data
max_subsamples = 500; % maximal number of samples for Hessian estimation
max_samples = 60000; % maximal number of samples for training
n_classes = numel(classes_to_select);
n_train_samples = n_classes * train_samples_per_class;
n_subsamples = min(min(max(ceil(n_train_samples*1.0), 20), max_subsamples), n_train_samples);
% set the path to MNIST dataset...
% the original MNIST dataset can be found on Yann LeCun's web-site: http://yann.lecun.com/exdb/mnist/index.html
% MNIST converted into Matlab format can be found on my web-page:
% train dataset: http://sites.google.com/site/chumerin/projects/mycnn/train_data.mat
% test dataset: http://sites.google.com/site/chumerin/projects/mycnn/test_data.mat
if ispc,
mnist_dir = './'; % Windows
else
mnist_dir = '/data/data2_sidonia/nick/datasets/MNIST/'; % *nix
end
if ~exist(mnist_dir, 'dir'),
error(['Can''t find the MNIST dataset directory [%s]! ' ...
'This example needs the MNIST dataset, which can be downloaded from ' ...
'http://yann.lecun.com/exdb/mnist/ or from ' ...
'http://sites.google.com/site/chumerin/projects/mycnn/, ' ...
'also don''t forget to update the mnist_dir variable in the source code.'], mnist_dir)
end
% read and preprocess data for training from MNIST dataset
disp('Loading train data from MNIST')
[train_samples train_targets] = get_MNIST_data([mnist_dir 'train_data.mat'], inp_rows, inp_cols, ...
classes_to_select, train_samples_per_class, resize_samples, samples_coeff);%准备训练数据
% read and preprocess data for testing from MNIST dataset
disp('Loading test data from MNIST')
[test_samples test_targets] = get_MNIST_data([mnist_dir 'test_data.mat'], inp_rows, inp_cols, ...
classes_to_select, test_samples_per_class, resize_samples, samples_coeff);%准备测试数据
% create a new LeNet5 network
LeNet5 = newLeNet5(inp_rows, inp_cols, n_classes);
% show some crucial info
fprintf('Number of classes: %g\n', n_classes)
fprintf('Train samples: %-6g (%gx%g)\n', train_samples_per_class*n_classes, n_classes, train_samples_per_class)
fprintf('Test samples: %-6g (%gx%g)\n', test_samples_per_class*n_classes, n_classes, test_samples_per_class)
fprintf('Number of subsamples for diagonal Hessian estimation: %g\n', n_subsamples)
% train the network using stochastic version of the Levenberg-Marquardt
% algorithm
[LeNet5 E MCR] = train_LM(LeNet5, train_samples, train_targets, test_samples, test_targets, show_perf_it, ...
use_LeCun_eta, max_subsamples, recom_Hes_it, n_iterations, train_samples_per_class);%用训练样本数据进行训练
2 训练函数train_LM:
function [net train_err train_mcr test_err test_mcr] = ...
train_LM(net, train_samples, train_labels, test_samples, test_labels, show_it, ...
use_LeCun_eta, max_subsamples, recom_Hes_it, ...
n_iterations, samples_per_class, noise)
% processing function input
if ~exist('show_it', 'var') || isempty(show_it), show_it = inf; end
if ~exist('use_LeCun_eta', 'var') || isempty(use_LeCun_eta), use_LeCun_eta = true; end
if ~exist('n_iterations', 'var') || isempty(n_iterations), n_iterations = 50; end
if ~exist('max_subsamples', 'var') || isempty(max_subsamples), max_subsamples = 100; end
if ~exist('recom_Hes_it', 'var') || isempty(recom_Hes_it), recom_Hes_it = 16; end
if ~exist('noise', 'var') || isempty(noise), noise = 0; end
if 1 %exist('test_inp', 'var') && exist('test_out', 'var'),
test_mcr = [];
test_err = [];
end
show_performance_plots = isfinite(show_it) && show_it>0;
if use_LeCun_eta,
eta_from_LeCun = [50 50 20 20 20 10 10 10 5 5 5 5 1]/100000;
else
% if ~exist('eta_max', 'var') || isempty(eta_max), eta_max = .0002; end
% if ~exist('eta_min', 'var') || isempty(eta_min), eta_min = .00001; end
% if ~exist('eta_fading', 'var') || isempty(eta_fading), eta_fading = .8; end
eta_max = .0002;
eta_min = .00001;
eta_fading = .8;
end
classes_to_select = [1,2,3,4,5,6,7,8,9,10];
[in_rows in_cols ] = size(train_samples(:,:,1));
n_train_samples = samples_per_class*numel(classes_to_select);
n_subsamples = min(min(max(ceil(n_train_samples*1.0), 20), max_subsamples), n_train_samples);
% Normalizing train_inp:
% for i = 1:n_train_samples,
% sample = train_inp(:,:,i);
% sample = sample - mean(sample(:));
% sample = sample / std(sample(:));
% train_inp(:,:,i) = sample;
% end
if use_LeCun_eta,
eta = eta_from_LeCun(1);
else
eta = eta_max;
end
if show_performance_plots,
perf_fig = figure('Name', ['SDHLM training of myCNN (@' host ')'], 'NumberTitle', 'off');
end
[train_inp, train_out] = select_samples(train_samples, train_labels, classes_to_select, samples_per_class);
[test_inp, test_out] = select_samples(test_samples, test_labels, classes_to_select, samples_per_class);
% initial evaluation
[train_err perf out train_mcr] = get_performance(net, train_inp, train_out);
log_it('Network initial performance on train data: RMSE:%-10.7f MCR:%-5.2f%% ', train_err, 100*train_mcr);
if exist('test_err', 'var')
[test_err perf out test_mcr] = get_performance(net, test_inp, test_out);
log_it('Network initial performance on test data: RMSE:%-10.7f MCR:%-5.2f%% ', test_err, 100*test_mcr);
end
etas = nan;
net = forget_derivatives(net); % Clear first derivatives data of the net
start_index = 1;
for i = 1:n_iterations,
if (i>1)
[train_inp, train_out] = select_samples(train_samples, train_labels, classes_to_select, samples_per_class);%这是我自己加的函数,使每次训练数据再从整体训练数据中重新提取。
[test_inp, test_out] = select_samples(test_samples, test_labels, classes_to_select, samples_per_class);
end
recompute_Hessian = (recom_Hes_it == 1) || (mod(i, recom_Hes_it) == 1);
net = set_global_learning_rate(net, eta);
if recompute_Hessian,
fprintf('Hessian diagonal: [reestimation: ')
net = forget_ddeltas(net);
% Get indeces of the new subset for the Hessian diagonal reestimation
end_index = min(n_train_samples, start_index+n_subsamples-1);
sub_i = start_index:end_index;
if end_index-start_index+1 < n_subsamples,
end_index = n_subsamples - (end_index-start_index+1);
start_index = 1;
sub_i = [sub_i start_index:end_index];
end
% Compute new start subset index (for next Hessian reestimation)
start_index = end_index + 1;
if start_index > n_train_samples, start_index = 1; end
%下面这段函数的目的是利用部分样本来训练卷积神经网络的权重和修正值更新的自适应学习率。
for s = 1:n_subsamples,
fprintf('\b\b\b\b\b\b%5.1f%%', 100*(s-1)/n_subsamples)
subsample_i = sub_i(s);
sample = train_inp(:,:,subsample_i);
target = train_out(:,subsample_i); % target = mat2cell(train_out(:,subsample_i), ones(1,n_classes), 1);
net = propagate(net, sample);
net = forget_derivatives(net); % Clear first derivatives data of the net
net = backpropagate(net, target);
net = forget_second_derivatives(net); % Clear second derivatives data of the net
net = backbackpropagate(net);
net = accumulate_ddeltas(net);
fprintf('\b\b\b\b\b\b%5.1f%%', 100*s/n_subsamples)
end % of subsample loop
fprintf(' done] ')
fprintf('[averaging:')
net = average_ddeltas(net, n_subsamples); % average second derivatives data of the net
fprintf('done] ')
fprintf('[learning rates reestimation:')
net = compute_learning_rates(net);
fprintf('done]\n')
end % of recompute Hessian condition
order_of_samples = randperm(n_train_samples);
log_it('Iteration:%-4g eta:%-9.7f', i, eta);
fprintf(' [training: ');
net = forget_deltas(net);
%利用得到的自适应学习率来进行训练,这是训练主体代码
% Training
for s_i = 1:n_train_samples,
fprintf('\b\b\b\b\b\b%5.1f%%', 100*(s_i-1)/n_train_samples)
s = order_of_samples(s_i);
sample = train_inp(:,:,s);
target = train_out(:,s);
sample = sample + randn(size(sample)) * noise;
net = propagate(net, sample);
net = forget_derivatives(net); % Clear first derivatives data of the net
net = backpropagate(net, target);
net = adapt_deltas(net);
net = adapt_net(net);
fprintf('\b\b\b\b\b\b%5.1f%%', 100*s_i/n_train_samples)
end
fprintf(' done]\n')
%绘制训练样本和测试样本在已训练好的CNN模型下的误差曲线
% evaluation
if exist('train_err', 'var')
[trRMSE perf out trMCR] = get_performance(net, train_inp, train_out);
% fprintf('Train RMSE: %-10.7f MCR:%-6.2f%% ', trRMSE, 100*trMCR);
log_it('Network performance on iteration %-03g on train data: RMSE:%-10.7f MCR:%-5.2f%% ', i, trRMSE, 100*trMCR);
train_err = [train_err trRMSE];
train_mcr = [train_mcr trMCR]; %#ok<*AGROW>
end
if exist('test_err', 'var')
[teRMSE perf out teMCR] = get_performance(net, test_inp, test_out);
log_it('Network performance on iteration %-03g on test data: RMSE:%-10.7f MCR:%-5.2f%% ', i, teRMSE, 100*teMCR);
test_err = [test_err teRMSE];
test_mcr = [test_mcr teMCR];
end
if show_performance_plots && ( mod(i, show_it) == 0 || i==n_iterations ),
figure(perf_fig)
% root mean square error
subplot(211);
hold off
semilogy(0:i, train_err, 'b--', 'LineWidth', 2)
xlabel('iterations')
if exist('test_err', 'var'),
hold on
semilogy(0:i, test_err, 'r:', 'LineWidth', 2)
legend('train', 'test')
title( sprintf('RMSE [train: %g, test: %g]', trRMSE, teRMSE) )
else
legend('train')
title( sprintf('RMSE [train: %g]', trRMSE) )
end
% misclassification rate
subplot(212);
hold off
plot(0:i, 100*train_mcr, 'b--', 'LineWidth', 2)
xlabel('iterations')
if exist('test_mcr', 'var'),
hold on
plot(0:i, 100*test_mcr, 'r:', 'LineWidth', 2)
legend('train', 'test')
title( sprintf('Misclassification rate [train: %g%%, test: %g%%]', 100*trMCR, 100*teMCR) )
else
legend('train')
title( sprintf('Misclassification rate [train: %g%%]', 100*trMCR) )
end
drawnow
end % show performance condition
fprintf('\n');
etas = [etas eta];
% new global learning rate
if use_LeCun_eta,
if i<numel(eta_from_LeCun),
eta = eta_from_LeCun(i+1);
else
eta = eta_from_LeCun(end);
end
else
eta = eta_min + (eta_max - eta_min)*eta_fading^i;
end % new eta value selection
str = ['scnn32_100_' num2str(i) '.mat'];
save(str, 'net');
end % of iteration loop
end % of function TRAIN_LM
- 【Tensorflow】实现简单的卷积神经网络CNN实际代码
- 【html】【一个简单的用户登录页面代码】
- 代码练习 简单文件读写 字符串 数组的处理 list Dictionary
- KNN的matlab简单实现代码
- PHP设计模式之单例模式简单代码介绍
- 用装饰模式优化代码的一个简单例子
- 【转载】读懂IL代码就这么简单(三)完结篇
- JAVA播放MP3的完整代码---简单写个小例子
- 关于STL中vector的内存管理与简单代码实现
- python简单网页爬虫代码
- 最简单关闭软键盘的方法——只需要一行代码
- Javascript浮动广告图片代码,已封装!兼容性强!使用简单!
- android中简单的listview代码
- IDA简单Switch-Case结构的汇编代码分析
- 代码录播:jQueryMobile 实现一个简单的弹出框效果
- C#使用读写锁三行代码简单解决多线程并发写入文件时线程同步的问题
- php导出word文档与excel电子表格的简单示例代码
- Android之Box2D学习(二)——简单的跑跳游戏模型(含代码)
- Python代码实现简单的MNIST手写数字识别(适合初学者看)
- 初学者利用git 上传代码到Coding的简单操作步骤