Info
此问题已关闭。 请重新打开它进行编辑或回答。
Invalid input data error in custom layer...
4 次查看(过去 30 天)
显示 更早的评论
Hi, I have a custom layer defined as follows:
classdef reshapeLayer < nnet.layer.Layer
properties
NumFilters;
Height;
Width;
end
methods
function self = reshapeLayer(name, height, width, numFilters)
self.Name = name;
self.Height = height;
self.Width = width;
self.NumFilters = numFilters;
self.Description = "reshape from 1x1x" + height*width*numFilters + ...
" to " + height + "x" + width + "x" + numFilters;
end
function Z = predict(self, X)
numFilters = self.NumFilters;
height = self.Height;
width = self.Width;
Z = reshape(X, height, width, numFilters, []);
end
end
end
This layer has passed validity tests by "checkLayer ", however, it doesn't work in the middle of the layers...
The set of layers, and the corresponding layer graph and dlnetwork respectively are;
layers = [
imageInputLayer([1,1,latentDim], 'Normalization', 'none', 'Name', 'input')
fullyConnectedLayer(2*2*numFilters, 'Name', 'dense')
eluLayer('Name', 'dense_elu')
reshapeLayer('reshape', 2, 2, numFilters)
convolution2dLayer(3, numFilters, 'Padding', 'same', 'Name', 'conv2d')
];
lgraph = layerGraph(layers);
net = dlnetwork(lgraph);
and I tested it as
z = ones([1, 1, latentDim, batchSize], 'single');
dlz = dlarray(z, 'SSCB');
x = predict(net, dlz);
Unfortunately, I ran into the following error:
Error using nnet.internal.cnn.dlnetwork/predict (line 198)
Layer 'reshape': Invalid input data. Number of dimension labels must be greater
than or equal to the number of dimensions in the input array.
Error in dlnetwork/predict (line 205)
[varargout{1:nargout}] = predict(net.PrivateNetwork, x,
layerIndices, layerOutputIndices);
I found that the reason of this error was because the input and the output of this custom layer are not matched each other.
In the CustomLayerFunctionalStrategy class, there is a reapplying dimensions for the output, namely, (line 35)
Z = cellfun(@(zi) dlarray(zi, Xdims), Z, 'UniformOutput', false);
But, some how the dimension of the input(Xdims) became 'CB' not 'SSCB.'
The output of reshapeLayer, Z has dimension of 4 (e.g. 'SSCB') but Xdims is 'CB,' so they have different dimensions.
Why does this happen, and how can I avoid this problem??
Please help me........
P.S. this custom layer works well by its own, that is...
layer = reshapeLayer('reshape', height, width, numFilters);
checkLayer(layer, [1, 1, height*width*numFilters], 'ObservationDimension', 4); % All passed
z = rand(1,1,height*width*numFilters);
dlz = dlarray(z, 'SSCB');
dlx = predict(layer, dlz); % works
z2 = rand(height*width*numFilters, 1);
dlz2 = dlarray(z2, 'CB'); % works
0 个评论
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!