How to use arrayfun with a function that has different dimensions inputs

7 次查看(过去 30 天)
Hello everyone,
I have been trying to do the following for the past two days without any success. I am running a linear regression algorithm based on gradient descent on a big set of data (1M values). On each point I need to perform a gradient descent with a function that I have created:
function [Theta, J_history] = GradientDescentLinear(X, Y, Theta, Alpha, Num_iters)
The inputs have different dimensions, [1500x150], [1500x1], [150x1], [1x1] and [1x1] respecively. It works really well but it takes a very long time as expected. What I would like, if possible, is to speed it up by running 384 gradient descen instead of 1 at a time on the 384 cores of my GPU. From my searches, it seems that this could be done using arrayfun but I can not make it work.
I have tried different things, for instance adding a 3rd [x384] dimension to all my inputs or turn the 5 inputs into 5 [384x1] cell vectors that contain in each cells the respective values (altough it seems the cell type cannot be transfered to the gpu anyway). This is the syntax I use to call the arrayfun:
A = arrayfun(GradientDescentLinear, X_TrainingSetCell, Y_TrainingSetCell, Theta, Alpha, Num_iters);
When the code is executed, there is an error from the first line of the function:
Not enough input arguments.
Error in GradientDescentLinear (line 5)
m = single(length(Y)); % number of training examples
Is it that I don't understand something or just that this is not possible? Do not hesitate to ask me to provide more informations if this is not clear.
I would like to thank you very much for your help and your time.
Guilhem.
  1 个评论
Star Strider
Star Strider 2016-6-1
As I understand, to use a GPU for your MATLAB computations, you have to have the Parallel Computing Toolbox. I don’t have it, so I cannot comment further.

请先登录,再进行评论。

回答(2 个)

Joss Knight
Joss Knight 2016-6-8
You need an @ symbol in front of GradientDescentLinear to pass it as a function handle. At the moment it's trying to invoke the function with no arguments, hence the error.

Guillaume
Guillaume 2016-6-8
I don't have the parallel computing toolbox so can't check for sure, but as far as I'm aware arrayfun does not do parallelisation unless the function it calls can run on a GPU.
Anyway, you're using arrayfun the wrong way. The inputs to arrayfun is a function handle (not a function) and the arrays to iterate, all of the same size. constants needed by the function are not inputs to arrayfun. You can either define these constants in a separate function that simply calls GradientDescentLinear, or wrap it up in an anonymous function. Also, since your training sets are in cell arrays, you need to use cellfun instead of arrayfun:
[Theta, J_history] = cellfun(@(X, Y) GradientDescentLinear(X, Y, Theta, Alpha, Num_iters), ...
X_TrainingSetCell, Y_TrainingSetCell);
Note that this assumes that GradientDescentLinear returns scalar values. If not:
[Theta, J_history] = cellfun(@(X, Y) GradientDescentLinear(X, Y, Theta, Alpha, Num_iters), ...
X_TrainingSetCell, Y_TrainingSetCell, ...
'UniformOutput', false);

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by