如何在Matlab parfor循环中处理未分类的变量? [英] How to deal with unclassified variable in Matlab parfor loop?

查看:500
本文介绍了如何在Matlab parfor循环中处理未分类的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我想使其平行.但是不幸的是,当我使用parfor循环时,出现以下错误.这是代码:

I have following code and I want to make it parallel. But unfortunately when I use parfor loop I get following error. Here is the code:

    parfor l1 = 1:length(lambda1_list) % over l1

        for l2 = 1:length(lambda2_list)

            params.lambda1 = lambda1_list(l1);
            params.lambda2 = lambda2_list(l2);


            [totBeta,theta,omega,rho,nu] = Learn_weights(dictionary(train_set,:), y(train_set,:), params); 

end
end

这是错误:

Error: The variable params in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".

这是 params 的外观 参数=

lambda1_list: [1 1.5000 2 2.5000 3 3.5000 4 4.5000 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10]
lambda2_list: [1 1.5000 2 2.5000 3 3.5000 4 4.5000 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10]
    features: [50x2 double]
        mcut: [1 1 1 1 1 1 1 1 1 1]
learnWeights: 0
   beta_init: [50x10 double]
     lambda1: 10
     lambda2: 10
     stopVal: 1.0000e-05

我想知道有人可以帮助我吗?我需要在程序中添加 params ,因为它包含成千上万行代码,并且在不同的地方使用. 我正在寻找解决这个问题的技巧.

I was wondering can someone help me with this? I need to have params in program and cuz it's thousands lines of codes and it used in different places. I'm looking for trick to overcome to this problem.

推荐答案

问题在于,您正在修改 params(完全相同的数据),每次循环parfor.如果您有两个并行的作业,理论上它们都将试图更改相同 params结构,这显然会引起问题.

The issue is that you are modifying params (the same exact data) within each iteration of the parfor loop. If you have two parallel jobs going, theoretically they would both be trying to change the same params structure which obviously is going to cause issues.

您的选择是在循环前创建一个数组,然后使用该数组中的每个元素.

Your options are to create an array of params before the loop and then use each of the elements in the array.

%// Duplicate params once for each time through the parfor loop
params_array = repmat(params, size(lambda1_list));

parfor l1 = 1:length(lambda1_list)
    for l2 = 1:length(lambda2_list)
        params_array(l1).lambda1 = lambda1_list(l1);
        params_array(l1).lambda2 = lambda2_list(l2);

        [totBeta,theta,omega,rho,nu] = Learn_weights(dictionary(train_set,:), y(train_set,:), params_array(l1)); 
    end
end

另一种选择是在修改params之前创建它的副本.

The other option is to create a copy of params prior to modifying it.

parfor l1 = 1:length(lambda1_list)

    %// Make a copy of the params that is local to this loop iteration
    this_param = params;

    for l2 = 1:length(lambda2_list)
        %// Modify this copy
        this_param.lambda1 = lambda1_list(l1);
        this_param.lambda2 = lambda2_list(l2);

        %// Pass this modified copy to Learn_weights
        [totBeta,theta,omega,rho,nu] = Learn_weights(dictionary(train_set,:), y(train_set,:), this_param); 
    end
end

这篇关于如何在Matlab parfor循环中处理未分类的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆