matlab手写神经网络实现识别手写数字

matlab⼿写神经⽹络实现识别⼿写数字
实验说明
⼀直想⾃⼰写⼀个神经⽹络来实现⼿写数字的识别,⽽不是套⽤别⼈的框架。恰巧前⼏天,有幸从同学那拿到5000张已经贴好标签的⼿写数字图⽚,于是我就尝试⽤matlab写⼀个⽹络。
内蒙古大学图书馆实验数据:5000张⼿写数字图⽚(.jpg),图⽚命名为1.jpg,2.jpg…5000.jpg。还有⼀个放着标签的excel⽂件。
数据处理:前4000张作为训练样本,后1000张作为测试样本。
图⽚处理:⽤matlab的imread()函数读取图⽚的灰度值矩阵(28,28),然后把每张图⽚的灰度值矩阵reshape为
(28*28,1),然后把前4000张图⽚的灰度值矩阵合并为x_train,把后1000张图⽚的灰度值矩阵合并为x_test。
脚博士
神经⽹络设计
⽹络层设计:⼀层隐藏层,⼀层输出层
输⼊层:⼀张图⽚的灰度值矩阵reshape后的784个数,也就是x_train中的某⼀列
输出层:(10,1)的列向量,其中列向量中最⼤的数所在的索引就是预测的数字
激励函数:sigmoid函数()
更新法则:后向传播算法()
⼀点说明:这⾥的训练我分别⽤了普通梯度下降法和mini_batch(batch size 为10)梯度下降法来实现
测试:⽤了两种⽅式表⽰正确率,⼀是统计预测正确的个数,⽽是利⽤matlab的plotconfusion函数
⽹络实现
全部实现包括5个函数(gedata.m / layerout.m / mytrain.m / mytrain_mini.m / test.m)和⼀个main.m⽂件。
读取数据(getdata.m)
function[x_train,y_train,x_test,y_test]=getdata()
%把图⽚变成像素矩阵
%path :图⽚路径
% x_train:训练样本像素矩阵(784,4000)
%y_train:训练样本标签(10,4000)
%x_test:测试样本像素矩阵(784,1000)
%y_test:测试样本标签(10,1000)
% photopath = './photo/';
% snames=dir([photopath  '*' '.jpg'])%get all filenames in photopath
% l = length(snames)
%
% %get x_ data
% x_train = [];
% x_test = [];
%
% for i=1:4000
%    iname=[photopath snames(i).name] %the path of jpg
%    x = imread(iname);  % the shape of x is (28,28)
%    x = reshape(x,784,1);  %reshape x to (784,1)
%    x_train = [x_train,x];
% end
%
% for k=4001:5000
拼车生活
%    kname=[photopath snames(k).name];  %the path of jpg
%    x = imread(kname);  %the shape of x is (28,28)
%    x = reshape(x,784,1);  %reshape x  to (784,1)
%    x_test = [x_test,x];
% end
x_train=[];
for i=1:4000
x=im2double(imread(strcat(num2str(i),'.jpg')));
x=reshape(x,784,1);
x_train=[x_train,x];
end
x_test =[];
for k=4001:5000
x=im2double(imread(strcat(num2str(k),'.jpg')));
x=reshape(x,784,1);
x_test=[x_test,x];
end
data=xlsread('label.xlsx');
y_train=data(:,1:4000);
y_test = data(:,4001:5000);
x_train;
y_train;
x_test;
y_test;
end
这⾥踩了⼀个坑。我本来读取图⽚,是按⽬录来读取的,然后训练出来的效果⼀直不好。⼀度怀疑⾃⼰的更新函数写错了,改了很久,才发现按⽬录读取的图⽚顺序是错误的!按⽬录读取的图⽚并不是按1,2,3…这样读的,⽽是按下⾯的顺序读取的,这样就和label对不上
layerout 函数
训练⼀(mytrain.m )
function  [y] = layerout (w,b,x)
%output function
y = w*x + b;
n = length(y);
for  i =1:n
y(i)=1.0/(1+exp(-y(i)));
end
y;
end
苏州蚕桑专科学校function[w,b,w_h,b_h]=mytrain(x_train,y_train)
%train function:设置⼀个隐藏层,784-->隐藏层神经元个数-->10
%x_train:训练样本的像素数据
%y_train:训练样本的标签
%w:输出层权重
%b:输出层偏置
%w_h:隐藏层权重
%b_h:隐藏层偏置
%step:循环步数
step=input('迭代步数:');
a=input('学习因⼦:');
in = 784; %输⼊神经元个数
hid = input('隐藏层神经元个数:');%隐藏层神经元个数
out = 10; %输出层神经元个数
o =1;
w = randn(out,hid);
b = randn(out,1);
w_h =randn(hid,in);
b_h = randn(hid,1);
for i=0:step
%打乱训练样本
r=randperm(4000);
x_train = x_train(:,r);
y_train = y_train(:,r);
for j=1:4000
x = x_train(:,j);
y = y_train(:,j);
hid_put = layerout(w_h,b_h,x);
out_put = layerout(w,b,hid_put);
%更新公式的实现
o_update = (y-out_put).*out_put.*(1-out_put);
h_update = ((w')*o_update).*hid_put.*(1-hid_put);
outw_update = a*(o_update*(hid_put'));
outb_update = a*o_update;
hidw_update = a*(h_update*(x'));
hidb_update = a*h_update;
w = w + outw_update;
b = b+ outb_update;
w_h = w_h +hidw_update;
b_h =b_h +hidb_update;
end
end
end
训练⼆(mytrain_mini.m)
function[w,b,w_h,b_h]=mytrain_mini(x_train,y_train)
%train function:设置⼀个隐藏层,784-->隐藏层神经元个数-->10
%x_train:训练样本的像素数据
%y_train:训练样本的标签
%w:输出层权重
%b:输出层偏置
%w_h:隐藏层权重
%b_h:隐藏层偏置
%step:循环步数
step=ipout('迭代步数:');
a=input('学习因⼦:');
黎曼几何in = 784; %输⼊神经元个数
hid = input('隐藏层神经元个数:');%隐藏层神经元个数
out = 10; %输出层神经元个数
o =1;
w = randn(out,hid);
b = randn(out,1);
w_h =randn(hid,in);
b_h = randn(hid,1);
for i=0:step
%打乱训练样本
r=randperm(4000);
x_train = x_train(:,r);
y_train = y_train(:,r);
%mini_batch
for jj=0:399
%取batch为10  更新取10次的平均值
for j=jj*10+1:(jj+1)*10
x = x_train(:,j);
y = y_train(:,j);
hid_put = layerout(w_h,b_h,x);
out_put = layerout(w,b,hid_put);
%更新公式的实现
o_update = (y-out_put).*out_put.*(1-out_put);
h_update = ((w')*o_update).*hid_put.*(1-hid_put);
if j==1
outw_update = (double(a)/10)*(o_update*(hid_put'));
outb_update = (double(a)/10)*o_update;
hidw_update = (double(a)/10)*(h_update*(x'));
hidb_update = (double(a)/10)*h_update;
end
if j~=1
outw_update = outw_update + (double(a)/10)*(o_update*(hid_put'));                outb_update = outb_update -(double(a)/10)*o_update;
hidw_update = hidw_update + (double(a)/10)*(h_update*(x'));
hidb_update = hidb_update -(double(a)/10)*h_update;
end
end
w = w + outw_update;
b = b+ outb_update;
w_h = w_h +hidw_update;
b_h =b_h +hidb_update;
太原科技大学图书馆end
end
end

本文发布于:2024-09-22 14:23:37,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/104999.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实现   隐藏   读取   矩阵   训练样本   个数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议