将元组中的二进制运算符与字典项匹配

2024-09-30 19:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我在做一个Pybrain类型的项目,我被困在其中的一部分。 到目前为止,程序接受一个元组,并使用'one of them funcyvars()['string']语句为其分配一个变量。具体地说,它接受一个数字元组并将其赋给一个layerx值,其中x是层的编号(按顺序,层1、2、3等),这样数字就是该层的维度。你知道吗

这个程序的一部分我非常诚恳地向你寻求帮助,那就是程序的下一步应该是什么;它包含一个元组(元组的数量必须=层的数量),元组包含1/0

它应该确定在哪一层中使用哪种类型的Pybrain层,然后插入该层的维度值,本质上,创建该层变量。我…玩了一段时间,我得到了一个非常…扭曲…混乱的代码块。你知道吗

请原谅这些复杂的变量名,我认为我把它们弄得有些具体是很聪明的:

    moduleconbuff = 0
    modulebuffer = 'module'
    correspondinglayerbuff = 0
    moduleconfigcopy = tuple(moduleconfig)

    try:  #Always triggers except, but it's pretty screwed up
                while correspondinglayerbuff <= len(self.layers):     #keeps track of how many layer/module pairs have been assigned
                    for elm in moduleconfigcopy:
                        for x in elm:
                            if x == 1:
                                moduledimmension = [layerbuff+'%s'%(correspondinglayerbuff)]
                                modulesdict = {1: pybrain.GaussianLayer(moduledimmension), 2: pybrain.LinearLayer(moduledimmension),\
                                3: pybrain.LSTMLayer(moduledimmension),4: pybrain.SigmoidLayer(moduledimmension),5: pybrain.TanhLayer(moduledimmension)}   #this dict pairs integers with pybrain modules
                                vars()[modulebuffer +'%s'%(correspondinglayerbuff)]=modulesdict(moduleconbuff)  #should return something like 'Module1 = pybrain.GaussianLayer(5) when complete
                                print vars()[modulebuffer+'%s'%(correspondinglayerbuff)]
                                moduleconbuff=0
                                correspondinglayerbuff+=1
                                print 'Valid: ', moduleconfigcopy, elm
                                continue
                            else:
                                elm = elm[1:]
                                print 'Invalid: ', moduleconfigcopy, elm
                                moduleconbuff+=1
    except:  
        print 'Invalid!!!'

我真的不知道里面发生了什么。元组“moduleconfig”在开头 应该是一个包含二进制运算符的元组(嵌套元组),当其中一个元组有一个1时,应该停止,将该运算符与Pybrain中的右模块匹配,然后插入该运算符,以便相应的层=具有已列出的dimensions的模块。你知道吗

很明显,有些东西出了严重的问题,而且它太远了,我的大脑无法理解它…它失去了所有的理由,每次我看到它我都会害怕…请帮助我或告诉我我创造了一个可憎的东西,我猜。。。你知道吗


Tags: of程序类型数量运算符元组printelm
1条回答
网友
1楼 · 发布于 2024-09-30 19:25:28

影响代码可读性的一个巨大障碍是变量命名和样式。我试着帮你清理一下。它可能仍然不起作用,但现在更容易看到发生了什么。请参阅政治公众人物第8章,即Python style guide

首先,我在下面重命名了一些变量。注意,在python中,变量都应该是小写的,单独的单词之间用下划线连接。常量应全部为大写:

assigned_layers = correspondinglayerbuff = 0
tuple_of_tuples = moduleconfigcopy = ((0, 1), (0, 0, 1), (0, 1))
dimension = moduledimension
MOD_BUFFER = modulebuffer = 'buffer'
c_buff = moduleconbuff = 0

下面是while循环(替换了变量名并正确缩进,try... except块被删除:

while assigned_layers <= len(self.layers):
    for element_tuple in tuple_of_tuples:
        for item in element_tuple:
            if item: # in python, 0 is treated as boolean False, 1 or any other value is treated as boolean True.
                dimension = [layerbuff + str(assigned_layers)] #what is layerbuff?
                modules_dict = {
                    1: pybrain.GaussianLayer(dimension),
                    2: pybrain.LinearLayer(dimension),
                    3: pybrain.LSTMLayer(dimension),
                    4: pybrain.SigmoidLayer(dimension),
                    5: pybrain.TanhLayer(dimension)
                    } # Notice how this dict is much easier to read.

                vars()[MOD_BUFFER + str(assigned_layers)] = modules_dict[c_buff]  #modules_dict is a dict and not a callable object
                c_buff = 0
                assigned_layers +=1
                #No need for continue here, since that's what the if...else does here.
            else:
                element_tuple = element_tuple[1:] #what is this for?
                print 'Invalid: ', tuple_of_tuples, element_tuple

我不知道你到底想在这行做什么:

vars()[MOD_BUFFER + str(assigned_layers)] = modules_dict[c_buff]  #modules_dict is a dict and not a callable object

另外,您最初有modules_dict(moduleconbuff),它将引发TypeError,因为dict不是可调用对象。我假设你想通过键检索一个值。你知道吗

至于你的代码改名的方式,我很可能还没看到你的代码改名方式,因为我不知道你的代码改名方式。如果你回答我的问题/评论,我将继续编辑。你知道吗

相关问题 更多 >