1

I have this Matlab code that runs, but i want to make it faster by removing the for loops and essentially do the same calculations using only matrices (my dataset is very large so i need this kind of optimizations):

Matrices dimensions: x(N,D), m(K,D), p(K), fL(N,K), maxf(N), maxfL(N), z(N,K).
Also p, maxf, maxfL are row vectors

Code1:

f = zeros(N,K);
maxf = zeros(1,N);

for n=1:N
   for k=1:K
      % here i had a loop for d dimension but made it more efficient like this:
      f2 = x(n,:) * log(m(k,:))' + (1 - x(n,:)) * log(1 - m(k,:))';
      f(n,k) = log(p(k)) + f2;
   end
   maxf(n) = max(f(n,:));
   f(n,:) = f(n,:) - maxf(n);
end

Code2:

for k=1:K
  sum2 = sum(z(:,k)); 
  p(k)= sum2/N;
  for d=1:D
     % here i had a n loop for sum1 and made it like this:
     sum1 = z(:,k)' * x(:,d); 
     m(k,d) = sum1/sum2;
  end
end

Code3:

L_new = 0;
for n=1:N
  suma = sum(fL(n,:));
  L_new = L_new + maxfL(n) + log(suma);
end

I will now summarize some average execution times (results are in seconds) from using the below provided answers in my workload (N = 1000, K = 2, D = 784):

CodeNumber                      Execution Time
    1           3.98(for_loops), 1.01(Divakar), 0.5(Nishant)
    2           0.2(for_loops), 0.40-0.42(Divakar-2 approaches), 0.13(Nishant)
    3           0.03(for_loops), 0.0026(Divakar), 0.0024(Nishant)

Thanks for the answers!!

11
  • Since these are three unrelated codes, I think it would be better to split this into three separate questions. Commented May 31, 2014 at 8:47
  • They are part of the same code and they are small blocks of it. Someone can provide an answer to one, two or all three of them. I don't believe that splitting them would be beneficial! Commented May 31, 2014 at 8:58
  • On Stackoverflow we don't do partial answers. Still to get on with it, what are the dimensions for z, maxfL? Commented May 31, 2014 at 9:00
  • I added the dimensions in the question above! Commented May 31, 2014 at 9:31
  • In code2, would it be for d=1:K rather? Commented May 31, 2014 at 9:47

2 Answers 2

2

For code 1:

f2 = x*log(m)' + (1-x)*(log(1-m))' ;
f = f2 + ones(N,1)*log(p);
maxf = max(f');
f = f - maxf'*ones(1,K);

For code 2:

sum2 = sum(z);
sum1 = z'*x;
temp = sum2'*ones(1,D);
m = sum1./temp;
p  = sum2/N;  

For code 3:

L_new = sum(log(sum(fL'))) + sum(maxfL');
Sign up to request clarification or add additional context in comments.

16 Comments

I don't think code3 is correct! i re-runed my code and while time was reduced from ~0.001 to ~0.00001 secs, the output value for L_new was different from before!
And i think the code1 you mention it is actually code2, correct?
yes you are correct ,check it ,tell me if there is an error, I am looking into code 3 now
For code2 at: m = sum1./sum2; i get: ??? Error using ==> rdivide Matrix dimensions must agree.
maxFL is a row vector, i will added it also above!
|
1

Try these -

Code 1:

tp1 = squeeze(sum(bsxfun(@times,x,permute(log(m),[3 2 1])),2))
tp2 = squeeze(sum(bsxfun(@times,1-x,permute(log(1-m),[3 2 1])),2))
tp3 = tp1 + tp2
f = bsxfun(@plus,tp3,log(p))
f = bsxfun(@minus,f,max(f,[],2))

Code 2:

p = sum(z)./N;
m = bsxfun(@rdivide,squeeze(sum(bsxfun(@times,x,permute(z,[1 3 2]))))',sum(z)');

Code 2: [Approach 2] -

p = sum(z)./N;
sum2_1 = sum(z);
m = zeros(K,D);
for k=1:K  
  m(k,:) = sum(bsxfun(@times,x,z(:,k)))./sum2_1(k);
end

Code 3: (Assuming you are getting maxfL from somewhere)

L_new = sum(maxfL' + log(sum(fL,2)))

19 Comments

@JohnZobolas Great!! Make sure other codes run and check how they perform against your for-loop codes!!
Code 2 gets wrong result, but also you don't calculate the p(K) vector
@JohnZobolas Added calculation for p in Code 2. Well I did check the m values for Code 2, so could you double check those?
You code3 is a little faster than nishantsny's! Some average total execution times in my algorithm give: Code3(Divakar)=~0.0024 and Code3(nishantsny)=~0.0026. Awesome!
@JohnZobolas What about Code 1 and 2?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.