classdef codegenPreluLayer < nnet.layer.Layer
    % Example custom PReLU layer with codegen support.
    
    %#codegen
    
    properties (Learnable)
        % Layer learnable parameters.
        
        % Scaling coefficient.
        Alpha
    end
    
    methods
        function layer = codegenPreluLayer(numChannels, name)
            % layer = codegenPreluLayer(numChannels, name) creates a PReLU
            % layer with numChannels channels and specifies the layer name.
            
            % Set layer name.
            layer.Name = name;
            
            % Set layer description.
            layer.Description = "PReLU with " + numChannels + " channels";
            
            % Initialize scaling coefficient.
            layer.Alpha = rand([1 1 numChannels]);
        end
        
        function Z = predict(layer, X)
            % Z = predict(layer, X) forwards the input data X through the
            % layer and outputs the result Z.
            
            if isdlarray(X)
                Z = max(X,0) + layer.Alpha .* min(0,X);
            else
                Z = max(X,0) + bsxfun(@times, layer.Alpha, min(0,X));
            end
        end
    end
end