Update custom layer's parameter every iteration

1 Ansicht (letzte 30 Tage)
Anirudh Roy
Anirudh Roy am 24 Nov. 2020
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).

Antworten (0)

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by