Can I code a deep learning layer without a backward function?

2 views (last 30 days)
Hello guys,
hope u r doing well
here it is, I want to create a layer that make two copies of the previous layer (input layer) and that's my code:
classdef CopyLayer < nnet.layer.Layer
% properties
% % (Optional) Layer properties.
%
% % Layer properties go here.
%
% end
%
% properties (Learnable)
% % (Optional) Layer learnable parameters.
%
% % Layer learnable parameters go here.
% end
methods
function layer = CopyLayer(numOutputs,name)
% (Optional) Create a myLayer.
% This function must have the same name as the class.
% Layer constructor function goes here.
% Set number of inputs.
layer.NumOutputs = numOutputs;
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = "Make " + numOutputs + ...
" copies of the input layer";
end
function [varargout] = predict(X)
% Forward input data through the layer at prediction time and
% output the result.
%
% Inputs:
% layer - Layer to forward propagate through
% X1, ..., Xn - Input data
% Outputs:
% Z1, ..., Zm - Outputs of layer forward function
% Layer forward function for prediction goes here.
numOutputs = layer.NumOutputs;
[h,w,c] = size(X);
Z = zeros(h,w,c,numOutputs);
for i= 1 : numOutputs
Z(:,:,:,i) = X;
end
varargout = Z;
end
% function [dLdX1, …, dLdXn, dLdW1, …, dLdWk] = ...
% backward(layer, X1, …, Xn, Z1, …, Zm, dLdZ1, …, dLdZm, memory)
% % Backward propagate the derivative of the loss function through
% % the layer.
% %
% % Inputs:
% % layer - Layer to backward propagate through
% % X1, ..., Xn - Input data
% % Z1, ..., Zm - Outputs of layer forward function
% % dLdZ1, ..., dLdZm - Gradients propagated from the next layers
% % memory - Memory value from forward function
% % Outputs:
% % dLdX1, ..., dLdXn - Derivatives of the loss with respect to the
% % inputs
% % dLdW1, ..., dLdWk - Derivatives of the loss with respect to each
% % learnable parameter
%
% % Layer backward function goes here.
% end
end
end
and when I try to create the layer by:
layer = CopyLayer(2,'copy');
an error appears says:
Abstract classes cannot be instantiated. Class 'CopyLayer' inherits abstract methods or properties but does not implement them.
See the list of methods and properties that 'CopyLayer' must implement if you do not intend the class to be abstract.
Error in SplitLayer (line 1)
layer = CopyLayer(2,'copy');
and I think it's because of the no existing of backward function.
what you guess guys?
anyone can help?
thanks in advance

Accepted Answer

ihssane houhou
ihssane houhou on 30 Aug 2019
I got the answer from someone on StackOverflow, check it in here.

More Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!