How to get diagonal element in a custom layer in NN tool box?
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Aiswarya
2023 年 10 月 5 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Deep Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!