【问题标题】:Why does MatConvNet say that data and derivatives don't have matching formats?为什么 MatConvNet 说数据和导数没有匹配的格式?
【发布时间】:2016-10-12 16:16:41
【问题描述】:

问题陈述

我使用 MatConvNet 构建了一个非常简单的 1D 示例和小型网络,使用示例库附带的函数 cnn_train。按照他们的示例,我构建了一个小型 CNN 示例,如下所示:

    clc;clear;clc;clear;
%% prepare Data
M = 32; %batch size
X_train = zeros(1,1,1,M); % (1 1 1 2) = (1 1 1 M)
for m=1:M,
    X_train(:,:,:,m) = m; %training example value
end
Y_test = 10*X_train;
split = ones(1,M);
split(floor(M*0.75):end) = 2;
% load image dadabase (imgdb)
imdb.images.data = X_train;
imdb.images.label = Y_test;
imdb.images.set = split;
%% prepare parameters
L1=3;
w1 = randn(1,1,1,L1); %1st layer weights
w2 = randn(1,1,1,L1); %2nd layer weights
b1 = randn(1,1,1,L1); %1st layer biases
b2 = randn(1,1,1,L1); %2nd layer biases
G1 = ones(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN scale, one per  dimension
B1 = zeros(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN shift, one per  dimension
EPS = 1e-4;
%% make CNN layers: conv, BN, relu, conv, pdist, l2-loss
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
                           'name', 'conv1', ...
                           'weights', {{w1, b1}}, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'bnorm', ...
                           'weights', {{G1, B1}}, ...
                           'EPSILON', EPS, ...
                           'learningRate', [1 1 0.05], ...
                           'weightDecay', [0 0]) ;                       
net.layers{end+1} = struct('type', 'relu', ...
                           'name', 'relu1' ) ;
net.layers{end+1} = struct('type', 'conv', ...
                           'name', 'conv2', ...
                           'weights', {{w2, b2}}, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'pdist', ...
                           'name', 'averageing1', ...
                           'class', 0, ...
                           'p', 1) ;
%% add L2-loss                   
fwfun = @l2LossForward;
bwfun = @l2LossBackward;
net = addCustomLossLayer(net, fwfun, bwfun) ;
net.layers{end}.class = Y_test; % its the test set
net = vl_simplenn_tidy(net) ;
res = vl_simplenn(net, X_train);
%% prepare train options
trainOpts.expDir = 'results/' ; %save results/trained cnn
trainOpts.gpus = [] ;
trainOpts.batchSize = 2 ;
trainOpts.learningRate = 0.02 ;
trainOpts.plotDiagnostics = false ;
%trainOpts.plotDiagnostics = true ; % Uncomment to plot diagnostics
trainOpts.numEpochs = 20 ; % number of training epochs
trainOpts.errorFunction = 'none' ;
%% CNN TRAIN
vl_simplenn_display(net) ;
net = cnn_train(net, imdb, @getBatch, trainOpts) ;

我根据the example they provided 创建了这个,每当我运行示例时都会出现错误:

Error using vl_nnconv
DATA and DEROUTPUT do not have compatible formats.

Error in vl_simplenn (line 397)
          [res(i).dzdx, dzdw{1}, dzdw{2}] = vl_nnconv(res(i).x, l.weights{1},
          l.weights{2}, res(i+1).dzdx)

Error in cnn_train>process_epoch (line 323)
    res = vl_simplenn(net, im, dzdy, res, ...

Error in cnn_train (line 139)
    [net,stats.train,prof] = process_epoch(opts, getBatch, epoch, train, learningRate,
    imdb, net) ;

Error in main_1D_1layer_hard_coded_example (line 64)
net = cnn_train(net, imdb, @getBatch, trainOpts) ;

有人知道发生了什么吗?这个例子实际上很简单,所以它让我很困惑可能是什么问题。


我试图解决这个问题的补充部分

有关我尝试解决此问题的更多详细信息,请提前阅读。

我转到文件中导致错误的那一行并打印了该函数的输入,以确保我给出的参数是有意义的,并且在这方面看起来一切都很好:

  case 'conv'
      size(res(i).x)
      size(res(i+1).dzdx)
      size(l.weights{1})
      size(l.weights{2})
      [res(i).dzdx, dzdw{1}, dzdw{2}] = vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx)
    [res(i).dzdx, dzdw{1}, dzdw{2}] = ...
      vl_nnconv(res(i).x, l.weights{1}, l.weights{2}, res(i+1).dzdx, ...
      'pad', l.pad, ...
      'stride', l.stride, ...
      l.opts{:}, ...
      cudnn{:}) ;

打印:

ans =

     1     1     3    16


ans =

     1     1     3    16


ans =

     1     1     1     3


ans =

     1     1     1     3

我的预期。

我什至继续手动硬编码网络应该计算的衍生链,该文件似乎工作正常:

clc;clear;clc;clear;
%% prepare Data
M = 3;
x = zeros(1,1,1,M); % (1 1 1 2) = (1 1 1 M)
for m=1:M,
    x(:,:,:,m) = m;
end
Y = 5;
r=Y;
%% parameters
L1 = 3;
w1 = randn(1,1,1,L1); % (1 1 1 L1) = (1 1 1 3)
b1 = ones(1,L1);
w2 = randn(1,1,1,L1); % (1 1 1 L1) = (1 1 1 3)
b2 = ones(1,L1);
G1 = ones(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN scale, one per  dimension
B1 = zeros(1,1,1,L1); % (1 1 1 3) = (1 1 1 L1) BN shift, one per  dimension
EPS = 1e-4;
%% Forward Pass
z1 = vl_nnconv(x,w1,b1); % (1 1 3 2) = (1 1 L1 M)
%bn1 = z1;
bn1 = vl_nnbnorm(z1,G1,B1,'EPSILON',EPS); % (1 1 3 2) = (1 1 L1 M)
a1 = vl_nnrelu(bn1); % (1 1 3 2) = (1 1 L1 M) 
z2 = vl_nnconv(a1,w2,b2);
y1 = vl_nnpdist(z2, 0, 1);
loss_forward = l2LossForward(y1,Y);
%%
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
                           'name', 'conv1', ...
                           'weights', {{w1, b1}}, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'bnorm', ...
                           'weights', {{G1, B1}}, ...
                           'EPSILON', EPS, ...
                           'learningRate', [1 1 0.05], ...
                           'weightDecay', [0 0]) ;                       
net.layers{end+1} = struct('type', 'relu', ...
                           'name', 'relu1' ) ;
net.layers{end+1} = struct('type', 'conv', ...
                           'name', 'conv2', ...
                           'weights', {{w2, b2}}, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'pdist', ...
                           'name', 'averageing1', ...
                           'class', 0, ...
                           'p', 1) ;
fwfun = @l2LossForward;
bwfun = @l2LossBackward;
net = addCustomLossLayer(net, fwfun, bwfun) ;
net.layers{end}.class = Y;
net = vl_simplenn_tidy(net) ;
res = vl_simplenn(net, x);
%%
loss_forward = squeeze( loss_forward ) % (1 1)
loss_res = squeeze( res(end).x ) % (1 1)
%% Backward Pass
p = 1;
dldx = l2LossBackward(y1,r,p);
dy1dx = vl_nnpdist(z2, 0, 1, dldx);
[dz2dx, dz2dw2] = vl_nnconv(a1, w2, b2, dy1dx);
da1dx = vl_nnrelu(bn1, dz2dx);
[dbn1dx,dbn1dG1,dbn1dB1] = vl_nnbnorm(z1,G1,B1,da1dx);
[dz1dx, dz1dw1] = vl_nnconv(x, w1, b1, dbn1dx);
%%
dzdy = 1;
res = vl_simplenn(net, x, dzdy, res);
%%
% func = @(x) proj(p, forward(x, x0)) ;
% err = checkDerivativeNumerically(f, x, dx)
% %%
dz1dx = squeeze(dz1dx)
dz1dx_vl_simplenn = squeeze(res(1).dzdx)

导数似乎是数学运算,因此我假设该文件中的所有内容都有效。它不会抛出错误,所以它甚至没有运行的事实让我非常困惑。有谁知道怎么回事?


我加载 CNN 的方式是基于他们在该教程中提供的 the example file。我将粘贴该文件的重要方面的摘要(使用 cnn_train 函数运行良好,而我的则没有)。

setup() ;
% setup('useGpu', true); % Uncomment to initialise with a GPU support
%% Part 3.1: Prepare the data
% Load a database of blurred images to train from
imdb = load('data/text_imdb.mat') ;

%% Part 3.2: Create a network architecture

net = initializeSmallCNN() ;
%net = initializeLargeCNN() ;
% Display network
vl_simplenn_display(net) ;

%% Part 3.3: learn the model
% Add a loss (using a custom layer)
net = addCustomLossLayer(net, @l2LossForward, @l2LossBackward) ;

% Train
trainOpts.expDir = 'data/text-small' ;
trainOpts.gpus = [] ;
% Uncomment for GPU training:
%trainOpts.expDir = 'data/text-small-gpu' ;
%trainOpts.gpus = [1] ;
trainOpts.batchSize = 16 ;
trainOpts.learningRate = 0.02 ;
trainOpts.plotDiagnostics = false ;
%trainOpts.plotDiagnostics = true ; % Uncomment to plot diagnostics
trainOpts.numEpochs = 20 ;
trainOpts.errorFunction = 'none' ;

net = cnn_train(net, imdb, @getBatch, trainOpts) ;

【问题讨论】:

  • 你能想出一个更少的例子吗...... non-minimal?
  • @AndrasDeak 当然,你的意思是复杂吗?它假设是一个简单的网络,我只是提供了一些我试图修复它的东西。
  • 我尝试对示例进行更多评论,以使其非常简单。希望对您有所帮助。
  • 你应该简化你的例子,它太长了,我需要一个小时来阅读你写的东西。提供一个更简短的示例将增加答案的数量,并使您有机会自己找到问题。
  • @Pinocchio 这个问题你解决了吗?

标签: matlab machine-learning neural-network conv-neural-network


【解决方案1】:

w2的尺寸应该是1x1x3x3。

通常偏差也会以 1x3 的形式给出,因为它们只有一个维度(或权重为 1x1x3xN,相应的偏差为 1xN,其中 N 是过滤器的数量),B1 和 G1 也是如此(这里是 1xM,其中 M 是前一层的过滤器数量)。但它可能会以任何一种方式工作。

在您的示例中,第一次卷积后 x 的尺寸为 1x1x3x16。这意味着一批中有 16 个元素,其中每个元素的宽度和高度为 1,深度为 3。深度为 3,因为第一次卷积是使用 3 个过滤器完成的(w1 的尺寸为 1x1x1x3)。

在您的示例中,w2 的尺寸为 1x1x1x3,表示宽度、高度和深度为 1 的 3 个过滤器。因此过滤器的深度与输入的深度不匹配。

【讨论】:

  • 两个 cmets,1) 我做了 w2 1x1x3x3 但不幸的是它仍然没有运行。 2)你的推理在逻辑上是有道理的,但仍然让我感到困惑的是,如果你的论点是正确的,那么为什么我可以成功地在模型中运行前向传递?如果您注意到我给出的代码中有res = vl_simplenn(net, X_train); 行,它评估模型并且令我惊讶的是实际运行。如果尺寸不匹配,那么为什么向前传球还要跑呢?我还不知道,但似乎回传是错误输出的问题。我会尝试检查。
  • 我一定忽略了这一点。那很奇怪。也许我在接下来的几天里有时间亲自尝试你的代码。
【解决方案2】:

我通过创建自定义层遇到了同样的问题。我终于通过跟踪 matconvnet 的实现找到了解决方案。希望以后对其他人有所帮助。

简而言之,你需要确保这两个数据不为空,不为空,并且具有相同的设备类型(GPU 或 CPU)和相同的数据类型(float、single 或 char)。

就我而言,两个数据必须具有相同的“gpuArray”和“single”。

====== 详情=================== 一、错误

DATA 和 FILTERS 的格式不兼容

DATA 和 BIASES 的格式不兼容

DATA 和 DEROUTPUT 的格式不兼容

确切地说,这两个变量没有兼容的格式。 那么 Matconvnet 是什么意思“兼容格式”呢? 它在 vl_nnconv.cu 第 269~278 行实现

 /* check for GPU/data class consistency */


if (hasFilters && ! vl::areCompatible(data, filters)) {
    vlmxError(VLMXE_IllegalArgument, "DATA and FILTERS do not have compatible formats.") ;
  }
  if (hasBiases && ! vl::areCompatible(data, biases)) {
    vlmxError(VLMXE_IllegalArgument, "DATA and BIASES do not have compatible formats.") ;
  }
  if (backMode && ! vl::areCompatible(data, derOutput)) {
    vlmxError(VLMXE_IllegalArgument, "DATA and DEROUTPUT do not have compatible formats.") ;
  }

错误来自函数 vl::areCompatible 实现为

inline bool areCompatible(Tensor const & a, Tensor const & b)
  {
    return
    (a.isEmpty() || a.isNull()) ||
    (b.isEmpty() || b.isNull()) ||
    ((a.getDeviceType() == b.getDeviceType()) & (a.getDataType() == b.getDataType())) ;
  }

因此,基本上,它会检查任何输入是否为空或 null,并确保两个输入具有相同的数据类型(double、single、vs char)和设备类型(GPU、CPU)。

 /// Type of device: CPU or GPU
  enum DeviceType {
    VLDT_CPU = 0,
    VLDT_GPU
  }  ;

  /// Type of data (char, float, double, ...)
  enum DataType {
    VLDT_Char,
    VLDT_Float,
    VLDT_Double
  } ;

【讨论】:

    猜你喜欢
    • 2013-11-04
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2017-05-20
    • 2020-09-26
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多