Newbie here. Im trying to use the sigmoid function in a distributed array in matlab, but im getting the following error:

>> a = magic(10);
>> a = distributed(a);
>> a^2
Undefined function 'mpower' for input arguments of type 'distributed'.

Undefined function 'mpower' for input arguments of type 'distributed'.

I made some internet research, but i really did not find nothing useful.

But i need to do a exponential on a distributed array.

Is there a way to do this?

有帮助吗?

解决方案

You probably intended to do

b = a.^2;

instead of

b = a^2;

Without the ., the multiplication is a "matrix multiplication" (in fact, the name mpower suggests that it is literally exponentiation of a matrix); with the ., it is an "element by element multiplication". I am guessing that matrix multiplication doesn't work well (at all?) for distributed matrices - on the other hand, according to this link it actually happens implicitly without the need for distribution.

It leaves the question: did you intend for matrix multiplication, or element-by-element multiplication?

EDIT

You indicated in the comment that the reason for your question was that you were evaluating a function:

function g = sigmoid2(z) 
  g = distributed.zeros(size(z)); 
  %g = arrayfun(@(x) (1 / (1+(exp^-(x)))),z); 
  for idx = 1: numel(z) 
    g(idx) = 1 / (1+ (exp(1).^-z(idx))); 
  end 
end

and were wondering if there is a quicker way to do this. Indeed there is. Instead of looping over idx, use the single matrix operation

g = 1./(1+exp(-z)); 

as your function definition - it will be many times faster. Matlab is powerful but there is a lot of overhead executing each line - so avoid loops if you can. Learn about vectorization in Matlab (look it up). Distributing doesn't always speed things up - it depends on the operation and the size of the matrices. Use the profiling tool to find out what line takes the most time - then use that knowledge to focus your speed up efforts. Good luck!

其他提示

This may be a late answer but...

The following: in order of performance:

The arrayfun is the slowest

g = arrayfun(@(x) 1/(1+exp(-x)), z);

Next is the for loop

for idx = 1: numel(z) 
    g(idx) = 1 / (1+ (exp(1).^-z(idx))); 
end 

The fastest is the dot notation

g = 1./(1+exp(-z)); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top