计算向量函数的梯度

时间:2015-11-26 16:31:59

标签: matlab math

如何计算向量函数的梯度? 我有

U = sym('U',[5 1]);
X = sym('X',[5 5]);
Y = sym('Y',[5 1]);
f = Cost(U,X,Y)
gradient(f,[U,X,Y])

我已经

  

使用sym / gradient时出错(第35行)   第二个参数必须是变量向量。

PS:

function J = Cost(U,X,Y)
 thr = calc_threshold(X,Y,U);
 Y_pred = X*U;
 Y_pred = (Y_pred <= thr);
 J=sum((Y_pred - Y).^2)/(2*size(Y,1));
end
function [threshold] = calc_threshold(X,Y,U)
 Q = X*U;
 Q0 = Q(Y == 0); %non target
 Q1 = Q(Y == 1); %target
 ths = Q + eps;
 ths = sort(ths);
 N1 = sum(Y == 1);
 N0 = sum(Y == 0);
 for k = 1:length(ths)                
    spec_all(k) = length(find(Q0 > ths(k)));       
 end;
 idx = find(spec_all >= 0.95*N0, 1, 'last');
 threshold = ths(idx);  
end

0 个答案:

没有答案