0

I want to have N parallel neural networks working side by side with the same input parameters. So I decided first to start with 2 neural networks to generalize to N next round.

To do this I created a function called getUntrainedNet as follows:

function [net] = getUntrainedNet()
%GETUNTRAINEDNET Summary of this function goes here
%   Detailed explanation goes here

% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.

% Create a Fitting Network
hiddenLayerSize = 30;
net = fitnet(hiddenLayerSize,trainFcn);
end

Next I created a cell array nets of neural network objects:

nets = cell(1,lengthTargets);
nets(:) = {getUntrainedNet()};

where lengthTargets comes from:

Targets = [experiments.TargetOne; experiments.TargetTwo];
lengthTargets = size(Targets,1);

Then neural networks are trained with:

nets{k} = trainNet(nets{k}, experimentCoordinates, Targets(k,:));

To detect the best operating point with a multi objective optimization method called gamultiobj, I use the following cost function:

costFunction = @(varargin) [nets{1}(varargin{:}'), nets{2}(varargin{:}')];

But Instead, I would like to apply varargin{:}' parameters to all the neural network objects present in the cell array without having to specify each network by its indexer to make the computation generic.

1) How to do this here?

Once I have the best coordinates I want to apply the parameter of the best coordinates to each neural network object in the cell array.

This is currently being done by:

bestCoordinatesTargetOne = nets{1}(bestCoordinates);
bestCoordinatesTargetTwo = nets{2}(bestCoordinates);

2) How to do this here without indexing each cell to make the computation generic?

Prophet Daniel
  • 327
  • 2
  • 15
  • 1
    Use `cellfun` to do the same thing to each element in a cell, in your case these elements are NNs and the thing you want to do is call them with some arguments... – Wolfie Dec 11 '17 at 13:05
  • Thank you @Wolfie, I just structured your comment into an answer. – Prophet Daniel Dec 11 '17 at 13:25

1 Answers1

0

According to @Wolfie's comment I updated the cost function to:

costFunction = @(varargin) cell2mat(cellfun(@(net) net(varargin{:}'), nets, 'UniformOutput',false));

And the optimization went through.

And to calculate best coordinates targets:

bestCoordinatesTargets = cellfun(@(net) net(bestCoordinates), nets, 'UniformOutput',false);
Prophet Daniel
  • 327
  • 2
  • 15