DeepLearningtoolbox(2):cnnexample.m

clear all; close all; clc; 
load mnist_uint8;%导入数据集

train_x = double(reshape(train_x',28,28,60000))/255; 
%使用MNIST数据集,将图像进行归一化
test_x = double(reshape(test_x',28,28,10000))/255; 
%同理,将测试图像进行归一化

 train_y=double(train_y');
 %强制转换类型,为double型,大小为10*60000
 test_y=double(test_y');
 %大小为10*10000

% 训练一个6c-2s-12c-2s形式的卷积神经网络
rng(0)%rng(sd)用于生成随机数,这个地方为什么要有这个函数···留待···
cnn.layers = {  
    struct('type', 'i') %input layer
    struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer,输出6个特征图,卷积核的大小为5*5  
    struct('type', 's', 'scale', 2)%sub sampling layer,滑动窗口大小为2*2
    struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer,输出12个特征图,卷积核的大小为5*5 
    struct('type', 's', 'scale', 2)%subsampling layer,滑动窗口大小为2*2
};  

% 这里把cnn的设置给cnnsetup,它会据此构建一个完整的CNN网络,并返回  
cnn = cnnsetup(cnn, train_x, train_y);  

% 学习率  
opts.alpha = 0.1; 

 % 每次挑出一个batchsize的batch来训练,也就是每用batchsize个样本就调整一次权值,而不是  
% 把所有样本都输入了,计算所有样本的误差了才调整一次权值  
opts.batchsize = 50; 

% 训练次数,用同样的样本集  
% 1的时候 11.41% error  
% 5的时候 4.2% error  
% 10的时候 2.73% error 
% 50的时候 1.89% error
opts.numepochs = 1;

 % 把训练样本给网络,开始训练这个CNN网络  
cnn = cnntrain(cnn, train_x, train_y, opts);  
%save CNN_5 cnn; 

% 用测试样本来测试  
%load CNN_5 cnn;
[er, bad] = cnntest(cnn, test_x, test_y); 

%输出均方误差
figure,plot(cnn.rL);  
%assert(er<0.12, 'Too big error');

%画图显示
disp([num2str(er*100) '% error']);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值