将元组中的二元运算符与字典项匹配 [英] Matching Binary operators in Tuples to Dictionary Items
问题描述
因此,我正在研究Pybrain型项目,但我坚持其中的一部分.
到目前为止,该程序接受一个元组,并使用其中一个花式vars()['string']
语句"为其分配变量.具体来说,它接收一个数字元组并将其分配给一个'layerx
'值,其中x
是层的编号(依次是第1、2、3等层),因此这些数字是该层的尺寸.
So, I'm working on a Pybrain-type project and I'm stuck on part of it.
So far the program takes in a tuple and assigns a variable to it using 'one of them fancy vars()['string']
statements. Specifically, it takes in a tuple of numbers and assigns it to a 'layerx
' value, where x
is the number of the layer (in order, layer 1, 2, 3, etc), such that the numbers are the dimensions of that layer.
我迫切而谦卑地向您寻求帮助的程序部分是程序的下一步.它接收一个元组的元组(元组的数量必须=层的数量),并且元组包含1/0的元组.
The part of the program I desperately and humbly come to you for help in is what should be the next step in the program; it takes in a tuple of tuples (the number of tuples must = the number of layers), and the tuples contain 1/0's.
应该确定在哪个图层中使用哪种类型的Pybrain图层,然后插入该图层的尺寸值,并从本质上创建该图层变量.我已经玩了一段时间,并且得到了一个……扭曲的,令人困惑的代码块.
It is supposed to determine what type of Pybrain Layer to use in what layer, and then plugs in that layer's dimension value and, essentially, creates that layer-variable. I've...played with it for a while, and I've gotten a really...twisted...confusing block of code.
请原谅那些令人费解的变量名,我认为通过使它们变得更具体可以使我很聪明:
Please pardon the convoluted variable names, I thought I was being smart by making them somewhat specific:
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中的正确模块匹配,然后将其插入相应的层=具有已列出的尺寸的模块.
I honestly lost track of what was going on in it. The tuple "moduleconfig
" in the beginning
was supposed to be a tuple of tuples (nested tuples) with binary operators, it was supposed to stop when one of the tuples has a 1, match that operator with the right module in Pybrain, and then plug this in so the corresponding layer = that module with the dimmensions already listed.
显然出了点大问题,它是如此之遥以至于我的大脑无法理解……失去了所有的原因,每次我看到它都会感到害怕……请帮助我或告诉我我我制造了可憎的东西,我想...
Obviously something went terribly wrong, and it's so fargone that my brain can't make any sense of it...it's lost all it's reason and every time I look at it I get scared...please help me or tell me I created an abomination or something, I guess...
推荐答案
影响代码可读性的一个巨大障碍是变量命名和样式.我已经尝试为您清理一点.它可能仍然不起作用,但是现在更容易看到发生了什么.请参考PEP 8, Python样式指南
One huge hindrance that's affecting code readability for you is variable naming and style. I've tried to clean it up a little bit for you. It still might not work, but now it's a LOT easier to see what's going on. Please refer to PEP 8, the Python style guide
对于初学者,我在下面重命名了一些变量.请注意,在python中,变量应全部为小写字母,并用下划线连接单独的单词.常量应为ALL_UPPERCASE:
For starters, I renamed some variables, below. Note that in python, variables should be all lowercase, with separate words connected by an underscore. Constants should be ALL_UPPERCASE:
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
块:
And here is the while loop (with variable names replaced, and properly indented, with the try... except
block removed:
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
我不确定您在此行中到底要做什么:
I'm not sure exactly what you are trying to do in this line:
vars()[MOD_BUFFER + str(assigned_layers)] = modules_dict[c_buff] #modules_dict is a dict and not a callable object
此外,您最初有modules_dict(moduleconbuff)
,它将引发TypeError
,因为字典不是可调用的对象.我假设您打算通过键来检索值.
Also, you originally had modules_dict(moduleconbuff)
which will raise a TypeError
as a dict is not a callable object. I'm assuming you meant to retrieve a value by key.
正如我所说,我不太确定您要在这里做什么(可能是因为我没有看到其余的代码),但是重命名变量和使用良好的样式应该对您有所帮助能够调试您的代码.如果您回答我的问题/评论,我将继续进行编辑.
As I said, I'm not quite sure what your trying to do here (probably because I haven't seen the rest of your code), but renaming your variables and using good style should go a long way towards you being able to debug your code. I will continue to edit if you answer my questions/comment.
这篇关于将元组中的二元运算符与字典项匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!