神经网络训练matlab parfor问题

Neural network train matlab parfor problems

我正在努力找出哪里出错了。如果你能帮助我,我将非常高兴。

这是我的问题:

在串行火车中,来自神经网络工具箱的函数以一种方式运行,但是当我将它放入 parfor 循环时,一切都变得疯狂。

>> version

ans =

8.3.0.532 (R2014a)

这是一个函数

function per = neuralTr(tSet,Y,CrossVal,Ycv)

hiddenLayerSize = 94;
redeT = patternnet(hiddenLayerSize);
redeT.input.processFcns = {'removeconstantrows','mapminmax'};
redeT.output.processFcns = {'removeconstantrows','mapminmax'};
redeT.divideFcn = 'dividerand';  % Divide data randomly
redeT.divideMode = 'sample';  % Divide up every sample
redeT.divideParam.trainRatio = 80/100;
redeT.divideParam.valRatio = 10/100;
redeT.divideParam.testRatio = 10/100;
redeT.trainFcn = 'trainscg';  % Scaled conjugate gradient
redeT.performFcn = 'crossentropy';  % Cross-entropy
redeT.trainParam.showWindow=0; %default is 1)
redeT = train(redeT,tSet,Y);    
outputs = sim(redeT,CrossVal);
per = perform(redeT,Ycv,outputs);

end

这是我输入的代码:

Data loaded in workspace
whos
        Name            Size              Bytes  Class     Attributes

        CrossVal      282x157            354192  double
        Y               2x363              5808  double
        Ycv             2x157              2512  double
        per             1x1                   8  double
        tSet          282x363            818928  double

串行执行的函数

per = neuralTr(tSet,Y,CrossVal,Ycv)

        per =

        0.90

并行开始

>> parpool local
Starting parallel pool (parpool) using the 'local' profile ... connected to 12 workers.

ans = 

 Pool with properties: 

            Connected: true
           NumWorkers: 12
              Cluster: local
        AttachedFiles: {}
          IdleTimeout: Inf (no automatic shut down)
          SpmdEnabled: true

初始化并并行执行函数12次

per = cell(12,1);
parfor ii = 1 : 12
per{ii} = neuralTr(tSet,Y,CrossVal,Ycv);
end        

per

per = 

    [0.96]
    [0.83]
    [0.92]
    [1.08]
    [0.85]
    [0.89]
    [1.06]
    [0.83]
    [0.90]
    [0.93]
    [0.95]
    [0.81]

再次执行看随机初始化是否带来不同的值

per = cell(12,1);
parfor ii = 1 : 12
per{ii} = neuralTr(tSet,Y,CrossVal,Ycv);
end
per

per = 

    [0.96]
    [0.83]
    [0.92]
    [1.08]
    [0.85]
    [0.89]
    [1.06]
    [0.83]
    [0.90]
    [0.93]
    [0.95]
    [0.81]

编辑 1: 运行 函数只有 for

per = cell(12,1);
for ii = 1 : 12
    per{ii} = neuralTr(tSet,Y,CrossVal,Ycv);
end
    per

    per =

    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]
    [0.90]

编辑 2: 我修改了我的功能,现在一切正常。也许问题出在并行划分数据时。所以我在发送到并行之前划分了数据。非常感谢

function per = neuralTr(tSet,Y,CrossVal,Ycv)
indt = 1:round(size(tSet,2) * 0.8) ;
indv = round(size(tSet,2) * 0.8):round(size(tSet,2) * 0.9);
indte = round(size(tSet,2) * 0.9):size(tSet,2);
hiddenLayerSize = 94;
redeT = patternnet(hiddenLayerSize);
redeT.input.processFcns = {'removeconstantrows','mapminmax'};
redeT.output.processFcns = {'removeconstantrows','mapminmax'};
redeT.divideFcn = 'dividerand';  % Divide data randomly
redeT.divideMode = 'sample';  % Divide up every sample
redeT.divideParam.trainRatio = 80/100;
redeT.divideParam.valRatio =  10/100;
redeT.divideParam.testRatio = 10/100;

redeT.trainFcn = 'trainscg';  % Scaled conjugate gradient
redeT.performFcn = 'crossentropy';  % Cross-entropy
redeT.trainParam.showWindow=0; %default is 1)
redeT = train(redeT,tSet,Y);    
outputs = sim(redeT,CrossVal);
per = zeros(12,1);
parfor ii = 1 : 12
    redes = train(redeT,tSet,Y);
    per(ii) = perform(redes,Ycv,outputs);
end
end

结果:

>> per = neuralTr(tSet,Y,CrossVal,Ycv)

per =

          0.90
          0.90
          0.90
          0.90
          0.90
          0.90
          0.90
          0.90
          0.90
          0.90
          0.90
          0.90

哦!我想我找到了,但无法测试。

您的代码中有:

redeT.divideFcn = 'dividerand';  % Divide data randomly

如果每个工人随机选择数据,那么他们会得到不同的结果,不是吗?

尝试下一个:

per = cell(12,1);
parfor ii = 1 : 12
   rng(1); % set the seed for random number generation, so every time the number generated will be the same
   per{ii} = neuralTr(tSet,Y,CrossVal,Ycv);
end
per

不确定 neuralTr 是否将种子放在里面,但试一试。