Board logo

标题: 卷积神经网络算法的一个实现(3) [打印本页]

作者: yuyang911220    时间: 2015-10-22 21:07     标题: 卷积神经网络算法的一个实现(3)

为了描述清楚对卷积核旋转180与卷积层的残差的卷积所关联的权重与单元,正是前向计算所关联的权重与单元,我们选一个稍微大一点的卷积核,即假设卷积层采用用3*3的卷积核,其上一层采样层的输出map的大小是5*5,那么前向传输由采样层得到卷积层的过程如下:
   
  图8
  这里我们采用自己实现的convn(即内部不会对卷积核旋转),并假定上面的矩阵A、B下标都从1开始,那么有:
  B11 = A11*K11 + A12*K12 + A13*K13 + A21*K21 + A22*K22 + A23*K23 + A31*K31 + A32*K32 + A33*K33B12 = A12*K11 + A13*K12 + A14*K13 + A22*K21 + A23*K22 + A24*K23 + A32*K31 + A33*K32 + A34*K33B13 = A13*K11 + A14*K12 + A15*K13 + A23*K21 + A24*K22 + A25*K23 + A33*K31 + A34*K32 + A35*K33B21 = A21*K11 + A22*K12 + A23*K13 + A31*K21 + A32*K22 + A33*K23 + A41*K31 + A42*K32 + A43*K33B22 = A22*K11 + A23*K12 + A24*K13 + A32*K21 + A33*K22 + A34*K23 + A42*K31 + A43*K32 + A44*K33B23 = A23*K11 + A24*K12 + A25*K13 + A33*K21 + A34*K22 + A35*K23 + A43*K31 + A44*K32 + A45*K33B31 = A31*K11 + A32*K12 + A33*K13 + A41*K21 + A42*K22 + A43*K23 + A51*K31 + A52*K32 + A53*K33B32 = A32*K11 + A33*K12 + A34*K13 + A42*K21 + A43*K22 + A44*K23 + A52*K31 + A53*K32 + A54*K33B33 = A33*K11 + A34*K12 + A35*K13 + A43*K21 + A44*K22 + A45*K23 + A53*K31 + A54*K32 + A55*K33  我们可以得到B矩阵每个单元与哪些卷积核单元和哪些A矩阵的单元之间有关联:
  A11 [K11] [B11]A12 [K12, K11] [B12, B11]A13 [K13, K12, K11] [B12, B13, B11]A14 [K13, K12] [B12, B13]A15 [K13] [B13]A21 [K21, K11] [B21, B11]A22 [K22, K21, K12, K11] [B12, B22, B21, B11]A23 [K23, K22, K21, K13, K12, K11] [B23, B22, B21, B12, B13, B11]A24 [K23, K22, K13, K12] [B23, B12, B13, B22]A25 [K23, K13] [B23, B13]A31 [K31, K21, K11] [B31, B21, B11]A32 [K32, K31, K22, K21, K12, K11] [B31, B32, B22, B12, B21, B11]A33 [K33, K32, K31, K23, K22, K21, K13, K12, K11] [B23, B22, B21, B31, B12, B13, B11, B33, B32]A34 [K33, K32, K23, K22, K13, K12] [B23, B22, B32, B33, B12, B13]A35 [K33, K23, K13] [B23, B13, B33]A41 [K31, K21] [B31, B21]A42 [K32, K31, K22, K21] [B32, B22, B21, B31]A43 [K33, K32, K31, K23, K22, K21] [B31, B23, B22, B32, B33, B21]A44 [K33, K32, K23, K22] [B23, B22, B32, B33]A45 [K33, K23] [B23, B33]A51 [K31] [B31]A52 [K32, K31] [B31, B32]A53 [K33, K32, K31] [B31, B32, B33]A54 [K33, K32] [B32, B33]A55 [K33] [B33]  然后再用matlab的convn(内部会对卷积核进行180度旋转)进行一次convn(B,K,'full'),结合图7,看红色部分,除去0,A11=B'33*K'33=B11*K11,发现A11正好与K11、B11关联对不对;我们再看一个A24=B'34*K'21+B'35*K'22+B'44*K'31+B'45*K'32=B12*K23+B13*K22+B22*K13+B23*K12,发现参与A24计算的卷积核单元与B矩阵单元,正好是前向计算时关联的单元,所以我们可以通过旋转卷积核后进行卷积而得到采样层的残差。
  残差计算出来后,剩下的就是用更新权重和偏置,这和BP是一样的,因此不再细究,有问题欢迎交流。
   5、代码实现    详细的代码不再这里贴了,我依旧放在了 github ,欢迎参考和指正。我又是在重造车轮了,没有使用任何第三方的库类,这里贴一下调用代码:
  public static void runCnn() {
//创建一个卷积神经网络

LayerBuilder builder = new LayerBuilder();
builder.addLayer(Layer.buildInputLayer(
new Size(28, 28)));
builder.addLayer(Layer.buildConvLayer(
6, new Size(5, 5)));
builder.addLayer(Layer.buildSampLayer(
new Size(2, 2)));
builder.addLayer(Layer.buildConvLayer(
12, new Size(5, 5)));
builder.addLayer(Layer.buildSampLayer(
new Size(2, 2)));
builder.addLayer(Layer.buildOutputLayer(
10));
CNN cnn
= new CNN(builder, 50);

//导入数据集

String fileName = "dataset/train.format";
Dataset dataset
= Dataset.load(fileName, ",", 784);
cnn.train(dataset,
3);//
String modelName = "model/model.cnn";
cnn.saveModel(modelName);       
dataset.clear();
dataset
= null;

//预测
// CNN cnn = CNN.loadModel(modelName);   

Dataset testset = Dataset.load("dataset/test.format", ",", -1);
cnn.predict(testset,
"dataset/test.predict");    }




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0