CNN代码系列之训练初始化(三)

 

本博客为CNN卷积代码系列之训练初始化

注意:本博客是系列博客,请链接上一博客http://blog.youkuaiyun.com/samylee/article/details/69471988

CNN.hpp定义网络参数:

 

#ifndef _CNN_HPP_
#define _CNN_HPP_

#include <vector>

namespace ANN {

#define width_image_input_CNN		32 //归一化图像宽
#define height_image_input_CNN		32 //归一化图像高
#define width_image_C1_CNN		28
#define height_image_C1_CNN		28
#define width_image_S2_CNN		14
#define height_image_S2_CNN		14
#define width_image_C3_CNN		10
#define height_image_C3_CNN		10
#define width_image_S4_CNN		5
#define height_image_S4_CNN		5
#define width_image_C5_CNN		1
#define height_image_C5_CNN		1
#define width_image_output_CNN		1
#define height_image_output_CNN		1

#define width_kernel_conv_CNN		5 //卷积核大小
#define height_kernel_conv_CNN		5
#define width_kernel_pooling_CNN	2
#define height_kernel_pooling_CNN	2
#define size_pooling_CNN		2

#define num_map_input_CNN		1 //输入层map个数
#define num_map_C1_CNN			6 //C1层map个数
#define num_map_S2_CNN			6 //S2层map个数
#define num_map_C3_CNN			16 //C3层map个数
#define num_map_S4_CNN			16 //S4层map个数
#define num_map_C5_CNN			120 //C5层map个数
#define num_map_output_CNN		10 //输出层map个数

#define num_patterns_train_CNN		60000 //训练模式对数(总数)
#define num_patterns_test_CNN		10000 //测试模式对数(总数)
#define num_epochs_CNN			100 //最大迭代次数
#define accuracy_rate_CNN		0.985 //要求达到的准确率
#define learning_rate_CNN		0.01 //学习率
#define eps_CNN				1e-8

#define len_weight_C1_CNN		150 //C1层权值数,(5*5*1)*6=150
#define len_bias_C1_CNN			6 //C1层阈值数,6
#define len_weight_S2_CNN		6 //S2层权值数,1*6=6
#define len_bias_S2_CNN			6 //S2层阈值数,6
#define len_weight_C3_CNN		2400 //C3层权值数,(5*5*6)*16=2400
#define len_bias_C3_CNN			16 //C3层阈值数,16
#define len_weight_S4_CNN		16 //S4层权值数,1*16=16
#define len_bias_S4_CNN			16 //S4层阈值数,16
#define len_weight_C5_CNN		48000 //C5层权值数,(5*5*16)*120=48000
#define len_bias_C5_CNN			120 //C5层阈值数,120
#define len_weight_output_CNN		1200 //输出层权值数,(1*120)*10=1200
#define len_bias_output_CNN		10 //输出层阈值数,10

#define num_neuron_input_CNN		1024 //输入层神经元数,(32*32)*1=1024
#define num_neuron_C1_CNN		4704 //C1层神经元数,(28*28)*6=4704
#define num_neuron_S2_CNN		1176 //S2层神经元数,(14*14)*6=1176
#define num_neuron_C3_CNN		1600 //C3层神经元数,(10*10)*16=1600
#define num_neuron_S4_CNN		400 //S4层神经元数,(5*5)*16=400
#define num_neuron_C5_CNN		120 //C5层神经元数,(1*1)*120=120
#define num_neuron_output_CNN		10 //输出层神经元数,(1*1)*10=10

class CNN {
public:
	CNN();
	~CNN();

	void init(); //初始化,分配空间

protected:

	double E_weight_C1[len_weight_C1_CNN];
	double E_bias_C1[len_bias_C1_CNN];
	double E_weight_S2[len_weight_S2_CNN];
	double E_bias_S2[len_bias_S2_CNN];
	double E_weight_C3[len_weight_C3_CNN];
	double E_bias_C3[len_bias_C3_CNN];
	double E_weight_S4[len_weight_S4_CNN];
	double E_bias_S4[len_bias_S4_CNN];
	double* E_weight_C5;
	double* E_bias_C5;
	double* E_weight_output;
	double* E_bias_output;
};

}

#endif //_CNN_HPP_

 

funset.cpp中的cnn1.init()定义

 

void CNN::init()
{
	int len1 = width_image_input_CNN * height_image_input_CNN * num_patterns_train_CNN;//训练集输入
	data_input_train = new double[len1];
	init_variable(data_input_train, -1.0, len1);//初始化为-1

	int len2 = num_map_output_CNN * num_patterns_train_CNN;//训练集输出
	data_output_train = new double[len2];
	init_variable(data_output_train, -0.8, len2);//初始化-0.8

	int len3 = width_image_input_CNN * height_image_input_CNN * num_patterns_test_CNN;//测试集输入
	data_input_test = new double[len3];
	init_variable(data_input_test, -1.0, len3);//初始化为-1

	int len4 = num_map_output_CNN * num_patterns_test_CNN;//测试集输出
	data_output_test = new double[len4];
	init_variable(data_output_test, -0.8, len4);//初始化-0.8

	std::fill(E_weight_C1, E_weight_C1 + len_weight_C1_CNN, 0.0);//初始化0.0
	std::fill(E_bias_C1, E_bias_C1 + len_bias_C1_CNN, 0.0);
	std::fill(E_weight_S2, E_weight_S2 + len_weight_S2_CNN, 0.0);
	std::fill(E_bias_S2, E_bias_S2 + len_bias_S2_CNN, 0.0);
	std::fill(E_weight_C3, E_weight_C3 + len_weight_C3_CNN, 0.0);
	std::fill(E_bias_C3, E_bias_C3 + len_bias_C3_CNN, 0.0);
	std::fill(E_weight_S4, E_weight_S4 + len_weight_S4_CNN, 0.0);
	std::fill(E_bias_S4, E_bias_S4 + len_bias_S4_CNN, 0.0);
	E_weight_C5 = new double[len_weight_C5_CNN];
	std::fill(E_weight_C5, E_weight_C5 + len_weight_C5_CNN, 0.0);
	E_bias_C5 = new double[len_bias_C5_CNN];
	std::fill(E_bias_C5, E_bias_C5 + len_bias_C5_CNN, 0.0);
	E_weight_output = new double[len_weight_output_CNN];
	std::fill(E_weight_output, E_weight_output + len_weight_output_CNN, 0.0);
	E_bias_output = new double[len_bias_output_CNN];
	std::fill(E_bias_output, E_bias_output + len_bias_output_CNN, 0.0);

	initWeightThreshold();//初始化权重
	getSrcData();//载入数据
}//初始化为-1

	int len4 = num_map_output_CNN * num_patterns_test_CNN;//测试集输出
	data_output_test = new double[len4];
	init_variable(data_output_test, -0.8, len4);//初始化-0.8

	std::fill(E_weight_C1, E_weight_C1 + len_weight_C1_CNN, 0.0);//初始化0.0
	std::fill(E_bias_C1, E_bias_C1 + len_bias_C1_CNN, 0.0);
	std::fill(E_weight_S2, E_weight_S2 + len_weight_S2_CNN, 0.0);
	std::fill(E_bias_S2, E_bias_S2 + len_bias_S2_CNN, 0.0);
	std::fill(E_weight_C3, E_weight_C3 + len_weight_C3_CNN, 0.0);
	std::fill(E_bias_C3, E_bias_C3 + len_bias_C3_CNN, 0.0);
	std::fill(E_weight_S4, E_weight_S4 + len_weight_S4_CNN, 0.0);
	std::fill(E_bias_S4, E_bias_S4 + len_bias_S4_CNN, 0.0);
	E_weight_C5 = new double[len_weight_C5_CNN];
	std::fill(E_weight_C5, E_weight_C5 + len_weight_C5_CNN, 0.0);
	E_bias_C5 = new double[len_bias_C5_CNN];
	std::fill(E_bias_C5, E_bias_C5 + len_bias_C5_CNN, 0.0);
	E_weight_output = new double[len_weight_output_CNN];
	std::fill(E_weight_output, E_weight_output + len_weight_output_CNN, 0.0);
	E_bias_output = new double[len_bias_output_CNN];
	std::fill(E_bias_output, E_bias_output + len_bias_output_CNN, 0.0);

	initWeightThreshold();//初始化权重
	getSrcData();//载入数据
}

 

 

 

init_variable函数定义:

 

void CNN::init_variable(double* val, double c, int len)
{
	//printf("%d\n", &val);
	for (int i = 0; i < len; i++) {
		val[i] = c;
	}
}

 

未完待续。。。

 

 

 

任何问题请加唯一QQ2258205918(名称samylee)!

唯一VX:samylee_csdn

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值