atlab cnn程序例子

Posted chengxufuwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了atlab cnn程序例子相关的知识,希望对你有一定的参考价值。

clear all; close all; clc;  
addpath(‘../data‘);  
addpath(‘../util‘);  
load mnist_uint8;  
  
train_x = double(reshape(train_x‘,28,28,60000))/255;  
test_x = double(reshape(test_x‘,28,28,10000))/255;  
train_y = double(train_y‘);  
test_y = double(test_y‘);  
  
%% ex1   
%will run 1 epoch in about 200 second and get around 11% error.   
%With 100 epochs you‘ll get around 1.2% error  
  
cnn.layers = {  
    struct(‘type‘, ‘i‘) %input layer  
    struct(‘type‘, ‘c‘, ‘outputmaps‘, 6, ‘kernelsize‘, 5) %convolution layer  
    struct(‘type‘, ‘s‘, ‘scale‘, 2) %sub sampling layer  
    struct(‘type‘, ‘c‘, ‘outputmaps‘, 12, ‘kernelsize‘, 5) %convolution layer  
    struct(‘type‘, ‘s‘, ‘scale‘, 2) %subsampling layer  
};  
  
% 这里把cnn的设置给cnnsetup,它会据此构建一个完整的CNN网络,并返回  
cnn = cnnsetup(cnn, train_x, train_y);  
  
% 学习率  
opts.alpha = 1;  
% 每次挑出一个batchsize的batch来训练,也就是每用batchsize个样本就调整一次权值,而不是  
% 把所有样本都输入了,计算所有样本的误差了才调整一次权值  
opts.batchsize = 50;   
% 训练次数,用同样的样本集。我训练的时候:  
% 1的时候 11.41% error  
% 5的时候 4.2% error  
% 10的时候 2.73% error  
opts.numepochs = 10;  
  
% 然后开始把训练样本给它,开始训练这个CNN网络  
cnn = cnntrain(cnn, train_x, train_y, opts);  
  
% 然后就用测试样本来测试  
[er, bad] = cnntest(cnn, test_x, test_y);  
  
%plot mean squared error  
plot(cnn.rL);  
%show test error  
disp([num2str(er*100) ‘% error‘]);  

  

以上是关于atlab cnn程序例子的主要内容,如果未能解决你的问题,请参考以下文章

分享几个实用的代码片段(附代码例子)

golang goroutine例子[golang并发代码片段]

在android中显示隐藏片段

tensorflow学习笔记五:mnist实例--卷积神经网络(CNN)

如何创建片段以重复变量编号中的代码行

基于pytorch使用实现CNN 如何使用pytorch构建CNN卷积神经网络