Update custom layer's parameter every iteration

4 views (last 30 days)
I have defined a custom regression layer as follows
classdef Sparse_Layer < nnet.layer.RegressionLayer
% Example custom regression layer with mean-absolute-error loss.
properties
% (Optional) Layer properties.
L1_Lambda = 0;
% Layer properties go here.
end
methods
function layer = Sparse_Layer(L1_Lambda)
% Lambda should be 1xN, N-> Batch size
layer.L1_Lambda = L1_Lambda;
% Set layer description.
layer.Description = 'Mean absolute error';
end
function layer_out = myFun(layer_in)
end
function loss = forwardLoss(layer, Y, T)
% loss = forwardLoss(layer, Y, T) returns the MAE loss between
% the predictions Y and the training targets T.
% Calculate L1 error mean over batch.
Dif = Y-T;
Batch_sz = size(Y,2);
L1_err = sum(abs(Dif),1);
L2_err = sum(Dif.*Dif,1);
loss = sum(layer.L1_Lambda(iteration_num)*L1_err + .5*L2_err )/Batch_sz;
end
end
end
Now, i want to multiply Lambda(iteration_no), this iteration number is something that i am unable to update. I tried having a layer property that counts the iteration,but that cant work as this a Value Class and not a handler. The reason i want to do this is beacuse i want this lambda to change for every batch (the values i have pre-calcuated based on some feautures of the data in each batch).

Answers (0)

Community Treasure Hunt

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

Start Hunting!