# Optimization code. One line takses 86% of working time.

2 views (last 30 days)

Show older comments

Hello Everyone! I am developing a Clasificator for hand written signs and digits using Logical Regression and Softmax function.

I have a problem with one line in one function because it takes too much time.

function [grad] = logistic_cost_function(xTrain, yTrain, w)

%xTrain = [30134 x 11760]

%xTrain = [30134 x 1]

%w = [36 x 11760]

N = size(xTrain,1);

K = 36;

grad = zeros(size(w));

ARG = w * xTrain';

for n = 1 : N

xRow = xTrain(n,:);

Probability = softmax(ARG(:,n));

label_n = yTrain(n);

for k = 1 : K

IND = label_n == k;

prob_k = Probability(k);

grad(k,:) = grad(k,:) + (IND - prob_k) * xRow;

end

end

grad = -grad/N;

end

It is the time counted on only 2 epochs. My point is to count a few thousands of epchos.

Does anyone have any idea how to improve an algorithm?

Bet Regards

##### 3 Comments

Ahmet Cecen
on 14 May 2016

### Answers (1)

Ahmet Cecen
on 14 May 2016

Edited: Ahmet Cecen
on 14 May 2016

for k = 1 : K

IND = label_n == k;

prob_k = Probability(k);

grad(k,:) = grad(k,:) + (IND - prob_k) * xRow;

end

Here is a way out of this loop (well hopefully, as I can't actually run the code to verify):

IND = zeros(K,1); IND(label_n)=1;

prob_k = Probability;

% (IND - prob_k) IS A COLUMN VECTOR, SO OUTER PRODUCT.

grad = grad + (IND - prob_k)*xRow;

Hmm... turns out no need for bsxfun.

##### 4 Comments

Ahmet Cecen
on 14 May 2016

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!