我如何在 pytorch 中限制触发器?移动设置

问题描述

我一直在寻找一种将失败次数限制在 6 亿以下的方法,这是移动设置的条件。 许多“神经架构搜索代码使用移动设置来训练“图像网络”。

搜索了许多 flops 约束代码,但没有找到确切的内容

如何限制代码的失败?

非常感谢您提前申请!

我在 git hub 中看到了这两个定义代码,但我还没弄明白。

    def get_path(weight,constraint,FLOPS):
        # n=20
        # weight=np.array([np.random.rand(8) for i in range(n)])
        # constraint=np.array([np.random.rand(8) for i in range(n)])
        # print(weight.shape)
        # print(constraint.shape)
        # FLOPS=10
        n = weight.shape[0]
        c = weight.shape[1]
        max_weight = 0
        for i in range(n):
            max_weight += int(np.max(weight[i]) * 100)  
        # print('max_weight:',max_weight)  

        dp = [[FLOPS * 10 for i in range(max_weight + 5)] for i in range(n)]

        pre = np.zeros((n,max_weight + 5),int)  
        chose = np.zeros((n,int)  

        for i in range(c): 
            w = getw(weight[0][i])
            dp[0][w] = constraint[0][i]
            pre[0][w] = -1  
            chose[0][w] = i
        ans = 0  
        endk = 0  
        for i in range(1,n): 
            for j in range(max_weight + 1):  
                for k in range(c):  
                    w = getw(weight[i][k])
                    if (j >= w):
                        if (dp[i][j] > dp[i - 1][j - w] + constraint[i][k]):
                            dp[i][j] = dp[i - 1][j - w] + constraint[i][k]
                            pre[i][j] = j - w  
                            chose[i][j] = k  
                    if i == n - 1:
                        if dp[i][j] <= FLOPS and j > ans:
                            ans = j
                             endk = k
        path = []
        path.append(endk)
        Nowj = ans
        Nownode = n - 1
        while (pre[Nownode][Nowj] != -1):  
            Nowj = pre[Nownode][Nowj]
            Nownode -= 1
            # print(dp[Nownode][Nowj])
            # print(chose[Nownode][Nowj])
            path.append(chose[Nownode][Nowj])

        path = path[::-1]
        # print('max_weight:',ans)
        # print('path:',path)
        return path


    def get_MB_network(dir_name,flops_constraint=600,name=None):
        flops_constraint = flops_constraint
        if not os.path.exists(os.path.join(dir_name,'probability.npy')):
            return None
        flops_list = json.load(open(os.path.join(dir_name,'flops.json')))
        super_net = json.load(open(os.path.join(dir_name,'supernet.json')))
        prob = np.load(os.path.join(dir_name,'probability.npy'))
        total_flops = 0
        total_flops += (flops_list['first_conv_flpos'] + flops_list['feature_mix_layer_flops'] +
                        flops_list['classifier_flops'] + flops_list['block_flops'][0][0])
        if 'final_expand_layer_flops' in flops_list.keys():
            total_flops += flops_list['final_expand_layer_flops']
        total_flops = total_flops
        block_flops = np.array(flops_list['block_flops'][1:])
        assert block_flops.shape[0] == prob.shape[0]
        # print(prob)
        path = get_path(prob,block_flops,flops_constraint - total_flops)
        _net = copy.deepcopy(super_net)
        assert len(path) == len(_net['blocks']) - 1
        for i in range(len(path)):
            _net['blocks'][i + 1]['mobile_inverted_conv'] = \
                _net['blocks'][i + 1]['mobile_inverted_conv']['selection'][path[i]]
        if name is None:
            save_path = os.path.join(dir_name,str(flops_constraint) + '.json')
        else:
            save_path = os.path.join(dir_name,name + '.json')
        json.dump(_net,open(save_path,'a+'))
        return path

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...