Demo entry 6741808

k

   

Submitted by anonymous on May 16, 2018 at 14:48
Language: Matlab. Code size: 2.1 kB.

clear all; close all; clc;
load mnist_uint8;
%% Êý¾Ý´¦Àí
train_x = double(reshape(train_x',28,28,60000))/255;  %Êý¾Ý¹éÒ»»¯ÖÁ[0 1]Ö®¼ä
test_x = double(reshape(test_x',28,28,10000))/255;  %Êý¾Ý¹éÒ»»¯ÖÁ[0 1]Ö®¼ä
%%תÖÃ
train_x=permute(train_x,[2 1 3]);
test_x=permute(test_x,[2 1 3]);
train_y = double(train_y');
test_y = double(test_y');
%%ϸ»¯
% for i=1:60000
%     x1=train_x(:,:,i);
%     x2=bwmorph(x1,'thin',5);
%     train_xx(:,:,i)=x2;
% end
% for i=1:10000
%     x1=test_x(:,:,i);
%     x2=bwmorph(x1,'thin',5);
%     test_xx(:,:,i)=x2;
% end
% train_x=train_xx;
% test_x=test_xx;

%% ÍøÂç´î½¨
cnn.layers = {
    struct('type', 'i') %input layer
    struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer£¬6¸ö5*5µÄ¾í»ýºË£¬¿ÉÒԵõ½6¸öoutputmaps
    struct('type', 's', 'scale', 2) %sub sampling layer  £¬2*2µÄϲÉÑù¾í»ýºË
    struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer £¬12¸ö5*5µÄ¾í»ýºË£¬¿ÉÒԵõ½12¸öoutputmaps   
    struct('type', 's', 'scale', 2) %subsampling layer    £¬2*2µÄϲÉÑù¾í»ýºË
    };  %¶¨ÒåÁËÒ»¸ö5²ãÉñ¾­ÍøÂ磬»¹ÓÐÒ»¸öÊä³ö²ã£¬²¢Î´ÔÚÕâÀﶨÒå¡£

cnn = cnnsetup(cnn, train_x, train_y);  %ͨ¹ý¸Ãº¯Êý£¬¶ÔÍøÂç³õʼȨÖؾØÕóºÍÆ«Ïò½øÐгõʼ»¯

%% ÍøÂç²ÎÊý
opts.alpha = 1;  % ѧϰÂÊ
opts.batchsize = 50; %ÿbatchsizeÕÅͼÏñÒ»ÆðѵÁ·Ò»ÂÖ£¬µ÷ÕûÒ»´ÎȨֵ¡£
% opts.batchsize = 2;
% ѵÁ·´ÎÊý£¬ÓÃͬÑùµÄÑù±¾¼¯¡£ÎÒѵÁ·µÄʱºò£º
% 1µÄʱºò 11.41% error
% 5µÄʱºò 4.2% error
% 10µÄʱºò 2.73% error
opts.numepochs = 10;  %ÿ¸öepochÄÚ£¬¶ÔËùÓÐѵÁ·Êý¾Ý½øÐÐѵÁ·£¬¸üУ¨ÑµÁ·Í¼Ïñ¸öÊý/batchsize£©´ÎÍøÂç²ÎÊý

%% ÍøÂçѵÁ·
cnn = cnntrain(cnn, train_x, train_y, opts);  %ÍøÂçѵÁ·
% save cnn_50 cnn;
% load cnn_50;
save cnn_50_1_y cnn;
load cnn_50_1_y;

%% ÍøÂç²âÊÔ
[er_test, bad_test] = cnntest(cnn, test_x, test_y);  %ÍøÂç²âÊÔ
%plot mean squared error
plot(cnn.rL);
%show test error
disp(['10000 test images ' num2str(er_test*100) '% error']);
[er_train, bad_train] = cnntest(cnn, train_x, train_y);  %ÍøÂç²âÊÔ
disp(['60000 train images ' num2str(er_train*100) '% error']);

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).