deep learning layer with different output dimension than the input

조회 수: 10 (최근 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 CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by