필터 지우기
필터 지우기

dlgradient: covariance matrix derivative.

조회 수: 3 (최근 30일)
MA
MA 2021년 12월 13일
답변: MA 2021년 12월 16일
Assuming I have a matrix x of size (mxn), the covariance matrix is of the size nxn. I want to find the gradient of the covariance matrix with respect to the input. So, starting with this code:
function [y,dx]=cov_der(x)
y=x'*x;
dx=dlgradient(y,x,'EnableHigherDerivatives',true);
end
and evaluating it as:
[y,dx]=dlfeval(@cov_der,x)
This does not work for matrices but it works for scalars. So, is there anyway I could find the gradient with respect to every element in the matrix. THanks.

채택된 답변

MA
MA 2021년 12월 16일
I actually solve it. For anyone looking for the same problem:
function [y,dx]=cov_der(x)
%simple example
%x=dlarray(and(3,3));
%[y,dx]=dlfeval(@cov_der,x)
y=zeros(size(x,2),size(x,2));
z=size(x,1);
y=dlarray(y);
for i=1:size(x,2)
for j=1:size(x,2)
y(i,j)=sum(x(:,i).*x(:,j))./z;
end
end
dx=zeros(size(x,2)*size(x,2),size(x,1),size(x,2));
index=1;
for i=1:size(x,2)
for j=1:size(x,2)
dx(index,:,:)=dlgradient(y(i,j),x,'EnableHigherDerivatives',true);
index=index+1;
end
end
end

추가 답변 (1개)

yanqi liu
yanqi liu 2021년 12월 14일
yes,sir,may be use loop for every element in matrix
clc; clear all; close all;
[X1, X2] = meshgrid(linspace(0,1,10));
X1 = dlarray(X1(:));
for i = 1:length(X1)
[y(i),dx(i)]=dlfeval(@cov_der, dlarray(X1(i)));
end
% figure; plot(extractdata(X1),extractdata(y))
% hold on;
% plot(extractdata(X1),extractdata(dx))
function [y,dx]=cov_der(x)
y=x'*x;
dx=dlgradient(y,x,'EnableHigherDerivatives',true);
end
  댓글 수: 1
MA
MA 2021년 12월 14일
THanks for your answer, but in this example you gave the covariance is calculated for each point. I want to calculate the covariance matrix for a matrix of size mxn so the output (y) will be of size nxn.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by