MATLAB编码器在BSXFUN中导致矩阵扩展问题



我一直在试图从ACE中矢量化某些单身学生为我们写的操作。他使用了我试图将其更改为BSXFUN调用的丑陋嵌套环。当我按原样使用此函数时,这可以正常工作,但是当我尝试使用编码器将其编译时,我会收到一个错误。

编辑:我意识到我应该澄清代码的某些部分。S,W和N是NXN矩阵,表明对成对比较IJ的偏爱。MU是包含绝对评分重建的(预测)得分的行矢量。

要检查代码的作用,您可能想检查页面的底部。

原始代码如下:

for i = 1:n
    for j = i:n
        if i~= j
            mu = x(i) - x(j);
            md1 = normcdf(-delta1, mu); %minus d0
            md0 = normcdf(-delta0, mu);
            d0 = normcdf( delta0, mu);
            d1 = normcdf( delta1, mu);
            Y = Y + S(i,j) .* log(1  - d1) + ...
                    W(i,j) .* log(d1 - d0) + ...
                    N(i,j) .* log(d0 - md0) + ...
                    W(j,i) .* log(md0 - md1) + ...
                    S(j,i) .* log(md1);
        end
    end
end
Y = -Y; %this will be used in fminsearch, therefore the negative sign

我为此写了一个快速而肮脏的矢量解决方案:

diffs = bsxfun(@minus,x,x');
% create the deltas array augmented by -Inf and Inf for easy diff calculation
deltass = sort([-Inf, Inf, deltas, -1*deltas]);
deltass = reshape(deltass, [1, 1, length(deltass)]);
normcdfs = diff(1-bsxfun(@normcdfupper, diffs, deltass), 1, 3);
normcdfs(repmat(logical(tril(ones(size(normcdfs(:,:,1))))),[1,1,length(deltass)-1])) = 1;
Y = -sum(sum(sum(normcdfs_input.*log(normcdfs))));

编辑:normcdfupper是我写的包装程序类,我写了normcdf(diff,delta,'upper'),因为我遇到了问题,尾巴端可能会导致错误。

现在,就像我说的那样,这在日常使用中可以正常工作。但是,由于mex nested for Loop版本带来了巨大的加速,所以我尝试使用此Sped UP版本进行同样的操作,只是在编码器中遇到以下错误:

Expansion is only supported along dimensions where one input argument or the other has a fixed length of 1.
Error in lik_ml (line 27)
normcdfs = diff(1-bsxfun(@normcdfupper,diffs,deltass),1,3);

diffs是:infx:inf double,deltass是1x1x:inf double。编码器告诉我:

差异deltass

如果有人可以帮助我处理此错误消息,我将非常感谢。

编辑:一些代码以:

clear all;
% rng(322);
delta0 = 1;
delta1 = 2;
deltas = [delta0, delta1];
sigma = 0.5;
options = 5;
maxVotes = 10000;
voteStep = 3;
initialVoteStep = 3;
raw_mu = rand(1,options);
mu = sort(zscore(raw_mu));
S = zeros(options);
W = zeros(options);
N = zeros(options);
for i = 1:options
    for j = i:options
        if i ~= j
            S(i,j) = 1 - normcdf(delta1, mu(i)-mu(j));
            W(i,j) = normcdf( delta1, mu(i)-mu(j)) - normcdf( delta0, mu(i)-mu(j));
            N(i,j) = normcdf( delta0, mu(i)-mu(j)) - normcdf(-delta0, mu(i)-mu(j));
            W(j,i) = normcdf(-delta0, mu(i)-mu(j)) - normcdf(-delta1, mu(i)-mu(j));
            S(j,i) = normcdf(-delta1, mu(i)-mu(j));
        end
    end
end
diffs = bsxfun(@minus,mu,mu');
deltass = sort([-Inf, Inf, deltas, -1*deltas]);
deltass = reshape(deltass,[1,1,length(deltass)]);
normcdfs = diff(bsxfun(@(x,y) normcdf(y,x),diffs,deltass),1,3);

这是for loop的矢量化版本

[I,J] = meshgrid(1:n);
idx = I<J;
I = I(idx);
J = J(idx);
IJ = sub2ind([n n],I,J);
JI = sub2ind([n n],J,I);
mu = x(I)-x(J);
md1 = normcdf(-delta1, mu).'; 
md0 = normcdf(-delta0, mu).';
d0 = normcdf( delta0, mu).';
d1 = normcdf( delta1, mu).';
Y = sum(S(IJ) .* log(max(0,1  - d1)) + ...
W(IJ) .* log(max(0,d1 - d0)) + ...
N(IJ) .* log(max(0,d0 - md0)) + ...
W(JI) .* log(max(0,md0 - md1)) + ...
S(JI) .* log(max(0,md1)));

相关内容

  • 没有找到相关文章

最新更新