Effacer les filtres
Effacer les filtres

How to get diagonal element in a custom layer in NN tool box?

1 vue (au cours des 30 derniers jours)
Archer Ao
Archer Ao le 12 Fév 2022
function Uout = forward(layer, Uin)
W = Uin(:,:,1) * layer.C;
W = Uin(:,:,1) + repmat(layer.alpha .* diag(W*Uin(:,:,1)'),1,layer.usize(2)) ...
.* Uin(:,:,1) + layer.B1 * Uin(:,:,1) - repmat(layer.alpha,1,layer.usize(2)).* Uin(:,:,1);
Wnorm = norms(W,2,2);
Uout = W./repmat(Wnorm,1,layer.usize(2));
end
I define the forward function like this, but it is said that dlarray has no compatible function diag, then how I solve this problem? I didnt find answer in the official documents.

Réponses (1)

Aiswarya
Aiswarya le 5 Oct 2023
Hi,
I understand that you are trying to extract the diagonal elements of dlarray using diag function. The diag function can't be used with the dlarray datatype and there are no alternative functions provided by dlarray as well. However, there is a work around to get the diagonal elements by using the following script
diagonal_elements = W(logical(eye(size(W))));
The eye function (https://www.mathworks.com/help/matlab/ref/eye.html) creates an identity matrix of input size. On passing the size of weights matrix, the eye function will create identity matrix of that size which can be used as a logical index to obtain only the diagonal elements of weight matrix W as a dlarray vector.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by