cnn卷积matlab,matlab – 计算CNN实现中的卷积层

为了计算卷积层的过滤器,我正在使用稀疏自动递减器来训练卷积神经网络。我正在使用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,:),…,= = =池的功能;

在那里我得到的信息是变量不匹配。大小的pooledFeatures这是100x3x2x2,其中pooledFeaturesTrain的大小是400x8862x2x2。什么是pooledFeaturesTrain代表什么?每个过滤器的2×2结果是? CnnConvolve可以找到here:

编辑:我已经改变了一点我的代码,它的作品。不过我有点担心代码的理解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值