首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >计算CNN实现中的卷积层

计算CNN实现中的卷积层
EN

Stack Overflow用户
提问于 2015-05-12 22:36:06
回答 1查看 1.3K关注 0票数 13

我正在尝试使用稀疏自动编码器来训练卷积神经网络,以便计算卷积层的滤波器。我正在使用UFLDL代码来构建补丁和训练CNN网络。我的代码如下:

===========================================================================
imageDim = 30;         % image dimension
imageChannels = 3;     % number of channels (rgb, so 3)

patchDim = 10;          % patch dimension
numPatches = 100000;    % number of patches

visibleSize = patchDim * patchDim * imageChannels;  % number of input units 
outputSize = visibleSize;   % number of output units
hiddenSize = 400;           % number of hidden units 

epsilon = 0.1;         % epsilon for ZCA whitening

poolDim = 10;          % dimension of pooling region

optTheta =  zeros(2*hiddenSize*visibleSize+hiddenSize+visibleSize, 1);
ZCAWhite =  zeros(visibleSize, visibleSize);
meanPatch = zeros(visibleSize, 1);

load patches_16_1
===========================================================================

% Display and check to see that the features look good
W = reshape(optTheta(1:visibleSize * hiddenSize), hiddenSize, visibleSize);
b =     optTheta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize);

displayColorNetwork( (W*ZCAWhite));

stepSize = 100; 
assert(mod(hiddenSize, stepSize) == 0, stepSize should divide hiddenSize);

load train.mat % loads numTrainImages, trainImages, trainLabels
load train.mat  % loads numTestImages,  testImages,  testLabels
% size 30x30x3x8862

numTestImages = 8862;
numTrainImages = 8862;

pooledFeaturesTrain = zeros(hiddenSize, numTrainImages, floor((imageDim -     patchDim + 1) / poolDim), floor((imageDim - patchDim + 1) / poolDim) );
pooledFeaturesTest = zeros(hiddenSize, numTestImages, ...
floor((imageDim - patchDim + 1) / poolDim), ...
floor((imageDim - patchDim + 1) / poolDim) );

 tic();

 testImages = trainImages;

for convPart = 1:(hiddenSize / stepSize)

 featureStart = (convPart - 1) * stepSize + 1;
 featureEnd = convPart * stepSize;

  fprintf('Step %d: features %d to %d\n', convPart, featureStart, featureEnd);  
  Wt = W(featureStart:featureEnd, :);
  bt = b(featureStart:featureEnd);    

  fprintf('Convolving and pooling train images\n');
  convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ...
    trainImages, Wt, bt, ZCAWhite, meanPatch);
  pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis);
  pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis;   
  toc();
  clear convolvedFeaturesThis pooledFeaturesThis;

  fprintf('Convolving and pooling test images\n');
  convolvedFeaturesThis = cnnConvolve(patchDim, stepSize, ...
    testImages, Wt, bt, ZCAWhite, meanPatch);
  pooledFeaturesThis = cnnPool(poolDim, convolvedFeaturesThis);
  pooledFeaturesTest(featureStart:featureEnd, :, :, :) = pooledFeaturesThis;   
  toc();

  clear convolvedFeaturesThis pooledFeaturesThis;

 end

我在计算卷积层和池化层时遇到了问题。我得到pooledFeaturesTrain(featureStart : featureEnd,:)= pooledFeaturesThis;下标赋值维度不匹配。路径通常会计算出来,它们是:

我正在尝试理解convPart变量到底在做什么以及pooledFeaturesThis到底在做什么。其次,我注意到我的问题是pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis;这一行中的不匹配,在那里我得到变量不匹配的消息。pooledFeaturesThis的THe大小为100x3x2x2,其中pooledFeaturesTrain的大小为400x8862x2x2。pooledFeaturesTrain到底代表了什么?是每个过滤器的2x2结果吗?可以在here中找到CnnConvolve:

编辑:我稍微修改了一下我的代码,它就可以工作了。然而,我有点担心代码的理解。

EN

回答 1

Stack Overflow用户

发布于 2015-05-25 07:49:49

好的,在这一行中,您设置的是池化区域。

poolDim = 10;          % dimension of pooling region

这部分意味着,对于每一层中的每个内核,您将获取10x10像素的图像和池化区域。从你的代码看,你正在应用一个均值函数,这意味着它是一个面片,计算均值,并在下一层输出它……也就是从100x100到10x10的图像。在您的网络中,您正在重复convolution+pooling,直到根据此输出得到一个2x2图像(顺便说一句,根据我的经验,这通常不是一个好的做法)。

400x8862x2x2

无论如何,回到你的代码。请注意,在训练开始时,您需要执行以下初始化:

 pooledFeaturesTrain = zeros(hiddenSize, numTrainImages, floor((imageDim -     patchDim + 1) / poolDim), floor((imageDim - patchDim + 1) / poolDim) );

所以你的错误是非常简单和正确的--保存convolution+pooling输出的矩阵的大小并不是你初始化的矩阵的大小。

现在的问题是如何修复它。我认为懒人修复它的方法是删除初始化。它会极大地减慢你的代码,并且如果你有超过1层的话也不能保证工作。

我建议你应该让pooledFeaturesTrain成为一个三维数组的结构。所以不是这样

pooledFeaturesTrain(featureStart:featureEnd, :, :, :) = pooledFeaturesThis; 

你可以像这样做更多的事情:

pooledFeaturesTrain{n}(:, :, :) = pooledFeaturesThis; 

其中n是当前层。

CNN网络并不像人们所说的那样容易--即使它们没有崩溃,让它们得到良好的训练也是一项壮举。我强烈建议阅读CNNs的理论-它将使编码和调试变得更容易。

祝你好运!:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30194055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档