function cnn = cnn_initialize(cnn)
%CNN_INIT initialize the weights and biases, and other parameters
%
index = 0;
num_layer = numel(cnn.layer);
for in = 1:num_layer
switch cnn.layer{in}{1}
case 'input'
index = index + 1;
height = cnn.layer{in}{3}(1);
width = cnn.layer{in}{3}(2);
mini_size = cnn.layer{in}{2};
cnn.weights{index} = [];
cnn.biases{index} = [];
cnn.nabla_w{index} = [];
cnn.nabla_b{index} = [];
%n*n*m
cnn.a{index} = [];
cnn.z{index} = [];
cnn.delta{index} = [];
cnn.mini_size = mini_size;
case 'conv'
index = index + 1;
%kernel height, width, number
ker_height = cnn.layer{in}{3}(1);
ker_width = cnn.layer{in}{3}(2);
ker_num = cnn.layer{in}{2};
cnn.weights{index} = grand(ker_height,ker_width,ker_num) - 0.5;
cnn.biases{index} = grand(1,ker_num) - 0.5;
cnn.nabla_w{index} = zeros(ker_height,ker_width,ker_num);
cnn.nabla_b{index} = zeros(1,ker_num);
height = height - ker_height + 1;
width = width - ker_width + 1;
cnn.a{index} = zeros(height,width,mini_size,ker_num);
cnn.z{index} = zeros(height,width,mini_size,ker_num);
cnn.delta{index} = zeros(height,width,mini_size,ker_num);
case 'pool'
index = index + 1;
%kernel height, width, number
ker_height = cnn.layer{in}{3}(1);
ker_width = cnn.layer{in}{3}(2);
cnn.weights{index} = [];
cnn.biases{index} = [];
cnn.nabla_w{index} = [];
cnn.nabla_b{index} = [];
height = height / ker_height;
width = width / ker_width;
cnn.a{index} = zeros(height,width,mini_size,ker_num);
cnn.z{index} = [];
cnn.delta{index} = zeros(height,width,mini_size,ker_num);
case 'flat'
index = index + 1;
cnn.weights{index} = [];
cnn.biases{index} = [];
cnn.nabla_w{index} = [];
cnn.nabla_b{index} = [];
cnn.a{index} = zeros(height*width*ker_num,mini_size);
cnn.z{index} = [];
cnn.delta{index} = zeros(height*width*ker_num,mini_size);
case 'full'
index = index + 1;
%kernel height, width, number
neuron_num = cnn.layer{in}{2};
neuron_num0 = size(cnn.a{in-1},1);
cnn.weights{index} = grand(neuron_num,neuron_num0) - 0.5;
cnn.biases{index} = grand(neuron_num,1) - 0.5;
cnn.nabla_w{index} = zeros(neuron_num,neuron_num0);
cnn.nabla_b{index} = zeros(neuron_num,1);
cnn.a{index} = zeros(neuron_num,mini_size);
cnn.z{index} = zeros(neuron_num,mini_size);
cnn.delta{index} = zeros(neuron_num,mini_size);
case 'output'
index = index + 1;
%kernel height, width, number
neuron_num = cnn.layer{in}{2};
neuron_num0 = size(cnn.a{in-1},1);
cnn.weights{index} = grand(neuron_num,neuron_num0) - 0.5;
cnn.biases{index} = grand(neuron_num,1);
cnn.nabla_w{index} = zeros(neuron_num,neuron_num0);
cnn.nabla_b{index} = zeros(neuron_num,1);
cnn.a{index} = zeros(neuron_num,mini_size);
cnn.z{index} = zeros(neuron_num,mini_size);
cnn.delta{index} = zeros(neuron_num,mini_size);
otherwise
end
end
end
没有合适的资源?快使用搜索试试~ 我知道了~
由于MATLAB版本的问题,有的同学下载之后可能出错,不保证个别版本出错呀 包含BP和CNN程序。不依赖任何库,包含MNIST数据,BP网络可达到98.3%的识别率,CNN可达到99%的识别率。CNN比较耗时,关于CNN的程序介绍:https://blog.csdn.net/hoho1151191150/article/details/79714691
资源详情
资源评论
资源推荐
收起资源包目录





















































共 44 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9













oio328Loio
- 粉丝: 507
- 资源: 41

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0