deep learning layer with different output dimension than the input

9 ビュー (過去 30 日間)
Vishnu Keyen
Vishnu Keyen 2022 年 12 月 8 日
回答済み: Kartik 2023 年 3 月 21 日
I want to create a layer where it inputs 3D data with dimesnion labels 'CBT' and outputs reshaped data with dimesion 'SCBT'.
I tried using the 'ProjectAndReshapeLayer' given by Mathworks but it says outputs must have the same dimensions as inputs.
I tried using a stripdim() command inside the forward function definiton but with no success.

回答 (1 件)

Kartik
Kartik 2023 年 3 月 21 日
Hi,
You can create a custom layer in MATLAB to achieve this. Here's an example implementation:
classdef ReshapeLayer < nnet.layer.Layer
properties
InputDimLabels = {'CBT'} % input dimension labels
OutputDimLabels = {'SCBT'} % output dimension labels
end
methods
function layer = ReshapeLayer(name)
layer.Name = name;
layer.Description = "Reshape Layer";
end
function Z = predict(layer, X)
% Reshape input data to output shape
Z = reshape(X, [], size(X,3));
end
function [dLdX] = backward(layer, X, Z, dLdZ, ~)
% Reshape gradients back to input shape
dLdX = reshape(dLdZ, size(X));
end
function outputSize = forwardPropagateSize(layer, inputSize)
% Output size calculation based on input size and output dimension labels
outputSize = [prod(inputSize(1:end-1)), length(layer.OutputDimLabels)];
end
function inputSize = backwardPropagateSize(layer, outputSize)
% Input size calculation based on output size and input dimension labels
inputSize = [outputSize(1:end-1), length(layer.InputDimLabels)];
end
function outputNames = getOutputArguments(layer)
% Output argument names based on output dimension labels
outputNames = layer.OutputDimLabels;
end
function inputNames = getInputArguments(layer)
% Input argument names based on input dimension labels
inputNames = layer.InputDimLabels;
end
function outputSize = getOutputSize(layer, ~)
% Output size based on output dimension labels
outputSize = [NaN, length(layer.OutputDimLabels)];
end
function inputSize = getInputSize(layer)
% Input size based on input dimension labels
inputSize = [NaN, length(layer.InputDimLabels)];
end
function tf = isValidInputSize(layer, inputSize)
% Check if input size is compatible with input dimension labels
tf = isequal(inputSize(end), length(layer.InputDimLabels));
end
function tf = isValidOutputSize(layer, outputSize)
% Check if output size is compatible with output dimension labels
tf = isequal(outputSize(end), length(layer.OutputDimLabels));
end
end
end
You can use this layer in your neural network by creating an instance of the layer and adding it to the network:
reshapeLayer = ReshapeLayer('reshape_layer');
layers = [
imageInputLayer([32 32 3], 'Name', 'input', 'Normalization', 'none', 'DataAugmentation', 'none', 'DimensionLabels', {'Height', 'Width', 'Channels'})
reshapeLayer
fullyConnectedLayer(10, 'Name', 'fc')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
lgraph = layerGraph(layers);

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by