pinv function is supported for dlarray input while the pageinv function is not supported
显示 更早的评论
In the deep learning pipeline, I want to use inverse operation on every page of 3D dlarray object. I have tried pageinv function. I find that the pageinv function is not supported for dlarray object while the pinv function is supported for dlarray object.
what is the best practice to obtain the inverse of every page in a 3D dlarray object ?
dlA = dlarray(rand(3,3,10));
dlInv1 = pinv(dlA(:,:,1))
% pageinv(dlA)
I have used cellfun to inverse every page.
dlC = num2cell(dlA,[1 2]); % [1,1,B] cell array
dlInvB = cell2mat(cellfun(@pinv,dlC,"UniformOutput",false));
isequal(dlInvB(:,:,1),dlInv1)
1 个评论
In the deep learning pipeline, I want to use inverse operation on every page of 3D dlarray object.
If you're doing this because there is a step in your network which is the solution of a linear equation, then you are probably barking up the wrong tree. The network probably needs to be reformulated.
采纳的回答
更多回答(1 个)
If your inputs to the operation will always be a 3x3XN stack, as in your example, then you can use the code below in conjunction with a functionLayer. This uses basic matrix arithemtic only, and so should not break the automatic differentiation graph. Keep in mind though that matrix inversion is not a continuous, differentiable operation (not everywhere), so you are taking your chances if you are using the standard derivative-based solvers of the Deep Learning Toolbox.
function B = inv3x3pages(A)
%INV3X3PAGES Page-wise inverse of a 3-by-3-by-N x B array.
%
% B = INV3X3PAGES(A) computes the inverse of each 3-by-3 page of A
% using the explicit adjugate/determinant formula.
Asiz= size(A);
A=A(:,:,:); %reshape to 3x3xN*B
a = A(1,1,:);
b = A(1,2,:);
c = A(1,3,:);
d = A(2,1,:);
e = A(2,2,:);
f = A(2,3,:);
g = A(3,1,:);
h = A(3,2,:);
i = A(3,3,:);
% Cofactors
C11 = e.*i - f.*h;
C12 = f.*g - d.*i;
C13 = d.*h - e.*g;
C21 = c.*h - b.*i;
C22 = a.*i - c.*g;
C23 = b.*g - a.*h;
C31 = b.*f - c.*e;
C32 = c.*d - a.*f;
C33 = a.*e - b.*d;
% Determinant
detA = a.*C11 + b.*C12 + c.*C13;
% inverse(A) = adj(A)/det(A)
B = zeros(size(A), 'like', A);
B(1,1,:) = C11 ./ detA;
B(1,2,:) = C21 ./ detA;
B(1,3,:) = C31 ./ detA;
B(2,1,:) = C12 ./ detA;
B(2,2,:) = C22 ./ detA;
B(2,3,:) = C32 ./ detA;
B(3,1,:) = C13 ./ detA;
B(3,2,:) = C23 ./ detA;
B(3,3,:) = C33 ./ detA;
B=reshape(B,Asiz);
end
类别
在 帮助中心 和 File Exchange 中查找有关 Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
