%% L1 Regularization with dlupdate
% Perform L1 regularization on a structure of parameter gradients.
% Create the sample input data.

dlX = dlarray(rand(100,100,3),'SSC');

% Initialize the learnable parameters for the convolution operation.

params.Weights = dlarray(rand(10,10,3,50));
params.Bias = dlarray(rand(50,1));

% Calculate the gradients for the convolution operation using the helper function convGradients, defined at the end of this example.

gradients = dlfeval(@convGradients,dlX,params);

% Define the regularization factor.

L1Factor = 0.001;

% Create an anonymous function that regularizes the gradients. By using an anonymous function to pass a scalar constant to the function, you can avoid having to expand the constant value to the same size and structure as the parameter variable.

L1Regularizer = @(grad,param) grad + L1Factor.*sign(param);

% Use dlupdate to apply the regularization function to each of the gradients.

gradients = dlupdate(L1Regularizer,gradients,params);

%
% The gradients in grads are now regularized according to the function L1Regularizer.
%% convGradients Function
% The convGradients helper function takes the learnable parameters of the convolution operation and a mini-batch of input data dlX, and returns the gradients with respect to the learnable parameters.

function gradients = convGradients(dlX,params)
dlY = dlconv(dlX,params.Weights,params.Bias);
dlY = sum(dlY,'all');
gradients = dlgradient(dlY,params);
end

% Copyright 2019 The MathWorks, Inc.